src/Services/MysqlQueries.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Config;
  4. use App\Entity\AdvertiserOfferCount;
  5. use App\Entity\AffiliateOfferCapping;
  6. use App\Entity\AutomaticDisableLinkParams;
  7. use App\Entity\BulkCap;
  8. use App\Entity\BulkCapByAffiliate;
  9. use App\Entity\DisableLinks;
  10. use App\Entity\DisableLinksDump;
  11. use App\Entity\GoalDisableLinkAlertSent;
  12. use App\Entity\GoalDisableLinkParams;
  13. use App\Entity\RetentionOptimisation;
  14. use App\Entity\ScheduledDisableLinks;
  15. use App\Entity\SourceBlock;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use App\Entity\ImpressionOptimisation;
  18. /**
  19.  *
  20.  * Mysql queries were written here before Mysql quries were written in Repository corresponding to Entity
  21.  *
  22.  * Class MysqlQueries
  23.  * @package App\Services
  24.  */
  25. class MysqlQueries
  26. {
  27.     private $em;
  28.     /**
  29.      * MysqlQueries constructor.
  30.      *
  31.      * @param $em
  32.      */
  33.     public function __construct(EntityManagerInterface $em)
  34.     {
  35.         $this->em = $em;
  36.         $this->em->getConnection()->getConfiguration()->setSQLLogger(null);
  37.     }
  38.     public function insertToSourceBlock($affiliateId, $source, $advertiserId)
  39.     {
  40.         $combinationExist = $this->checkSourceBlockExist($affiliateId, $source, $advertiserId);
  41.         if ($combinationExist != null) {
  42.             return;
  43.         }
  44.         $entity = new SourceBlock();
  45.         $entity->setAffiliateId($affiliateId);
  46.         $entity->setSource($source);
  47.         $entity->setAdvertiserId($advertiserId);
  48.         $entity->setDateInserted(new \DateTime('now'));
  49.         $entity->setDateUpdated(new \DateTime('now'));
  50.         $this->em->persist($entity);
  51.         $this->em->flush();
  52.     }
  53.     public function checkSourceBlockExist($affiliateId, $source, $advertiserId)
  54.     {
  55.         return $this->em->getRepository('App\Entity\SourceBlock')->findOneBy([
  56.             'advertiserId' => $advertiserId,
  57.             'affiliateId'  => $affiliateId,
  58.             'source'       => $source
  59.         ]);
  60.     }
  61.     public function getSourceBlock()
  62.     {
  63.         return $this->em->getRepository('App\Entity\SourceBlock')->findAll();
  64.     }
  65.     public function deleteSourceBlockById($id)
  66.     {
  67.         $entity = $this->em->getRepository('App\Entity\SourceBlock')->findOneBy(array('id' => $id));
  68.         if ($entity != null) {
  69.             $this->em->remove($entity);
  70.             $this->em->flush();
  71.         }
  72.     }
  73.     public function deleteAutomaticDisableLink($id)
  74.     {
  75.         $entity = $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(array('id' => $id));
  76.         if ($entity != null) {
  77.             $this->em->remove($entity);
  78.             $this->em->flush();
  79.         }
  80.     }
  81.     public function deleteGoalDisableLink($id)
  82.     {
  83.         $entity = $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy(array('id' => $id));
  84.         if ($entity != null) {
  85.             $this->em->remove($entity);
  86.             $this->em->flush();
  87.         }
  88.     }
  89.     public function getSourceBlockData()
  90.     {
  91.         $repository = $this->em->getRepository('App\Entity\SourceBlock');
  92.         $result = $repository->createQueryBuilder('a')->select('a.affiliateId', 'a.source', 'a.advertiserId')->getQuery()->getResult();
  93.         return $result;
  94.     }
  95.     public function getDisableLinkByMd5($md5)
  96.     {
  97.         return $this->em->getRepository('App\Entity\DisableLinks')->findOneBy(['md5' => $md5]);
  98.     }
  99.     public function getDisableLinksByMd5Arr($md5Arr)
  100.     {
  101.         $repo = $this->em->getRepository('App\Entity\DisableLinks');
  102.         $entity = $repo->createQueryBuilder('a')
  103.             ->select('a.offerId', 'a.affiliateId', 'a.source', 'a.addedFrom', 'a.md5', 'a.meta', 'a.trackingAccount')
  104.             ->where('a.md5 IN (:md5Arr)')
  105.             ->setParameters(['md5Arr' => $md5Arr])
  106.             ->getQuery()
  107.             ->getResult();
  108.         return $entity;
  109.     }
  110.     public function insertToDisableLink($md5, $offerId, $affiliateId, $source, $affsub2, $affSub3, $affSub5, $addedFrom, $advertiserId, $metaData, $trackingAccount = Config::TUNE_ACCOUNT_DEFAULT)
  111.     {
  112.         $entity = new DisableLinks();
  113.         $entity->setMd5($md5);
  114.         $entity->setOfferId($offerId);
  115.         $entity->setAffiliateId($affiliateId);
  116.         $entity->setSource($source);
  117.         $entity->setAffsub2($affsub2);
  118.         $entity->setAffsub3($affSub3);
  119.         $entity->setAffsub5($affSub5);
  120.         $entity->setTrackingAccount($trackingAccount);
  121.         $entity->setDateInserted(new \DateTime('now'));
  122.         $entity->setAddedFrom($addedFrom);
  123.         $entity->setAdvertiserId($advertiserId);
  124.         $entity->setmeta($metaData);
  125.         $this->em->persist($entity);
  126.         $this->em->flush();
  127.     }
  128.     public function insertToDisableLinkDump($md5, $offerId, $affiliateId, $source, $affsub2, $addedFrom, $advertiserId, $metaData, $wasAddedOn, $affsub3, $affsub5)
  129.     {
  130.         $entity = new DisableLinksDump();
  131.         $entity->setMd5($md5);
  132.         $entity->setOfferId($offerId);
  133.         $entity->setAffiliateId($affiliateId);
  134.         $entity->setSource($source);
  135.         $entity->setAffsub2($affsub2);
  136.         $entity->setAffsub3($affsub3);
  137.         $entity->setAffsub5($affsub5);
  138.         $entity->setDateInserted(new \DateTime('now'));
  139.         $entity->setAddedFrom($addedFrom);
  140.         $entity->setAdvertiserId($advertiserId);
  141.         $entity->setMeta($metaData);
  142.         $entity->setWasAddedOn($wasAddedOn);
  143.         $this->em->persist($entity);
  144.         $this->em->flush();
  145.     }
  146.     public function checkAutomaticDisableLinkByAdvertiser($advertiserId, $tuneAccount)
  147.     {
  148.         return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['advertiserId' => $advertiserId, 'tuneAccount' => $tuneAccount]);
  149.     }
  150.     public function checkAutomaticDisableLinkByAffiliate($affiliateId, $tuneAccount)
  151.     {
  152.         return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['affiliateId' => $affiliateId, 'tuneAccount' => $tuneAccount]);
  153.     }
  154.     public function checkAutomaticDisableLinkByOffer($offerId, $tuneAccount)
  155.     {
  156.         return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['offerId' => $offerId, 'tuneAccount' => $tuneAccount]);
  157.     }
  158.     public function saveAutomaticDisableLinkByAdvertiser($advertiserId, $advertiserName, $tuneAccount)
  159.     {
  160.         $entity = new AutomaticDisableLinkParams();
  161.         $entity->setAdvertiserId($advertiserId);
  162.         $entity->setAdvertiserName($advertiserName);
  163.         $entity->setDateUpdated(new \DateTime('now'));
  164.         $entity->setDateInserted(new \DateTime('now'));
  165.         $entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_ADVERTISER);
  166.         $entity->setTuneAccount($tuneAccount);
  167.         $this->em->persist($entity);
  168.         $this->em->flush();
  169.     }
  170.     public function saveAutomaticDisableLinkByAffiliate($affiliateId, $affiliateName, $tuneAccount)
  171.     {
  172.         $entity = new AutomaticDisableLinkParams();
  173.         $entity->setAffiliateId($affiliateId);
  174.         $entity->setAffiliateName($affiliateName);
  175.         $entity->setDateUpdated(new \DateTime('now'));
  176.         $entity->setDateInserted(new \DateTime('now'));
  177.         $entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_AFFILIATE);
  178.         $entity->setTuneAccount($tuneAccount);
  179.         $this->em->persist($entity);
  180.         $this->em->flush();
  181.     }
  182.     public function saveAutomaticDisableLinkByOffer($offerId, $offerName, $tuneAccount)
  183.     {
  184.         $entity = new AutomaticDisableLinkParams();
  185.         $entity->setOfferId($offerId);
  186.         $entity->setOfferName($offerName);
  187.         $entity->setDateUpdated(new \DateTime('now'));
  188.         $entity->setDateInserted(new \DateTime('now'));
  189.         $entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_OFFER);
  190.         $entity->setTuneAccount($tuneAccount);
  191.         $this->em->persist($entity);
  192.         $this->em->flush();
  193.     }
  194.     public function getAutomaticDisableLinkData($tuneAccount = Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE)
  195.     {
  196.         $repository = $this->em->getRepository('App\Entity\AutomaticDisableLinkParams');
  197.         $result = $repository
  198.             ->createQueryBuilder('a')
  199.             ->select(
  200.                 'a.affiliateId',
  201.                 'a.offerId',
  202.                 'a.advertiserId',
  203.                 'a.advertiserName',
  204.                 'a.affiliateName',
  205.                 'a.offerName',
  206.                 'a.dateInserted',
  207.                 'a.id',
  208.                 'a.tuneAccount'
  209.             );
  210.         if ($tuneAccount) {
  211.             $result->where('a.tuneAccount = :tuneAccount');
  212.             $result->setParameter('tuneAccount', $tuneAccount);
  213.         }
  214.         return $result->getQuery()->getResult();
  215.     }
  216.     public function getDisableLinkData()
  217.     {
  218.         $repository = $this->em->getRepository('App\Entity\DisableLinks');
  219.         $result = $repository->createQueryBuilder('a')->select('a.affiliateId', 'a.offerId', 'a.advertiserId', 'a.addedFrom', 'a.dateInserted', 'a.source', 'a.meta')->orderBy('a.id', 'DESC')->getQuery()->getResult();
  220.         return $result;
  221.     }
  222.     public function getGoalDisableLinkByGoalAndOffer($offerId, $goalId)
  223.     {
  224.         return $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy([
  225.             'goalId'  => $goalId,
  226.             'offerId' => $offerId
  227.         ]);
  228.     }
  229.     public function insertToGoalDisableLinkByOffer($offerId, $offerName, $advertiserId, $advertiserName, $goalId, $goalName, $budget, $sendAlert, $setAutoblock, $cr, $relationshipType)
  230.     {
  231.         $entity = new GoalDisableLinkParams();
  232.         $entity->setOfferName($offerName);
  233.         $entity->setOfferId($offerId);
  234.         $entity->setAdvertiserId($advertiserId);
  235.         $entity->setAdvertiserName($advertiserName);
  236.         $entity->setGoalId($goalId);
  237.         $entity->setGoalName($goalName);
  238.         $entity->setAddedFrom(Config::GOAL_DISABLE_LINK_ADDED_FROM_OFFER);
  239.         $entity->setBudget($budget);
  240.         $entity->setSendAlert($sendAlert);
  241.         $entity->setAutoBlock($setAutoblock);
  242.         $entity->setRelationshipType($relationshipType);
  243.         $entity->setCr($cr);
  244.         $entity->setDateUpdated(new \DateTime('now'));
  245.         $entity->setDateInserted(new \DateTime('now'));
  246.         $this->em->persist($entity);
  247.         $this->em->flush();
  248.     }
  249.     public function getGoalDisableLinkData($offerId = null)
  250.     {
  251.         $repository = $this->em->getRepository('App\Entity\GoalDisableLinkParams');
  252.         $entity = $repository->createQueryBuilder('a')
  253.             ->select('a.id', 'a.goalId', 'a.goalName', 'a.offerId', 'a.offerName', 'a.advertiserId', 'a.advertiserName', 'a.budget', 'a.addedFrom', 'a.sendAlert', 'a.autoBlock', 'a.relationshipType', 'a.cr')
  254.             ->where('a.offerId > 0');
  255.         if ($offerId) {
  256.             $entity->andWhere('a.offerId = :offerId');
  257.             $entity->setParameter('offerId', $offerId);
  258.         }
  259.         return $entity
  260.             ->getQuery()
  261.             ->getResult();
  262.     }
  263.     public function updateGoalDisableLinks($id, $sendAlert, $autoBlock)
  264.     {
  265.         $entity = $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy(['id' => $id]);
  266.         if ($entity) {
  267.             $entity->setSendAlert($sendAlert);
  268.             $entity->setAutoBlock($autoBlock);
  269.             $this->em->persist($entity);
  270.             $this->em->flush();
  271.         }
  272.     }
  273.     public function getDisableLinkLogs($affiliateArr, $offerArr, $advertiserArr, $sourceArr, $addedFrom, $startDate, $endDate)
  274.     {
  275.         $repository = $this->em->getRepository('App\Entity\DisableLinks');
  276.         $result = $repository
  277.             ->createQueryBuilder('a')
  278.             ->select(
  279.                 'a.affiliateId',
  280.                 'a.offerId',
  281.                 'a.source',
  282.                 'a.advertiserId',
  283.                 'a.dateInserted',
  284.                 'a.addedFrom',
  285.                 'a.source',
  286.                 'a.meta',
  287.                 'a.id',
  288.                 'a.md5',
  289.                 'a.trackingAccount'
  290.             )
  291.             ->where('a.dateInserted >= :startDate')
  292.             ->andWhere('a.dateInserted <= :endDate')
  293.             ->setParameter('startDate', $startDate)
  294.             ->setParameter('endDate', $endDate);
  295.         if (! empty($affiliateArr)) {
  296.             $result->andWhere('a.affiliateId IN (:affiliateId)');
  297.             $result->setParameter('affiliateId', $affiliateArr);
  298.         }
  299.         if (! empty($offerArr)) {
  300.             $result->andWhere('a.offerId IN (:offerId)');
  301.             $result->setParameter('offerId', $offerArr);
  302.         }
  303.         if (! empty($addedFrom)) {
  304.             $result->andWhere('a.addedFrom IN (:addedFrom)');
  305.             $result->setParameter('addedFrom', $addedFrom);
  306.         }
  307.         if (! empty($advertiserArr)) {
  308.             $result->andWhere('a.advertiserId IN (:advertiserId)');
  309.             $result->setParameter('advertiserId', $advertiserArr);
  310.         }
  311.         if (! empty($sourceArr)) {
  312.             $result->andWhere('a.source IN (:source)');
  313.             $result->setParameter('source', $sourceArr);
  314.         }
  315.         return $result->orderBy('a.id', 'DESC')->getQuery()->getResult();
  316.     }
  317.     public function getDisableLinkDataByAddedFromAndNotOfAdvertiser($addedFrom, $advertiserIdArr)
  318.     {
  319.         $repo = $this->em->getRepository('App\Entity\DisableLinks');
  320.         $entity = $repo->createQueryBuilder('a')->select('a.md5', 'a.offerId', 'a.affiliateId', 'a.source', 'a.affsub2', 'a.dateInserted', 'a.addedFrom', 'a.advertiserId', 'a.meta')->where('a.addedFrom = :addedFrom')->andWhere('a.advertiserId NOT IN (:advertiserIdArr)')->setParameters([
  321.             'addedFrom'       => $addedFrom,
  322.             'advertiserIdArr' => $advertiserIdArr
  323.         ])->getQuery()->getResult();
  324.         return $entity;
  325.     }
  326.     public function getDisableLinkDataByAddedFrom($addedFrom)
  327.     {
  328.         $repo = $this->em->getRepository('App\Entity\DisableLinks');
  329.         $entity = $repo->createQueryBuilder('a')->select('a.md5', 'a.offerId', 'a.affiliateId', 'a.source', 'a.affsub2', 'a.dateInserted', 'a.addedFrom', 'a.advertiserId', 'a.meta', 'a.id')->where('a.addedFrom = :addedFrom')->setParameters(['addedFrom' => $addedFrom])->getQuery()->getResult();
  330.         return $entity;
  331.     }
  332.     public function getDisableLinkDataByAddedFromAndNotOfOffer($addedFrom, $offerIdArr)
  333.     {
  334.         $repo = $this->em->getRepository('App\Entity\DisableLinks');
  335.         $entity = $repo->createQueryBuilder('a')->select('a.md5', 'a.offerId', 'a.affiliateId', 'a.source', 'a.affsub2', 'a.dateInserted', 'a.addedFrom', 'a.advertiserId', 'a.meta', 'a.id')->where('a.addedFrom = :addedFrom')->andWhere('a.offerId NOT IN (:offerIdArr)')->setParameters([
  336.             'addedFrom'  => $addedFrom,
  337.             'offerIdArr' => $offerIdArr
  338.         ])->getQuery()->getResult();
  339.         return $entity;
  340.     }
  341.     public function getDisableLinkDataByAddedFromAndNotOfAffiliate($addedFrom, $affiliateIdArr)
  342.     {
  343.         $repo = $this->em->getRepository('App\Entity\DisableLinks');
  344.         $entity = $repo->createQueryBuilder('a')->select('a.md5', 'a.offerId', 'a.affiliateId', 'a.source', 'a.affsub2', 'a.dateInserted', 'a.addedFrom', 'a.advertiserId', 'a.meta', 'a.id')->where('a.addedFrom = :addedFrom')->andWhere('a.affiliateId NOT IN (:affiliateIdArr)')->setParameters([
  345.             'addedFrom'      => $addedFrom,
  346.             'affiliateIdArr' => $affiliateIdArr
  347.         ])->getQuery()->getResult();
  348.         return $entity;
  349.     }
  350.     public function deleteDisableLinkByMd5($md5)
  351.     {
  352.         $entity = $this->em->getRepository('App\Entity\DisableLinks')->findOneBy(array('md5' => $md5));
  353.         if ($entity != null) {
  354.             $this->em->remove($entity);
  355.             $this->em->flush();
  356.         }
  357.     }
  358.     public function insertToBulkCap($affiliateTagId, $affiliateTagName, $affiliateOfferCapType, $offerId, $offerName, $capValue, $addedBy)
  359.     {
  360.         $entity = new BulkCap();
  361.         $entity->setAffiliateTagId($affiliateTagId);
  362.         $entity->setAffiliateTagName($affiliateTagName);
  363.         $entity->setAffiliateOfferCapType($affiliateOfferCapType);
  364.         $entity->setOfferId($offerId);
  365.         $entity->setOfferName($offerName);
  366.         $entity->setCapValue($capValue);
  367.         $entity->setDateInserted(new \DateTime('now'));
  368.         $entity->setAddedBy($addedBy);
  369.         $this->em->persist($entity);
  370.         $this->em->flush();
  371.     }
  372.     public function deleteFromBulkCapById($id)
  373.     {
  374.         $entity = $this->em->getRepository('App\Entity\BulkCap')->findOneBy(['id' => $id]);
  375.         if ($entity != null) {
  376.             $this->em->remove($entity);
  377.             $this->em->flush();
  378.         }
  379.     }
  380.     public function getOfferIdAffiliateTagIdFromBulkCap($offerId, $affiliateTagId, $capType)
  381.     {
  382.         return $this->em->getRepository('App\Entity\BulkCap')->findOneBy([
  383.             'offerId'               => $offerId,
  384.             'affiliateTagId'        => $affiliateTagId,
  385.             'affiliateOfferCapType' => $capType
  386.         ]);
  387.     }
  388.     public function getBulkCapData($hourRange = 4380)
  389.     {
  390.         $dataAfterDatetime = date('Y-m-d H:s:i', strtotime("-$hourRange hours"));
  391.         $repo              = $this->em->getRepository('App\Entity\BulkCap');
  392.         $entity = $repo
  393.             ->createQueryBuilder('a')
  394.             ->select('a.affiliateTagId', 'a.affiliateTagName', 'a.affiliateOfferCapType', 'a.offerId', 'a.offerName', 'a.dateInserted', 'a.id', 'a.capValue', 'a.addedBy')
  395.             ->where('a.dateInserted > :dateTime')
  396.             ->setParameters(['dateTime' => $dataAfterDatetime])
  397.             ->orderBy('a.id', 'DESC')
  398.             ->getQuery()->getResult();
  399.         return $entity;
  400.     }
  401.     public function getBulkCapByAffiliateData($hourRange = 48)
  402.     {
  403.         $dataAfterDatetime = date('Y-m-d H:s:i', strtotime("-$hourRange hours"));
  404.         $repo              = $this->em->getRepository('App\Entity\BulkCapByAffiliate');
  405.         $entity = $repo
  406.             ->createQueryBuilder('a')
  407.             ->select('a.affiliateId', 'a.affiliateName', 'a.affiliateOfferCapType', 'a.offerId', 'a.offerName', 'a.dateInserted', 'a.id', 'a.capValue', 'a.addedBy')
  408.             ->where('a.dateInserted > :dateTime')
  409.             ->setParameters(['dateTime' => $dataAfterDatetime])
  410.             ->orderBy('a.id', 'DESC')
  411.             ->getQuery()
  412.             ->getResult();
  413.         return $entity;
  414.     }
  415.     public function getCombinationFromAffiliateOfferCapping($affiliateId, $offerId, $capType, $tuneAccount)
  416.     {
  417.         return $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy([
  418.             'offerId'     => $offerId,
  419.             'affiliateId' => $affiliateId,
  420.             'capType'     => $capType,
  421.             'tuneAccount' => $tuneAccount
  422.         ]);
  423.     }
  424.     public function deleteAffiliateOfferCapById($id)
  425.     {
  426.         $entity = $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy(['id' => $id]);
  427.         if ($entity != null) {
  428.             $this->em->remove($entity);
  429.             $this->em->flush();
  430.         }
  431.     }
  432.     public function insertToAffiliateOfferCap($affiliateId, $offerId, $capType, $capValue, $tuneAccount)
  433.     {
  434.         $entity = new AffiliateOfferCapping();
  435.         $entity->setAffiliateId($affiliateId);
  436.         $entity->setOfferId($offerId);
  437.         $entity->setCapType($capType);
  438.         $entity->setCapValue($capValue);
  439.         $entity->setDateInserted(new \DateTime('now'));
  440.         $entity->setDateUpdated(new \DateTime('now'));
  441.         $entity->setTuneAccount($tuneAccount);
  442.         $this->em->persist($entity);
  443.         $this->em->flush();
  444.     }
  445.     public function insertToGoalDisableLinkAlertSent($goalId, $offerId, $affiliateId, $source)
  446.     {
  447.         $entity = new GoalDisableLinkAlertSent();
  448.         $entity->setGoalId($goalId);
  449.         $entity->setOfferId($offerId);
  450.         $entity->setAffiliateId($affiliateId);
  451.         $entity->setDateInserted(new \DateTime('now'));
  452.         $entity->setSource($source);
  453.         $this->em->persist($entity);
  454.         $this->em->flush();
  455.     }
  456.     public function checkGoalDisableLinkAlertSent($goalId, $offerId, $affiliateId, $source)
  457.     {
  458.         return $this->em->getRepository('App\Entity\GoalDisableLinkAlertSent')->findOneBy([
  459.             'offerId'     => $offerId,
  460.             'goalId'      => $goalId,
  461.             'affiliateId' => $affiliateId,
  462.             'source'      => $source
  463.         ]);
  464.     }
  465.     public function getRetentionOptimisationByParams($offerId, $goalId, $tuneAccount)
  466.     {
  467.         return $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
  468.             'offerId' => $offerId,
  469.             'goalId'  => $goalId,
  470.             'tuneAccount' => $tuneAccount
  471.         ]);
  472.     }
  473.     public function updateRetentionOptimisation($offerId, $goalId, $retentionRate, $minimumBudget, $sendAlert, $autoBlock, $tuneAccount)
  474.     {
  475.         $entity = $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
  476.             'offerId' => $offerId,
  477.             'goalId'  => $goalId,
  478.             'tuneAccount' => $tuneAccount
  479.         ]);
  480.         if ($entity != null) {
  481.             $entity->setRetentionRate($retentionRate);
  482.             $entity->setMinimumBudget($minimumBudget);
  483.             $entity->setSendAlert($sendAlert);
  484.             $entity->setDateUpdated(new \DateTime('now'));
  485.             $entity->setAutoBlock($autoBlock);
  486.             $entity->setTuneAccount($tuneAccount);
  487.             $this->em->persist($entity);
  488.             $this->em->flush();
  489.         }
  490.     }
  491.     public function insertRetentionOptimisation($offerId, $goalId, $retentionRate, $minimumBudget, $sendAlert, $autoBlock, $tuneAccount)
  492.     {
  493.         $entity = new RetentionOptimisation();
  494.         $entity->setOfferId($offerId);
  495.         $entity->setGoalId($goalId);
  496.         $entity->setRetentionRate($retentionRate);
  497.         $entity->setMinimumBudget($minimumBudget);
  498.         $entity->setSendAlert($sendAlert);
  499.         $entity->setAutoBlock($autoBlock);
  500.         $entity->setDateInserted(new \DateTime('now'));
  501.         $entity->setDateUpdated(new \DateTime('now'));
  502.         $entity->setTuneAccount($tuneAccount);
  503.         $this->em->persist($entity);
  504.         $this->em->flush();
  505.     }
  506.     public function getRetentionOptimisation($offerId, $tuneAccount = Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE)
  507.     {
  508.         $repo = $this->em->getRepository('App\Entity\RetentionOptimisation');
  509.         $entity = $repo->createQueryBuilder('a')
  510.             ->select(
  511.                 'a.offerId',
  512.                 'a.goalId',
  513.                 'a.retentionRate',
  514.                 'a.minimumBudget',
  515.                 'a.sendAlert',
  516.                 'a.autoBlock',
  517.                 'a.dateUpdated as dateUpdated',
  518.                 'a.dateInserted as dateInserted',
  519.                 'a.id as id',
  520.                 'b.tuneAccount as tuneAccount',
  521.                 'c.company as advertiserName',
  522.                 'b.name as offerName',
  523.                 'b.advertiserId as advertiserId',
  524.                 'd.name as goalName'
  525.             )
  526.             ->innerJoin('App\Entity\OfferInfo', 'b', 'WITH', 'a.offerId = b.offerId AND a.tuneAccount = b.tuneAccount')
  527.             ->leftJoin('App\Entity\AdvertiserInfo', 'c', 'WITH', 'b.advertiserId = c.advertiserId AND b.tuneAccount = c.tuneAccount')
  528.             ->leftJoin('App\Entity\OfferGoalsInfo', 'd', 'WITH', 'a.goalId = d.goalId AND a.tuneAccount = d.tuneAccount')
  529.             ->where('a.offerId > 0');
  530.         if ($tuneAccount == Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE) {
  531.             $entity->andWhere('a.tuneAccount = :tuneAccount');
  532.             $entity->setParameter('tuneAccount', $tuneAccount);
  533.         }
  534.         if ($offerId) {
  535.             $entity->andWhere('a.offerId = :offerId');
  536.             $entity->setParameter('offerId', $offerId);
  537.         }
  538.         return $entity
  539.             ->orderBy('dateUpdated', 'DESC')
  540.             ->getQuery()
  541.             ->getResult();
  542.     }
  543.     public function deleteRetentionOptimisation($id)
  544.     {
  545.         $entity = $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy(['id' => $id]);
  546.         if ($entity != null) {
  547.             $this->em->remove($entity);
  548.             $this->em->flush();
  549.         }
  550.     }
  551.     public function checkScheduledDisableLinksByParam($source, $affiliateId, $offerId)
  552.     {
  553.         return $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
  554.             'source'      => $source,
  555.             'affiliateId' => $affiliateId,
  556.             'offerId'     => $offerId
  557.         ]);
  558.     }
  559.     public function insertToScheduledDisableLinks($source, $affiliate, $offer)
  560.     {
  561.         $entity = new ScheduledDisableLinks();
  562.         $entity->setAffiliateId($affiliate);
  563.         $entity->setOfferId($offer);
  564.         $entity->setSource($source);
  565.         $entity->setIsScheduled(1);
  566.         $entity->setDateInserted(new \DateTime('now'));
  567.         $entity->setDateUpdated(new \DateTime('now'));
  568.         $this->em->persist($entity);
  569.         $this->em->flush();
  570.     }
  571.     public function getScheduledDisableLinkByIsScheduled($isScheduled)
  572.     {
  573.         $repo = $this->em->getRepository('App\Entity\ScheduledDisableLinks');
  574.         $entity = $repo->createQueryBuilder('a')->select('a.offerId', 'a.affiliateId', 'a.source', 'a.isScheduled', 'a.id')->where('a.isScheduled = :isScheduled')->setParameters(['isScheduled' => $isScheduled])->getQuery()->getResult();
  575.         return $entity;
  576.     }
  577.     public function setIsScheduledForScheduledOfferDisableLink($offerId, $affiliateId, $source, $isScheduled)
  578.     {
  579.         $entity = $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
  580.             'offerId'     => $offerId,
  581.             'affiliateId' => $affiliateId,
  582.             'source'      => $source
  583.         ]);
  584.         if ($entity) {
  585.             $entity->setIsScheduled($isScheduled);
  586.             $this->em->persist($entity);
  587.             $this->em->flush();
  588.         }
  589.     }
  590.     public function setIsScheduledForScheduledOfferDisableLinkById($id, $isScheduled)
  591.     {
  592.         $entity = $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy(['id' => $id]);
  593.         if ($entity) {
  594.             $entity->setIsScheduled($isScheduled);
  595.             $this->em->persist($entity);
  596.             $this->em->flush();
  597.         }
  598.     }
  599.     public function insertToBulkCapByAffiliate($affiliateId, $affiliateName, $affiliateOfferCapType, $offerId, $offerName, $capValue, $addedBy)
  600.     {
  601.         $entity = new BulkCapByAffiliate();
  602.         $entity->setAffiliateId($affiliateId);
  603.         $entity->setAffiliateName($affiliateName);
  604.         $entity->setAffiliateOfferCapType($affiliateOfferCapType);
  605.         $entity->setOfferId($offerId);
  606.         $entity->setOfferName($offerName);
  607.         $entity->setCapValue($capValue);
  608.         $entity->setAddedBy($addedBy);
  609.         $entity->setDateInserted(new \DateTime('now'));
  610.         $this->em->persist($entity);
  611.         $this->em->flush();
  612.     }
  613.     public function getBulkCapByAffiliate($offerId, $affiliateId, $capType)
  614.     {
  615.         return $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy([
  616.             'offerId'               => $offerId,
  617.             'affiliateId'           => $affiliateId,
  618.             'affiliateOfferCapType' => $capType
  619.         ]);
  620.     }
  621.     public function deleteBulkCapByAffiliateById($id)
  622.     {
  623.         $entity = $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy(['id' => $id]);
  624.         if ($entity) {
  625.             $this->em->remove($entity);
  626.             $this->em->flush();
  627.         }
  628.     }
  629.     public function insertToAdvertiserOfferCount($advertiserId, $offerCount, $status)
  630.     {
  631.         $entity = new AdvertiserOfferCount();
  632.         $entity->setAdvertiserId($advertiserId);
  633.         $entity->setOfferCount($offerCount);
  634.         $entity->setOfferStatus($status);
  635.         $entity->setDateInserted(new \DateTime('now'));
  636.         $this->em->persist($entity);
  637.         $this->em->flush();
  638.     }
  639.     public function getDataForAdvertiserOfferCountForChart($advertiserIdArr, $dateStart, $dateEnd, $chooseBy, $status)
  640.     {
  641.         $repo = $this->em->getRepository('App\Entity\AdvertiserOfferCount');
  642.         $entity = $repo->createQueryBuilder('a');
  643.         if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
  644.             $entity->select('a.advertiserId', 'AVG(a.offerCount) as average', 'HOUR(a.dateInserted) as by_hour', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  645.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
  646.             $entity->select('a.advertiserId', 'AVG(a.offerCount) as average', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  647.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
  648.             $entity->select('a.advertiserId', 'AVG(a.offerCount) as average', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  649.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
  650.             $entity->select('a.advertiserId', 'AVG(a.offerCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  651.         }
  652.         $entity->where('a.advertiserId IN (:advertiserIds)')->andWhere('a.offerStatus = :status')->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
  653.             'advertiserIds' => $advertiserIdArr,
  654.             'status'        => $status,
  655.             'dateStart'     => $dateStart,
  656.             'dateEnd'       => $dateEnd
  657.         ]);
  658.         $entity->groupBy('a.advertiserId');
  659.         if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
  660.             $entity->addGroupBy('by_hour', 'by_day', 'by_week', 'by_month', 'by_year');
  661.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
  662.             $entity->addGroupBy('by_day', 'by_week', 'by_month', 'by_year');
  663.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
  664.             $entity->addGroupBy('by_week', 'by_month', 'by_year');
  665.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
  666.             $entity->addGroupBy('by_month', 'by_year');
  667.         }
  668.         return $entity->getQuery()->getResult();
  669.     }
  670.     public function getDataForAffiliateOfferCountForChart($affiliateIdArr, $dateStart, $dateEnd, $chooseBy, $offerCountBy)
  671.     {
  672.         $repo = $this->em->getRepository('App\Entity\AffiliateOfferCount');
  673.         $entity = $repo->createQueryBuilder('a');
  674.         if ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_ACTIVE_OFFER_COUNT) {
  675.             if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  676.                 $entity->select('a.affiliateId', 'AVG(a.approvedActiveOfferCount) as average', 'HOUR(a.dateInserted) as by_hour', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  677.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  678.                 $entity->select('a.affiliateId', 'AVG(a.approvedActiveOfferCount) as average', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  679.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  680.                 $entity->select('a.affiliateId', 'AVG(a.approvedActiveOfferCount) as average', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  681.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  682.                 $entity->select('a.affiliateId', 'AVG(a.approvedActiveOfferCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  683.             }
  684.         } elseif ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_OFFER_COUNT) {
  685.             if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  686.                 $entity->select('a.affiliateId', 'AVG(a.approvedOfferCount) as average', 'HOUR(a.dateInserted) as by_hour', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  687.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  688.                 $entity->select('a.affiliateId', 'AVG(a.approvedOfferCount) as average', 'DAY(a.dateInserted) as by_day', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  689.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  690.                 $entity->select('a.affiliateId', 'AVG(a.approvedOfferCount) as average', 'WEEK(a.dateInserted) as by_week', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  691.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  692.                 $entity->select('a.affiliateId', 'AVG(a.approvedOfferCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
  693.             }
  694.         }
  695.         $entity->where('a.affiliateId IN (:affiliateIds)') #->andWhere('a.offerStatus = :status')
  696.             ->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
  697.                 'affiliateIds' => $affiliateIdArr,
  698.                 'dateStart'    => $dateStart,
  699.                 'dateEnd'      => $dateEnd
  700.             ]);
  701.         $entity->groupBy('a.affiliateId');
  702.         if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  703.             $entity->addGroupBy('by_hour', 'by_day', 'by_week', 'by_month', 'by_year');
  704.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  705.             $entity->addGroupBy('by_day', 'by_week', 'by_month', 'by_year');
  706.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  707.             $entity->addGroupBy('by_week', 'by_month', 'by_year');
  708.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  709.             $entity->addGroupBy('by_month', 'by_year');
  710.         }
  711.         return $entity->getQuery()->getResult();
  712.     }
  713.     public function getImpressionOptimisationByParams($offerId, $affiliateId)
  714.     {
  715.         return $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy([
  716.             'offerId'     => $offerId,
  717.             'affiliateId' => $affiliateId
  718.         ]);
  719.     }
  720.     public function deleteImpressionOptimisation($id, $deletedBy)
  721.     {
  722.         $entity = $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy(['id' => $id]);
  723.         if ($entity != null) {
  724.             $entity->setAddedBy($deletedBy);
  725.             $entity->setDateUpdated(new \DateTime('now'));
  726.             $entity->setIsDeleted(true);
  727.             $this->em->persist($entity);
  728.             $this->em->flush();
  729.         }
  730.         return $entity;
  731.     }
  732.     public function getActiveImpressionOptimisation()
  733.     {
  734.         $repo   = $this->em->getRepository('App\Entity\ImpressionOptimisation');
  735.         $entity = $repo->createQueryBuilder('imp')->select('imp.id', 'imp.offerId', 'imp.affiliateId', 'imp.ctr', 'imp.is_deleted', 'imp.dateInserted', 'imp.dateUpdated', 'imp.addedBy')->where('imp.is_deleted = :isDeleted')->setParameters(['isDeleted' => false])->orderBy('imp.dateUpdated', 'DESC')->getQuery()->getResult();
  736.         return $entity;
  737.     }
  738.     public function createUpdateImpression($offerId, $affiliateId, $ctr, $addedBy)
  739.     {
  740.         $entity = $this->getImpressionOptimisationByParams($offerId, $affiliateId);
  741.         if ($entity != null) {
  742.             $entity->setCtr($ctr);
  743.             $entity->setAddedBy($addedBy);
  744.             $entity->setDateUpdated(new \DateTime('now'));
  745.             $this->em->persist($entity);
  746.             $this->em->flush();
  747.         } else {
  748.             $entity = $this->insertImpressionOptimisation($offerId, $affiliateId, $ctr, $addedBy);
  749.         }
  750.         return $entity;
  751.     }
  752.     public function insertImpressionOptimisation($offerId, $affiliateId, $ctr, $addedBy)
  753.     {
  754.         $entity = new ImpressionOptimisation();
  755.         $entity->setAddedBy($addedBy);
  756.         $entity->setCtr($ctr);
  757.         $entity->setAffiliateId($affiliateId);
  758.         $entity->setOfferId($offerId);
  759.         $entity->setDateUpdated(new \DateTime('now'));
  760.         $entity->setDateInserted(new \DateTime('now'));
  761.         $entity->setIsDeleted(false);
  762.         $this->em->persist($entity);
  763.         $this->em->flush();
  764.         return $entity;
  765.     }
  766.     public function getFraudFlagLogs($affiliateArr, $offerArr, $advertiserArr, $startDate, $endDate)
  767.     {
  768.         $repository = $this->em->getRepository('App\Entity\FraudFlagLogs');
  769.         $result = $repository->createQueryBuilder('a')
  770.             ->select('a.affiliateId', 'a.offerId', 'a.source', 'a.advertiserId', 'a.dateInserted', 'a.advSub1', 'a.source', 'a.id', 'a.transactionId')
  771.             ->where('a.dateInserted >= :startDate')
  772.             ->andWhere('a.dateInserted <= :endDate')
  773.             ->andWhere('a.newStatus <= :newStatus')
  774.             ->setParameter('startDate', $startDate)
  775.             ->setParameter('endDate', $endDate)
  776.             ->setParameter('newStatus', Config::REJECTED_STATUS);
  777.         if (! empty($affiliateArr)) {
  778.             $result->andWhere('a.affiliateId IN (:affiliateId)');
  779.             $result->setParameter('affiliateId', $affiliateArr);
  780.         }
  781.         if (! empty($offerArr)) {
  782.             $result->andWhere('a.offerId IN (:offerId)');
  783.             $result->setParameter('offerId', $offerArr);
  784.         }
  785.         if (! empty($advertiserArr)) {
  786.             $result->andWhere('a.advertiserId IN (:advertiserId)');
  787.             $result->setParameter('advertiserId', $advertiserArr);
  788.         }
  789.         return $result->orderBy('a.id', 'DESC')->getQuery()->getResult();
  790.     }
  791.     public function deleteFraudFlagLogs($id)
  792.     {
  793.         $entity = $this->em->getRepository('App\Entity\FraudFlagLogs')->findOneBy(array('id' => $id));
  794.         if ($entity != null) {
  795.             $this->em->remove($entity);
  796.             $this->em->flush();
  797.         }
  798.     }
  799. }