<?php

namespace Lc\ShopBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\TicketInterface;
use Lc\ShopBundle\Context\UnitInterface;
use Lc\ShopBundle\Model\Ticket;
use Lc\ShopBundle\Repository\BaseRepository;

/**
 * @method TicketInterface|null find($id, $lockMode = null, $lockVersion = null)
 * @method TicketInterface|null findOneBy(array $criteria, array $orderBy = null)
 * @method TicketInterface[]    findAll()
 * @method TicketInterface[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class TicketRepository extends BaseRepository implements DefaultRepositoryInterface
{
        public function getInterfaceClass()
        {
                return TicketInterface::class;
        }

        public function filterStatus($query, $statusArray)
        {
                $query->andWhere('e.status IN (:status)')->setParameter('status', $statusArray) ;
                return $query ;
        }

        public function findAllOpen()
        {
                $query = $this->findByMerchantQuery() ;
                $this->filterStatus($query, [Ticket::TICKET_STATUS_OPEN, Ticket::TICKET_STATUS_BEING_PROCESSED]) ;
                $query->addOrderBy('e.id', 'DESC') ;
                return $query->getQuery()->getResult() ;
        }

        public function countAllOpen()
        {
                $query = $this->findByMerchantQuery() ;
                $query->select('count(e.id)');
                $this->filterStatus($query, [Ticket::TICKET_STATUS_OPEN, Ticket::TICKET_STATUS_BEING_PROCESSED]) ;
                return $query->getQuery()->getSingleScalarResult() ;
        }
}

