<?php
namespace WebBundle\Repository;
use DateTime;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use FlexApp\Constant\TimeConstant;
use Import1CBundle\Helper\v3\BiConst;
use WebBundle\Entity\InteriorHistory;
use WebBundle\Helper\App;
use WebBundle\Helper\LocaleHelper;
/**
* InteriorHistoriesRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class InteriorHistoryRepository extends ExtendEntityRepository
{
/**
* @param $interior
* @param $idea
* @return int|mixed|string|null
* @throws NonUniqueResultException
*/
public function getInteriorHistory($interior, $idea)
{
$r = $this->createQueryBuilder('ih')
->andWhere('ih.interior = :interior')
->andWhere('ih.idea = :idea')
->setParameters([
'interior' => $interior,
'idea' => $idea,
])
->getQuery()
->setMaxResults(1)
->useQueryCache(true);
try {
return $r->getSingleResult();
} catch (NoResultException $e) {
return null;
}
}
/**
* Получение TOPа коллекций
*
* @param $days
* @param int $limit
* @return array|null
* @internal param DateTime $date
* @internal param int $maxResult
*
*/
public function getTopCollections($days, $limit = 20): ?array
{
$date = new DateTime();
$date->modify('-' . $days . 'day')
->setTime(23, 59);
$r = $this->createQueryBuilder('ih')
->addSelect('c.id, count(c.id) as ac')
->leftJoin('ih.interior', 'ir')
->leftJoin('ir.collection', 'c')
->leftJoin('c.factory', 'f')
->leftJoin('c.interiors', 'i')
->andWhere('c.status = :c_status')
->andWhere('c.factory != :factory')
->andWhere('f.status = :f_status')
->andWhere('i.isMain = true')
->andWhere('ih.date >= :date')
->andWhere('ih.action = :action')
->setParameters([
'factory' => '',
'action' => InteriorHistory::ADD,
'date' => $date->format('Y-m-d H:i:s'),
'f_status' => BiConst::STATE_PUBLISHED,
'c_status' => BiConst::STATE_PUBLISHED,
])
->setMaxResults($limit)
->groupBy('c.id')
->orderBy('ac DESC, ih.date', 'DESC')
->getQuery();
if (!defined('DB_NOT_CASHED')) {
$r->useQueryCache(true)
->enableResultCache((int) TimeConstant::DAY); // сутки
}
$output = [];
foreach ($r->getArrayResult() as $key => $item) {
$output[] = $item['id'];
}
return $output;
}
/**
* @param int $days
* @param $page
* @param int $limit
* @return array|null
*/
public function getCollections(int $days, $page, $limit = 20): ?array
{
$memcache = App::getMemcache();
$memKey = 'getColls.' . md5(
$days . $page . $limit . App::getCurLocale() . LocaleHelper::getCur() . LocaleHelper::getUserMeasure()
);
$output = $memcache->get($memKey);
if (!$output) {
$date = new DateTime();
$date->modify('-' . $days . 'day')
->setTime(23, 59);
$r = $this->createQueryBuilder('ih')
->select('c.id, count(c.id) as ac')
->leftJoin('ih.interior', 'ir')
->leftJoin('ir.collection', 'c')
->leftJoin('c.interiors', 'i')
->leftJoin('c.factory', 'f')
->andWhere('c.status = :status')
->andWhere('c.showMain = :showMain')
->andWhere('c.factory != :factory')
->andWhere('f.status = :status')
->andWhere('i.isMain = true')
->andWhere('ih.date >= :date')
->andWhere('ih.action = :action')
->setParameters([
'factory' => '',
'action' => InteriorHistory::ADD,
'date' => $date->format('Y-m-d H:i:s'),
'status' => BiConst::STATE_PUBLISHED,
'showMain' => true
])
->setMaxResults($limit)
->setFirstResult($limit * $page - $limit)
->groupBy('c.id')
->orderBy('ac DESC, ih.date', 'DESC')
->getQuery()// ->enableResultCache(120)
;
$output = [];
$new = $r->getArrayResult();
$date2 = new DateTime();
$date2->modify('-' . ($days + 1) . 'day')
->setTime(23, 59);
$ides = [0];
foreach ($new as $key => $item) {
$ides[] = $item['id'];
}
$nowDt = new DateTime();
$q = $this->createQueryBuilder('ih');
$r2 = $q->addSelect('c.id, count(c.id) as ac')
->leftJoin('ih.interior', 'ir')
->leftJoin('ir.collection', 'c')
->leftJoin('c.interiors', 'i')
->leftJoin('c.factory', 'f')
->andWhere('c.status = :status')
->andWhere('c.showMain = :showMain')
->andWhere('c.factory != :factory')
->andWhere('f.status = :status')
->andWhere('i.isMain = true')
->andWhere('ih.date >= :date')
->andWhere('ih.date < :nowDt')
->andWhere('ih.action = :action')
->andWhere($q->expr()->in('c.id', ':ideas'))
->setParameters([
'factory' => '',
'action' => InteriorHistory::ADD,
'date' => $date2->format('Y-m-d H:i:s'),
'nowDt' => $nowDt->modify('-1day')->format('Y-m-d H:i:s'),
'ideas' => $ides,
'status' => BiConst::STATE_PUBLISHED,
'showMain' => true
])
->setMaxResults($limit)
->setFirstResult($limit * $page - $limit)
// ->groupBy('c.id')
->getQuery()// ->enableResultCache(120)
;
$last = $r2->getArrayResult();
$lastRating = [];
foreach ($last as $key => $item) {
$lastRating[$item['id']] = $item['ac'];
}
foreach ($new as $key => $item) {
$output[$item['id']] = [
'ratingMonth' => $item['ac'],
'arrow' => empty($lastRating[$item['id']]) || $lastRating[$item['id']] <= $item['ac'] ? 'up' : 'down',
'last' => empty($lastRating[$item['id']]) ? 0 : $lastRating[$item['id']],
'key' => $key,
];
}
$memcache->add($memKey, $output, false, 600);
}
return $output;
}
public function save(InteriorHistory $idea, bool $flush = false): void
{
$this->getEntityManager()->persist($idea);
if ($flush) {
$this->flush();
}
}
public function flush(): void
{
$this->getEntityManager()->flush();
}
}