<?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\OtherWarehouseOrderGoods;

class OtherWarehouseOrderGoodsRepository extends EntityRepository
{
    /**
     * 获取入库总数
     * @param array $search
     * @return \Doctrine\ORM\Query
     */
    public function OtherWarehouseOrderGoodsAmount($search = [])
    {
        $query = $this->getEntityManager()->createQueryBuilder()
            ->select('SUM(o.warehouseGoodsBuyNum)')
            ->from(OtherWarehouseOrderGoods::class, 'o');
            $this->querySearchData($search, $query);
        $OtherWarehouseAmount= $query->getQuery()->getSingleScalarResult();
        return $OtherWarehouseAmount ? $OtherWarehouseAmount : 0;
    }

    private function querySearchData($search, QueryBuilder $queryBuilder)
    {
        if(isset($search['warehouse_order_goods_id']) && !empty($search['warehouse_order_goods_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('o.warehouseOrderGoodsId', $search['warehouse_order_goods_id']));
        if(isset($search['other_warehouse_order_id']) && !empty($search['other_warehouse_order_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('o.otherWarehouseOrderId', $search['other_warehouse_order_id']));
        if(isset($search['goods_id']) && !empty($search['goods_id']))               $queryBuilder->andWhere($queryBuilder->expr()->eq('o.goodsId', $search['goods_id']));
        if(isset($search['goods_name']) && !empty($search['goods_name']))           $queryBuilder->andWhere($queryBuilder->expr()->like('o.goodsName', "'%".$search['goods_name']."%'"));
        if(isset($search['goods_number']) && !empty($search['goods_number']))       $queryBuilder->andWhere($queryBuilder->expr()->like('o.goodsNumber', "'%".$search['goods_number']."%'"));
        if(isset($search['goods_spec']) && !empty($search['goods_spec']))           $queryBuilder->andWhere($queryBuilder->expr()->like('o.goodsSpec', "'%".$search['goods_spec']."%'"));

        return $queryBuilder;
    }

}