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

namespace Store\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Store\Entity\AllExWarehouseOrderGoods;

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

    /**
     * 获取入库总金额
     * @param array $search
     * @return \Doctrine\ORM\Query
     */
    public function exWarehouseOrderGoodsAmountPrice($search = [])
    {
        $query = $this->getEntityManager()->createQueryBuilder()
            ->select('SUM(e.warehouseGoodsAmount)')
            ->from(AllExWarehouseOrderGoods::class, 'e');
        $this->querySearchData($search, $query);
        $OtherWarehouseAmount= $query->getQuery()->getSingleScalarResult();
        return $OtherWarehouseAmount ? $OtherWarehouseAmount : 0;
    }

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

    private function querySearchData($search, QueryBuilder $queryBuilder)
    {
        if(isset($search['all_ex_warehouse_order_goods_id']) && !empty($search['all_ex_warehouse_order_goods_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('e.allExWarehouseOrderGoodsId', $search['all_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;
    }

}