<?php
namespace App\Services;
use App\Config;
use App\Entity\AdvertiserOfferCount;
use App\Entity\AffiliateOfferCapping;
use App\Entity\AutomaticDisableLinkParams;
use App\Entity\BulkCap;
use App\Entity\BulkCapByAffiliate;
use App\Entity\DisableLinks;
use App\Entity\DisableLinksDump;
use App\Entity\GoalDisableLinkAlertSent;
use App\Entity\GoalDisableLinkParams;
use App\Entity\RetentionOptimisation;
use App\Entity\ScheduledDisableLinks;
use App\Entity\SourceBlock;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\ImpressionOptimisation;
/**
*
* Mysql queries were written here before Mysql quries were written in Repository corresponding to Entity
*
* Class MysqlQueries
* @package App\Services
*/
class MysqlQueries
{
private $em;
/**
* MysqlQueries constructor.
*
* @param $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
}
public function insertToSourceBlock($affiliateId, $source, $advertiserId)
{
$combinationExist = $this->checkSourceBlockExist($affiliateId, $source, $advertiserId);
if ($combinationExist != null) {
return;
}
$entity = new SourceBlock();
$entity->setAffiliateId($affiliateId);
$entity->setSource($source);
$entity->setAdvertiserId($advertiserId);
$entity->setDateInserted(new \DateTime('now'));
$entity->setDateUpdated(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
}
public function checkSourceBlockExist($affiliateId, $source, $advertiserId)
{
return $this->em->getRepository('App\Entity\SourceBlock')->findOneBy([
'advertiserId' => $advertiserId,
'affiliateId' => $affiliateId,
'source' => $source
]);
}
public function getSourceBlock()
{
return $this->em->getRepository('App\Entity\SourceBlock')->findAll();
}
public function deleteSourceBlockById($id)
{
$entity = $this->em->getRepository('App\Entity\SourceBlock')->findOneBy(array('id' => $id));
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function deleteAutomaticDisableLink($id)
{
$entity = $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(array('id' => $id));
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function deleteGoalDisableLink($id)
{
$entity = $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy(array('id' => $id));
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function getSourceBlockData()
{
$repository = $this->em->getRepository('App\Entity\SourceBlock');
$result = $repository->createQueryBuilder('a')->select('a.affiliateId', 'a.source', 'a.advertiserId')->getQuery()->getResult();
return $result;
}
public function getDisableLinkByMd5($md5)
{
return $this->em->getRepository('App\Entity\DisableLinks')->findOneBy(['md5' => $md5]);
}
public function getDisableLinksByMd5Arr($md5Arr)
{
$repo = $this->em->getRepository('App\Entity\DisableLinks');
$entity = $repo->createQueryBuilder('a')
->select('a.offerId', 'a.affiliateId', 'a.source', 'a.addedFrom', 'a.md5', 'a.meta', 'a.trackingAccount')
->where('a.md5 IN (:md5Arr)')
->setParameters(['md5Arr' => $md5Arr])
->getQuery()
->getResult();
return $entity;
}
public function insertToDisableLink($md5, $offerId, $affiliateId, $source, $affsub2, $affSub3, $affSub5, $addedFrom, $advertiserId, $metaData, $trackingAccount = Config::TUNE_ACCOUNT_DEFAULT)
{
$entity = new DisableLinks();
$entity->setMd5($md5);
$entity->setOfferId($offerId);
$entity->setAffiliateId($affiliateId);
$entity->setSource($source);
$entity->setAffsub2($affsub2);
$entity->setAffsub3($affSub3);
$entity->setAffsub5($affSub5);
$entity->setTrackingAccount($trackingAccount);
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedFrom($addedFrom);
$entity->setAdvertiserId($advertiserId);
$entity->setmeta($metaData);
$this->em->persist($entity);
$this->em->flush();
}
public function insertToDisableLinkDump($md5, $offerId, $affiliateId, $source, $affsub2, $addedFrom, $advertiserId, $metaData, $wasAddedOn, $affsub3, $affsub5)
{
$entity = new DisableLinksDump();
$entity->setMd5($md5);
$entity->setOfferId($offerId);
$entity->setAffiliateId($affiliateId);
$entity->setSource($source);
$entity->setAffsub2($affsub2);
$entity->setAffsub3($affsub3);
$entity->setAffsub5($affsub5);
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedFrom($addedFrom);
$entity->setAdvertiserId($advertiserId);
$entity->setMeta($metaData);
$entity->setWasAddedOn($wasAddedOn);
$this->em->persist($entity);
$this->em->flush();
}
public function checkAutomaticDisableLinkByAdvertiser($advertiserId, $tuneAccount)
{
return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['advertiserId' => $advertiserId, 'tuneAccount' => $tuneAccount]);
}
public function checkAutomaticDisableLinkByAffiliate($affiliateId, $tuneAccount)
{
return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['affiliateId' => $affiliateId, 'tuneAccount' => $tuneAccount]);
}
public function checkAutomaticDisableLinkByOffer($offerId, $tuneAccount)
{
return $this->em->getRepository('App\Entity\AutomaticDisableLinkParams')->findOneBy(['offerId' => $offerId, 'tuneAccount' => $tuneAccount]);
}
public function saveAutomaticDisableLinkByAdvertiser($advertiserId, $advertiserName, $tuneAccount)
{
$entity = new AutomaticDisableLinkParams();
$entity->setAdvertiserId($advertiserId);
$entity->setAdvertiserName($advertiserName);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_ADVERTISER);
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
public function saveAutomaticDisableLinkByAffiliate($affiliateId, $affiliateName, $tuneAccount)
{
$entity = new AutomaticDisableLinkParams();
$entity->setAffiliateId($affiliateId);
$entity->setAffiliateName($affiliateName);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_AFFILIATE);
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
public function saveAutomaticDisableLinkByOffer($offerId, $offerName, $tuneAccount)
{
$entity = new AutomaticDisableLinkParams();
$entity->setOfferId($offerId);
$entity->setOfferName($offerName);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedFrom(Config::AUTOMATIC_DISABLE_LINK_ADDED_BY_OFFER);
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
public function getAutomaticDisableLinkData($tuneAccount = Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE)
{
$repository = $this->em->getRepository('App\Entity\AutomaticDisableLinkParams');
$result = $repository
->createQueryBuilder('a')
->select('a.affiliateId', 'a.offerId', 'a.advertiserId', 'a.advertiserName',
'a.affiliateName', 'a.offerName', 'a.dateInserted', 'a.id', 'a.tuneAccount');
if ($tuneAccount) {
$result->where('a.tuneAccount = :tuneAccount');
$result->setParameter('tuneAccount', $tuneAccount);
}
return $result->getQuery()->getResult();
}
public function getDisableLinkData()
{
$repository = $this->em->getRepository('App\Entity\DisableLinks');
$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();
return $result;
}
public function getGoalDisableLinkByGoalAndOffer($offerId, $goalId)
{
return $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy([
'goalId' => $goalId,
'offerId' => $offerId
]);
}
public function insertToGoalDisableLinkByOffer($offerId, $offerName, $advertiserId, $advertiserName, $goalId, $goalName, $budget, $sendAlert, $setAutoblock, $cr, $relationshipType)
{
$entity = new GoalDisableLinkParams();
$entity->setOfferName($offerName);
$entity->setOfferId($offerId);
$entity->setAdvertiserId($advertiserId);
$entity->setAdvertiserName($advertiserName);
$entity->setGoalId($goalId);
$entity->setGoalName($goalName);
$entity->setAddedFrom(Config::GOAL_DISABLE_LINK_ADDED_FROM_OFFER);
$entity->setBudget($budget);
$entity->setSendAlert($sendAlert);
$entity->setAutoBlock($setAutoblock);
$entity->setRelationshipType($relationshipType);
$entity->setCr($cr);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setDateInserted(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
}
public function getGoalDisableLinkData($offerId = null)
{
$repository = $this->em->getRepository('App\Entity\GoalDisableLinkParams');
$entity = $repository->createQueryBuilder('a')
->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')
->where('a.offerId > 0');
if ($offerId) {
$entity->andWhere('a.offerId = :offerId');
$entity->setParameter('offerId', $offerId);
}
return $entity
->getQuery()
->getResult();
}
public function updateGoalDisableLinks($id, $sendAlert, $autoBlock)
{
$entity = $this->em->getRepository('App\Entity\GoalDisableLinkParams')->findOneBy(['id' => $id]);
if ($entity) {
$entity->setSendAlert($sendAlert);
$entity->setAutoBlock($autoBlock);
$this->em->persist($entity);
$this->em->flush();
}
}
public function getDisableLinkLogs($affiliateArr, $offerArr, $advertiserArr, $sourceArr, $addedFrom, $startDate, $endDate)
{
$repository = $this->em->getRepository('App\Entity\DisableLinks');
$result = $repository
->createQueryBuilder('a')
->select(
'a.affiliateId',
'a.offerId',
'a.source',
'a.advertiserId',
'a.dateInserted',
'a.addedFrom',
'a.source',
'a.meta',
'a.id',
'a.md5',
'a.trackingAccount'
)
->where('a.dateInserted >= :startDate')
->andWhere('a.dateInserted <= :endDate')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate);
if (! empty($affiliateArr)) {
$result->andWhere('a.affiliateId IN (:affiliateId)');
$result->setParameter('affiliateId', $affiliateArr);
}
if (! empty($offerArr)) {
$result->andWhere('a.offerId IN (:offerId)');
$result->setParameter('offerId', $offerArr);
}
if (! empty($addedFrom)) {
$result->andWhere('a.addedFrom IN (:addedFrom)');
$result->setParameter('addedFrom', $addedFrom);
}
if (! empty($advertiserArr)) {
$result->andWhere('a.advertiserId IN (:advertiserId)');
$result->setParameter('advertiserId', $advertiserArr);
}
if (! empty($sourceArr)) {
$result->andWhere('a.source IN (:source)');
$result->setParameter('source', $sourceArr);
}
return $result->orderBy('a.id', 'DESC')->getQuery()->getResult();
}
public function getDisableLinkDataByAddedFromAndNotOfAdvertiser($addedFrom, $advertiserIdArr)
{
$repo = $this->em->getRepository('App\Entity\DisableLinks');
$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([
'addedFrom' => $addedFrom,
'advertiserIdArr' => $advertiserIdArr
])->getQuery()->getResult();
return $entity;
}
public function getDisableLinkDataByAddedFrom($addedFrom)
{
$repo = $this->em->getRepository('App\Entity\DisableLinks');
$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();
return $entity;
}
public function getDisableLinkDataByAddedFromAndNotOfOffer($addedFrom, $offerIdArr)
{
$repo = $this->em->getRepository('App\Entity\DisableLinks');
$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([
'addedFrom' => $addedFrom,
'offerIdArr' => $offerIdArr
])->getQuery()->getResult();
return $entity;
}
public function getDisableLinkDataByAddedFromAndNotOfAffiliate($addedFrom, $affiliateIdArr)
{
$repo = $this->em->getRepository('App\Entity\DisableLinks');
$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([
'addedFrom' => $addedFrom,
'affiliateIdArr' => $affiliateIdArr
])->getQuery()->getResult();
return $entity;
}
public function deleteDisableLinkByMd5($md5)
{
$entity = $this->em->getRepository('App\Entity\DisableLinks')->findOneBy(array('md5' => $md5));
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function insertToBulkCap($affiliateTagId, $affiliateTagName, $affiliateOfferCapType, $offerId, $offerName, $capValue, $addedBy)
{
$entity = new BulkCap();
$entity->setAffiliateTagId($affiliateTagId);
$entity->setAffiliateTagName($affiliateTagName);
$entity->setAffiliateOfferCapType($affiliateOfferCapType);
$entity->setOfferId($offerId);
$entity->setOfferName($offerName);
$entity->setCapValue($capValue);
$entity->setDateInserted(new \DateTime('now'));
$entity->setAddedBy($addedBy);
$this->em->persist($entity);
$this->em->flush();
}
public function deleteFromBulkCapById($id)
{
$entity = $this->em->getRepository('App\Entity\BulkCap')->findOneBy(['id' => $id]);
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function getOfferIdAffiliateTagIdFromBulkCap($offerId, $affiliateTagId, $capType)
{
return $this->em->getRepository('App\Entity\BulkCap')->findOneBy([
'offerId' => $offerId,
'affiliateTagId' => $affiliateTagId,
'affiliateOfferCapType' => $capType
]);
}
public function getBulkCapData($hourRange = 4380)
{
$dataAfterDatetime = date('Y-m-d H:s:i', strtotime("-$hourRange hours"));
$repo = $this->em->getRepository('App\Entity\BulkCap');
$entity = $repo
->createQueryBuilder('a')
->select('a.affiliateTagId', 'a.affiliateTagName', 'a.affiliateOfferCapType', 'a.offerId', 'a.offerName', 'a.dateInserted', 'a.id', 'a.capValue', 'a.addedBy')
->where('a.dateInserted > :dateTime')
->setParameters(['dateTime' => $dataAfterDatetime])
->orderBy('a.id', 'DESC')
->getQuery()->getResult();
return $entity;
}
public function getBulkCapByAffiliateData($hourRange = 48)
{
$dataAfterDatetime = date('Y-m-d H:s:i', strtotime("-$hourRange hours"));
$repo = $this->em->getRepository('App\Entity\BulkCapByAffiliate');
$entity = $repo
->createQueryBuilder('a')
->select('a.affiliateId', 'a.affiliateName', 'a.affiliateOfferCapType', 'a.offerId', 'a.offerName', 'a.dateInserted', 'a.id', 'a.capValue', 'a.addedBy', 'a.tuneAccount')
->where('a.dateInserted > :dateTime')
->setParameters(['dateTime' => $dataAfterDatetime])
->orderBy('a.id', 'DESC')
->getQuery()
->getResult();
return $entity;
}
public function getCombinationFromAffiliateOfferCapping($affiliateId, $offerId, $capType, $tuneAccount)
{
return $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy([
'offerId' => $offerId,
'affiliateId' => $affiliateId,
'capType' => $capType,
'tuneAccount' => $tuneAccount
]);
}
public function deleteAffiliateOfferCapById($id)
{
$entity = $this->em->getRepository('App\Entity\AffiliateOfferCapping')->findOneBy(['id' => $id]);
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function insertToAffiliateOfferCap($affiliateId, $offerId, $capType, $capValue, $tuneAccount)
{
$entity = new AffiliateOfferCapping();
$entity->setAffiliateId($affiliateId);
$entity->setOfferId($offerId);
$entity->setCapType($capType);
$entity->setCapValue($capValue);
$entity->setDateInserted(new \DateTime('now'));
$entity->setDateUpdated(new \DateTime('now'));
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
public function insertToGoalDisableLinkAlertSent($goalId, $offerId, $affiliateId, $source)
{
$entity = new GoalDisableLinkAlertSent();
$entity->setGoalId($goalId);
$entity->setOfferId($offerId);
$entity->setAffiliateId($affiliateId);
$entity->setDateInserted(new \DateTime('now'));
$entity->setSource($source);
$this->em->persist($entity);
$this->em->flush();
}
public function checkGoalDisableLinkAlertSent($goalId, $offerId, $affiliateId, $source)
{
return $this->em->getRepository('App\Entity\GoalDisableLinkAlertSent')->findOneBy([
'offerId' => $offerId,
'goalId' => $goalId,
'affiliateId' => $affiliateId,
'source' => $source
]);
}
public function getRetentionOptimisationByParams($offerId, $goalId, $tuneAccount)
{
return $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
'offerId' => $offerId,
'goalId' => $goalId,
'tuneAccount' => $tuneAccount
]);
}
public function updateRetentionOptimisation($offerId, $goalId, $retentionRate, $minimumBudget, $sendAlert, $autoBlock, $tuneAccount)
{
$entity = $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy([
'offerId' => $offerId,
'goalId' => $goalId,
'tuneAccount' => $tuneAccount
]);
if ($entity != null) {
$entity->setRetentionRate($retentionRate);
$entity->setMinimumBudget($minimumBudget);
$entity->setSendAlert($sendAlert);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setAutoBlock($autoBlock);
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
}
public function insertRetentionOptimisation($offerId, $goalId, $retentionRate, $minimumBudget, $sendAlert, $autoBlock, $tuneAccount)
{
$entity = new RetentionOptimisation();
$entity->setOfferId($offerId);
$entity->setGoalId($goalId);
$entity->setRetentionRate($retentionRate);
$entity->setMinimumBudget($minimumBudget);
$entity->setSendAlert($sendAlert);
$entity->setAutoBlock($autoBlock);
$entity->setDateInserted(new \DateTime('now'));
$entity->setDateUpdated(new \DateTime('now'));
$entity->setTuneAccount($tuneAccount);
$this->em->persist($entity);
$this->em->flush();
}
public function getRetentionOptimisation($offerId, $tuneAccount = Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE)
{
$repo = $this->em->getRepository('App\Entity\RetentionOptimisation');
$entity = $repo->createQueryBuilder('a')
->select('a.offerId', 'a.goalId', 'a.retentionRate', 'a.minimumBudget', 'a.sendAlert',
'a.autoBlock', 'a.dateUpdated as dateUpdated', 'a.dateInserted as dateInserted', 'a.id as id',
'b.tuneAccount as tuneAccount', 'c.company as advertiserName', 'b.name as offerName', 'b.advertiserId as advertiserId',
'd.name as goalName'
)
->innerJoin('App\Entity\OfferInfo', 'b', 'WITH', 'a.offerId = b.offerId AND a.tuneAccount = b.tuneAccount')
->leftJoin('App\Entity\AdvertiserInfo', 'c', 'WITH', 'b.advertiserId = c.advertiserId AND b.tuneAccount = c.tuneAccount')
->leftJoin('App\Entity\OfferGoalsInfo', 'd', 'WITH', 'a.goalId = d.goalId AND a.tuneAccount = d.tuneAccount')
->where('a.offerId > 0');
if ($tuneAccount == Config::MAFO_SYSTEM_IDENTIFIER_TUNE_MOBILE) {
$entity->andWhere('a.tuneAccount = :tuneAccount');
$entity->setParameter('tuneAccount', $tuneAccount );
}
if ($offerId) {
$entity->andWhere('a.offerId = :offerId');
$entity->setParameter('offerId', $offerId);
}
return $entity
->orderBy('dateUpdated', 'DESC')
->getQuery()
->getResult();
}
public function deleteRetentionOptimisation($id)
{
$entity = $this->em->getRepository('App\Entity\RetentionOptimisation')->findOneBy(['id' => $id]);
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function checkScheduledDisableLinksByParam($source, $affiliateId, $offerId)
{
return $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
'source' => $source,
'affiliateId' => $affiliateId,
'offerId' => $offerId
]);
}
public function insertToScheduledDisableLinks($source, $affiliate, $offer)
{
$entity = new ScheduledDisableLinks();
$entity->setAffiliateId($affiliate);
$entity->setOfferId($offer);
$entity->setSource($source);
$entity->setIsScheduled(1);
$entity->setDateInserted(new \DateTime('now'));
$entity->setDateUpdated(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
}
public function getScheduledDisableLinkByIsScheduled($isScheduled)
{
$repo = $this->em->getRepository('App\Entity\ScheduledDisableLinks');
$entity = $repo->createQueryBuilder('a')->select('a.offerId', 'a.affiliateId', 'a.source', 'a.isScheduled', 'a.id')->where('a.isScheduled = :isScheduled')->setParameters(['isScheduled' => $isScheduled])->getQuery()->getResult();
return $entity;
}
public function setIsScheduledForScheduledOfferDisableLink($offerId, $affiliateId, $source, $isScheduled)
{
$entity = $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy([
'offerId' => $offerId,
'affiliateId' => $affiliateId,
'source' => $source
]);
if ($entity) {
$entity->setIsScheduled($isScheduled);
$this->em->persist($entity);
$this->em->flush();
}
}
public function setIsScheduledForScheduledOfferDisableLinkById($id, $isScheduled)
{
$entity = $this->em->getRepository('App\Entity\ScheduledDisableLinks')->findOneBy(['id' => $id]);
if ($entity) {
$entity->setIsScheduled($isScheduled);
$this->em->persist($entity);
$this->em->flush();
}
}
public function insertToBulkCapByAffiliate($affiliateId, $affiliateName, $affiliateOfferCapType, $offerId, $offerName, $capValue, $addedBy)
{
$entity = new BulkCapByAffiliate();
$entity->setAffiliateId($affiliateId);
$entity->setAffiliateName($affiliateName);
$entity->setAffiliateOfferCapType($affiliateOfferCapType);
$entity->setOfferId($offerId);
$entity->setOfferName($offerName);
$entity->setCapValue($capValue);
$entity->setAddedBy($addedBy);
$entity->setDateInserted(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
}
public function getBulkCapByAffiliate($offerId, $affiliateId, $capType)
{
return $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy([
'offerId' => $offerId,
'affiliateId' => $affiliateId,
'affiliateOfferCapType' => $capType
]);
}
public function deleteBulkCapByAffiliateById($id)
{
$entity = $this->em->getRepository('App\Entity\BulkCapByAffiliate')->findOneBy(['id' => $id]);
if ($entity) {
$this->em->remove($entity);
$this->em->flush();
}
}
public function insertToAdvertiserOfferCount($advertiserId, $offerCount, $status)
{
$entity = new AdvertiserOfferCount();
$entity->setAdvertiserId($advertiserId);
$entity->setOfferCount($offerCount);
$entity->setOfferStatus($status);
$entity->setDateInserted(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
}
public function getDataForAdvertiserOfferCountForChart($advertiserIdArr, $dateStart, $dateEnd, $chooseBy, $status)
{
$repo = $this->em->getRepository('App\Entity\AdvertiserOfferCount');
$entity = $repo->createQueryBuilder('a');
if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
$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');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
$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');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
$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');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
$entity->select('a.advertiserId', 'AVG(a.offerCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
}
$entity->where('a.advertiserId IN (:advertiserIds)')->andWhere('a.offerStatus = :status')->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
'advertiserIds' => $advertiserIdArr,
'status' => $status,
'dateStart' => $dateStart,
'dateEnd' => $dateEnd
]);
$entity->groupBy('a.advertiserId');
if ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_HOUR) {
$entity->addGroupBy('by_hour', 'by_day', 'by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_DAY) {
$entity->addGroupBy('by_day', 'by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_WEEK) {
$entity->addGroupBy('by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::ADVERTISER_INFO_CHOOSE_BY_MONTH) {
$entity->addGroupBy('by_month', 'by_year');
}
return $entity->getQuery()->getResult();
}
public function getDataForAffiliateOfferCountForChart($affiliateIdArr, $dateStart, $dateEnd, $chooseBy, $offerCountBy)
{
$repo = $this->em->getRepository('App\Entity\AffiliateOfferCount');
$entity = $repo->createQueryBuilder('a');
if ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_ACTIVE_OFFER_COUNT) {
if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
$entity->select('a.affiliateId', 'AVG(a.approvedActiveOfferCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
}
} elseif ($offerCountBy == Config::AFFILIATE_INFO_CHOOSE_BY_APPROVED_OFFER_COUNT) {
if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
$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');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
$entity->select('a.affiliateId', 'AVG(a.approvedOfferCount) as average', 'MONTH(a.dateInserted) as by_month', 'YEAR(a.dateInserted) as by_year');
}
}
$entity->where('a.affiliateId IN (:affiliateIds)') #->andWhere('a.offerStatus = :status')
->andWhere('a.dateInserted >= :dateStart')->andWhere('a.dateInserted <= :dateEnd')->setParameters([
'affiliateIds' => $affiliateIdArr,
'dateStart' => $dateStart,
'dateEnd' => $dateEnd
]);
$entity->groupBy('a.affiliateId');
if ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_HOUR) {
$entity->addGroupBy('by_hour', 'by_day', 'by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_DAY) {
$entity->addGroupBy('by_day', 'by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_WEEK) {
$entity->addGroupBy('by_week', 'by_month', 'by_year');
} elseif ($chooseBy == Config::AFFILIATE_INFO_CHOOSE_BY_MONTH) {
$entity->addGroupBy('by_month', 'by_year');
}
return $entity->getQuery()->getResult();
}
public function getImpressionOptimisationByParams($offerId, $affiliateId)
{
return $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy([
'offerId' => $offerId,
'affiliateId' => $affiliateId
]);
}
public function deleteImpressionOptimisation($id, $deletedBy)
{
$entity = $this->em->getRepository('App\Entity\ImpressionOptimisation')->findOneBy(['id' => $id]);
if ($entity != null) {
$entity->setAddedBy($deletedBy);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setIsDeleted(true);
$this->em->persist($entity);
$this->em->flush();
}
return $entity;
}
public function getActiveImpressionOptimisation()
{
$repo = $this->em->getRepository('App\Entity\ImpressionOptimisation');
$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();
return $entity;
}
public function createUpdateImpression($offerId, $affiliateId, $ctr, $addedBy)
{
$entity = $this->getImpressionOptimisationByParams($offerId, $affiliateId);
if ($entity != null) {
$entity->setCtr($ctr);
$entity->setAddedBy($addedBy);
$entity->setDateUpdated(new \DateTime('now'));
$this->em->persist($entity);
$this->em->flush();
} else {
$entity = $this->insertImpressionOptimisation($offerId, $affiliateId, $ctr, $addedBy);
}
return $entity;
}
public function insertImpressionOptimisation($offerId, $affiliateId, $ctr, $addedBy)
{
$entity = new ImpressionOptimisation();
$entity->setAddedBy($addedBy);
$entity->setCtr($ctr);
$entity->setAffiliateId($affiliateId);
$entity->setOfferId($offerId);
$entity->setDateUpdated(new \DateTime('now'));
$entity->setDateInserted(new \DateTime('now'));
$entity->setIsDeleted(false);
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
public function getFraudFlagLogs($affiliateArr, $offerArr, $advertiserArr, $startDate, $endDate)
{
$repository = $this->em->getRepository('App\Entity\FraudFlagLogs');
$result = $repository->createQueryBuilder('a')
->select('a.affiliateId', 'a.offerId', 'a.source', 'a.advertiserId', 'a.dateInserted', 'a.advSub1', 'a.source', 'a.id', 'a.transactionId')
->where('a.dateInserted >= :startDate')
->andWhere('a.dateInserted <= :endDate')
->andWhere('a.newStatus <= :newStatus')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->setParameter('newStatus', Config::REJECTED_STATUS);
if (! empty($affiliateArr)) {
$result->andWhere('a.affiliateId IN (:affiliateId)');
$result->setParameter('affiliateId', $affiliateArr);
}
if (! empty($offerArr)) {
$result->andWhere('a.offerId IN (:offerId)');
$result->setParameter('offerId', $offerArr);
}
if (! empty($advertiserArr)) {
$result->andWhere('a.advertiserId IN (:advertiserId)');
$result->setParameter('advertiserId', $advertiserArr);
}
return $result->orderBy('a.id', 'DESC')->getQuery()->getResult();
}
public function deleteFraudFlagLogs($id)
{
$entity = $this->em->getRepository('App\Entity\FraudFlagLogs')->findOneBy(array('id' => $id));
if ($entity != null) {
$this->em->remove($entity);
$this->em->flush();
}
}
}