<?php

namespace App\Services;

use App\Owner;
use App\Traits\ModelSearchWay;
use Illuminate\Support\Facades\DB;
use App\Traits\ServiceAppAop;


class AllInventoryService
{
    use ServiceAppAop;
    use ModelSearchWay;

    protected $modelClass=AllInventory::class;
    public function getSql(array $params, $page=null, $paginate=null){
        $ownerCodes=Owner::filterAuthorities()->select('code')->get();
        $date_start=$params['date_start'] ?? null;
        $range = $params['range'] ?? null;
        if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
        $date_end=$params['date_end'] ?? null;
        $TOLocation=$params['TOLocation'] ?? null;
        $LotAtt05=$params['LotAtt05'] ?? null;
        $LotAtt02_start=$params['LotAtt02_start'] ?? null;
        $customerid=$params['customerid'] ?? null;
        $SKU=$params['SKU'] ?? null;
        $ALTERNATE_SKU1=$params['ALTERNATE_SKU1'] ?? null;
        $LotAtt02_end=$params['LotAtt02_end'] ?? null;
        $sql='select * from (select result.*,rownum rn from (';
        $sql.=' select customer.descr_c as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
        $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
        $sql.=' lot.LotAtt04 批号 ';
        $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
        $sql.=' INV_LOT_LOC_ID storeStatus';
        $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
        $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
        $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
        if ($customerid){
            $sql .= ' where storeStatus.CUSTOMERID in (';
            $arr = explode(',',$customerid);
            foreach ($arr as $index => $data){
                if ($index != 0)$sql .= ',';
                $sql .= "'".$data."'";
            }
            $sql .= ') ';
        }
        if (!$customerid&&$ownerCodes){
            $sql .= ' where storeStatus.CUSTOMERID in (';
            foreach ($ownerCodes as $index => $data){
                if ($index != 0)$sql .= ',';
                $sql .= "'".$data['code']."'";
            }
            $sql .= ') ';
        }
        $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
        $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
        $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME  ';
        $sql.=' )result where 1=1 ';
//        if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
//        if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
//        if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
        if ($TOLocation)$sql=$this->sqlSearchWay($sql,$TOLocation,"库位");
        if ($SKU)$sql=$this->sqlSearchWay($sql,$SKU,"产品编码");
        if ($LotAtt05)$sql=$this->sqlSearchWay($sql,$LotAtt05,"属性仓");
        if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
        if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
        if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
        if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
//        if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
        if ($ALTERNATE_SKU1)$sql=$this->sqlSearchWay($sql,$ALTERNATE_SKU1,"产品条码");
        if ($page&&$paginate)$sql.="  and ROWNUM<='".$page*$paginate."'";
        $sql.=' )  ';
        if ($page&&$paginate)$sql.="  where rn>'".($page-1)*$paginate."'";
        return $sql;
    }

    public function paginate(array $params){
        return DB::connection('oracle')->select(DB::raw($this->getSql($params,$params['page'] ?? 1, $params['paginate'] ?? 50)));
    }
}
