src/WebBundle/Repository/InteriorHistoryRepository.php line 95

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Repository;
  3. use DateTime;
  4. use Doctrine\ORM\NonUniqueResultException;
  5. use Doctrine\ORM\NoResultException;
  6. use FlexApp\Constant\TimeConstant;
  7. use Import1CBundle\Helper\v3\BiConst;
  8. use WebBundle\Entity\InteriorHistory;
  9. use WebBundle\Helper\App;
  10. use WebBundle\Helper\LocaleHelper;
  11. /**
  12.  * InteriorHistoriesRepository
  13.  *
  14.  * This class was generated by the Doctrine ORM. Add your own custom
  15.  * repository methods below.
  16.  */
  17. class InteriorHistoryRepository extends ExtendEntityRepository
  18. {
  19.     /**
  20.      * @param $interior
  21.      * @param $idea
  22.      * @return int|mixed|string|null
  23.      * @throws NonUniqueResultException
  24.      */
  25.     public function getInteriorHistory($interior$idea)
  26.     {
  27.         $r $this->createQueryBuilder('ih')
  28.             ->andWhere('ih.interior = :interior')
  29.             ->andWhere('ih.idea = :idea')
  30.             ->setParameters([
  31.                 'interior' => $interior,
  32.                 'idea' => $idea,
  33.             ])
  34.             ->getQuery()
  35.             ->setMaxResults(1)
  36.             ->useQueryCache(true);
  37.         try {
  38.             return $r->getSingleResult();
  39.         } catch (NoResultException $e) {
  40.             return null;
  41.         }
  42.     }
  43.     /**
  44.      * Получение TOPа коллекций
  45.      *
  46.      * @param $days
  47.      * @param int $limit
  48.      * @return array|null
  49.      * @internal param DateTime $date
  50.      * @internal param int $maxResult
  51.      *
  52.      */
  53.     public function getTopCollections($days$limit 20): ?array
  54.     {
  55.         $date = new DateTime();
  56.         $date->modify('-' $days 'day')
  57.             ->setTime(2359);
  58.         $r $this->createQueryBuilder('ih')
  59.             ->addSelect('c.id, count(c.id) as ac')
  60.             ->leftJoin('ih.interior''ir')
  61.             ->leftJoin('ir.collection''c')
  62.             ->leftJoin('c.factory''f')
  63.             ->leftJoin('c.interiors''i')
  64.             ->andWhere('c.status = :c_status')
  65.             ->andWhere('c.factory != :factory')
  66.             ->andWhere('f.status = :f_status')
  67.             ->andWhere('i.isMain = true')
  68.             ->andWhere('ih.date >= :date')
  69.             ->andWhere('ih.action = :action')
  70.             ->setParameters([
  71.                 'factory' => '',
  72.                 'action' => InteriorHistory::ADD,
  73.                 'date' => $date->format('Y-m-d H:i:s'),
  74.                 'f_status' => BiConst::STATE_PUBLISHED,
  75.                 'c_status' => BiConst::STATE_PUBLISHED,
  76.             ])
  77.             ->setMaxResults($limit)
  78.             ->groupBy('c.id')
  79.             ->orderBy('ac DESC, ih.date''DESC')
  80.             ->getQuery();
  81.         if (!defined('DB_NOT_CASHED')) {
  82.             $r->useQueryCache(true)
  83.                 ->enableResultCache((int) TimeConstant::DAY); // сутки
  84.         }
  85.         $output = [];
  86.         foreach ($r->getArrayResult() as $key => $item) {
  87.             $output[] = $item['id'];
  88.         }
  89.         return $output;
  90.     }
  91.     /**
  92.      * @param int $days
  93.      * @param $page
  94.      * @param int $limit
  95.      * @return array|null
  96.      */
  97.     public function getCollections(int $days$page$limit 20): ?array
  98.     {
  99.         $memcache App::getMemcache();
  100.         $memKey 'getColls.' md5(
  101.                 $days $page $limit App::getCurLocale() . LocaleHelper::getCur() . LocaleHelper::getUserMeasure()
  102.             );
  103.         $output $memcache->get($memKey);
  104.         if (!$output) {
  105.             $date = new DateTime();
  106.             $date->modify('-' $days 'day')
  107.                 ->setTime(2359);
  108.             $r $this->createQueryBuilder('ih')
  109.                 ->select('c.id, count(c.id) as ac')
  110.                 ->leftJoin('ih.interior''ir')
  111.                 ->leftJoin('ir.collection''c')
  112.                 ->leftJoin('c.interiors''i')
  113.                 ->leftJoin('c.factory''f')
  114.                 ->andWhere('c.status = :status')
  115.                 ->andWhere('c.showMain = :showMain')
  116.                 ->andWhere('c.factory != :factory')
  117.                 ->andWhere('f.status = :status')
  118.                 ->andWhere('i.isMain = true')
  119.                 ->andWhere('ih.date >= :date')
  120.                 ->andWhere('ih.action = :action')
  121.                 ->setParameters([
  122.                     'factory' => '',
  123.                     'action' => InteriorHistory::ADD,
  124.                     'date' => $date->format('Y-m-d H:i:s'),
  125.                     'status' => BiConst::STATE_PUBLISHED,
  126.                     'showMain' => true
  127.                 ])
  128.                 ->setMaxResults($limit)
  129.                 ->setFirstResult($limit $page $limit)
  130.                 ->groupBy('c.id')
  131.                 ->orderBy('ac DESC, ih.date''DESC')
  132.                 ->getQuery()//            ->enableResultCache(120)
  133.             ;
  134.             $output = [];
  135.             $new $r->getArrayResult();
  136.             $date2 = new DateTime();
  137.             $date2->modify('-' . ($days 1) . 'day')
  138.                 ->setTime(2359);
  139.             $ides = [0];
  140.             foreach ($new as $key => $item) {
  141.                 $ides[] = $item['id'];
  142.             }
  143.             $nowDt = new DateTime();
  144.             $q $this->createQueryBuilder('ih');
  145.             $r2 $q->addSelect('c.id, count(c.id) as ac')
  146.                 ->leftJoin('ih.interior''ir')
  147.                 ->leftJoin('ir.collection''c')
  148.                 ->leftJoin('c.interiors''i')
  149.                 ->leftJoin('c.factory''f')
  150.                 ->andWhere('c.status = :status')
  151.                 ->andWhere('c.showMain = :showMain')
  152.                 ->andWhere('c.factory != :factory')
  153.                 ->andWhere('f.status = :status')
  154.                 ->andWhere('i.isMain = true')
  155.                 ->andWhere('ih.date >= :date')
  156.                 ->andWhere('ih.date < :nowDt')
  157.                 ->andWhere('ih.action = :action')
  158.                 ->andWhere($q->expr()->in('c.id'':ideas'))
  159.                 ->setParameters([
  160.                     'factory' => '',
  161.                     'action' => InteriorHistory::ADD,
  162.                     'date' => $date2->format('Y-m-d H:i:s'),
  163.                     'nowDt' => $nowDt->modify('-1day')->format('Y-m-d H:i:s'),
  164.                     'ideas' => $ides,
  165.                     'status' => BiConst::STATE_PUBLISHED,
  166.                     'showMain' => true
  167.                 ])
  168.                 ->setMaxResults($limit)
  169.                 ->setFirstResult($limit $page $limit)
  170. //            ->groupBy('c.id')
  171.                 ->getQuery()//            ->enableResultCache(120)
  172.             ;
  173.             $last $r2->getArrayResult();
  174.             $lastRating = [];
  175.             foreach ($last as $key => $item) {
  176.                 $lastRating[$item['id']] = $item['ac'];
  177.             }
  178.             foreach ($new as $key => $item) {
  179.                 $output[$item['id']] = [
  180.                     'ratingMonth' => $item['ac'],
  181.                     'arrow' => empty($lastRating[$item['id']]) || $lastRating[$item['id']] <= $item['ac'] ? 'up' 'down',
  182.                     'last' => empty($lastRating[$item['id']]) ? $lastRating[$item['id']],
  183.                     'key' => $key,
  184.                 ];
  185.             }
  186.             $memcache->add($memKey$outputfalse600);
  187.         }
  188.         return $output;
  189.     }
  190.     public function save(InteriorHistory $ideabool $flush false): void
  191.     {
  192.         $this->getEntityManager()->persist($idea);
  193.         if ($flush) {
  194.             $this->flush();
  195.         }
  196.     }
  197.     public function flush(): void
  198.     {
  199.         $this->getEntityManager()->flush();
  200.     }
  201. }