<?php
declare (strict_types = 1);

namespace app\txx\controller;

use app\BaseController;
use app\txx\model\Act;
use app\txx\model\ActGood;
use app\txx\model\YzGood;
use app\txx\model\PlatformYouzan;
use think\App;
use think\Exception;
use think\facade\Db;
use think\facade\Validate;
use think\Request;

class Good extends BaseController
{
	protected $uid=0;
	protected $uname='';
	protected $platformid=37;
	/**@param array snArr 商品编号sn数组
     * @param string actCode 活动编号
	* @return \think\response\Json
	* @throws \think\exception\DbException
	 */
    private $acton=[];
    public $noble=[];
    public  $origin_img_host ="http://stock.api.wanyuhengtong.com";
    public  $ssl_img_host ="https://image.wanyuhengtong.com";
	public function __construct(App $app) {
		parent::__construct($app);
		if($this->request->isCx==1){
			$this->uid=$this->request->uid;
			$this->uname=$this->request->uname;
		}

	}

    public function AddGood()
    {
    	$post = $this->request->only(["snArr"=>[],"actCode"=>''],"post");
    	$snArr= $post['snArr'];
    	if(empty($snArr)) return  json_show(1004,"参数错误 snArr 不能为空");
    	$platformYouzan =new PlatformYouzan();
    	$skuCodes = array_column($snArr,"skuCode");
		$goodArr = $platformYouzan->where(["skuCode"=>$skuCodes,"is_del"=>0])->select()->toArray();
		if(empty($goodArr)) return json_show(1005,"未找到商品有效数据");
		$actCode= $post['actCode']!=''? trim($post['actCode']):"";
		if($actCode=='') return json_show(1004,"参数错误 actCode 不能为空");
//	    $roundId= $post['roundId']!=''? trim($post['roundId']):"";
//	    if($roundId=='') return json_show(1004,"参数错误 roundId 不能为空");
		$act =new Act();
		$isActExit=$act->where(['actCode'=>$actCode,"is_del"=>0])->find();
		if($isActExit==false) return json_show(1005,"未找到活动有效数据");

		$actGood=new ActGood();
		Db::startTrans();
		try{
			$data=[];
			foreach ($snArr as $item){
				$isExit = $actGood->find(["actCode"=>$actCode,"yz_good_code"=>$item['skuCode'],"roundId"=>$item['roundId']??"","is_del"=>0]);
				if($isExit) throw new Exception("活动商品已添加",1006);
				$temp=[
					"actCode"=>$actCode,
					"yz_good_code"=>$item['skuCode'],
					"stock_num"=>$item['stock_num'],
					"used_num"=>0,
					"balance_num"=>$item['stock_num'],
					"apply_id"=>$this->uid,
					"apply_name"=>$this->uname,
					"roundId"=>$item['roundId']??"",
					"status"=>0,
					"addtime"=>date("Y-m-d H:i:s"),
					"updatetime"=>date("Y-m-d H:i:s")
					];
				$data[]=$temp;
			}
			if (empty($data)){
					throw new Exception("活动商品不能为空",1006);
			}
			$isAdd=ActGood::insertAll($data);
			if ($isAdd==false){
					throw new Exception("活动商品添加失败",1006);
			}
			$up =$act->where($isActExit->toArray())->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
			if ($up==false){
				throw new Exception("活动商品添加失败",1006);
			}
			Db::commit();
			return json_show(0,"活动商品添加成功");
		}catch (\Exception $e){
			Db::rollback();
			return json_show(1008,$e->getMessage());
		}
    }

    /**活动商品列表
      * @param int page 页数
      * @param int size 每页显示数量
      * @param string act_name  活动名称
      * @param string actCode 活动编号
      * @param string company_name 活动公司名称
      * @param string contactor 活动联系人
      * @param int act_type 活动类型 暂无
      * @param int cat_id  采销分类
      * @param string spuCode 商品成本编号
      * @param string skuCode 商品上线编号
      * @param int exam_status 商品上线审核状态
     */
    public function list()
    {
       $param = $this->request->only([
      	"page"=>1,
      	"size"=>15,
      	"act_name"=>'',
      	"actCode"=>'',
      	"company_name"=>'',
       	"contactor"=>'',
        "act_type"=>'',
        "good_name"=>'',
        "cat_id"=>'',
        "spuCode"=>'',
        "skuCode"=>'',
        "status"=>"",
        "roundId"=>"",
        "exam_status"=>'',
      	],"post",'trim');
      $condition=[["a.is_del","=",0],["b.is_del","=",0]];
      if($param['act_name']!='') $condition[]=["b.act_name","like","%{$param['act_name']}%"];
      if($param['actCode']!='') $condition[]=["a.actCode","like","%{$param['actCode']}%"];
      if($param['company_name']!='') $condition[]=["b.company_name","like","%{$param['company_name']}%"];
      if($param['contactor']!='') $condition[]=["b.contactor","like","%{$param['contactor']}%"];
      if($param['good_name']!='') $condition[]=["d.good_name","like","%{$param['good_name']}%"];
      if($param['skuCode']!='') $condition[]=["c.skuCode","like","%{$param['skuCode']}%"];
      if($param['act_type']!='') $condition[]=["b.act_type","=",$param["act_type"]];
      if($param['roundId']!=='') $condition[]=["a.roundId","=",$param["roundId"]];
      if($param['status']!=='') $condition[]=["a.status","=",$param["status"]];
       if ($param['exam_status'] !== '') {
        	if($param['exam_status']==1){
        		$where[] = ['c.exam_status', "=", 6];
        	}else{
        		$where[] = ['c.exam_status', "<>", 6];
        	}
        }
      $actm=new ActGood();
       if($this->request->isCx==1){
       	$field="a.id,a.actCode,a.yz_good_code,a.apply_id,a.apply_name,a.stock_num,a.used_num,a.balance_num,a.status,
      a.addtime,a.updatetime,c.plat_code,b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,
     c.final_price,c.desc,d.good_name,d.good_unit,c.yz_cat_id,d.cat_id,a.roundId,d.brand_id";
       }else{
		$field="a.id,a.actCode,a.yz_good_code,a.stock_num,a.used_num,a.balance_num,a.status,a.addtime,a.updatetime,
		b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,c.final_price,c.desc,d.good_name,
		d.good_unit,d.cat_id,d.brand_id,a.roundId";
       }
      $count =$actm->alias("a")
      ->leftJoin("act b","a.actCode=b.actCode")
      ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
      ->leftJoin("good d","c.spuCode=d.spuCode")
      ->where($condition)->count();
      $total =ceil($count/$param['size']);
      $page = $param['page']>= $total ?intval($total):intval($param['page']);
      $list=$actm->alias("a")
      ->leftJoin("act b","a.actCode=b.actCode")
      ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
      ->leftJoin("good d","c.spuCode=d.spuCode")
	   ->field($field)->where($condition)->page($page,intval($param['size']))
	   ->order("addtime desc")->select()->toArray();
       //6.补充数据,照搬list方法
        $all_brand = Db::name('brand')
            ->where('is_del', 0)
            ->whereIn('id', array_column($list, 'brand_id'))
            ->column('brand_name', 'id');
         $unit = Db::name("unit")->where(["id" => array_column($list, 'good_unit')])->column('unit', 'id');
            $value['unit'] = isset($unit['unit']) ? $unit['unit'] : "";
	 $yzgood =new YzGood();
      foreach ($list as &$item){
		$item['unit'] =$unit[$item['good_unit']] ?? "";
		$item['brand_name'] = $all_brand[$item['brand_id']]?? "";
		$item['web_url_num'] = Db::name("act_goodurl")->where(["act_good_id"=>$item['id'],"is_del"=>0])->count();
	 $item['origin_price'] =$yzgood->where(["item_no"=>$item['yz_good_code'],"is_del"=>0])->value("origin",'');
		if($this->request->isCx==1){
			$item['cat_info'] = made($item['cat_id'], []);
		}else{
			unset($item['cat_id']);
			unset($item['good_unit']);
			unset($item['brand_id']);
		}

      }
      return json_show(0,"获取成功",["list"=>$list,"count"=>$count]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function EditGood()
    {
    	$post = $this->request->only(["goodArr"=>[]],"post");
    	$snArr= $post['goodArr'];
    	if(empty($snArr)) return  json_show(1004,"参数 goodArr 不能为空");
    	$platformYouzan =new PlatformYouzan();
    	$goodCode = array_column($snArr,"yz_good_code");
		$goodArr = $platformYouzan->where(["skuCode"=>$goodCode,"is_del"=>0])->select()->toArray();
		if(empty($goodArr)) return json_show(1005,"未找到商品有效数据");
//	    $roundId= $post['roundId']!=''? trim($post['roundId']):"";
//	    if($roundId=='') return json_show(1004,"参数错误 roundId 不能为空");
		$actGood=new ActGood();
		Db::startTrans();
		try{
			$data=[];
			foreach ($snArr as $item){
				$isExit = $actGood->where(["actCode"=>$item['actCode'],"yz_good_code"=>$item['yz_good_code'],
				"is_del"=>0])->find();
				if($isExit==false) throw new Exception("活动商品不存在",1006);
				$temp=[
					"id"=>$isExit['id'],
					"stock_num"=>$item['stock_num'],
					"balance_num"=>$item['stock_num'],
					"roundId"=>$item['roundId']??"",
					"is_del"=>$item['is_del']??0,
					"updatetime"=>date("Y-m-d H:i:s")
					];
				$data[]=$temp;

			}
			$isAdd=$actGood->saveAll($data);
			if ($isAdd==false){
					throw new Exception("活动商品更新失败",1006);
			}
			Db::commit();
			return json_show(0,"活动商品更新成功");
		}catch (\Exception $e){
			Db::rollback();
			return json_show(1008,$e->getMessage());
		}

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id 参数详情id
     * @return \think\Response
     */
    public function GoodInfo()
    {
    	$param =$this->request->only(["id"=>''],"post","trim");
    	if($param['id']=='') return json_show(1004,"参数 id 不能为空");
    	 if($this->request->isCx==1){
       	    $field="a.id,a.actCode,a.yz_good_code,a.apply_id,a.apply_name,a.stock_num,a.used_num,a.balance_num,a.status,
            a.addtime,a.updatetime,c.plat_code,b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,
            c.final_price,c.desc,d.good_name,d.good_unit,c.yz_cat_id,d.cat_id,d.brand_id,a.roundId";
	       }else{
			$field="a.id,a.actCode,a.yz_good_code,a.stock_num,a.used_num,a.balance_num,a.status,a.addtime,a.updatetime,
			b.act_name,b.company_name,b.contactor,b.mobile,b.startTime,b.endTime,c.final_price,c.desc,d.good_name,
			d.good_unit,d.cat_id,d.brand_id,a.roundId";
            }
    	$actm=new ActGood();
    	$info=$actm->alias("a")
      ->leftJoin("act b","a.actCode=b.actCode")
      ->leftJoin("platform_youzan c","a.yz_good_code=c.skuCode")
      ->leftJoin("good d","c.spuCode=d.spuCode")
      ->field($field)
      ->find($param);
    	if($info==false){
    	    return json_show(1005,"未找到数据");
    	}
    	$info['brand_name'] = Db::name('brand')
            ->where('is_del', 0)
            ->where('id',$info['brand_id'])
            ->value('brand_name', '');
         $info['unit'] = Db::name("unit")->where("id" ,$info['good_unit'])->value('unit', '');
          if($this->request->isCx==1){
          	 $info['cat_info'] =made($info['cat_id']);
          }else{
          	unset($info['good_unit']);
            unset($info['brand_id']);
          }
        return json_show(0,"获取成功",$info->toArray());
    }

    /** 商品链接导入
     * @param  int  act_good_id 商品活动id
     * @param  array  web_url 商品链接url
     * @return \think\Response
     */
    public function GoodUrlImport()
    {
    	$param = $this->request->only(["web_url"=>[]],"post");
    	$validate=Validate::rule([
    		'web_url|商品链接' => 'require|array'
            ]);
    	if($validate->check($param)==false) return json_show(1004,$validate->getError());
    	$actm=new ActGood();

    	$web=[];
    	foreach ($param['web_url'] as $item){
    		$actGood =$actm->where(["id"=>$item['act_good_id'],"is_del"=>0])->find();
    	    if($actGood==false) return json_show(1005,'未找到商品数据');
				$temp=[
					"act_good_url"=>$item['act_good_url'],
					"act_good_id"=>$item['act_good_id'],
					"status"=>1,
					"is_del"=>0,
					"apply_id"=>$this->uid,
					"apply_name"=>$this->uname,
					"addtime"=>date("Y-m-d H:i:s"),
					"updatetime"=>date("Y-m-d H:i:s")
					];
				$web[]=$temp;
    	}

    	Db::startTrans();
    	try{
    	$isAdd=Db::name("act_goodurl")->insertAll($web);
    	if( $isAdd<=0 ){
    		Db::rollback();
    		return json_show(1006,"商品链接导入失败");
    	}
    	$ids = array_unique(array_column($param['web_url'],"act_good_id"));
		foreach ($ids as $item){
			$actGood =$actm->where(["id"=>$item,"is_del"=>0])->find();
    	    if($actGood==false)throw new Exception("未找到活动商品",1006);
    	    $num =Db::name("act_goodurl")->where(["act_good_id"=>$item,"is_del"=>0])->count();
    	    if($num>$actGood['stock_num'])throw new Exception("导入连接数量超过活动数量",1006);
    	    if($num==$actGood['stock_num']){
    	    	$up =$actm->where(["id"=>$item,"is_del"=>0])->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
    	    	if($up==false) return json_show(1005,'商品数据更新失败');
    	    	$upunum = $actm->where(["actCode"=>$actGood["actCode"],"is_del"=>0,"status"=>0])->count();
    	    	if($upunum==0){
    	    		$up1 =Db::name("Act")->where(["actCode"=>$actGood["actCode"],"is_del"=>0])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
			        if ($up1==false){
				        throw new Exception("活动数据更新失败",1006);
			        }
    	    	}
    	    }
		}
    	Db::commit();
    	return json_show(0,'商品链接导入成功');
    	}catch (\Exception $e){
    		Db::rollback();
    		return json_show(1008,$e->getMessage());
    	}
    }

    /**
     * 保存更新的资源
     * @param  int  $page
     * @param  int  $size
     * @param  string  $act_name
     * @param  string  $actCode
     * @param  int  $act_good_id
     * @return \think\Response
     */
    public function GoodUrlList()
    {
    	  $param = $this->request->only([
      	"page"=>1,
      	"size"=>15,
      	"act_name"=>'',
      	"actCode"=>'',
      	"act_good_id"=>'',
      	"good_code"=>'',
      	"roundId"=>'',
      	],"post","trim");
		$condition=[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0]];
		if($param['act_name']!='')$condition[]=["c.act_name","like","%{$param['act_name']}%"];
		if($param['actCode']!='')$condition[]=["c.actCode","like","%{$param['actCode']}%"];
		if($param['act_good_id']!='')$condition[]=["a.act_good_id","=",$param['act_good_id']];
		if($param['good_code']!='')$condition[]=["b.yz_good_code","=",$param['good_code']];
	    if($param['roundId']!=='') $condition[]=["b.roundId","=",$param["roundId"]];
		$count = Db::name("act_goodurl")->alias("a")
		->leftJoin("act_good b","a.act_good_id=b.id  and b.is_del=0")
		->leftJoin("act c","b.actCode=c.actCode and c.is_del=0")
		->where($condition)
		->count();
		$total =ceil($count/$param['size']);
		$page =$param['page']>=$total ? intval($total) : intval($param['page']);
		$list = Db::name("act_goodurl")->alias("a")
		->leftJoin("act_good b","a.act_good_id=b.id  and b.is_del=0")
		->leftJoin("act c","b.actCode=c.actCode and c.is_del=0")
		->where($condition)->page($page,intval($param['size']))
		->field("a.id,a.act_good_url,b.roundId,a.act_good_id,a.apply_id,a.apply_name,a.addtime,a.updatetime,b.stock_num")
		->select()->toArray();
		foreach ($list as &$item){
			$item['web_url_num'] = Db::name("act_goodurl")->where(["act_good_id"=>$item['act_good_id'],"is_del"=>0])->count();
		}
		return json_show(0,"获取成功",["list"=>$list,"count"=>$count]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function GoodUrlDel()
    {
     $param = $this->request->only(["web_url_ids"=>[]],"post");
     if(empty($param['web_url_ids'])|| $param["web_url_ids"]=="") return json_show(1004,"参数 web_url_ids 不能为空");
     $actm=new ActGood();
     $web=[];
    	foreach ($param['web_url_ids'] as $item){
    		$actGood =Db::name("act_goodurl")->where(["id"=>$item,"is_del"=>0])->find();
    	    if($actGood==false) return json_show(1005,'未找到商品链接数据');
    	    $web[]=$actGood['act_good_id'];
    	}
	    $web =array_unique($web);
    	Db::startTrans();
    	try{
		 $isDel=Db::name("act_goodurl")->where(["id"=>$param['web_url_ids']])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
		 if($isDel==false){
		 	 throw new Exception("活动商品链接删除失败",1006);
		 }
		foreach ($web as $item){
			$actGood =$actm->where(["id"=>$item,"is_del"=>0])->find();
    	    if($actGood==false) return json_show(1005,'未找到商品数据');
    	    $num =Db::name("act_goodurl")->where(["act_good_id"=>$item,"is_del"=>0])->count();
    	    if($num<$actGood['stock_num']){
    	    	$up =$actm->where(["id"=>$item,"is_del"=>0])->update(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
    	    	if($up==false) return json_show(1005,'商品数据更新失败');
    	    	$upunum = $actm->where(["actCode"=>$actGood["actCode"],"is_del"=>0,"status"=>0])->count();
    	    	if($upunum!=0){
    	    		$up1 =Db::name("Act")->where(["actCode"=>$actGood["actCode"],"is_del"=>0])->update(["status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
			        if ($up1==false){
				        throw new Exception("活动数据更新失败",1006);
			        }
    	    	}

    	    }
		}
    	Db::commit();
    	return json_show(0,'商品链接导入成功');
    	}catch (\Exception $e){
    		Db::rollback();
    		return json_show(1008,$e->getMessage());
    	}
    }

     //获取跟有赞对接的平台所属的商品上线信息
    public function listByYz()
    {
    	 $this->noble=\think\facade\Config::get("noble");
        $order=\think\facade\Config::get("order");
        $this->acton=$order['order_type'];
        $param = $this->request->only([
            'page' => 1,
            'size' => 15,
            'good_name' => '',
            'skucode' => '',
            'plat_code' => '',
            'exam_status' => '',
        ], 'post', 'trim');
        $where = [["b.is_del", "=", 0],["b.platform_id","=",$this->platformid]];
        if ($param['good_name'] !== "") $where[] = ['a.good_name', 'like', '%' . $param['good_name'] . '%'];
        if ($param['skucode'] !== '') $where [] = ['b.skucode', 'like', '%' . $param['skucode'] . '%'];
        if ($param['plat_code'] !== '') $where[] = ["b.plat_code", "like", '%' . $param['plat_code'] . '%'];
        if ($param['exam_status'] !== '') {
        	if($param['exam_status']==1){
        		$where[] = ['b.exam_status', "=", 6];
        	}else{
        		$where[] = ['b.exam_status', "<>", 6];
        	}
        }
		if($this->request->isCx==1){
			$field="b.skuCode,b.platform_id platform_code,b.plat_code,b.id as platform_youzan_id,a.cat_id,a.good_name,
			a.good_img,a.good_info_img,a.good_thumb_img,b.createrid,a.is_exclusive,a.brand_id,a.supplierNo,a.good_unit,
			a.noble_metal,a.companyNo,a.spuCode,a.good_type,b.creater,b.addtime,b.updatetime,b.exam_status,a.createrid purchase_id,
			a.creater purchase";
		}else{
			$field="b.skuCode,b.plat_code,b.id as platform_youzan_id,
            a.good_name,a.good_img,a.good_info_img,a.good_thumb_img,a.brand_id,
            a.good_unit,b.addtime,b.updatetime,b.exam_status,a.spuCode,
            b.sale_price,b.final_price";
		}

        $count = Db::name('platform_youzan')
            ->alias("b")
            ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
            ->where($where)
            ->count();

        $total = ceil($count / $param['size']);
        $param['page'] = $param['page'] >= $total ? $total : $param['page'];
        $list = Db::name('platform_youzan')
            ->alias("b")
            ->field($field)
            //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
            ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
            ->where($where)
            ->page(intval($param['page']),intval($param['size']))
            ->order("b.addtime desc")
            ->select()
            ->toArray();
        $all_createrid = array_column($list,'purchase_id');
        $item = get_company_name_by_uid($all_createrid);
        $data = [];
        $yzgood = new YzGood();
        foreach ($list as $value) {
            $brand = Db::name("brand")->field('id,brand_name')->where(["id" => $value['brand_id']])->find();
            $value["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";

            $unit = Db::name("unit")->field('id,unit')->where(["id" => $value['good_unit']])->find();
            $value['unit'] = isset($unit['unit']) ? $unit['unit'] : "";
		    $value['exam_status'] = $value['exam_status']==6?1:0;
            $value['origin_price'] =$yzgood->where(["item_no"=>$value['skuCode'],"is_del"=>0])->value("origin",'');

            $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['good_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_img']);
            $value['good_info_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_img']);
            $value['good_thumb_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$value['good_img']);
            if($this->request->isCx==1){
        	$value['cat_info'] = made($value['cat_id'], []);
            $platform = Db::name("platform")->field('id,platform_name')->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")->field('id,name')->where(["code" => $value['supplierNo']])->find();
            $value['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
            $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['exclusive'] = makeExcluse($value['is_exclusive']);
            $value['noble_name'] = isset($value['noble_metal']) && $value['noble_metal'] != 0 ? $this->noble[$value['noble_metal']] : "";
//            $itemid =Db::name("depart_user")->where(["uid"=>$value["purchase_id"],"is_del"=>0])->value("itemid",'');
            $value['company_name'] = $item[$value['purchase_id']]??'';
        	}else{
            	unset($value['brand_id']);
			unset($value['good_unit']);
			unset($value['spuCode']);
        	}

            $value['specinfo'] = $speclist;
            $data[] = $value;
        }
        return json_show(0, "获取成功", ['list' => $data, 'count' => $count]);
    }

	/**
	 * @return \think\response\Json
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function YzGoodInfo(){
	    $param =$this->request->only(["skuCode"=>''],"post","trim");
	    if($param['skuCode']=='') return json_show(1004,"参数 skuCode 不能为空");
	    if($this->request->isCx==1){
			$field="b.skuCode,b.platform_id platform_code,b.plat_code,b.id as platform_youzan_id,a.cat_id,a.good_name,
			a.good_img,a.good_info_img,a.good_thumb_img,b.createrid,a.is_exclusive,a.brand_id,a.supplierNo,a.good_unit,
			a.noble_metal,a.companyNo,a.spuCode,a.good_type,b.creater,b.addtime,b.updatetime,b.exam_status,a.createrid purchase_id,
			a.creater purchase";
		}else{
			$field="b.skuCode,b.plat_code,b.id as platform_youzan_id,
            a.good_name,a.good_img,a.good_info_img,a.good_thumb_img,a.brand_id,
            a.good_unit,b.addtime,b.updatetime,b.exam_status,a.spuCode,
            b.sale_price,b.final_price";
		}
	    $info = Db::name('platform_youzan')
	              ->alias("b")
	              ->field($field)
		    //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
		          ->leftJoin("good_basic a", "a.spuCode=b.spuCode")
	              ->where(["b.skuCode"=>$param['skuCode'],"b.is_del"=>0])->find();

	    if($info==false)  return json_show(1005,"商品数据未找到");
	    $yzgood = new YzGood();
	    $brand = Db::name("brand")->field('id,brand_name')->where(["id" => $info['brand_id']])->find();
	    $info["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
	    $unit = Db::name("unit")->field('id,unit')->where(["id" => $info['good_unit']])->find();
	    $info['unit'] = isset($unit['unit']) ? $unit['unit'] : "";
	    $info['exam_status'] = $info['exam_status']==6?1:0;
	    $info['origin_price'] =$yzgood->where(["item_no"=>$info['skuCode'],"is_del"=>0])->value("origin",'');
	    $spec = Db::name("good_spec")->where(["spuCode" => $info['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;
		    }
	    }
	    $info['good_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_img']);
	    $info['good_info_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_img']);
	    $info['good_thumb_img']=str_replace($this->origin_img_host,$this->ssl_img_host,$info['good_img']);
	    if($this->request->isCx==1){
        	$info['cat_info'] = made($info['cat_id'], []);
            $platform = Db::name("platform")->field('id,platform_name')->where(["id" => $info['platform_code']])->find();
            $info['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name'] : "";
            $info['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en'] : "";
            $supplier = Db::name("supplier")->field('id,name')->where(["code" => $info['supplierNo']])->find();
            $info['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
            $info['company'] = isset($company['company']) ? $company['company'] : "";
            $info['stock_total'] = Db::name("good_stock")->where(['spuCode' => $info['spuCode'], "is_del" => 0])->sum("usable_stock");
            $info['exclusive'] = makeExcluse($info['is_exclusive']);
            $info['noble_name'] = isset($info['noble_metal']) && $info['noble_metal'] != 0 ? $this->noble[$info['noble_metal']] : "";
//            $itemid =Db::name("depart_user")->where(["uid"=>$info["purchase_id"],"is_del"=>0])->value("itemid",'');

            $item = get_company_name_by_uid($info["purchase_id"]);
            $info['company_name'] = $item??'';

        	}else{
	    	unset($info['brand_id']);
		    unset($info['good_unit']);
		    unset($info['spuCode']);
        	}

	    $info['specinfo'] = $speclist;
	    return json_show(0,"获取成功",$info);
    }
}