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

class AllWarehouseOrderGoodsRepository extends EntityRepository
{

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


    /*public function updateWarehouseGoodsStock(array $data)
    {
        $em = $this->getEntityManager();
        $em->getConnection()->exec("update dberp_all_warehouse_order_goods set out_num=0  where all_warehouse_order_goods_id=2");
        return true;
    }*/


    public function updateOutNum(array $data)
    {
        if(empty($data)) return false;
        $warehouseGoods = $this->getEntityManager()->getRepository(AllWarehouseOrderGoods::class)->findOneBy(['allWarehouseOrderGoodsId' => $data['allWarehouseOrderGoodsId'], 'goodsId' => $data['goodsId']]);
        if(!$warehouseGoods || $warehouseGoods->getOutNum() < $data['sendNum']) return false;
        $warehouseGoods->setOutNum($warehouseGoods->getOutNum() - $data['sendNum']);
        $this->getEntityManager()->flush();
        $this->getEntityManager()->clear(AllWarehouseOrderGoods::class);

        return true;
    }

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

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

    private function querySearchData($search, QueryBuilder $queryBuilder)
    {
        if(isset($search['all_warehouse_order_goods_id']) && !empty($search['all_warehouse_order_goods_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('o.allWarehouseOrderGoodsId', $search['all_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['purchase_warehouse_order_id']) && !empty($search['purchase_warehouse_order_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('o.purchaseWarehouseOrderId', $search['purchase_warehouse_order_id']));
        if(isset($search['warehouse_order_sn']) && !empty($search['warehouse_order_sn']))           $queryBuilder->andWhere($queryBuilder->expr()->in('o.warehouseOrderSn', "'".str_replace(",","','",$search['warehouse_order_sn'])."'"));
        if(isset($search['warehouse_id']) && !empty($search['warehouse_id']))           $queryBuilder->andWhere($queryBuilder->expr()->eq('o.warehouseId', $search['warehouse_id']));
        if(isset($search['warehouseIds']) && !empty($search['warehouseIds']))           $queryBuilder->andWhere($queryBuilder->expr()->in('o.warehouseId', implode(",",$search['warehouseIds'])));
        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']."%'"));
        if(isset($search['out_num']) && !empty($search['out_num']))                 $queryBuilder->andWhere($queryBuilder->expr()->gte('o.outNum', $search['out_num']));

        return $queryBuilder;
    }

}