<?php


namespace app\admin\controller;

use app\admin\model\GoodLog;
use think\App;
use think\db\Raw;
use think\facade\Db;
use think\facade\Validate;

//线上商品
class Good extends Base
{

    private $acton=[
        "allotsend"=>'调拨发货',
        "allotin"=>'调拨入库',
        "allotincheck"=>'调拨验货',
        'CGD'=>'采购单',
        'BHD'=>'备库单',
        'FKD'=>'备库反馈单',
        'RKD'=>'入库单',
        'ZXQRD'=>'咨询单',
        'XSQRD'=>'销售单',
        'PDD'=>'盘点单',
        'DBD'=>'调拨单',
        'CKD'=>'出库单',
        'CGTHD'=>'采购退货单',
        'XSTHD'=>'销售退货单',
        'CGGCD'=>'采购工差单',
        'ZXGCD'=>'咨询工差单',
        'RKTHD'=>'入库退货单',
    ];
    public $noble=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->noble=\think\facade\Config::get("noble");
    }

    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
        $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
        $where =[["a.is_del","=",0]];
        $cat_id = isset($this->post['cat_id']) && $this->post['cat_id'] !=="" ? trim($this->post['cat_id']):"";
        if($cat_id!==""){
            $where[]=['a.cat_id',"=",$cat_id];
        }
        $good_name = isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name']):"";
        if($good_name!==""){
            $where[]=['a.good_name',"like","%$good_name%"];
        }
        $skucode = isset($this->post['skucode']) && $this->post['skucode'] !=="" ? trim($this->post['skucode']):"";
        if($skucode!==""){
            $where [] = ['b.skucode',"like","%$skucode%"];
        }
        $spucode = isset($this->post['spucode']) && $this->post['spucode'] !=="" ? trim($this->post['spucode']):"";
        if($spucode!==""){
            $where [] = ['b.spucode',"like","%$spucode%"];
        }
        $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
        if($start!==""){
            $where[]=['a.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
        }
        $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
        if($end!==""){
            $where[]=['a.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
        }
        $is_stock = isset($this->post['is_stock']) && $this->post['is_stock'] !=="" ? intval($this->post['is_stock'])
            :"";
        if($is_stock!==""){
            $where[]=['a.is_stock',"=",$is_stock];
        }
        $good_type = isset($this->post['good_type']) && $this->post['good_type'] !=="" ? intval($this->post['good_type'])
            :"";
        if($good_type!==""){
            $where[]=['a.good_type',"=",$good_type];
        }
        $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo'])
            :"";
        if($supplierNo!==""){
            $where[]=['a.supplierNo',"=",$supplierNo];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']) :"";
        if($companyNo!==""){
            $where[]=['a.companyNo',"like","%$companyNo%"];
        }
        $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
        if($creater!==""){
            $where[]=['a.creater',"like","%$creater%"];
        }
        $createrid = isset($this->post['createrid']) && $this->post['createrid'] !=="" ? intval($this->post['createrid']):"";
        if($createrid!==""){
            $where[]=['a.createrid',"=",$createrid];
        }
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
            :"";
        if($status!==""){
            $where[]=['a.status',"=",$status];
        }
        $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code'])
            :"";
        if($platform_code!==""){
            //$platform = Db::name("good_platform")->where(["platform_code"=>$platform_code,"is_del"=>0])->column
            //("spuCode");
            $where[]=["platform_code","=",$platform_code];
        }
        $plat_code = isset($this->post['plat_code']) && $this->post['plat_code'] !=="" ? trim($this->post['plat_code'])
            :"";
        if($plat_code!==""){
            $where[]=["b.plat_code","like","%$plat_code%"];
        }
        $exam_status = isset($this->post['exam_status']) && $this->post['exam_status'] !=="" ? intval($this->post['exam_status'])
            :"";
        if($exam_status!==""){
            $where[]=['b.exam_status',"=",$exam_status];
        }
        $brand_id = isset($this->post['brand_id']) && $this->post['brand_id'] !=="" ? intval($this->post['brand_id'])
            :"";
        if($brand_id!==""){
            $where[]=['a.brand_id',"=",$brand_id];
        }
        $count = Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")->where($where)
            ->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('good')
            ->alias("a")
            ->field("b.skuCode,b.platform_code,b.plat_code,b.id as good_id,a.*,b.addtime,b.updatetime,b.exam_status,gb.createrid purchase_id,gb.creater purchase")//成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
            ->leftJoin("good_platform b","a.spuCode=b.spuCode")
            ->leftJoin("good_basic gb","gb.spuCode=a.spuCode")
            ->where($where)
            ->page($page,$size)
            ->order("b.addtime desc")
            ->select()
            ->toArray();
        $data=[];
        foreach ($list as $value){
            $value['cat_info']= made($value['cat_id'],[]);
            $platform = Db::name("platform")->where(["id"=>$value['platform_code']])->find();
            $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
            $value['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en']:"";
            $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
            $value['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
            $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
            $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
            $unit = Db::name("unit")->where(["id"=>$value['good_unit']])->find();
            $value['unit'] =isset($unit['unit'])?$unit['unit']:"";
            $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
            $value['company'] = isset($company['company'])?$company['company']:"";
            $value['stock_total'] = Db::name("good_stock")->where(['spuCode'=>$value['spuCode'],"is_del"=>0])->sum("usable_stock");
            $value['status']=$value['exam_status'];
            $value['exclusive']=makeExcluse($value['is_exclusive']);
            $value['good_info_img']=$value['good_info_img'];
            $value['good_img']=$value['good_img'];
            $spec = Db::name("good_spec")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->select()->toArray();
            $speclist=[];
            if(!empty($spec)){
                foreach ($spec as $val){
                    $temp=[];
                    $temp['spec_id']=$val['spec_id'];
                    $temp['spec_value_id']=$val['spec_value_id'];
                    $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
                    $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
                    $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
                    $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
                    $speclist[]=$temp;
                }
            }
            $value['specinfo']=$speclist;
            $value['noble_name']=isset($value['noble_metal'])&&$value['noble_metal']!=0?$this->noble[$value['noble_metal']] :"";
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
    }

    public function info(){
        $skucode=isset($this->post['skuCode'])&&$this->post['skuCode']!=""?trim($this->post['skuCode']):"";
        if($skucode==""){
            return error_show(1005,"参数skuCode不能为空");
        }
        $good_platform = Db::name("good_platform")->where(['skuCode'=>$skucode,"is_del"=>0])->find();
        if($good_platform==false){
            return error_show(1004,"未找到商品数据");
        }
        $data = Db::name("good")->where(["spuCode"=>$good_platform['spuCode'],"is_del"=>0])->find();
        if($data==false){
            return error_show(1004,"未找到商品数据");
        }
        $plat =Db::name("platform")->where(["id"=>$good_platform['platform_code']])->find();
        $data['skuCode']=$good_platform['skuCode'];
        $data['platform_code']=$good_platform['id'];
        $data['platform_code_en']=isset($plat['platform_code'])?$plat['platform_code']:"";
        $data['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
        $data['online_reason']=$good_platform['online_reason'];
        $data['online_time']=$good_platform['online_time'];
        $data['online_remark']=$good_platform['online_remark'];
        $data['exam_status']=$good_platform['exam_status'];
        $data['is_online']=$good_platform['is_online'];
        $data['plat_code']=$good_platform['plat_code'];
        $data['exclusive']=makeExcluse($data['is_exclusive']);
        $unit =Db::name("unit")->where(["id"=>$data['good_unit']])->find();
        $data['unit'] = isset($unit['unit'])?$unit['unit']:'';
        $data['cat_info'] = made($data['cat_id'],[]);
        $spec = Db::name("good_spec")->where(["spuCode"=>$good_platform['spuCode'],"is_del"=>0])->select()->toArray();
        $supplier = Db::name("supplier")->where(["code"=>$data['supplierNo']])->find();
        $data['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
        $data['noble_name']=isset($data['noble_metal'])&&$data['noble_metal']!=0?$this->noble[$data['noble_metal']] :"";
        $company = Db::name("business")->where(["companyNo"=>$data['companyNo']])->find();
        $data['company'] = isset($company['company'])?$company['company']:"";
        if($data['brand_id']!=0){
            $brand=Db::name("brand")->where(["id"=>$data['brand_id']])->find();
            $data["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
        }else{
            $data["brand_name"]="";
            $data["brand_id"]="";
        }
        $data['origin_place_cn']="";
        $data['delivery_place_cn']="";
        if($data['delivery_place']!==""){
            $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
            list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$data['delivery_place']);
            $data['delivery_place_cn']=GetAddr(json_encode($place));
        }
        if($data['origin_place']!==""){
            $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
            list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$data['origin_place']);
            $data['origin_place_cn']=GetAddr(json_encode($place));
        }
        $excluse = makeExcluse($data['is_exclusive']);
        $data['exclusive']=$excluse;
        $data["good_info_img"]=$data['good_info_img']!=""? $data['good_info_img']:[];
        $data["good_img"]=$data['good_img']!=""? $data['good_img']:[];
        $speclist=[];
        if(!empty($spec)){
            foreach ($spec as $value){
                $temp=[];
                $temp['id']=$value['id'];
                $temp['spuCode']=$value['spuCode'];
                $temp['spec_id']=$value['spec_id'];
                $temp['spec_value_id']=$value['spec_value_id'];
                $temp['is_del']=$value['is_del'];
                $sp = Db::name("specs")->where(["id"=>$value['spec_id']])->find();
                $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
                $spv = Db::name("spec_value")->where(["id"=>$value['spec_value_id']])->find();
                $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
                $speclist[]=$temp;
            }
        }
        $data["speclist"]=empty($speclist)?[]:$speclist;
        $ladder = Db::name("good_ladder")->where(['skuCode'=>$skucode,"is_del"=>0])->select()->toArray();
        $data["ladderlist"]=!empty($ladder)?$ladder:[];
        $nakelist = Db::name("good_nake")->where(['spuCode'=>$good_platform['spuCode'],"is_del"=>0])->select()->toArray();
//        $catinfo = Db::name("cat")->where(["id"=>$data['cat_id']])->find();

        $cat_top_list = made($data['cat_id']);
        $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
        // $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
        $nakearry=[];
        if(!empty($nakelist)){

            //实时金价
            $gold_price = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->value('price', 0);
            foreach ($nakelist as $value){
                $value['sale_price'] =$value['nake_total'];

                //计算最终售价
                if ($data['is_gold_price']==1 && $cat_top_id==6) {
                    //普通商品:直接用财务提交的售价字段;
                    //贵金属( 商品重量* 最新金价 + 工艺费(财务定价提交的)* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)=商品库商品直接下单的售价
                    $value['sale_price'] = ($data['weight'] * $gold_price + $value['cost_fee'] * $data['weight'] + $value['package_fee'] + $value['mark_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['other_fee']);
                }
                $nakearry[] = $value;
            }
        }
        $proof =Db::name("good_proof")->where(["spuCode"=>$good_platform['spuCode'],"is_del"=>0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
        $data['proof'] = isset($proof)&&$proof!=false? $proof:[];
        $data["nakelist"]=$nakearry;

        //补充采购员字段
        //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
        $temp_basic_info = Db::name('good_basic')
            ->field('id,createrid purchase_id,creater purchase')
            ->where(['spuCode'=>$data['spuCode'],'is_del'=>0])
            ->find();
        $data['purchase_id'] = $temp_basic_info['purchase_id'];
        $data['purchase'] = $temp_basic_info['purchase'];

        return app_show(0,"获取成功",$data);
    }


    public function SetWarn(){
        $condition=["is_del"=>0];
        $wsm_code = isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']):"";
        if($wsm_code!=""){
            $condition["wsm_code"] = $wsm_code;
        }else{
            return error_show(1004,"参数wsm_code 不能为空");
        }
        $type_code = isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
        if($type_code!=""){
            $condition["good_type_code"] = $type_code;
        }else{
            return error_show(1004,"参数type_code 不能为空");
        }
        $good= Db::name("good_stock")->where($condition)->find();
        if(empty($good)){
            return error_show(1005,"未找到数据");
        }
        $warn_stock = isset($this->post['warn_stock']) &&$this->post['warn_stock'] !=="" ? intval($this->post['warn_stock']):"";
        if($warn_stock===""){
            return error_show(1005,"参数warn_stock 不能为空");
        }
        $good['warn_stock'] = $warn_stock;
        $good['updatetime'] = date("Y-m-d H:i:s");
        $up= Db::name("good_stock")->save($good);
        if($up){
            //商品变动日志表,good_log_code字段存储仓库编码
            $data = ['good_log_code' => $wsm_code, "stock_id" => $good['id'], "type" => 1, 'stock' => $warn_stock, "stock_name" => "warn_stock"];
            GoodLog::LogAdd($this->post['token'],$data,'setwarn');

            return app_show(0,"预警库存更新成功");
        }else{
            return error_show(1005,"预警库存更新失败");
        }
    }

    public function SetStatus(){
        $id = isset($this->post['id']) &&$this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id===""){
            return error_show(1005,"参数id 不能为空");
        }
        $good= Db::name("good_type")->where(["id"=>$id,"is_del"=>0])->find();
        if(empty($good)){
            return error_show(1005,"未找到数据");
        }
        $good['status'] = $good['status']==1?0 :1;
        $good['updatetime'] = date("Y-m-d H:i:s");
        $msg = $good['status']==1?"下架" :"上架";
        $up= Db::name("good_type")->save($good);
        return $up ? app_show(0,"商品{$msg}成功"): error_show(1005,"商品{$msg}失败");
    }

    public function GetStock(){
        $condition = [["a.is_del","=",0]];
        $type_code = isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
        if($type_code!==""){
            $condition[]=["a.spuCode","=",$type_code];
        }
        $wsm_code = isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code'])
            :"";
        if($wsm_code!==""){
            //$condition["a.wsm_code"]=$wsm_code;
            $condition[]=["a.wsm_code","=",$type_code];
        }
        $suplierNo = isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo'])
            :"";
        if($suplierNo!==""){
            $wsmcode = Db::name("warehouse_info")->where(['supplierNo'=>$suplierNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["a.wsm_code","in",$wsmcode];
        }
        $list = Db::name("good_stock")->alias("a")->join("warehouse_info b","a.wsm_code=b.wsm_code","left")->join("supplier c","b.supplierNo=c.code","left")
            ->where($condition)->field("a.id,b.name as wsm_name,c.code,c.name,a.wait_in_stock,a.wait_out_stock,a.usable_stock,a.intra_stock,a.total_stock,a.status,a.warn_stock")->select();
        if(empty($list)){
            return error_show(1004,"未找到数据");
        }
        return app_show(0,"获取成功",$list);
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function GetStockBet(){
        $page = isset($this->post['page']) &&$this->post['page'] !=="" ? intval($this->post['page']):1;
        $size = isset($this->post['size']) &&$this->post['size'] !=="" ? intval($this->post['size']):10;
        $condition = [["is_del","=",0]];
        $type_code = isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
        if($type_code!==""){
            $condition[]=["spuCode","like","%{$type_code}%"];
        }
        $wsm_code = isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code'])
            :"";
        if($wsm_code!==""){
            $condition[]=["wsm_code","=",$wsm_code];
        }
        $suplierNo = isset($this->post['suplierNo']) &&$this->post['suplierNo'] !=="" ? trim($this->post['suplierNo']):"";
        if($suplierNo!==""){
            $wsmcode = Db::name("warehouse_info")->where(['supplierNo'=>$suplierNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["wsm_code","in",$wsmcode];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
        if ($companyNo !== "") {
            $wsmcode = Db::name("warehouse_info")->where(['companyNo'=>$companyNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["wsm_code","in",$wsmcode];
        }
        $start = isset($this->post['start']) &&$this->post['start'] !=="" ? $this->post['start']:"";
        $end = isset($this->post['end']) &&$this->post['end'] !=="" ? $this->post['end']:"";
        if($start!=""){
            $condition[]=["addtime",">=",$start];
        }
        if($end!=""){
            $condition[]=["addtime","<=",$end];
        }
        $count = Db::name("good_stock")->where($condition)->count();
        $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
        $list = Db::name("good_stock")->where($condition)->page($page,$size)->order("addtime desc")->select();
        $data=[];
        foreach ($list as $key=>$value){
            $temp=[];
            if($value['wsm_code']!=""){
                $wsm = Db::name("warehouse_info")
                    ->alias("a")
                    ->leftJoin("supplier b","a.supplierNo=b.code")
                    ->leftJoin("business bus","a.companyNo=bus.companyNo")
                    ->where(['a.wsm_code'=>$value['wsm_code'],"a.is_del"=>0])
                    ->field("a.name as wsm_name,wsm_code,a.supplierNo,b.name supplierName,a.companyNo,bus.company")
                    ->find();
            }
            $temp['wsm_name'] = isset($wsm['wsm_name'])?$wsm['wsm_name']:"";
            $temp['wsm_code']=$value['wsm_code'];
            $temp['supplier_code']=isset($wsm['supplierNo'])?$wsm['supplierNo']:"";
            $temp['supplier_name']=isset($wsm['supplierName'])?$wsm['supplierName']:"";
            $temp['company_no']=isset($wsm['companyNo'])?$wsm['companyNo']:"";
            $temp['company_name']=isset($wsm['company'])?$wsm['company']:"";
            if($value['spuCode']!=""){
                $good=Db::name("good")
                    ->alias('g')
                    ->field('g.*,b.brand_name,u.unit')
                    ->where(["g.spuCode"=>$value['spuCode'],"g.is_del"=>0])
                    ->leftJoin('brand b','b.id=g.brand_id')
                    ->leftJoin('unit u','u.id=g.good_unit')
                    ->find();

                //规格信息
                $spec = Db::name("good_spec")->field('id,spec_id,spec_value_id')->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();

                $speclist = [];
                if (!empty($spec)) {
                    foreach ($spec as $val) {
                        $tmp = [];
                        $tmp['spec_id'] = $val['spec_id'];
                        $tmp['spec_value_id'] = $val['spec_value_id'];
                        $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
                        $tmp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
                        $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
                        $tmp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
                        $speclist[] = $tmp;
                    }
                }
                $temp['specinfo'] = $speclist;
            }
            $temp['good_code']=isset($good['spuCode'])?$good['spuCode']:"";
            //  $temp['type_code']=isset($good['type_code'])?$good['type_code']:"";
            $temp['good_name']=isset($good['good_name'])?$good['good_name']:"";
            $temp['unit']=isset($good['unit'])?$good['unit']:"";
            $temp['brand_name']=isset($good['brand_name'])?$good['brand_name']:"";
            //  $temp['attribute']=isset($good['attribute'])?$good['attribute']:"";
            $temp['sort_f']=isset($good['cat_id'])?$good['cat_id']:"";
            $temp['cat_info']=isset($good['cat_id'])?made($good['cat_id']):"";
            $temp['usable_stock'] = $value['usable_stock'];
            $temp['wait_in_stock'] = $value['wait_in_stock'];
            $temp['wait_out_stock'] = $value['wait_out_stock'];
            $temp['intra_stock'] = $value['intra_stock'];
            $temp['total_stock'] = $value['total_stock'];
            $temp['warn_stock'] = $value['warn_stock'];
            $temp['id'] = $value['id'];
            $temp['out_total'] =0;
            $temp['in_total'] = 0;
            $temp['addtime'] = $value['addtime'];
            $data[]=$temp;
        }
        return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetStockWsm(){
        $page = isset($this->post['page']) &&$this->post['page'] !=="" ? intval($this->post['page']):1;
        $size = isset($this->post['size']) &&$this->post['size'] !=="" ? intval($this->post['size']):10;
        $condition = [["gs.is_del","=",0]];
        $wsm_code = isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']):"";
        if($wsm_code!==""){
            $condition[]=["gs.wsm_code","=",$wsm_code];
        }
        $suplierNo = isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
        if($suplierNo!==""){
            $wsmcode = Db::name("warehouse_info")->where(['supplierNo'=>$suplierNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["gs.wsm_code","in",$wsmcode];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
        if ($companyNo !== "") {
            $wsmcode = Db::name("warehouse_info")->where(['companyNo'=>$companyNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["gs.wsm_code","in",$wsmcode];
        }
        $build= Db::name("good_stock")
            ->alias('gs')
            ->where($condition)
            ->where('whi.wsm_type','<>',2)//2表示虚拟仓,虚拟仓不参加统计
            ->leftJoin('warehouse_info whi','whi.wsm_code=gs.wsm_code AND whi.is_del=0')
            ->group("gs.wsm_code")
            ->field("gs.wsm_code")
            ->buildSql();
        $count = Db::table($build." a")->count();
        $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
        $list = Db::name("good_stock")
            ->alias('gs')
            ->where($condition)
            ->where('whi.wsm_type','<>',2)//2表示虚拟仓,虚拟仓不参加统计
            ->page($page,$size)
            ->leftJoin('warehouse_info whi','whi.wsm_code=gs.wsm_code AND whi.is_del=0')
            ->group("gs.wsm_code")
            ->field("gs.wsm_code,sum(wait_in_stock) wait_in_stock,sum(wait_out_stock) wait_out_stock,sum(usable_stock) usable_stock,sum(intra_stock) intra_stock,sum(warn_stock) warn_stock,sum(total_stock) total_stock")
            ->select()
            ->toArray();
        $data=[];
        foreach ($list as $key=>$value){
            $temp=[];
            if($value['wsm_code']!=""){
                $wsm = Db::name("warehouse_info")
                    ->alias("a")
                    ->leftJoin("business bus", "bus.companyNo=a.companyNo")
                    ->leftJoin("supplier b","a.supplierNo=b.code")
                    ->where(['a.wsm_code'=>$value['wsm_code'],"a.is_del"=>0])
                    ->field("a.name as wsm_name,wsm_code,a.supplierNo,b.name supplierName,a.companyNo,bus.company")
                    ->find();
                $good = Db::name("good_stock")
                    ->alias("a")
                    ->leftJoin("good c","c.spuCode=a.spuCode")
                    ->where(["a.wsm_code"=>$value['wsm_code'],"a.is_del"=>0,"c.is_del"=>0])
                    ->select()
                    ->toArray();

                //商品单位和品牌
                $good_unit_s = Db::name("unit")->whereIn('id', array_column($good, 'good_unit'))->where('is_del', 0)->column('unit', 'id');
                $brands = Db::name("brand")->whereIn('id', array_column($good, 'brand_id'))->where('is_del', 0)->column('brand_name', 'id');

                foreach ($good as &$item_good){
                    //规格信息
                    $spec = Db::name("good_spec")->field('id,spec_id,spec_value_id')->where(["spuCode" => $item_good['spuCode'], "is_del" => 0])->select()->toArray();

                    $speclist = [];
                    if (!empty($spec)) {
                        foreach ($spec as $val) {
                            $tmp = [];
                            $tmp['spec_id'] = $val['spec_id'];
                            $tmp['spec_value_id'] = $val['spec_value_id'];
                            $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
                            $tmp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
                            $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
                            $tmp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
                            $speclist[] = $tmp;
                        }
                    }
                    $item_good['specinfo'] = $speclist;
                    $item_good['unit'] = $good_unit_s[$item_good['good_unit']]??'';
                    $item_good['brand_name'] = $brands[$item_good['brand_id']]??'';
                    $item_good['can'] = made($item_good['cat_id'],[]);

                }


            }
            $temp['wsm_name'] = isset($wsm['wsm_name'])?$wsm['wsm_name']:"";
            $temp['wsm_code']=$value['wsm_code'];
            $temp['supplier_code']=isset($wsm['supplierNo'])?$wsm['supplierNo']:"";
            $temp['supplier_name']=isset($wsm['supplierName'])?$wsm['supplierName']:"";
            $temp['company_no']=isset($wsm['companyNo'])?$wsm['companyNo']:"";
            $temp['company_name']=isset($wsm['company'])?$wsm['company']:"";
            $temp['usable_stock'] = $value['usable_stock'];
            $temp['wait_in_stock'] = $value['wait_in_stock'];
            $temp['wait_out_stock'] = $value['wait_out_stock'];
            $temp['intra_stock'] = $value['intra_stock'];
            $temp['total_stock'] = $value['total_stock'];
            // $temp['warn_stock'] = $value['warn_stock'];
            $temp['child']=isset($good)&&!empty($good) ? $good:[];
            $data[]=$temp;
        }
        return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
    }

    public function GetStockGod(){
        $page = isset($this->post['page']) &&$this->post['page'] !=="" ? intval($this->post['page']):1;
        $size = isset($this->post['size']) &&$this->post['size'] !=="" ? intval($this->post['size']):10;
        $condition = [["b.is_del","=",0],["c.usable_stock",">",0]];
        $type_code = isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
        if($type_code!==""){
            $condition[]=["b.spuCode","like","%{$type_code}%"];
        }
        $count = Db::name("good")->alias("b")
            ->join("good_stock c","c.spuCode = b.spuCode","left")
            ->where($condition)
            ->group("b.spuCode,b.good_name,b.good_unit,b.cat_id,b.craft_desc,b.addtime")
            ->count();
        $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
        $list = Db::name("good")
            ->alias("b")
            ->join("good_stock c","c.spuCode = b.spuCode","left")
            ->where($condition)
            ->field("b.spuCode,b.good_name,b.good_unit,b.brand_id,b.cat_id,b.craft_desc,b.addtime,sum(c.wait_in_stock) wait_in_stock,sum(c.wait_out_stock) wait_out_stock,sum(c.usable_stock) usable_stock,sum(c.intra_stock) intra_stock,sum(c.warn_stock) warn_stock,sum(c.total_stock) total_stock")
            ->page($page,$size)
            ->order("b.addtime desc")
            ->group("b.spuCode,b.good_name,b.good_unit,b.brand_id,b.cat_id,b.craft_desc,b.addtime")
            ->select()
            ->toArray();
        // echo Db::name("good_type")->getLastSql();
        $data=[];

        if($list) {

            //商品单位和品牌
            $good_unit_s = Db::name("unit")->whereIn('id', array_column($list, 'good_unit'))->where('is_del', 0)->column('unit', 'id');
            $brands = Db::name("brand")->whereIn('id', array_column($list, 'brand_id'))->where('is_del', 0)->column('brand_name', 'id');

            foreach ($list as $key => $value) {
                $temp = [];
                if ($value['spuCode'] != "") {
                    $child = Db::name("good_stock")
                        ->alias("a")
                        ->join("warehouse_info b", "a.wsm_code=b.wsm_code", "left")
                        ->join("business bus", "bus.companyNo=b.companyNo", "left")
                        ->join("supplier s", "s.code=b.supplierNo", "left")
                        ->where(['a.spuCode' => $value['spuCode'], "b.wsm_type" => 1, "a.is_del" => 0])
                        ->field("a.wsm_code,b.name wsm_name,a.wait_in_stock,a.wait_out_stock,a.usable_stock,a.intra_stock,a.total_stock,a.warn_stock,b.supplierNo,s.name supplierName,b.companyNo,bus.company")
                        ->select()
                        ->toArray();
                }
                $temp['good_name'] = $value['good_name'];
                $temp['good_code'] = $value['spuCode'];
                $temp['type_code'] = '';
//                $unit = Db::name("unit")->where(['id' => $value['good_unit']])->find();
                $temp['unit'] =$good_unit_s[$value['spuCode']]??'';
                $temp['brand_name'] =$brands[$value['brand_id']]??'';
                $temp['sort_f'] = isset($value['cat_id']) ? made($value['cat_id']) : [];
                $temp['sort_f'] = implode('/', array_column($temp['sort_f'], 'name'));//由于这个字段原样在页面显示,所以在这里特意拼接成字符串
                $temp['attribute'] = $value['craft_desc'];
                $temp['addtime'] = $value['addtime'];
                $temp['usable_stock'] = isset($value['usable_stock']) ? $value['usable_stock'] : "";
                $temp['wait_in_stock'] = isset($value['wait_in_stock']) ? $value['wait_in_stock'] : "";
                $temp['wait_out_stock'] = isset($value['wait_out_stock']) ? $value['wait_out_stock'] : "";
                $temp['intra_stock'] = isset($value['intra_stock']) ? $value['intra_stock'] : "";
                $temp['total_stock'] = isset($value['total_stock']) ? $value['total_stock'] : "";
                // $temp['warn_stock'] = isset($value['warn_stock'])? $value['warn_stock']:"";
                $temp['child'] = isset($child) && !empty($child) ? $child : [];

                //规格信息
                $spec = Db::name("good_spec")->field('id,spec_id,spec_value_id')->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();

                $speclist = [];
                if (!empty($spec)) {
                    foreach ($spec as $val) {
                        $tmp = [];
                        $tmp['spec_id'] = $val['spec_id'];
                        $tmp['spec_value_id'] = $val['spec_value_id'];
                        $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
                        $tmp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
                        $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
                        $tmp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
                        $speclist[] = $tmp;
                    }
                }
                $temp['specinfo'] = $speclist;

                $data[] = $temp;
            }
        }
        return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
    }
    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function Stat(){
        $condition =[["gs.is_del","=",0]];
        $wsm_code = isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']):"";
        if($wsm_code!=""){
            //$condition["gs.wsm_code"] = $wsm_code;
            $condition[]=["gs.wsm_code","=",$wsm_code];
        }
        $suplierNo = isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
        if($suplierNo!==""){
            $wsmcode = Db::name("warehouse_info")->where(['supplierNo'=>$suplierNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["gs.wsm_code","in",$wsmcode];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
        if ($companyNo !== "") {
            // $where['khNo'] = $khNo;
            $wsmcode = Db::name("warehouse_info")->where(['companyNo'=>$companyNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["gs.wsm_code","in",$wsmcode];
        }
//        $type_code = isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
//        if($type_code!=""){
//            $condition[] = ["gs.good_type_code","=",$type_code];
//        }
        $statlist = Db::name("good_stock")
            ->alias('gs')
            ->where($condition)
            ->where('whi.wsm_type','<>',2)//2表示虚拟仓,虚拟仓不参与
            ->leftJoin('warehouse_info whi','whi.wsm_code=gs.wsm_code AND whi.is_del=0')
            ->field("sum(gs.wait_in_stock) as wait_in_stock,sum(gs.wait_out_stock) as wait_out_stock ,sum(gs.usable_stock) as usable_stock,sum(gs.intra_stock) as intra_stock,sum(gs.usable_stock+gs.wait_out_stock) as total_stock,sum(gs.warn_stock) as warn_stock")
            ->find();
        return app_show(0,"获取成功",$statlist);
    }

    public function all(){
        $condtion=["a.is_del"=>0,"b.is_del"=>0];
        $wsmcode =isset($this->post['wsmcode']) &&$this->post['wsmcode'] !=="" ? trim($this->post['wsmcode']):"";
        if($wsmcode!=""){
            $typecode= Db::name("good_stock")->where(["wsm_code"=>$wsmcode,'is_del'=>0])->column("good_type_code");
            $condtion["b.type_code"] = $typecode;
        }
        $good_code =isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
        if($good_code!=""){
            $condtion['b.type_code'] = $good_code;
        }
        $goodcode =isset($this->post['good_code']) &&$this->post['good_code'] !=="" ? trim($this->post['good_code'])
            :"";
        if($goodcode!=""){
            $condtion['a.good_code'] = $goodcode;
        }
        $good_name =isset($this->post['good_name']) &&$this->post['good_name'] !=="" ? trim($this->post['good_name'])
            :"";
        if($good_name!=""){
            $condtion['a.good_name'] = Db::raw("like '%{$good_name}%'");
        }
        $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
        if($supplierNo!=""){
            $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find();
            if(empty($supplier)){
                return error_show(1004,"未找到供应商信息");
            }
            $condtion["a.gys_code"] = $supplierNo;
        }

        $list = Db::name("good_type")->alias("b")->join("good a","a.good_code = b.good_code","left")
            ->where($condtion)->field("b.id,a.good_code,a.good_name,a.good_desc,a.brand,a.original_price,a.cg_saler,a.good_type,a.market_fee,a.gys_code,a.unit,
                a.sort_f,a.sort_s,a.sort_t,b.color,b.material,b.specs,b.is_main,b.type_code,b.attribute,b.status as bstatus,a.addtime,a.updatetime")
            ->select();
        $data=[];
        foreach ($list as $key=>$value){
            $stock = Db::name("good_stock")->where(["good_type_code"=>$value['type_code'],"is_del"=>0])->sum("usable_stock");
            $value['usable_stock']=$stock;
            $supplier = Db::name("supplier")->where(["code"=>$value['gys_code']])->find();
            $value['supplier_name'] =isset($supplier['name']) ?$supplier['name'] :"";
            $data[]=$value;
        }
        return app_show(0,"获取成功",$data);
    }
    public function loglist(){
        $page = isset($this->post['page']) && $this->post['page']!==""? intval($this->post['page']) :"1";
        $size = isset($this->post['size']) && $this->post['size']!==""? intval($this->post['size']) :"10";
        $where=[];
        $wsm_code= isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
        if($wsm_code!=""){
            $where[]=['b.wsm_code',"like","%$wsm_code%"];
        }
        $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
        if($supplierNo!=""){
            $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find();
            if(empty($supplier)){
                return error_show(1004,"未找到供应商信息");
            }
            $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$supplierNo])->column("wsm_code");
            $where[]=['b.wsm_code',"in",$wsmcode];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
        if ($companyNo !== "") {
            $wsmcode = Db::name("warehouse_info")->where(['companyNo'=>$companyNo,"is_del"=>0])->column("wsm_code");
            $condition[]=["b.wsm_code","in",$wsmcode];
        }
        $count = Db::name("good_log")
            ->alias('a')
            ->join("good_stock b","b.id=a.id","left")
            ->where($where)
            ->count();
        $total = ceil($count/$size);
        $page = $total>=$page ? $page :$total;
        $list = Db::name("good_log")
            ->alias('a')
            ->join("good_stock b","b.id=a.stock_id","left")
            ->where($where)
            ->page($page,$size)
            ->order("a.addtime desc")
            ->field("a.good_log_code,a.action_name,a.type,a.stock_name,a.stock,a.action_type,b.spuCode,a.addtime,a.action_uid,b.wsm_code")
            ->select()
            ->toArray();
        $data=[];

        $action_uids = array_column($list, 'action_uid');
        $action_uids = array_unique($action_uids);
        $item = [];
        foreach ($action_uids as $action_uid) {
            $items = Db::name("depart_user")->where('uid', $action_uid)->column("itemid");
            foreach ($items as $it) {
                $item[$action_uid][] = implode('/', array_column(GetPart($it), 'name'));
            }
        }

        foreach ($list as $value){
            $wsm = Db::name("warehouse_info")
                ->alias("a")
                ->Join("supplier b","a.supplierNo=b.code","left")
                ->Join("business bus","bus.companyNo=a.companyNo","left")
                ->where(['a.wsm_code'=>$value['wsm_code'],"a.is_del"=>0])
                ->field("a.name as wsm_name,wsm_code,b.code,b.name,bus.company,bus.companyNo")
                ->find();

            $godd =Db::name("good")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
            $value['good_name']=isset($godd['good_name'])? $godd['good_name']:"";
            $value['cat_id']=isset($godd['cat_id'])? $godd['cat_id']:"";
            $int = isset($value['cat_id']) && $value['cat_id'] != 0 ? made($value['cat_id']) : [];
            $value['wsm_name'] =$wsm['wsm_name'];
            $value['wsm_code'] =$wsm['wsm_code'];
            $value['code'] =$wsm['code'];
            $value['name'] =$wsm['name'];
            $value['action_type_cn'] =$this->acton[$value['action_type']];
            $value['item'] =$item[$value['action_uid']];
            $value['can'] =$int;
            $value['company'] =$wsm['company']??'';
            $value['companyNo'] =$wsm['companyNo']??'';
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
    }

    public  function  goodlist(){
        $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
        $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
        $where =[["a.is_del","=",0],["b.exam_status","=",6],["b.is_del","=",0],["c.is_del","=",0],["c.status","=",1]];
        $paltcode = isset($this->post["platform_code"])&& $this->post["platform_code"]!=""? trim($this->post["platform_code"]):"";
        if($paltcode!=""){
            $where[]=["platform_code","=",$paltcode];
        }
        $companyNo = isset($this->post["companyNo"])&& $this->post["companyNo"]!=""? trim($this->post["companyNo"])
            :"";
        if($companyNo!=""){
            $where[]=["companyNo","like","%$companyNo%"];
        }

        $cat_id = isset($this->post["cat_id"])&& $this->post["cat_id"]!==""? intval($this->post["cat_id"]):"";
        if($cat_id!==""){
            $where[]=["cat_id","=",$cat_id];
        }

        $buy_num = isset($this->post["buy_num"])&& $this->post["buy_num"]!==""? intval($this->post["buy_num"]):"";
        if($buy_num!==""){
            $where[]=["min_num","<=",$buy_num];
        }
        $price = isset($this->post["price"])&& $this->post["price"]!==""? floatval($this->post["price"]):"";
        if($buy_num!==""){
            $where[]=["sale_price","<=",$price];
        }
        $count = Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")->leftJoin("good_ladder c","b.skuCode=c.skuCode")
            ->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")->leftJoin("good_ladder c","b.skuCode=c.skuCode")->where($where)->page
        ($page,$size)->field("c.id,c.min_num,c.sale_price,.a.good_name,a.spuCode,a.cat_id,a.brand_id,a.companyNo,a.supplierNo,a.good_unit,b.skuCode,b.platform_code,b.plat_code,b.id as good_id,b.addtime,b.updatetime,b.exam_status")
            ->order("b.addtime desc")
            ->select();
        $data=[];
        foreach ($list as $value){
            $value['cat_info']= made($value['cat_id'],[]);
            $platform = Db::name("platform")->where(["id"=>$value['platform_code']])->find();
            $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
            $value['platform_code_en'] = isset($platform['platform_code']) ? $platform['platform_code']:"";
            $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
            $value['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
            $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
            $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
            $unit = Db::name("unit")->where(["id"=>$value['good_unit']])->find();
            $value['unit'] =isset($unit['unit'])?$unit['unit']:"";
            $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
            $value['company'] = isset($company['company'])?$company['company']:"";
            $value['stock_total'] = Db::name("good_stock")->where(['spuCode'=>$value['spuCode'],"is_del"=>0])->sum("usable_stock");
            $value['status']=$value['exam_status'];
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
    }


    //根据价格区间,筛选符合条件的商品列表
    public function getGoodListByLadder()
    {
        //1.请求参数
        $param = $this->request->only([
            'is_stock' => '',
            'min_price' => 0,
            'max_price' => 0,
            'moq' => 0,
            'cat_id' => 0,
            'good_name' => '',
            'skucode' => '',
            'spucode' => '',
            'start' => '',
            'end' => '',
            'good_type' => '',
            'supplierNo' => '',
            'companyNo' => '',
            'creater' => '',
            'createrid' => '',
            'status' => '',
            'platform_code' => '',
            'plat_code' => '',
            'exam_status' => '',
            'brand_id' => 0,
            'page' => 1,
            'size' => 15
        ], 'post', 'trim');

        $where = $ladder_where = [];

        if ($param['min_price'] && $param['max_price']) {
            $skuCodes = Db::name('good_ladder')
                ->whereBetween('sale_price', [$param['min_price'], $param['max_price']])
                ->where(['is_del' => 0, 'status' => 1])
                ->group('skuCode')
                ->column('skuCode');
            $where[] = ['b.skuCode', 'in', $skuCodes];
            $ladder_where[] = ['sale_price', 'between', [$param['min_price'], $param['max_price']]];
        }

        if ($param['is_stock'] != '') $where[] = ['a.is_stock', '=', $param['is_stock']];
        if ($param['moq']) $where[] = ['a.moq', '>=', $param['moq']];
        if ($param['cat_id']) $where[] = ['a.cat_id', '=', $param['cat_id']];
        if ($param['good_name']) $where[] = ['a.good_name', 'like', '%' . $param['good_name'] . '%'];
        if ($param['skucode']) $where[] = ['b.skucode', 'like', '%' . $param['skucode'] . '%'];
        if ($param['spucode']) $where[] = ['b.spucode', 'like', '%' . $param['spucode'] . '%'];
        if ($param['start']) $where[] = ['a.addtime', '>=', date('Y-m-d H:i:s', strtotime($param['start']))];
        if ($param['end']) $where[] = ['a.addtime', '<', date('Y-m-d H:i:s', strtotime($param['end'] + 24 * 3600))];
        if ($param['good_type']!='') $where[] = ['a.good_type', '=', $param['good_type']];
        if ($param['supplierNo']) $where[] = ['a.supplierNo', '=', $param['supplierNo']];
        if ($param['companyNo']) $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
        if ($param['creater']) $where[] = ['a.creater', 'like', '%' . $param['creater'] . '%'];
        if ($param['createrid']) $where[] = ['a.createrid', '=', $param['createrid']];
        if ($param['status'] != '') $where[] = ['a.status', '=', $param['status']];
        if ($param['platform_code']) $where[] = ['platform_code', '=', $param['platform_code']];
        if ($param['plat_code']) $where[] = ['b.plat_code', 'like', '%' . $param['plat_code'] . '%'];
        if ($param['exam_status'] != '') $where[] = ['b.exam_status', '=', $param['exam_status']];
        if ($param['brand_id']) $where[] = ['a.brand_id', '=', $param['brand_id']];


        //5.获取结果

        $count = Db::name('good')
            ->alias("a")
//            ->field("b.skuCode,b.platform_code,b.plat_code,b.id as good_id,a.*,b.addtime,b.updatetime,b.exam_status,gb.createrid purchase_id,gb.creater purchase,gl.min_num,gl.max_num")//成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
            ->leftJoin("good_platform b", "a.spuCode=b.spuCode")
            ->leftJoin("good_basic gb", "gb.spuCode=a.spuCode")
//            ->leftJoin("good_ladder gl", "gl.skuCode=b.skuCode")
            ->where($where)
            ->where('b.exam_status', '=', 6)//6表示上线成功
            ->order("b.addtime desc")
            ->count('a.id');

        $list = Db::name('good')
            ->alias("a")
            ->field("b.skuCode,b.platform_code,b.plat_code,b.id as good_id,a.*,b.addtime,b.updatetime,b.exam_status,gb.createrid purchase_id,gb.creater purchase")//成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
            ->leftJoin("good_platform b", "a.spuCode=b.spuCode")
            ->leftJoin("good_basic gb", "gb.spuCode=a.spuCode")
//            ->leftJoin("good_ladder gl", "gl.skuCode=b.skuCode")
            ->page($param['page'], $param['size'])
            ->where($where)
            ->where('b.exam_status', '=', 6)//6表示上线成功
            ->order("b.addtime desc")
//            ->fetchSql()
            ->select()
            ->toArray();
//        halt($list);

        //6.补充数据,照搬list方法
        $data = [];
        foreach ($list as $value) {
            $value['cat_info'] = made($value['cat_id'], []);
            $platform = Db::name("platform")->where(["id" => $value['platform_code']])->find();
            $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name'] : "";
            $value['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en'] : "";
            $supplier = Db::name("supplier")->where(["code" => $value['supplierNo']])->find();
            $value['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
            $brand = Db::name("brand")->where(["id" => $value['brand_id']])->find();
            $value["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
            $unit = Db::name("unit")->where(["id" => $value['good_unit']])->find();
            $value['unit'] = isset($unit['unit']) ? $unit['unit'] : "";
            $company = Db::name("business")->where(["companyNo" => $value['companyNo']])->find();
            $value['company'] = isset($company['company']) ? $company['company'] : "";
            $value['stock_total'] = Db::name("good_stock")->where(['spuCode' => $value['spuCode'], "is_del" => 0])->sum("usable_stock");
            $value['status'] = $value['exam_status'];
            $value['exclusive'] = makeExcluse($value['is_exclusive']);
            $value['good_info_img'] = $value['good_info_img'];
            $value['good_img'] = $value['good_img'];
            $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
            $speclist = [];
            if (!empty($spec)) {
                foreach ($spec as $val) {
                    $temp = [];
                    $temp['spec_id'] = $val['spec_id'];
                    $temp['spec_value_id'] = $val['spec_value_id'];
                    $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
                    $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
                    $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
                    $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
                    $speclist[] = $temp;
                }
            }
            $value['specinfo'] = $speclist;
            $value['noble_name'] = isset($value['noble_metal']) && $value['noble_metal'] != 0 ? $this->noble[$value['noble_metal']] : "";
            
            //补充阶梯数据
            $ladder_info = Db::name('good_ladder')
                ->field('min_num,max_num,sale_price')
                ->where(['skuCode' => $value['skuCode']])
                ->where($ladder_where)
                ->order('min_num', 'asc')
                ->find();

            $value['min_num'] = isset($ladder_info['min_num']) ? $ladder_info['min_num'] : 0;
            $value['max_num'] = isset($ladder_info['max_num']) ? $ladder_info['max_num'] : 0;
            $value['sale_price'] = isset($ladder_info['sale_price']) ? $ladder_info['sale_price'] : 0;


            $data[] = $value;
        }

        return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);

    }

}