<?php
/**
 * DBERP 进销存系统
 *
 * ==========================================================================
 * @link      http://www.dberp.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   http://www.dberp.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Stock\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Stock\Entity\ExWarehouseOrderGoods;

class ExWarehouseOrderGoodsRepository extends EntityRepository
{
    /**
     * 获取出库总数
     * @param array $search
     * @return \Doctrine\ORM\Query
     */
    public function ExWarehouseOrderGoodsAmount($search = [])
    {
        $query = $this->getEntityManager()->createQueryBuilder()
            ->select('SUM(e.warehouseGoodsExNum)')
            ->from(ExWarehouseOrderGoods::class, 'e');
        $this->querySearchData($search, $query);
        $ExWarehouseAmount= $query->getQuery()->getSingleScalarResult();
        return $ExWarehouseAmount ? $ExWarehouseAmount : 0;
    }

    /**
     * 检索商品id
     * @param $goodsName
     * @return mixed
     */
    public function findGoodsIdSearch($search = [])
    {
        //当定义为某个字段时，输出的是数组
        $query = $this->getEntityManager()->createQueryBuilder()
            ->select('e.goodsId,e.goodsName')
            ->from(ExWarehouseOrderGoods::class, 'e');
        $this->querySearchData($search, $query);
        $goodsIdResult = $query->getQuery()->getResult();
        return $goodsIdResult;
    }

    private function querySearchData($search, QueryBuilder $queryBuilder)
    {
        if(isset($search['ex_warehouse_order_goods_id']) && !empty($search['ex_warehouse_order_goods_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('e.exWarehouseOrderGoodsId', $search['ex_warehouse_order_goods_id']));
        if(isset($search['ex_warehouse_order_id']) && !empty($search['ex_warehouse_order_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('e.exWarehouseOrderId', $search['ex_warehouse_order_id']));
        if(isset($search['goods_id']) && !empty($search['goods_id']))               $queryBuilder->andWhere($queryBuilder->expr()->eq('e.goodsId', $search['goods_id']));
        if(isset($search['goods_name']) && !empty($search['goods_name']))           $queryBuilder->andWhere($queryBuilder->expr()->like('e.goodsName', "'%".$search['goods_name']."%'"));
        if(isset($search['goods_number']) && !empty($search['goods_number']))       $queryBuilder->andWhere($queryBuilder->expr()->like('e.goodsNumber', "'%".$search['goods_number']."%'"));
        if(isset($search['goods_spec']) && !empty($search['goods_spec']))           $queryBuilder->andWhere($queryBuilder->expr()->like('e.goodsSpec', "'%".$search['goods_spec']."%'"));

        return $queryBuilder;
    }
}