src/Services/MysqlQueries.php line 38

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('a.affiliateId''a.offerId''a.advertiserId''a.advertiserName',
  200.          'a.affiliateName''a.offerName''a.dateInserted''a.id''a.tuneAccount');
  201.         if ($tuneAccount) {
  202.             $result->where('a.tuneAccount = :tuneAccount');
  203.             $result->setParameter('tuneAccount'$tuneAccount);
  204.         }
  205.         return $result->getQuery()->getResult();
  206.     }
  207.     public function getDisableLinkData()
  208.     {
  209.         $repository $this->em->getRepository('App\Entity\DisableLinks');
  210.         $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();
  211.         return $result;
  212.     }
  213.     public function getGoalDisableLinkByGoalAndOffer($offerId$goalId)
  214.     {
  215.         return $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy([
  216.             'goalId'  => $goalId,
  217.             'offerId' => $offerId
  218.         ]);
  219.     }
  220.     public function insertToGoalDisableLinkByOffer($offerId$offerName$advertiserId$advertiserName$goalId$goalName$budget$sendAlert$setAutoblock$cr$relationshipType)
  221.     {
  222.         $entity = new GoalDisableLinkParams();
  223.         $entity->setOfferName($offerName);
  224.         $entity->setOfferId($offerId);
  225.         $entity->setAdvertiserId($advertiserId);
  226.         $entity->setAdvertiserName($advertiserName);
  227.         $entity->setGoalId($goalId);
  228.         $entity->setGoalName($goalName);
  229.         $entity->setAddedFrom(Config::GOAL_DISABLE_LINK_ADDED_FROM_OFFER);
  230.         $entity->setBudget($budget);
  231.         $entity->setSendAlert($sendAlert);
  232.         $entity->setAutoBlock($setAutoblock);
  233.         $entity->setRelationshipType($relationshipType);
  234.         $entity->setCr($cr);
  235.         $entity->setDateUpdated(new \DateTime('now'));
  236.         $entity->setDateInserted(new \DateTime('now'));
  237.         $this->em->persist($entity);
  238.         $this->em->flush();
  239.     }
  240.     public function getGoalDisableLinkData($offerId null)
  241.     {
  242.         $repository $this->em->getRepository('App\Entity\GoalDisableLinkParams');
  243.         $entity $repository->createQueryBuilder('a')
  244.             ->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')
  245.             ->where('a.offerId > 0');
  246.         if ($offerId) {
  247.             $entity->andWhere('a.offerId = :offerId');
  248.             $entity->setParameter('offerId'$offerId);
  249.         }
  250.         return $entity
  251.             ->getQuery()
  252.             ->getResult();
  253.     }
  254.     public function updateGoalDisableLinks($id$sendAlert$autoBlock)
  255.     {
  256.         $entity $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy(['id' => $id]);
  257.         if ($entity) {
  258.             $entity->setSendAlert($sendAlert);
  259.             $entity->setAutoBlock($autoBlock);
  260.             $this->em->persist($entity);
  261.             $this->em->flush();
  262.         }
  263.     }
  264.     public function getDisableLinkLogs($affiliateArr$offerArr$advertiserArr$sourceArr$addedFrom$startDate$endDate)
  265.     {
  266.         $repository $this->em->getRepository('App\Entity\DisableLinks');
  267.         $result $repository
  268.             ->createQueryBuilder('a')
  269.             ->select(
  270.                 'a.affiliateId',
  271.                 'a.offerId',
  272.                 'a.source',
  273.                 'a.advertiserId',
  274.                 'a.dateInserted',
  275.                 'a.addedFrom',
  276.                 'a.source',
  277.                 'a.meta',
  278.                 'a.id',
  279.                 'a.md5',
  280.                 'a.trackingAccount'
  281.             )
  282.             ->where('a.dateInserted >= :startDate')
  283.             ->andWhere('a.dateInserted <= :endDate')
  284.             ->setParameter('startDate'$startDate)
  285.             ->setParameter('endDate'$endDate);
  286.         if (! empty($affiliateArr)) {
  287.             $result->andWhere('a.affiliateId IN (:affiliateId)');
  288.             $result->setParameter('affiliateId'$affiliateArr);
  289.         }
  290.         if (! empty($offerArr)) {
  291.             $result->andWhere('a.offerId IN (:offerId)');
  292.             $result->setParameter('offerId'$offerArr);
  293.         }
  294.         if (! empty($addedFrom)) {
  295.             $result->andWhere('a.addedFrom IN (:addedFrom)');
  296.             $result->setParameter('addedFrom'$addedFrom);
  297.         }
  298.         if (! empty($advertiserArr)) {
  299.             $result->andWhere('a.advertiserId IN (:advertiserId)');
  300.             $result->setParameter('advertiserId'$advertiserArr);
  301.         }
  302.         if (! empty($sourceArr)) {
  303.             $result->andWhere('a.source IN (:source)');
  304.             $result->setParameter('source'$sourceArr);
  305.         }
  306.         return $result->orderBy('a.id''DESC')->getQuery()->getResult();
  307.     }
  308.     public function getDisableLinkDataByAddedFromAndNotOfAdvertiser($addedFrom$advertiserIdArr)
  309.     {
  310.         $repo $this->em->getRepository('App\Entity\DisableLinks');
  311.         $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([
  312.             'addedFrom'       => $addedFrom,
  313.             'advertiserIdArr' => $advertiserIdArr
  314.         ])->getQuery()->getResult();
  315.         return $entity;
  316.     }
  317.     public function getDisableLinkDataByAddedFrom($addedFrom)
  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''a.id')->where('a.addedFrom = :addedFrom')->setParameters(['addedFrom' => $addedFrom])->getQuery()->getResult();
  321.         return $entity;
  322.     }
  323.     public function getDisableLinkDataByAddedFromAndNotOfOffer($addedFrom$offerIdArr)
  324.     {
  325.         $repo $this->em->getRepository('App\Entity\DisableLinks');
  326.         $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([
  327.             'addedFrom'  => $addedFrom,
  328.             'offerIdArr' => $offerIdArr
  329.         ])->getQuery()->getResult();
  330.         return $entity;
  331.     }
  332.     public function getDisableLinkDataByAddedFromAndNotOfAffiliate($addedFrom$affiliateIdArr)
  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.affiliateId NOT IN (:affiliateIdArr)')->setParameters([
  336.             'addedFrom'      => $addedFrom,
  337.             'affiliateIdArr' => $affiliateIdArr
  338.         ])->getQuery()->getResult();
  339.         return $entity;
  340.     }
  341.     public function deleteDisableLinkByMd5($md5)
  342.     {
  343.         $entity $this->em->getRepository('App\Entity\DisableLinks')->findOneBy(array('md5' => $md5));
  344.         if ($entity != null) {
  345.             $this->em->remove($entity);
  346.             $this->em->flush();
  347.         }
  348.     }
  349.     public function insertToBulkCap($affiliateTagId$affiliateTagName$affiliateOfferCapType$offerId$offerName$capValue$addedBy)
  350.     {
  351.         $entity = new BulkCap();
  352.         $entity->setAffiliateTagId($affiliateTagId);
  353.         $entity->setAffiliateTagName($affiliateTagName);
  354.         $entity->setAffiliateOfferCapType($affiliateOfferCapType);
  355.         $entity->setOfferId($offerId);
  356.         $entity->setOfferName($offerName);
  357.         $entity->setCapValue($capValue);
  358.         $entity->setDateInserted(new \DateTime('now'));
  359.         $entity->setAddedBy($addedBy);
  360.         $this->em->persist($entity);
  361.         $this->em->flush();
  362.     }
  363.     public function deleteFromBulkCapById($id)
  364.     {
  365.         $entity $this->em->getRepository('App\Entity\BulkCap')->findOneBy(['id' => $id]);
  366.         if ($entity != null) {
  367.             $this->em->remove($entity);
  368.             $this->em->flush();
  369.         }
  370.     }
  371.     public function getOfferIdAffiliateTagIdFromBulkCap($offerId$affiliateTagId$capType)
  372.     {
  373.         return $this->em->getRepository('App\Entity\BulkCap')->findOneBy([
  374.             'offerId'               => $offerId,
  375.             'affiliateTagId'        => $affiliateTagId,
  376.             'affiliateOfferCapType' => $capType
  377.         ]);
  378.     }
  379.     public function getBulkCapData($hourRange 4380)
  380.     {
  381.         $dataAfterDatetime date('Y-m-d H:s:i'strtotime("-$hourRange hours"));
  382.         $repo              $this->em->getRepository('App\Entity\BulkCap');
  383.         $entity $repo
  384.             ->createQueryBuilder('a')
  385.             ->select('a.affiliateTagId''a.affiliateTagName''a.affiliateOfferCapType''a.offerId''a.offerName''a.dateInserted''a.id''a.capValue''a.addedBy')
  386.             ->where('a.dateInserted > :dateTime')
  387.             ->setParameters(['dateTime' => $dataAfterDatetime])
  388.             ->orderBy('a.id''DESC')
  389.             ->getQuery()->getResult();
  390.         return $entity;
  391.     }
  392.     public function getBulkCapByAffiliateData($hourRange 48)
  393.     {
  394.         $dataAfterDatetime date('Y-m-d H:s:i'strtotime("-$hourRange hours"));
  395.         $repo              $this->em->getRepository('App\Entity\BulkCapByAffiliate');
  396.         $entity $repo
  397.             ->createQueryBuilder('a')
  398.             ->select('a.affiliateId''a.affiliateName''a.affiliateOfferCapType''a.offerId''a.offerName''a.dateInserted''a.id''a.capValue''a.addedBy''a.tuneAccount')
  399.             ->where('a.dateInserted > :dateTime')
  400.             ->setParameters(['dateTime' => $dataAfterDatetime])
  401.             ->orderBy('a.id''DESC')
  402.             ->getQuery()
  403.             ->getResult();
  404.         return $entity;
  405.     }
  406.     public function getCombinationFromAffiliateOfferCapping($affiliateId$offerId$capType$tuneAccount)
  407.     {
  408.         return $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy([
  409.             'offerId'     => $offerId,
  410.             'affiliateId' => $affiliateId,
  411.             'capType'     => $capType,
  412.             'tuneAccount' => $tuneAccount
  413.         ]);
  414.     }
  415.     public function deleteAffiliateOfferCapById($id)
  416.     {
  417.         $entity $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy(['id' => $id]);
  418.         if ($entity != null) {
  419.             $this->em->remove($entity);
  420.             $this->em->flush();
  421.         }
  422.     }
  423.     public function insertToAffiliateOfferCap($affiliateId$offerId$capType$capValue$tuneAccount)
  424.     {
  425.         $entity = new AffiliateOfferCapping();
  426.         $entity->setAffiliateId($affiliateId);
  427.         $entity->setOfferId($offerId);
  428.         $entity->setCapType($capType);
  429.         $entity->setCapValue($capValue);
  430.         $entity->setDateInserted(new \DateTime('now'));
  431.         $entity->setDateUpdated(new \DateTime('now'));
  432.         $entity->setTuneAccount($tuneAccount);
  433.         $this->em->persist($entity);
  434.         $this->em->flush();
  435.     }
  436.     public function insertToGoalDisableLinkAlertSent($goalId$offerId$affiliateId$source)
  437.     {
  438.         $entity = new GoalDisableLinkAlertSent();
  439.         $entity->setGoalId($goalId);
  440.         $entity->setOfferId($offerId);
  441.         $entity->setAffiliateId($affiliateId);
  442.         $entity->setDateInserted(new \DateTime('now'));
  443.         $entity->setSource($source);
  444.         $this->em->persist($entity);
  445.         $this->em->flush();
  446.     }
  447.     public function checkGoalDisableLinkAlertSent($goalId$offerId$affiliateId$source)
  448.     {
  449.         return $this->em->getRepository('App\Entity\GoalDisableLinkAlertSent')->findOneBy([
  450.             'offerId'     => $offerId,
  451.             'goalId'      => $goalId,
  452.             'affiliateId' => $affiliateId,
  453.             'source'      => $source
  454.         ]);
  455.     }
  456.     public function getRetentionOptimisationByParams($offerId$goalId$tuneAccount)
  457.     {
  458.         return $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
  459.             'offerId' => $offerId,
  460.             'goalId'  => $goalId,
  461.             'tuneAccount' => $tuneAccount
  462.         ]);
  463.     }
  464.     public function updateRetentionOptimisation($offerId$goalId$retentionRate$minimumBudget$sendAlert$autoBlock$tuneAccount)
  465.     {
  466.         $entity $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
  467.             'offerId' => $offerId,
  468.             'goalId'  => $goalId,
  469.             'tuneAccount' => $tuneAccount
  470.         ]);
  471.         if ($entity != null) {
  472.             $entity->setRetentionRate($retentionRate);
  473.             $entity->setMinimumBudget($minimumBudget);
  474.             $entity->setSendAlert($sendAlert);
  475.             $entity->setDateUpdated(new \DateTime('now'));
  476.             $entity->setAutoBlock($autoBlock);
  477.             $entity->setTuneAccount($tuneAccount);
  478.             $this->em->persist($entity);
  479.             $this->em->flush();
  480.         }
  481.     }
  482.     public function insertRetentionOptimisation($offerId$goalId$retentionRate$minimumBudget$sendAlert$autoBlock$tuneAccount)
  483.     {
  484.         $entity = new RetentionOptimisation();
  485.         $entity->setOfferId($offerId);
  486.         $entity->setGoalId($goalId);
  487.         $entity->setRetentionRate($retentionRate);
  488.         $entity->setMinimumBudget($minimumBudget);
  489.         $entity->setSendAlert($sendAlert);
  490.         $entity->setAutoBlock($autoBlock);
  491.         $entity->setDateInserted(new \DateTime('now'));
  492.         $entity->setDateUpdated(new \DateTime('now'));
  493.         $entity->setTuneAccount($tuneAccount);
  494.         $this->em->persist($entity);
  495.         $this->em->flush();
  496.     }
  497.     public function getRetentionOptimisation($offerId$tuneAccount Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE)
  498.     {
  499.         $repo $this->em->getRepository('App\Entity\RetentionOptimisation');
  500.         $entity $repo->createQueryBuilder('a')
  501.             ->select('a.offerId''a.goalId''a.retentionRate''a.minimumBudget''a.sendAlert',
  502.              'a.autoBlock''a.dateUpdated as dateUpdated''a.dateInserted as dateInserted''a.id as id',
  503.               'b.tuneAccount as tuneAccount''c.company as advertiserName''b.name as offerName''b.advertiserId as advertiserId',
  504.               'd.name as goalName'
  505.               )
  506.             ->innerJoin('App\Entity\OfferInfo''b''WITH''a.offerId = b.offerId AND a.tuneAccount = b.tuneAccount')
  507.             ->leftJoin('App\Entity\AdvertiserInfo''c''WITH''b.advertiserId = c.advertiserId AND b.tuneAccount = c.tuneAccount')
  508.             ->leftJoin('App\Entity\OfferGoalsInfo''d''WITH''a.goalId = d.goalId AND a.tuneAccount = d.tuneAccount')
  509.             ->where('a.offerId > 0');
  510.         if ($tuneAccount == Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE) {
  511.             $entity->andWhere('a.tuneAccount = :tuneAccount');
  512.             $entity->setParameter('tuneAccount'$tuneAccount    );
  513.         }
  514.         if ($offerId) {
  515.             $entity->andWhere('a.offerId = :offerId');
  516.             $entity->setParameter('offerId'$offerId);
  517.         }
  518.         return $entity
  519.             ->orderBy('dateUpdated''DESC')
  520.             ->getQuery()
  521.             ->getResult();
  522.     }
  523.     public function deleteRetentionOptimisation($id)
  524.     {
  525.         $entity $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy(['id' => $id]);
  526.         if ($entity != null) {
  527.             $this->em->remove($entity);
  528.             $this->em->flush();
  529.         }
  530.     }
  531.     public function checkScheduledDisableLinksByParam($source$affiliateId$offerId)
  532.     {
  533.         return $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
  534.             'source'      => $source,
  535.             'affiliateId' => $affiliateId,
  536.             'offerId'     => $offerId
  537.         ]);
  538.     }
  539.     public function insertToScheduledDisableLinks($source$affiliate$offer)
  540.     {
  541.         $entity = new ScheduledDisableLinks();
  542.         $entity->setAffiliateId($affiliate);
  543.         $entity->setOfferId($offer);
  544.         $entity->setSource($source);
  545.         $entity->setIsScheduled(1);
  546.         $entity->setDateInserted(new \DateTime('now'));
  547.         $entity->setDateUpdated(new \DateTime('now'));
  548.         $this->em->persist($entity);
  549.         $this->em->flush();
  550.     }
  551.     public function getScheduledDisableLinkByIsScheduled($isScheduled)
  552.     {
  553.         $repo $this->em->getRepository('App\Entity\ScheduledDisableLinks');
  554.         $entity $repo->createQueryBuilder('a')->select('a.offerId''a.affiliateId''a.source''a.isScheduled''a.id')->where('a.isScheduled = :isScheduled')->setParameters(['isScheduled' => $isScheduled])->getQuery()->getResult();
  555.         return $entity;
  556.     }
  557.     public function setIsScheduledForScheduledOfferDisableLink($offerId$affiliateId$source$isScheduled)
  558.     {
  559.         $entity $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
  560.             'offerId'     => $offerId,
  561.             'affiliateId' => $affiliateId,
  562.             'source'      => $source
  563.         ]);
  564.         if ($entity) {
  565.             $entity->setIsScheduled($isScheduled);
  566.             $this->em->persist($entity);
  567.             $this->em->flush();
  568.         }
  569.     }
  570.     public function setIsScheduledForScheduledOfferDisableLinkById($id$isScheduled)
  571.     {
  572.         $entity $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy(['id' => $id]);
  573.         if ($entity) {
  574.             $entity->setIsScheduled($isScheduled);
  575.             $this->em->persist($entity);
  576.             $this->em->flush();
  577.         }
  578.     }
  579.     public function insertToBulkCapByAffiliate($affiliateId$affiliateName$affiliateOfferCapType$offerId$offerName$capValue$addedBy)
  580.     {
  581.         $entity = new BulkCapByAffiliate();
  582.         $entity->setAffiliateId($affiliateId);
  583.         $entity->setAffiliateName($affiliateName);
  584.         $entity->setAffiliateOfferCapType($affiliateOfferCapType);
  585.         $entity->setOfferId($offerId);
  586.         $entity->setOfferName($offerName);
  587.         $entity->setCapValue($capValue);
  588.         $entity->setAddedBy($addedBy);
  589.         $entity->setDateInserted(new \DateTime('now'));
  590.         $this->em->persist($entity);
  591.         $this->em->flush();
  592.     }
  593.     public function getBulkCapByAffiliate($offerId$affiliateId$capType)
  594.     {
  595.         return $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy([
  596.             'offerId'               => $offerId,
  597.             'affiliateId'           => $affiliateId,
  598.             'affiliateOfferCapType' => $capType
  599.         ]);
  600.     }
  601.     public function deleteBulkCapByAffiliateById($id)
  602.     {
  603.         $entity $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy(['id' => $id]);
  604.         if ($entity) {
  605.             $this->em->remove($entity);
  606.             $this->em->flush();
  607.         }
  608.     }
  609.     public function insertToAdvertiserOfferCount($advertiserId$offerCount$status)
  610.     {
  611.         $entity = new AdvertiserOfferCount();
  612.         $entity->setAdvertiserId($advertiserId);
  613.         $entity->setOfferCount($offerCount);
  614.         $entity->setOfferStatus($status);
  615.         $entity->setDateInserted(new \DateTime('now'));
  616.         $this->em->persist($entity);
  617.         $this->em->flush();
  618.     }
  619.     public function getDataForAdvertiserOfferCountForChart($advertiserIdArr$dateStart$dateEnd$chooseBy$status)
  620.     {
  621.         $repo $this->em->getRepository('App\Entity\AdvertiserOfferCount');
  622.         $entity $repo->createQueryBuilder('a');
  623.         if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
  624.             $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');
  625.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
  626.             $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');
  627.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
  628.             $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');
  629.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
  630.             $entity->select('a.advertiserId''AVG(a.offerCount) as average''MONTH(a.dateInserted) as by_month''YEAR(a.dateInserted) as by_year');
  631.         }
  632.         $entity->where('a.advertiserId IN (:advertiserIds)')->andWhere('a.offerStatus = :status')->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
  633.             'advertiserIds' => $advertiserIdArr,
  634.             'status'        => $status,
  635.             'dateStart'     => $dateStart,
  636.             'dateEnd'       => $dateEnd
  637.         ]);
  638.         $entity->groupBy('a.advertiserId');
  639.         if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
  640.             $entity->addGroupBy('by_hour''by_day''by_week''by_month''by_year');
  641.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
  642.             $entity->addGroupBy('by_day''by_week''by_month''by_year');
  643.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
  644.             $entity->addGroupBy('by_week''by_month''by_year');
  645.         } elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
  646.             $entity->addGroupBy('by_month''by_year');
  647.         }
  648.         return $entity->getQuery()->getResult();
  649.     }
  650.     public function getDataForAffiliateOfferCountForChart($affiliateIdArr$dateStart$dateEnd$chooseBy$offerCountBy)
  651.     {
  652.         $repo $this->em->getRepository('App\Entity\AffiliateOfferCount');
  653.         $entity $repo->createQueryBuilder('a');
  654.         if ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_ACTIVE_OFFER_COUNT) {
  655.             if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  656.                 $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');
  657.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  658.                 $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');
  659.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  660.                 $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');
  661.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  662.                 $entity->select('a.affiliateId''AVG(a.approvedActiveOfferCount) as average''MONTH(a.dateInserted) as by_month''YEAR(a.dateInserted) as by_year');
  663.             }
  664.         } elseif ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_OFFER_COUNT) {
  665.             if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  666.                 $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');
  667.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  668.                 $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');
  669.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  670.                 $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');
  671.             } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  672.                 $entity->select('a.affiliateId''AVG(a.approvedOfferCount) as average''MONTH(a.dateInserted) as by_month''YEAR(a.dateInserted) as by_year');
  673.             }
  674.         }
  675.         $entity->where('a.affiliateId IN (:affiliateIds)'#->andWhere('a.offerStatus = :status')
  676.             ->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
  677.                 'affiliateIds' => $affiliateIdArr,
  678.                 'dateStart'    => $dateStart,
  679.                 'dateEnd'      => $dateEnd
  680.             ]);
  681.         $entity->groupBy('a.affiliateId');
  682.         if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
  683.             $entity->addGroupBy('by_hour''by_day''by_week''by_month''by_year');
  684.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
  685.             $entity->addGroupBy('by_day''by_week''by_month''by_year');
  686.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
  687.             $entity->addGroupBy('by_week''by_month''by_year');
  688.         } elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
  689.             $entity->addGroupBy('by_month''by_year');
  690.         }
  691.         return $entity->getQuery()->getResult();
  692.     }
  693.     public function getImpressionOptimisationByParams($offerId$affiliateId)
  694.     {
  695.         return $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy([
  696.             'offerId'     => $offerId,
  697.             'affiliateId' => $affiliateId
  698.         ]);
  699.     }
  700.     public function deleteImpressionOptimisation($id$deletedBy)
  701.     {
  702.         $entity $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy(['id' => $id]);
  703.         if ($entity != null) {
  704.             $entity->setAddedBy($deletedBy);
  705.             $entity->setDateUpdated(new \DateTime('now'));
  706.             $entity->setIsDeleted(true);
  707.             $this->em->persist($entity);
  708.             $this->em->flush();
  709.         }
  710.         return $entity;
  711.     }
  712.     public function getActiveImpressionOptimisation()
  713.     {
  714.         $repo   $this->em->getRepository('App\Entity\ImpressionOptimisation');
  715.         $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();
  716.         return $entity;
  717.     }
  718.     public function createUpdateImpression($offerId$affiliateId$ctr$addedBy)
  719.     {
  720.         $entity $this->getImpressionOptimisationByParams($offerId$affiliateId);
  721.         if ($entity != null) {
  722.             $entity->setCtr($ctr);
  723.             $entity->setAddedBy($addedBy);
  724.             $entity->setDateUpdated(new \DateTime('now'));
  725.             $this->em->persist($entity);
  726.             $this->em->flush();
  727.         } else {
  728.             $entity $this->insertImpressionOptimisation($offerId$affiliateId$ctr$addedBy);
  729.         }
  730.         return $entity;
  731.     }
  732.     public function insertImpressionOptimisation($offerId$affiliateId$ctr$addedBy)
  733.     {
  734.         $entity = new ImpressionOptimisation();
  735.         $entity->setAddedBy($addedBy);
  736.         $entity->setCtr($ctr);
  737.         $entity->setAffiliateId($affiliateId);
  738.         $entity->setOfferId($offerId);
  739.         $entity->setDateUpdated(new \DateTime('now'));
  740.         $entity->setDateInserted(new \DateTime('now'));
  741.         $entity->setIsDeleted(false);
  742.         $this->em->persist($entity);
  743.         $this->em->flush();
  744.         return $entity;
  745.     }
  746.     public function getFraudFlagLogs($affiliateArr$offerArr$advertiserArr$startDate$endDate)
  747.     {
  748.         $repository $this->em->getRepository('App\Entity\FraudFlagLogs');
  749.         $result $repository->createQueryBuilder('a')
  750.             ->select('a.affiliateId''a.offerId''a.source''a.advertiserId''a.dateInserted''a.advSub1''a.source''a.id''a.transactionId')
  751.             ->where('a.dateInserted >= :startDate')
  752.             ->andWhere('a.dateInserted <= :endDate')
  753.             ->andWhere('a.newStatus <= :newStatus')
  754.             ->setParameter('startDate'$startDate)
  755.             ->setParameter('endDate'$endDate)
  756.             ->setParameter('newStatus'Config::REJECTED_STATUS);
  757.         if (! empty($affiliateArr)) {
  758.             $result->andWhere('a.affiliateId IN (:affiliateId)');
  759.             $result->setParameter('affiliateId'$affiliateArr);
  760.         }
  761.         if (! empty($offerArr)) {
  762.             $result->andWhere('a.offerId IN (:offerId)');
  763.             $result->setParameter('offerId'$offerArr);
  764.         }
  765.         if (! empty($advertiserArr)) {
  766.             $result->andWhere('a.advertiserId IN (:advertiserId)');
  767.             $result->setParameter('advertiserId'$advertiserArr);
  768.         }
  769.         return $result->orderBy('a.id''DESC')->getQuery()->getResult();
  770.     }
  771.     public function deleteFraudFlagLogs($id)
  772.     {
  773.         $entity $this->em->getRepository('App\Entity\FraudFlagLogs')->findOneBy(array('id' => $id));
  774.         if ($entity != null) {
  775.             $this->em->remove($entity);
  776.             $this->em->flush();
  777.         }
  778.     }
  779. }