<?php

namespace Lc\ShopBundle\Repository;

use Lc\ShopBundle\Context\DefaultRepositoryInterface;
use Lc\ShopBundle\Context\ProductInterface;
use Lc\ShopBundle\Model\ProductFamily;

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


        public function findProductByAvailabilitiesNegative()
        {
                $qb = $this->createQueryBuilder('e');
                $qb->innerJoin('e.productFamily', 'productFamily');
                $qb->addSelect('productFamily');
                $qb->where('productFamily.merchant = :currentMerchant');
                $qb->andWhere('productFamily.status = 1');
                $qb->setParameter('currentMerchant', $this->merchantUtils->getMerchantCurrent()->getId());

                $qb->andWhere(
                        $qb->expr()->orX(
                                $qb->expr()->andX(
                                        $qb->expr()->orX(
                                                'productFamily.behaviorCountStock LIKE :behaviorCountStockByProductFamily',
                                                'productFamily.behaviorCountStock LIKE :behaviorCountStockByMeasure'
                                        ),
                                        'productFamily.availableQuantity < 0 '
                                ),
                                $qb->expr()->andX(
                                        'productFamily.behaviorCountStock LIKE :behaviorCountStockByProduct',
                                        'e.availableQuantity < 0 '
                                )
                        )
                );
                $qb->setParameter('behaviorCountStockByProductFamily', ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY);
                $qb->setParameter('behaviorCountStockByMeasure', ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE);
                $qb->setParameter('behaviorCountStockByProduct', ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT);
                $qb->groupBy('productFamily.id');

                return $qb->getQuery()->getResult();
        }

}