<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\model\bbfxshop;

use think\Model;
use think\Db;
use think\Cache;
use app\common\model\bbfxshop\FenxiaoLevel;
use app\common\model\QRcode;
use app\common\model\bbfxshop\Weixin;

class Goods extends Model
{
    protected $name = 'bbfx_goods';
    
    public static function copy($id){
        $goods = Db::name("bbfx_goods")->where(['id'=>$id])->find();
        if(empty($goods)){
            return __json(0,"商品不存在");
        }
        
        $data = $goods;
        unset($data['id']);
        $data['title'] .= "  -  复制";
        $data['updatetime'] = time();
        $data['createtime'] = time();


        try{
            Db::startTrans();

            $goods_id = Db::name("bbfx_goods")->where(['id'=>$id])->insertGetId($data);

            $categorys = Db::name("bbfx_goods_category")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
            foreach ($categorys as $key => $val){
                $category_data = $val;
                unset($category_data['id']);
                $category_data['goods_id'] = $goods_id;
                Db::name("bbfx_goods_category")->insertGetId($category_data);
            }

            if($data['is_hexiao'] == 1){
                $store_goods = Db::name("bbfx_store_goods")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
                foreach ($store_goods as $key => $val){
                    $store_goods_data = $val;
                    unset($store_goods_data['id']);
                    $store_goods_data['goods_id'] = $goods_id;
                    Db::name("bbfx_store_goods")->insertGetId($store_goods_data);
                }
            }

            if($data['is_spec'] == 1){
                $spec_list = Db::name("bbfx_goods_spec")->where(['uniacid'=>UNIACID,'goods_id'=>$id])->select();
                $spec_item_ids = [];
                foreach ($spec_list as $key => $val){
                    $spec_data = $val;
                    unset($spec_data['id']);
                    $spec_data['goods_id'] = $goods_id;
                    $spec_id = Db::name("bbfx_goods_spec")->insertGetId($spec_data);

                    $spec_item_list = Db::name("bbfx_goods_spec_item")->where(['uniacid'=>UNIACID,'pid'=>$val['id']])->select();
                    foreach ($spec_item_list as $key => $val){
                        $spec_item_data = $val;
                        unset($spec_item_data['id']);
                        $spec_item_data['pid'] = $spec_id;
                        $spec_item_data['goods_id'] = $goods_id;
                        $spec_item_id = Db::name("bbfx_goods_spec_item")->insertGetId($spec_item_data);
                        $spec_item_ids[$val['id']] = $spec_item_id;
                    }
                }
                
                $spec_sku_list = Db::name("bbfx_goods_spec_sku")->where(['uniacid'=>UNIACID,'goods_id'=>$id])->select();
                foreach ($spec_sku_list as $key => $val){
                    $spec_sku_data = $val;
                    unset($spec_sku_data['id']);
                    $spec_sku_data['goods_id'] = $goods_id;

                    $sku_ids = explode(',',$val['sku']);
                    $sku = [];
                    foreach ($sku_ids as $k => $v){
                        $sku[] = $spec_item_ids[$v];
                    }

                    $spec_sku_data['sku'] = implode(',',$sku);

                    Db::name("bbfx_goods_spec_sku")->insertGetId($spec_sku_data);
                }
                
            }

            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return __json(0,$e->getMessage());
        }
        
        return __json(1,"操作成功");
    } 

    public static function setGoodsCommentScore($id){
        
        $comment = Db::name("bbfx_goods_comment")->where(['id'=>$id,'sh_status'=>1])->find();
        if(empty($comment)){
            return __json(0,"商品评论不存在");
        }

        $count = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'goods_id'=>$comment['goods_id'],'sh_status'=>1])->count();
        $score_total = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'goods_id'=>$comment['goods_id'],'sh_status'=>1])->sum("score");
        $count = intval($count);
        if($count > 0 && $score_total > 0){
            $score = round($score_total / $count,1);
            Db::name("bbfx_goods")->where(['id'=>$comment['goods_id']])->update(['score'=>$score]);
        }

        if($comment['mid'] > 0){
            $count = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'mid'=>$comment['mid'],'sh_status'=>1])->count();
            $score_total = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'mid'=>$comment['mid'],'sh_status'=>1])->sum("score");
            $count = intval($count);
            
            if($count > 0 && $score_total > 0){
                $score = round($score_total / $count,1);
                Db::name("bbfx_merchant")->where(['id'=>$comment['mid']])->update(['score'=>$score]);
            }
        }

        if($comment['store_id'] > 0){
            $count = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'store_id'=>$comment['store_id'],'sh_status'=>1])->count();
            $score_total = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'store_id'=>$comment['store_id'],'sh_status'=>1])->sum("score");
            $count = intval($count);
            
            if($count > 0 && $score_total > 0){
                $score = round($score_total / $count,1);
                Db::name("bbfx_store")->where(['id'=>$comment['store_id']])->update(['score'=>$score]);
            }
        }
        

        return __json(1,"操作成功");
    }
    
    public static function resetStock($uniacid,$goods_id=0){
        $where = ['uniacid'=>$uniacid];
        if(!empty($goods_id)){
            $where['id'] = $goods_id;
        }
        $goods_list = Db::name("bbfx_goods")->where($where)->where(function($query){
            $query->whereOr(function($query){
        		$query->where(['type'=>1]);
        	});
        	$query->whereOr(function($query){
        		$query->where(['type'=>2,'is_spec'=>1]);
        	});
        })->field("id,type,is_spec")->select();
        
        if(empty($goods_list)){
            return false;
        }
        
        foreach ($goods_list as $key => $goods){
            if($goods['type'] == 1 || ($goods['type'] == 2 && $goods['is_spec'] == 1)){
                if($goods['type'] == 1){
                    $goods_stock = Db::name("bbfx_serial_number")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id'],'status'=>0])->count();
                }
                else{
                    $goods_stock = Db::name("bbfx_goods_spec_sku")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->sum("stock");
                }
                $goods_stock = intval($goods_stock);
                Db::name("bbfx_goods")->where(['id'=>$goods['id']])->update(['stock'=>$goods_stock]);
            }
        }
    }
    
    public static function getCategoryList(){
        $category = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'status'=>1,'pid'=>0,'mid'=>0])->order('sort desc')->select();
        foreach ($category as $key => &$val) {
    	    $val['thumb'] = cdnurl($val['thumb']);
    	    $child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$val['id'],'mid'=>0])->order('sort desc')->select();
    	    if(empty($child)) $child = [];
    	    
    	    foreach ($child as $k => &$v){
    	        $v['thumb'] = cdnurl($v['thumb']);
    	        $three_child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$v['id'],'mid'=>0])->order('sort desc')->select();
    	        if(empty($three_child)) $three_child = [];
    	        foreach ($three_child as $k1 => &$v1){
    	            $v1['thumb'] = cdnurl($v1['thumb']);
    	        }
    	        $v['child'] = $three_child;
    	    }
    	    unset($v);
    	    
    	    $val['child'] = $child;
    	}
    	unset($val);
    	
    	return $category;
    }
    
    public static function setStock($goods_id,$sku_id,$stock,$user_id,$type,$itype,$remarks,$info=[],$startTrans=true){
        // $type 用户类型，0=管理员，1=用户
        // $itype 操作类型，0=管理员操作,1=用户下单,2=用户退款
        $stock = intval($stock);
        if($stock == 0){
            return __json(0,"变动库存不能为0");
        }
        
        $goods = Db::name("bbfx_goods")->where(['id'=>$goods_id])->field("id,is_spec,title,thumb,price,stock,type,o_price,minbuy,maxbuy,total_maxbuy,unit,sales_volume,virtual_sale")->find();
        
        if(empty($goods)){
            return __json(0,"商品信息错误");
        }
        
        if($goods['is_spec'] == 1){
            $sku = Db::name("bbfx_goods_spec_sku")->where(['goods_id'=>$goods['id'],'id'=>$sku_id])->find();
            if(empty($sku)){
                return __json(0,"规格信息错误");
            }
        }
        
        try {
            
            if($startTrans){
                Db::startTrans();
            }
            
            
            if($stock > 0){
                $res = Db::name("bbfx_goods")->where(['id'=>$goods['id']])->setInc("stock",$stock);
                if(empty($res)){
                    throw new \Exception("操作失败");
                }
            }
            else{
                $pstock = abs($stock);
                if($goods['stock'] < $pstock){
                    throw new \Exception("库存不足");
                }
                $res = Db::name("bbfx_goods")->where(['id'=>$goods['id']])->setDec("stock",$pstock);
                if(empty($res)){
                    throw new \Exception("库存不足");
                }
            }
            
            $title = $goods['title'];
            
            if($goods['is_spec'] == 1){
                
                if($stock > 0){
                    $res = Db::name("bbfx_goods_spec_sku")->where(['id'=>$sku['id']])->setInc("stock",$stock);
                    if(empty($res)){
                        throw new \Exception("操作失败");
                    }
                }
                else{
                    $pstock = abs($stock);
                    if($sku['stock'] < $pstock){
                        throw new \Exception("库存不足");
                    }
                    $res = Db::name("bbfx_goods_spec_sku")->where(['id'=>$sku['id']])->setDec("stock",$pstock);
                    if(empty($res)){
                        throw new \Exception("库存不足");
                    }
                }
                
                $title .= "-".$sku['sku_name'];
                $goods['sku'] = $sku;
                
                $new = Db::name("bbfx_goods_spec_sku")->where(['goods_id'=>$goods['id'],'id'=>$sku['id']])->find();
                $before = $sku['stock'];
                $after = $new['stock'];
            }
            else{
                $new = Db::name("bbfx_goods")->where(['id'=>$goods['id']])->find();
                $before = $goods['stock'];
                $after = $new['stock'];
            }
            
            $data = array(
                "uniacid"=>UNIACID,
                "user_id"=>$user_id,
                "goods_id"=>$goods_id,
                "sku_id"=>$sku_id,
                "stock"=>$stock,
                "before"=>$before,
                "after"=>$after,
                "remarks"=>$remarks,
                "title"=>$title,
                "info"=>iserializer($info),
                "goods"=>iserializer($goods),
                "type"=>$type,
                "itype"=>$itype,
                "createtime"=>time()
            );
            
            if(!empty($info['id'])){
                $data['item_id'] = $info['id'];
            }
            
            $id = Db::name("bbfx_goods_stock_log")->insertGetId($data);
            $data['id'] = $id;
            
            $result = __json(1,"操作成功",$data);
            
            if($startTrans){
                Db::commit();
            }
            
        } 
        catch (\Exception $e) {
            if($startTrans){
                Db::rollback();
            }
            $result = __json(0,$e->getMessage());
        }
         
        return $result;
    }

    public static function resetSort(){
        $list = Db::name("bbfx_goods")->where(['uniacid'=>UNIACID])->field("id,sort")->order("sort desc,id desc")->select();
        // print_r($list);die;
        $sort = count($list);
        foreach ($list as $key => $val){
            Db::name("bbfx_goods")->where(['id'=>$val['id']])->update(['sort'=>$sort]);
            $sort--;
        }
    }

    public static function updateGoodsUpgradeScore(){
        $goods_list = Db::name("bbfx_goods")->where(['uniacid'=>UNIACID,'upgrade_score'=>['>',0]])->field("id,upgrade_score")->select();
        if(empty($goods_list)){
            return __json(0,"没有商品");
        }
        $goods_list = array_skey($goods_list,"id");
        
        
        
        $page = 1;
        $limit = 500;
        
        $order_goods = Db::name("bbfx_order_goods")->where(['uniacid'=>UNIACID])->field("id,goods_id,total,upgrade_score")->order("id asc")->limit(($page-1)*$limit,$limit)->select();
        if(empty($order_goods)){
            return __json(0,"没有商品订单");
        }
        
        // echo "<pre>";
        // print_r($order_goods);die;

        foreach($order_goods as $val){
            $goods = $goods_list[$val['goods_id']];
            if(!empty($goods)){
                $upgrade_score = $goods['upgrade_score'] * $val['total'];
                
                if($val['upgrade_score'] != $upgrade_score){
                    Db::name("bbfx_order_goods")->where(['id'=>$val['id']])->update(['upgrade_score'=>$upgrade_score]);
                }
            }
        }

        return __json(1,"操作成功");
    }

    // 浏览
    public static function browse($id,$user_id,$utk){
        try {
            $data = [
                'uniacid'=>UNIACID,
                'type'=>0,
                'user_id'=>intval($user_id),
                'goods_id'=>$id,
                'utk'=>$utk,
                'createtime'=>time()
            ];
            Db::name("bbfx_goods_log")->insert($data);
        } catch (\Exception $e) {
            return __json(0,$e->getMessage());
        }
        return __json(1,"操作成功");
    }

    // 访客
    public static function visitor($id,$user_id,$utk){

        $data = [
            'uniacid'=>UNIACID,
            'type'=>1,
            'user_id'=>intval($user_id),
            'goods_id'=>$id,
            'utk'=>$utk,
            'createtime'=>time()
        ];

        if(!empty($data['user_id']) && !empty($data['utk'])){
            $where = ['uniacid'=>$data['uniacid'],'type'=>$data['type'],'user_id'=>$data['user_id'],'goods_id'=>$data['goods_id'],'utk'=>$data['utk']];
        }
        else if(empty($data['user_id'])  && !empty($data['utk'])){
            $where = ['uniacid'=>$data['uniacid'],'type'=>$data['type'],'goods_id'=>$data['goods_id'],'utk'=>$data['utk']];
        }
        else{
            return __json(0,"参数缺失");
        }

        $where['createtime'] = ['>',strtotime(date('Y-m-d 00:00:00'))];
        $log = Db::name("bbfx_goods_log")->where($where)->find();
        if(!empty($log)){
            return __json(0,"已记录");
        }

        try {
            Db::name("bbfx_goods_log")->insert($data);
        } catch (\Exception $e) {
            return __json(0,$e->getMessage());
        }

        return __json(1,"操作成功");
    }

    public static function saveGoods($params,$admin_id){
        $id = intval($params['id']);
        $goods = Db::name("bbfx_goods")->where(['id'=>$id])->find();
        
      	$sort = intval($params['sort']);
      	if(empty($params['title'])){
        	return __json(0,"请填写商品名称");
        }
      	if(empty($params['thumb'])){
        	return __json(0,"请上传商品图片");
        }
        if($params['plugin']['form']['status'] == 1 && empty($params['plugin']['form']['list'])){
            return __json(0,"请添加表单");
        }
      
        $type = max(1,intval($params['type']));
        
        if(!empty($goods)){
            $type = $goods['type'];
        }
        
        $price = floatval($params['price']);
        $o_price = floatval($params['o_price']);

        $mid = intval($params['mid']);
        if(!empty($goods)){
            $mid = $goods['mid'];
        }
        
    	$data = array(
        	"uniacid"=>UNIACID,
            "mid"=>$mid,
          	"yuyue_status"=>intval($params['yuyue_status']),
          	"title"=>$params['title'],
          	"subtitle"=>$params['subtitle'],
          	"thumb"=>$params['thumb'],
          	"price"=>$price,
          	"o_price"=>$o_price,
          	"cost_price"=>max(0,round($params['cost_price'],2)),
          	"express_fee"=>floatval($params['express_fee']),
          	"profit_amount"=>floatval($params['profit_amount']),
          	"stock_type"=>intval($params['stock_type']),
          	"minbuy"=>max(1,intval($params['minbuy'])),
          	"maxbuy"=>intval($params['maxbuy']),
          	"daily_buy"=>intval($params['daily_buy']),
          	"monthly_buy"=>intval($params['monthly_buy']),
          	"total_maxbuy"=>intval($params['total_maxbuy']), 
          	"shippingfee_template_id"=>intval($params['shippingfee_template_id']), 
          	"weight"=>round($params['weight'],2), 
          	"volume"=>round($params['volume'],2), 
          	"is_give"=>intval($params['is_give']),
          	"give_type"=>intval($params['give_type']),
          	"give"=>iserializer($params['give']),
          	"levels"=>iserializer($params['levels']),
          	"is_contract"=>intval($params['is_contract']),
          	"contract"=>iserializer($params['contract']),
          	"guarantee"=>iserializer($params['guarantee']),
          	"fastest_delivery"=>intval($params['fastest_delivery']),
          	"arrive_day"=>intval($params['arrive_day']),
          	"express_name"=>$params['express_name'],
          	"vip_free_shipping"=>intval($params['vip_free_shipping']),
          	"is_authentic"=>intval($params['is_authentic']),
          	"authentic_name"=>$params['authentic_name'],
          	"authentic_desc"=>$params['authentic_desc'],
          	"status"=>$params['status'],
          	"single_column_thumb"=>replaceSiteroot($params['single_column_thumb']),
          	"authentic_icon"=>replaceSiteroot($params['authentic_icon']),
          	"content"=>$params['content'],
          	"sort"=>$sort,
          	"is_recommend"=>0,
          	"is_new"=>0,
          	"is_hot"=>0,
          	"is_nodiscount"=>0,
          	"stock"=>intval($params['stock']),
          	"is_spec"=>intval($params['is_spec']),
          	"delivery_method"=>intval($params['delivery_method']),
          	"virtual_sale"=>intval($params['virtual_sale']),
          	"plugin"=>iserializer($params['plugin']),
          	"parameter"=>iserializer($params['parameter']),
          	"is_hexiao_quantity"=>intval($params['is_hexiao_quantity']),
          	"order_user_scroll"=>intval($params['order_user_scroll']),
          	"buy_btn"=>trim($params['buy_btn']),
          	"merchant_commission_rate"=>round($params['merchant_commission_rate'],2),
          	"product_code"=>trim($params['product_code']),
            "plugin_tongue_diagnosis"=>intval($params['plugin']['tongue_diagnosis']['status']),
            "plugin_shuziren"=>intval($params['plugin']['shuziren']['status']),
            "sh_status"=>1,
          	"updatetime"=>time()
        );

        if($sort == 0 && !empty($goods)){
            // $sort = Db::name("bbfx_goods")->where(['uniacid'=>UNIACID])->order("sort desc,id desc")->value("sort");
            // $sort = max(0,intval($sort)) + 1;
            // $data['sort'] = $sort;
        }
        
        if($type == 2 && ($params['delivery_method'] == 2 || $params['delivery_method'] == 3) || ($type == 4 && intval($params['is_hexiao']) == 1) || $type == 3){
            $data['is_hexiao'] = 1;
            $data['hexiao_num'] = max(1,intval($params['hexiao_num']));
            $data['hexiao_store_type'] = intval($params['hexiao_store_type']);
            $data['hexiao_info_required'] = intval($params['hexiao_info_required']);
            if($params['hexiao_store_type'] == 1 && empty($params['hexiao_store_ids'])){
                return __json(0,"请选择核销门店");
            }
        }
        else{
            $data['is_hexiao'] = 0;
        }
        
        $gift = $params['plugin']['gift'];
        if($gift['status'] == 1 && $gift['tx_status'] == 1){
            if(round($gift['cash'],2) <= 0){
                return __json(0,"请输入礼品提现金额");
            }
        }
        
        // 分销配置
        $data['is_fenxiao'] = intval($params['is_fenxiao']);
        $data['commission_rule'] = intval($params['commission_rule']);
        $data['commission1'] = iserializer($params['commission1']);
        $data['commission2'] = iserializer($params['commission2']);
        $data['commission3'] = iserializer($params['commission3']);
        $data['is_repurchase_rule'] = intval($params['is_repurchase_rule']);
        $data['repurchase_reward1'] = iserializer($params['repurchase_reward1']);
        $data['repurchase_reward2'] = iserializer($params['repurchase_reward2']);
        $data['repurchase_reward3'] = iserializer($params['repurchase_reward3']);
        $data['is_jicha_rule'] = intval($params['is_jicha_rule']);
        $data['is_jiandan_rule'] = intval($params['is_jiandan_rule']);
        $data['is_pingji_rule'] = intval($params['is_pingji_rule']);
        $data['is_abonus_rule'] = intval($params['is_abonus_rule']);
        $data['is_abonus'] = intval($params['is_abonus']);
        $data['upgrade_score'] = intval($params['upgrade_score']);
        $data['is_leveldiscount_rule'] = intval($params['is_leveldiscount_rule']);
        $data['is_return'] = intval($params['is_return']);
        $data['is_return_rule'] = intval($params['is_return_rule']);
        $data['is_agent'] = intval($params['is_agent']);
        $data['agent_rule'] = intval($params['agent_rule']);
        $data['jc_reward'] = iserializer($params['jc_reward']);
        $data['jd_reward'] = iserializer($params['jd_reward']);
        $data['pj_reward'] = iserializer($params['pj_reward']);
        $data['ab_reward'] = iserializer($params['ab_reward']);
        
        
        $data['is_repurchase'] = (int)$params['is_repurchase'];
        $data['is_repurchase_pingji'] = (int)$params['is_repurchase_pingji'];
        $data['is_repurchase_pingji_rule'] = (int)$params['is_repurchase_pingji_rule'];
        $data['repurchase_pingji_reward'] = iserializer($params['repurchase_pingji_reward']);
        
        $level_discount = $params['level_discount'];
        $data['level_discount'] = iserializer($level_discount);
        $data['returns'] = iserializer($params['returns']);
        
        $data['agent'] = iserializer($params['agent']);
        
        $data['is_cumulative_fanhuan'] = (int)$params['is_cumulative_fanhuan'];
        $data['cumulative_fanhuan_rule'] = (int)$params['cumulative_fanhuan_rule'];
        $data['cumulative_fanhuan'] = iserializer($params['cumulative_fanhuan']);
        
        $data['cloud_warehouse_rule'] = (int)$params['cloud_warehouse_rule'];
        $data['cloud_warehouse'] = iserializer($params['cloud_warehouse']);
        $data['cloud_warehouse_chuhuo'] = iserializer($params['cloud_warehouse_chuhuo']);
        
        
        $data['is_ladder_push'] = (int)$params['is_ladder_push'];
        $data['is_ladder_push_loop'] = (int)$params['is_ladder_push_loop'];
        $data['ladder_push_rule'] = (int)$params['ladder_push_rule'];
        $data['ladder_push'] = iserializer($params['ladder_push']);
        
        
        $attr = $params['attr'];
        if(in_array(1,$attr)){
            $data['is_recommend'] = 1;
        }
        if(in_array(2,$attr)){
            $data['is_new'] = 1;
        }
        if(in_array(3,$attr)){
            $data['is_hot'] = 1;
        }
        if(in_array(4,$attr)){
            $data['is_nodiscount'] = 1;
        }
        
        
        $data['share_title'] = $params['share_title'];
        $data['share_img'] = $params['share_img'];
        
        
        $data["slides"] = iserializer(replaceSiteroots($params['slides']));
        $data["videos"] = iserializer(replaceSiteroots($params['videos']));
        $data['share_img'] = replaceSiteroot($data['share_img']);
        $data['thumb'] = replaceSiteroot($data['thumb']);
        
        $spec_list = $params['spec'];
        if(empty($spec_list)){
            $data['is_spec'] = 0;
        }
        
        if($type == 1 && ($type == 2 && $data['is_spec'] == 1)){
            unset($data['stock']);
        }
        if($data['is_spec'] == 1){
            $data['is_setmeal'] = intval($params['is_setmeal']);
            foreach($spec_list as $key => $val){
                if(empty($val['name'])){
                    return __json(0,"规格名称不能为空");
                }
                foreach ($val['spec_item'] as $k => $v){
                    if(empty($v['name'])){
                        return __json(0,"规格项名称不能为空");
                    }
                }
            }
        }
         
        // echo "<pre>";
        // print_r($data['is_setmeal']);die;

        try {
            Db::startTrans();
            if($mid == 0){
                unset($data['stock']);
            }
            if(!empty($goods)){
                unset($data['mid']);
                Db::name("bbfx_goods")->where(["id"=>$goods['id']])->update($data);
            }
            else{
                $data['type'] = $type;
                $data['createtime'] = time();
                $id = Db::name("bbfx_goods")->insertGetId($data);
            }

            $data['id'] = $id;
            $res = true;
            $category_ids = $params['category_ids'];
            $categorys = Db::name("bbfx_goods_category")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
            $categorys = array_skey($categorys,'category_id');
            $c_ids = [];
            $goods_category_ids = $params['goods_category_ids'];
            $category_ids = [];
            foreach ($goods_category_ids as $key => $val){
                $category_ids[] = $val[count($val)-1];
            }
            
            if(!empty($category_ids)){
                foreach ($category_ids as $key => $val){
                    $c_ids[$val] = true;
                    if(!empty($val) && empty($categorys[$val])){
                        Db::name("bbfx_goods_category")->insert(['uniacid'=>UNIACID,'goods_id'=>$id,'category_id'=>$val]);
                    }
                }
            }
            foreach ($categorys as $key => $val){
                if(empty($c_ids[$val['category_id']])){
                    Db::name("bbfx_goods_category")->where(['id'=>$val['id']])->delete();
                }
            }
            
            
            if($data['is_hexiao'] == 1){
                $hexiao_store_ids = $params['hexiao_store_ids'];
                $store_goods = Db::name("bbfx_store_goods")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
                $store_goods = array_skey($store_goods,'store_id');
                $s_ids = [];
                if(!empty($hexiao_store_ids)){
                    foreach ($hexiao_store_ids as $key => $val){
                        $s_ids[$val] = true;
                        if(empty($store_goods[$val])){
                            Db::name("bbfx_store_goods")->insert(['uniacid'=>UNIACID,'goods_id'=>$id,'store_id'=>$val]);
                        }
                    }
                }
                foreach ($store_goods as $key => $val){
                    if(empty($s_ids[$val['store_id']])){
                        Db::name("bbfx_store_goods")->where(['id'=>$val['id']])->delete();
                    }
                }
                
            }
            
            if($data['is_spec'] == 1){
                $spec_item_ids = [];
                $spec_all = Db::name("bbfx_goods_spec")->where(['uniacid'=>UNIACID,'goods_id'=>$id])->select();
                $spec_item_all = Db::name("bbfx_goods_spec_item")->where(['uniacid'=>UNIACID,'goods_id'=>$id])->select();
                $spec_ok = [];
                $spec_item_ok = [];
                
                foreach($spec_list as $key => &$val){
                    $goods_spec = array(
                        "uniacid"=>UNIACID,
                        "goods_id"=>$id,
                        "name"=>$val['name']
                    );
                    $spec_id = intval($val['id']);
                    if($spec_id > 0){
                        Db::name("bbfx_goods_spec")->where(['id'=>$spec_id])->update($goods_spec);
                    }
                    else{
                        $spec_id = Db::name("bbfx_goods_spec")->insertGetId($goods_spec);
                    }
                    
                    $spec_ok[$spec_id] = true;
                    
                    foreach ($val['spec_item'] as $k => $v){
                        $goods_spec_item = array(
                            "uniacid"=>UNIACID,
                            "goods_id"=>$id,
                            "pid"=>$spec_id,
                            "name"=>$v['name']
                        );
                        $spec_item_id = intval($v['id']);
                        if($spec_item_id > 0){
                            Db::name("bbfx_goods_spec_item")->where(['id'=>$spec_item_id])->update($goods_spec_item);
                        }
                        else{
                            $spec_item_id = Db::name("bbfx_goods_spec_item")->insertGetId($goods_spec_item);
                        }
                        $spec_item_ok[$spec_item_id] = true;
                        $spec_item_ids[$key."_".$k] = $spec_item_id;
                        $val['spec_item'][$k]['id'] = $spec_item_id;
                    }
                }
                unset($val);
                
                foreach ($spec_all as $key => $val){
                    if(empty($spec_ok[$val['id']])){
                        Db::name("bbfx_goods_spec")->where(['id'=>$val['id']])->delete();
                    }
                }
                foreach ($spec_item_all as $key => $val){
                    if(empty($spec_item_ok[$val['id']])){
                        Db::name("bbfx_goods_spec_item")->where(['id'=>$val['id']])->delete();
                    }
                }
                
                
                $sku_list = $params['sku_list'];
                
                $spec_sku_all = Db::name("bbfx_goods_spec_sku")->where(['uniacid'=>UNIACID,'goods_id'=>$id])->select();
                $spec_sku_list = array_skey($spec_sku_all,"id");
                $spec_sku_all = array_skey($spec_sku_all,"sku");
                $spec_sku_ok = [];
                foreach ($sku_list as $key => $val){
                    $specs = $val['spec'];
                    $sku = [];
                    $sku_name = [];
                    foreach ($specs as $k => $v){
                        $spec_id = $spec_item_ids[$v['pindex']."_".$v['index']];
                        // print_r($v['pindex']."_".$v['index']);die;
                        if(empty($spec_id)){
                            $sku = [];
                            break;
                        }
                        $sku[] = $spec_id;
                        $sku_name[] = $v['name'];
                    }
                    $sku = implode(",",$sku);
                    $spec_sku_id = intval($spec_sku_all[$sku]['id']);
                    $sku_name = implode(",",$sku_name);
                    $thumb = replaceSiteroot($val['thumb']);
                    $spec_sku = array(
                        "uniacid"=>UNIACID,
                        "goods_id"=>$id,
                        "sku"=>$sku,
                        "sku_name"=>$sku_name,
                        "price"=>floatval($val['price']),
                        "profit_amount"=>floatval($val['profit_amount']),
                        "original_price"=>floatval($val['original_price']),
                        "weight"=>floatval($val['weight']),
                        "volume"=>floatval($val['volume']),
                        "stock"=>intval($val['stock']),
                        "cost_price"=>max(0,round($val['cost_price'],2)),
                        "dish"=>iserializer($val['dish']),
                        "product_code"=>trim($val['product_code']),
                        "thumb"=>$thumb
                    );
                    if($mid == 0){
                        unset($spec_sku['stock']);
                    }
                    $sku_stock = 0;
                    if($spec_sku_id > 0){
                        Db::name("bbfx_goods_spec_sku")->where(['id'=>$spec_sku_id])->update($spec_sku);
                        $sku_stock = intval($val['stock'] - $spec_sku_all[$sku]['stock']);
                    }
                    else{
                        $spec_sku_id = Db::name("bbfx_goods_spec_sku")->insertGetId($spec_sku);
                        $sku_stock = $val['stock'];
                    }
                    
                    if($sku_stock != 0 && $mid == 0){
                        self::setStock($id,$spec_sku_id,$sku_stock,$admin_id,0,0,"管理员操作");
                    }
                    
                    $spec_sku_ok[$spec_sku_id] = true;
                }
                foreach ($spec_sku_list as $key => $val){
                    if(empty($spec_sku_ok[$val['id']])){
                        Db::name("bbfx_goods_spec_sku")->where(['id'=>$val['id']])->delete();
                    }
                }
            }
            else{
                if($mid == 0){
                    if(!empty($goods)){
                        $stock = intval($params['stock']) - $goods['stock'];
                    }
                    else{
                        $stock = intval($params['stock']);
                    }
                    if($stock != 0){
                        self::setStock($id,0,$stock,$admin_id,0,0,"管理员操作");
                    }
                }
                
            }

            self::resetStock(UNIACID,$id);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return __json(0,$e->getMessage());
        }

      	return __json(1,"编辑成功",['goods'=>$goods,'new_data'=>$data]);
    }

    public static function getList($params){
        self::resetStock(UNIACID);
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = ["uniacid"=>UNIACID,'mid'=>0];
        $export = intval($params['export']);
        $keyword = trim($params['keyword']);
        $category_id = intval($params['category_id']);
        $status = intval($params['status']);
        $goods_type = intval($params['goods_type']);
        if($goods_type > 0){
            $where['type'] = $goods_type;
        }
        else{
            $where['type'] = ['in',[1,2,4]];
        }
        if($keyword){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        
        if($category_id > 0){
            $cids = [$category_id];
            $category_list = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$category_id])->select();
            foreach ($category_list as $k => $v){
                $cids[] = $v['id'];
                $child1 = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$v['id']])->select();
                if(!empty($child1)){
                    $cids = array_merge($cids,array_column($child1,"id"));
                }
            }
            
            $goods_category = Db::name("bbfx_goods_category")->where(['category_id'=>['in',$cids]])->select();
            $goods_ids = array_column($goods_category,"goods_id");
            $where['id'] = ['in',$goods_ids];
        }
        
        if($status == 1){
            $where['status'] = 1;
        }
        else if($status == 2){
            $where['status'] = 0;
        }
        if($export == 1){
            $pindex = 1;
            $psize = 999999;
        }
    	$list = Db::name("bbfx_goods")->where($where)->order('sort desc,id desc')->limit(($pindex - 1) * $psize,$psize)->select();
    	$export_list = [];
      	foreach($list as $key => &$val){
      	    $slides = iunserializer($val['slides']);
      	    foreach ($slides as $k => &$v){
      	        $v = cdnurl($v);
      	    }
      	    unset($v);
      	    $val['slides'] =$slides;
        	$val['thumb'] = cdnurl($val['thumb']);
        	$val['share_img'] = cdnurl($val['share_img']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
        	
            $categorys = Db::name("bbfx_goods_category")->alias("a")->join("bbfx_category b","a.category_id=b.id")->where(['a.uniacid'=>UNIACID,'a.goods_id'=>$val['id']])->field('b.id,b.title')->order('b.sort desc')->select();
        	
        	$val['categorys'] = $categorys;
        	
        	
        	$typeText = "";
        	if($val['type'] == 1){
        	    $typeText = "卡密";
        	}
        	else if($val['type'] == 2){
        	    $typeText = "实体";
        	}
        	else if($val['type'] == 3){
        	    $typeText = "核销";
        	}
        	else if($val['type'] == 4){
        	    $typeText = "虚拟";
        	}
        	$val['typeText'] = $typeText;
        	
        	if($val['sales_volume'] == 0){
        	    $sales_volume = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")->where(['o.uniacid'=>UNIACID,'o.order_source'=>['<>',6],'o.status'=>['>',0],'og.goods_id'=>$val['id']])->sum("og.total");
        	    $val['sales_volume'] = intval($sales_volume);
        	    
        	    if($val['sales_volume'] > 0){
        	        Db::name("bbfx_goods")->where(['id'=>$val['id']])->update(['sales_volume'=>$val['sales_volume']]);
        	    }
        	}
        	
        	if($export == 1){
        	    $category_name = "";
        	    if(!empty($categorys)){
        	        $category_name = implode(",",array_column($categorys,"title"));
        	    }
        	    $export_list[] = [
        	        $val['id'],
        	        $val['title'],
                    $val['product_code'],
        	        $typeText,
        	        $category_name,
        	        $val['price'],
        	        $val['stock'],
        	        $val['sales_volume'],
        	        $val['createtime']
        	    ];
        	}
        	else{
        	    
        	    $page = "pages/goods/detail?id=".$val['id'];
        	    
                $val['wxapp_url'] = $page;
                $val['h5_qrcode'] = "";
                $val['wxapp_qrcode'] = "";
                $result = Weixin::getUnlimitedQRCode($page,"goods_".$val['id'],1);
                if($result['code'] == 1){
                    $val['wxapp_qrcode'] = cdnurl($result['data']['url'])."?t=".time();
                }
                
                if(strstr($page, '?')){
                    $page .= "&i=".UNIACID;
                }
                else{
                    $page .= "?i=".UNIACID;
                }
                $h5_url = request()->domain()."/mobile/#/".$page;
                $val['h5_url'] = $h5_url;
                $result = QRcode::create($h5_url,"goods_h5_".$val['id']);
                if($result['code'] == 1){
                    $val['h5_qrcode'] = cdnurl($result['data']['url'])."?t=".time();
                }
        	}
        }
        unset($al);
        
        if($export == 1){
    	    $headArr = ["ID","商品名称","商品编码","商品类型","商品分类","商品价格","商品库存","商品销量","创建时间"];
    	    $result = \app\common\model\PhpOffice::excelExport("goods-".date("Y-m-d"),$headArr,$export_list);
    	}
        
    	$total = Db::name("bbfx_goods")->where($where)->count();

        return __json(1,"操作成功",['list'=>$list,'total'=>$total]);
    }

    public static function detail($params){
        global $_W;
        $base = $_W['base'];
        
        $id = intval($params['id']);
        $goods = Db::name("bbfx_goods")->where(['id'=>$id,'mid'=>0])->find();
        $levels = FenxiaoLevel::getLevels();
        
        
        $result = ['levels'=>$levels];
        if(!empty($goods)){
            $goods['parameter'] = iunserializer($goods['parameter']);
            $goods['levels'] = iunserializer($goods['levels']);
            if(empty($goods['levels'])){
                $goods['levels'] = [];
            }
            $goods['levels'] = iunserializer($goods['levels']);
            foreach ($goods['levels'] as $k => &$v){
                $v = intval($v);
            }
            unset($v);
            
            $goods['slides'] = cdnurls(iunserializer($goods['slides']));
            $goods['videos'] = cdnurls(iunserializer($goods['videos']));
            $goods['thumb'] = cdnurl($goods['thumb']);
            $goods['single_column_thumb'] = cdnurl($goods['single_column_thumb']);
            $goods['authentic_icon'] = cdnurl($goods['authentic_icon']);
            $goods['contract'] = iunserializer($goods['contract']);
            $goods['guarantee'] = iunserializer($goods['guarantee']);
            $goods['share_img'] = cdnurl($goods['share_img']);
            $goods['attr'] = [];
            if($goods['is_recommend'] == 1){
                $goods['attr'][] = 1;
            }
            if($goods['is_new'] == 1){
                $goods['attr'][] = 2;
            }
            if($goods['is_hot'] == 1){
                $goods['attr'][] = 3;
            }
            if($goods['is_nodiscount'] == 1){
                $goods['attr'][] = 4;
            }
            
            $category_ids = [];
            $goods_category = Db::name("bbfx_goods_category")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
            if(!empty($goods_category)){
                $category_ids = array_column($goods_category, 'category_id');
            }
            $goods['category_ids'] = $category_ids;
            
            $goods_category = Db::name("bbfx_category")->where(['uniacid'=>UNIACID,'id'=>['in',$category_ids]])->field("id,pid,title")->select();
            if(empty($goods_category)) $goods_category = [];
            $goods_category_ids = [];
            foreach ($goods_category as $key => $val){
                $two = Db::name("bbfx_category")->where(['uniacid'=>UNIACID,'id'=>$val['pid']])->find();
                if(!empty($two)){
                    $three = Db::name("bbfx_category")->where(['uniacid'=>UNIACID,'id'=>$two['pid']])->find();
                    if(!empty($three)){
                        $val['title'] = $three['title']."/".$two['title']."/".$val['title'];
                        $goods_category_ids[] = [$three['id'],$two['id'],$val['id']];
                    }
                    else{
                        $val['title'] = $two['title']."/".$val['title'];
                        $goods_category_ids[] = [$two['id'],$val['id']];
                    }
                }
                else{
                    $goods_category_ids[] = [$val['id']];
                }
            }
            $goods['goods_category_ids'] = $goods_category_ids;
            
            $hexiao_store_ids = [];
            $store_goods = Db::name("bbfx_store_goods")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
            if(!empty($store_goods)){
                $hexiao_store_ids = array_column($store_goods, 'store_id');
            }
            $goods['hexiao_store_ids'] = $hexiao_store_ids;
            
            
            
            $goods['commission1'] = iunserializer($goods['commission1']);
            if(empty($goods['commission1'])){
                $goods['commission1'] = [];
            }
            $goods['commission2'] = iunserializer($goods['commission2']);
            if(empty($goods['commission2'])){
                $goods['commission2'] = [];
            }
            $goods['commission3'] = iunserializer($goods['commission3']);
            if(empty($goods['commission3'])){
                $goods['commission3'] = [];
            }
            $goods['jc_reward'] = iunserializer($goods['jc_reward']);
            if(empty($goods['jc_reward'])){
                $goods['jc_reward'] = [];
            }
            $goods['jd_reward'] = iunserializer($goods['jd_reward']);
            if(empty($goods['jd_reward'])){
                $goods['jd_reward'] = [];
            }
            $goods['pj_reward'] = iunserializer($goods['pj_reward']);
            if(empty($goods['pj_reward'])){
                $goods['pj_reward'] = [];
            }
            $goods['ab_reward'] = iunserializer($goods['ab_reward']);
            if(empty($goods['ab_reward'])){
                $goods['ab_reward'] = [];
            }
            $goods['level_discount'] = iunserializer($goods['level_discount']);
            if(empty($goods['level_discount'])){
                $goods['level_discount'] = [];
            }
            $goods['returns'] = iunserializer($goods['returns']);
            if(empty($goods['returns'])){
                $goods['returns'] = ['type'=>0,'day'=>0,'day_score'=>0,'level'=>[],'money1'=>0,'money2'=>0,'money1_score'=>0,'money2_score'=>0];
            }
            $goods['agent'] = iunserializer($goods['agent']);
            if(empty($goods['agent'])){
                $goods['agent'] = [];
            }
            
            $goods['repurchase_reward1'] = iunserializer($goods['repurchase_reward1']);
            if(empty($goods['repurchase_reward1'])){
                $goods['repurchase_reward1'] = [];
            }
            $goods['repurchase_reward2'] = iunserializer($goods['repurchase_reward2']);
            if(empty($goods['repurchase_reward2'])){
                $goods['repurchase_reward2'] = [];
            }
            $goods['repurchase_reward3'] = iunserializer($goods['repurchase_reward3']);
            if(empty($goods['repurchase_reward3'])){
                $goods['repurchase_reward3'] = [];
            }
            
            $goods['repurchase_pingji_reward'] = iunserializer($goods['repurchase_pingji_reward']);
            if(empty($goods['repurchase_pingji_reward'])){
                $goods['repurchase_pingji_reward'] = [];
            }
            
            $goods['give'] = iunserializer($goods['give']);
            if(empty($goods['give'])){
                $goods['give'] = [];
            }
            
            $goods['cumulative_fanhuan'] = iunserializer($goods['cumulative_fanhuan']);
            if(empty($goods['cumulative_fanhuan'])){
                $goods['cumulative_fanhuan'] = ['level'=>[],'money1'=>'','money2'=>''];
            }
            
            $goods['cloud_warehouse'] = iunserializer($goods['cloud_warehouse']);
            if(empty($goods['cloud_warehouse'])){
                $goods['cloud_warehouse'] = ['level'=>[],'money1'=>'','money2'=>''];
            }
            $goods['cloud_warehouse_chuhuo'] = iunserializer($goods['cloud_warehouse_chuhuo']);
            if(empty($goods['cloud_warehouse_chuhuo'])){
                $goods['cloud_warehouse_chuhuo'] = ['level'=>[],'money1'=>'','money2'=>''];
            }
            
            
            $goods['ladder_push'] = iunserializer($goods['ladder_push']);
            if(empty($goods['ladder_push'])){
                $goods['ladder_push'] = ['level'=>[],'unified'=>[]];
            }
            
            $goods['plugin'] = iunserializer($goods['plugin']);
            if(empty($goods['plugin'])){
                $goods['plugin'] = [];
            }
            
            $commission1 = $goods['commission1'];
            $commission2 = $goods['commission2'];
            $commission3 = $goods['commission3'];
            $repurchase_reward1 = $goods['repurchase_reward1'];
            $repurchase_reward2 = $goods['repurchase_reward2'];
            $repurchase_reward3 = $goods['repurchase_reward3'];
            $jc_reward = $goods['jc_reward'];
            $jd_reward = $goods['jd_reward'];
            $pj_reward = $goods['pj_reward'];
            $ab_reward = $goods['ab_reward'];
            $level_discount = $goods['level_discount'];
            $returns = $goods['returns'];
            $agent = $goods['agent'];
            $give = $goods['give'];
            
            $goods['repurchase_pingji_reward']['money1'] = (array)$goods['repurchase_pingji_reward']['money1'];
            $goods['repurchase_pingji_reward']['money2'] = (array)$goods['repurchase_pingji_reward']['money2'];
            $repurchase_pingji_reward = $goods['repurchase_pingji_reward'];
            $cumulative_fanhuan = $goods['cumulative_fanhuan'];
            $cloud_warehouse = $goods['cloud_warehouse'];
            $cloud_warehouse_chuhuo = $goods['cloud_warehouse_chuhuo'];
            
            $ladder_push = $goods['ladder_push'];
            
            $plugin = $goods['plugin'];
        }
        else{
            $commission1 = [];
            $commission2 = [];
            $commission3 = [];
            $repurchase_reward1 = [];
            $repurchase_reward2 = [];
            $repurchase_reward3 = [];
            $jc_reward = [];
            $jd_reward = [];
            $pj_reward = [];
            $ab_reward = [];
            $repurchase_reward = [];
            $level_discount = [];
            $agent = [];
            $give = [];
            $returns = ['type'=>0,'day'=>0,'day_score'=>0,'level'=>[],'money1'=>0,'money2'=>0,'money1_score'=>0,'money2_score'=>0];
            $repurchase_pingji_reward = ['money1'=>[],'money2'=>[],'level'=>[]];
            $cumulative_fanhuan = ['level'=>[],'money1'=>'','money2'=>''];
            $cloud_warehouse = ['level'=>[],'money1'=>'','money2'=>''];
            $cloud_warehouse_chuhuo = ['level'=>[],'money1'=>'','money2'=>''];
            
            $ladder_push = ['level'=>[],'unified'=>[]];
            
            $plugin = [];
            
            $goods_category = [];
            $goods_category_ids = [];
        }
        
        if(empty($plugin['coupon'])){
            $plugin['coupon'] = ['is_nouse_coupon'=>0,'give_coupon'=>[],'give_coupon_list'=>[]];
        }
        if(is_null($plugin['coupon']['give_coupon_list'])) $plugin['coupon']['give_coupon_list'] = [];
        if(empty($plugin['queue_mode'])){
            $plugin['queue_mode'] = ['status'=>0,'rule'=>0,'miandan_total'=>['money1'=>'','money2'=>''],'each_order_rate'=>['money1'=>'','money2'=>'']];
        }
        if(empty($plugin['sabonus'])){
            $plugin['sabonus'] = ['status'=>0,'part'=>'','abonus_money'=>''];
        }
        if(is_null($plugin['sabonus']['abonus_money'])){
            $plugin['sabonus']['abonus_money'] = '';
        }
        if(empty($plugin['chaoyue'])){
            $plugin['chaoyue'] = ['status'=>1,'inde_rule'=>0,'reward'=>[]];
        }
        if(empty($plugin['pay'])){
            $plugin['pay'] = ['weixin_pay'=>1,'balance_pay'=>1,'deposit_release_pay'=>1,'repurchase_currency_pay'=>1,'zfb_pay'=>1,'cash_pay'=>1,'score_pay'=>0];
        }
        if(is_null($plugin['pay']['deposit_release_pay'])){
            $plugin['pay']['deposit_release_pay'] = 1;
        }
        if(is_null($plugin['pay']['repurchase_currency_pay'])){
            $plugin['pay']['repurchase_currency_pay'] = 1;
        }
        if(is_null($plugin['pay']['zfb_pay'])){
            $plugin['pay']['zfb_pay'] = 1;
        }
        if(is_null($plugin['pay']['cash_pay'])){
            $plugin['pay']['cash_pay'] = 1;
        }
        if(is_null($plugin['pay']['score_pay'])){
            $plugin['pay']['score_pay'] = 0;
        }
        if(empty($plugin['crazy'])){
            $plugin['crazy'] = ['status'=>0,'crazy_portion'=>''];
        }
        if(empty($plugin['virtual_goods'])){
            $plugin['virtual_goods'] = ['fahuo_content'=>''];
        }
        if(empty($plugin['npingji'])){
            $plugin['npingji'] = ['status'=>1,'inde_rule'=>0,'reward'=>[]];
        }
        if(empty($plugin['form'])){
            $plugin['form'] = ['status'=>0,'list'=>[]];
        }
        if(empty($plugin['capitalpool'])){
            $plugin['capitalpool'] = ['status'=>1,'capitalpool_money'=>''];
        }
        if(empty($plugin['super_maihuo'])){
            $plugin['super_maihuo'] = ['status'=>0,'super_maihuo_portion'=>''];
        }
        if(empty($plugin['jiandan'])){
            $plugin['jiandan'] = ['is_jd_pingji_rule'=>0,'jd_pingji_reward'=>[]];
        }
        if(empty($plugin['ladderabonus'])){
            $plugin['ladderabonus'] = ['status'=>1];
        }
        if(empty($plugin['management'])){
            $plugin['management'] = ['status'=>1];
        }
        if(empty($plugin['laddermanage'])){
            $plugin['laddermanage'] = ['status'=>1];
        }
        if(empty($plugin['gift'])){
            $plugin['gift'] = ['status'=>0,'tx_status'=>0,'cash'=>''];
        }
        if(empty($plugin['cloud_warehouse'])){
            $plugin['cloud_warehouse'] = ['capital'=>[],'buy'=>[]];
        }
        if(is_null($plugin['cloud_warehouse']['buy'])) $plugin['cloud_warehouse']['buy'] = [];
        if(empty($plugin['floatamount'])){
            $plugin['floatamount'] = ['status'=>0,'type'=>0,'price'=>''];
        }
        if(empty($plugin['luckywheel'])){
            $plugin['luckywheel'] = ['status'=>0,'lottery_num'=>'']; 
        }
        if(empty($plugin['tongue_diagnosis'])){
            $plugin['tongue_diagnosis'] = ['status'=>0,'num'=>'']; 
        }
        if(empty($plugin['cumulative_fanhuan'])){
            if(empty($goods)){
                $plugin['cumulative_fanhuan'] = ['status'=>0,'cumulative_fanhuan_rule'=>0,'rule'=>['level'=>[],'money1'=>'','money2'=>''],'cumulative_fanhuan_score_rule'=>0,'score_rule'=>['level'=>[],'money1'=>'','money2'=>'']];
            }
            else{
                $plugin['cumulative_fanhuan'] = ['status'=>$goods['is_cumulative_fanhuan'],'cumulative_fanhuan_rule'=>$goods['cumulative_fanhuan_rule'],'rule'=>$cumulative_fanhuan,'cumulative_fanhuan_score_rule'=>0,'score_rule'=>['level'=>[],'money1'=>'','money2'=>'']];
            }
        }
        if(empty($plugin['agent'])){
            $plugin['agent'] = ['develop_reward'=>['independent_rule'=>0,'money1_rate'=>'','money2_rate'=>'','money3_rate'=>'','money8_rate'=>'','money1'=>'','money2'=>'','money3'=>'','money8'=>'']]; 
        }
        if(empty($plugin['manabonus'])){
            $plugin['manabonus'] = ['status'=>1,'money'=>''];
        }
        if(empty($plugin['apabonus'])){
            $plugin['apabonus'] = ['status'=>1,'money'=>''];
        }
        if(empty($plugin['salequota'])){
            $plugin['salequota'] = ['status'=>0,'stock'=>[],'chuku_status'=>0];
        }
        if(is_null($plugin['salequota']['chuku_status'])){
            $plugin['salequota']['chuku_status'] = 0;
        }
        if(empty($plugin['fenxiao'])){
            $plugin['fenxiao'] = ['achievement_jisuan'=>1];
        }
        if(empty($plugin['shuziren'])){
            $plugin['shuziren'] = ['status'=>1,'number'=>''];
        }
        if(empty($plugin['sreturn'])){
            $plugin['sreturn'] = ['status'=>0,'sreturn_rule'=>0,'rule'=>['level'=>[],'money1'=>'','money2'=>''],'sreturn_score_rule'=>0,'score_rule'=>['level'=>[],'money1'=>'','money2'=>'']];
        }

        $salequota_stock = [];
        foreach($base['salequota']['stock'] as $key => $val){
            if(!empty($plugin['salequota']['stock'][$key])){
                $val['value'] = $plugin['salequota']['stock'][$key]['value'];
            }
            else{
                $val['value'] = '';
            } 
            $salequota_stock[] = $val;
        }
        
        $plugin['salequota']['stock'] = $salequota_stock;

        $plugin['coupon']['is_nouse_coupon'] = intval($plugin['coupon']['is_nouse_coupon']);
        $plugin['queue_mode']['status'] = intval($plugin['queue_mode']['status']);
        $plugin['queue_mode']['rule'] = intval($plugin['queue_mode']['rule']);
        $plugin['sabonus']['status'] = intval($plugin['sabonus']['status']);
        $plugin['chaoyue']['status'] = intval($plugin['chaoyue']['status']);
        $plugin['chaoyue']['inde_rule'] = intval($plugin['chaoyue']['inde_rule']);
        $plugin['pay']['weixin_pay'] = intval($plugin['pay']['weixin_pay']);
        $plugin['pay']['balance_pay'] = intval($plugin['pay']['balance_pay']);
        $plugin['pay']['deposit_release_pay'] = intval($plugin['pay']['deposit_release_pay']);
        $plugin['crazy']['status'] = intval($plugin['crazy']['status']);
        $plugin['npingji']['status'] = intval($plugin['npingji']['status']);
        $plugin['npingji']['inde_rule'] = intval($plugin['npingji']['inde_rule']);
        $plugin['form']['status'] = intval($plugin['form']['status']);
        
        $grade1 = [];
        $grade1_rate = [];
        $grade2 = [];
        $grade2_rate = [];
        $grade3 = [];
        $grade1_score = [];
        $grade2_score = [];
        $grade3_score = [];
        
        $repurchase_grade1 = [];
        $repurchase_grade2 = [];
        $repurchase_grade1_rate = [];
        $repurchase_grade2_rate = [];
        $repurchase_grade3 = [];
        
        $agent_grade1 = [];
        $agent_grade2 = [];
        $agent_grade3 = [];
        $agent_grade4 = [];
        $agent_grade8 = [];
        
        $chaoyue_reward = $plugin['chaoyue']['reward'];
        $pingji_layer = max(1,$base['fenxiao']['pingji_layer']);
        $npingji_reward = $plugin['npingji']['reward'];
        $jd_pingji_reward = $plugin['jiandan']['jd_pingji_reward'];
        
        foreach ($levels as $key => $val){
            $plugin['cloud_warehouse']['capital']['level'.$val['id']] = round($plugin['cloud_warehouse']['capital']['level'.$val['id']],2);

            $grade1['level'.$val['id']] = round($commission1['grade']['level'.$val['id']],2);
            $grade2['level'.$val['id']] = round($commission2['grade']['level'.$val['id']],2);
            $grade3['level'.$val['id']] = round($commission3['grade']['level'.$val['id']],2);
            $grade1_rate['level'.$val['id']] = round($commission1['grade_rate']['level'.$val['id']],2);
            $grade2_rate['level'.$val['id']] = round($commission2['grade_rate']['level'.$val['id']],2);
            $grade1_score['level'.$val['id']] = round($commission1['grade_score']['level'.$val['id']],2);
            $grade2_score['level'.$val['id']] = round($commission2['grade_score']['level'.$val['id']],2);
            $grade3_score['level'.$val['id']] = round($commission3['grade_score']['level'.$val['id']],2);
            
            $repurchase_grade1['level'.$val['id']] = round($repurchase_reward1['grade']['level'.$val['id']],2);
            $repurchase_grade2['level'.$val['id']] = round($repurchase_reward2['grade']['level'.$val['id']],2);
            $repurchase_grade3['level'.$val['id']] = round($repurchase_reward3['grade']['level'.$val['id']],2);
            $repurchase_grade1_rate['level'.$val['id']] = round($repurchase_reward1['grade_rate']['level'.$val['id']],2);
            $repurchase_grade2_rate['level'.$val['id']] = round($repurchase_reward2['grade_rate']['level'.$val['id']],2);
            
            $jc_reward['level'.$val['id']]['money1'] = round($jc_reward['level'.$val['id']]['money1'],2);
            $jc_reward['level'.$val['id']]['money2'] = round($jc_reward['level'.$val['id']]['money2'],2);
            
            $jd_reward['level'.$val['id']]['money1'] = round($jd_reward['level'.$val['id']]['money1'],2);
            $jd_reward['level'.$val['id']]['money2'] = round($jd_reward['level'.$val['id']]['money2'],2);

            $jd_pingji_reward['level'.$val['id']]['money1'] = round($jd_pingji_reward['level'.$val['id']]['money1'],2);
            $jd_pingji_reward['level'.$val['id']]['money2'] = round($jd_pingji_reward['level'.$val['id']]['money2'],2);
            
            $pj_reward['level'.$val['id']]['money1'] = round($pj_reward['level'.$val['id']]['money1'],2);
            $pj_reward['level'.$val['id']]['money2'] = round($pj_reward['level'.$val['id']]['money2'],2);
            for ($i = 0;$i < $pingji_layer;$i++){
                $pj_reward['level'.$val['id']]['layer'.$i] = round($pj_reward['level'.$val['id']]['layer'.$i],2);
                $pj_reward['level'.$val['id']]['layer_rate'.$i] = round($pj_reward['level'.$val['id']]['layer_rate'.$i],2);
            }
            
            $ab_reward['level'.$val['id']]['money1'] = round($ab_reward['level'.$val['id']]['money1'],2);
            $ab_reward['level'.$val['id']]['money2'] = round($ab_reward['level'.$val['id']]['money2'],2);
            
            $discount = round($level_discount['level'.$val['id']]['money1'],2);
            $discount_price = round($level_discount['level'.$val['id']]['money2'],2);
            if($discount > 10 || $discount <= 0){
                $discount = 10;
            }
            if($discount_price < 0){
                $discount_price = 0;
            }
             
            $level_discount['level'.$val['id']]['money1'] = $discount;
            $level_discount['level'.$val['id']]['money2'] = $discount_price;

            $returns['level']['level'.$val['id']]['money1'] = round($returns['level']['level'.$val['id']]['money1'],2);
            $returns['level']['level'.$val['id']]['money2'] = round($returns['level']['level'.$val['id']]['money2'],2);
            $returns['level']['level'.$val['id']]['money1_score'] = round($returns['level']['level'.$val['id']]['money1_score'],2);
            $returns['level']['level'.$val['id']]['money2_score'] = round($returns['level']['level'.$val['id']]['money2_score'],2);
            
            if(empty($returns['month']['level'.$val['id']])) $returns['month']['level'.$val['id']] = [];
            $month = intval($base['return']['return_day']);
            if($base['return']['return_rule_detailed'] == 1){
                $month = intval($base['return']['return_rule_content']['level'.$val['id']]['return_day']);
            }
            if($month > 0){
                $month_level = [];
                for($i = 0;$i < $month;$i++){
                    $month_level[] = [
                        'money1'=>round($returns['month']['level'.$val['id']][$i]['money1'],2),
                        'money2'=>round($returns['month']['level'.$val['id']][$i]['money2'],2)
                    ];
                }
                $returns['month']['level'.$val['id']] = $month_level;
            }
            else{
                $returns['month']['level'.$val['id']] = [];
            }

            /*
            if($base['return']['status'] == 1){
                if($base['return']['type'] == 1 || $base['return']['type'] == 2){
                    $returns['level']['level'.$val['id']]['money1'] = round($returns['level']['level'.$val['id']]['money1'],2);
                    $returns['level']['level'.$val['id']]['money2'] = round($returns['level']['level'.$val['id']]['money2'],2);
                    $returns['level']['level'.$val['id']]['money1_score'] = round($returns['level']['level'.$val['id']]['money1_score'],2);
                    $returns['level']['level'.$val['id']]['money2_score'] = round($returns['level']['level'.$val['id']]['money2_score'],2);
                }
                else if($base['return']['type'] == 4){
                    if(empty($returns['month']['level'.$val['id']])) $returns['month']['level'.$val['id']] = [];
                    // if(empty($returns['month_score']['level'.$val['id']])) $returns['month_score']['level'.$val['id']] = [];
                    $month = intval($base['return']['return_day']);
                    // $month_score = intval($base['return']['return_day_score']);
                    if($base['return']['return_rule_detailed'] == 1){
                        $month = intval($base['return']['return_rule_content']['level'.$val['id']]['return_day']);
                        // $month_score = intval($base['return']['return_rule_content']['level'.$val['id']]['return_day_score']);
                    }
                    if($month > 0){
                        $month_level = [];
                        for($i = 0;$i < $month;$i++){
                            $month_level[] = [
                                'money1'=>round($returns['month']['level'.$val['id']][$i]['money1'],2),
                                'money2'=>round($returns['month']['level'.$val['id']][$i]['money2'],2)
                            ];
                        }
                        $returns['month']['level'.$val['id']] = $month_level;
                    }
                    else{
                        $returns['month']['level'.$val['id']] = [];
                    }
                    // if($month_score > 0){
                    //     $month_level = [];
                    //     for($i = 0;$i < $month_score;$i++){
                    //         $month_level[] = [
                    //             'money1'=>round($returns['month_score']['level'.$val['id']][$i]['money1'],2),
                    //             'money2'=>round($returns['month_score']['level'.$val['id']][$i]['money2'],2)
                    //         ];
                    //     }
                    //     $returns['month_score']['level'.$val['id']] = $month_level;
                    // }
                    // else{
                    //     $returns['month_score']['level'.$val['id']] = [];
                    // }
                }
            }
            */
            
            $agent_grade1['level'.$val['id']]['money1'] = round($agent[1]['grade']['level'.$val['id']]['money1'],2);
            $agent_grade1['level'.$val['id']]['money2'] = round($agent[1]['grade']['level'.$val['id']]['money2'],2);
            
            $agent_grade2['level'.$val['id']]['money1'] = round($agent[2]['grade']['level'.$val['id']]['money1'],2);
            $agent_grade2['level'.$val['id']]['money2'] = round($agent[2]['grade']['level'.$val['id']]['money2'],2);
            
            $agent_grade3['level'.$val['id']]['money1'] = round($agent[3]['grade']['level'.$val['id']]['money1'],2);
            $agent_grade3['level'.$val['id']]['money2'] = round($agent[3]['grade']['level'.$val['id']]['money2'],2);
            
            $agent_grade4['level'.$val['id']]['money1'] = round($agent[4]['grade']['level'.$val['id']]['money1'],2);
            $agent_grade4['level'.$val['id']]['money2'] = round($agent[4]['grade']['level'.$val['id']]['money2'],2);
            
            $agent_grade8['level'.$val['id']]['money1'] = round($agent[8]['grade']['level'.$val['id']]['money1'],2);
            $agent_grade8['level'.$val['id']]['money2'] = round($agent[8]['grade']['level'.$val['id']]['money2'],2);
            
            
            $give['level'.$val['id']]['money1'] = round($give['level'.$val['id']]['money1'],2);
            $give['level'.$val['id']]['money2'] = round($give['level'.$val['id']]['money2'],2);
            
            
            $repurchase_pingji_reward['level']['level'.$val['id']]['money1'] = (array)$repurchase_pingji_reward['level']['level'.$val['id']]['money1'];
            $repurchase_pingji_reward['level']['level'.$val['id']]['money2'] = (array)$repurchase_pingji_reward['level']['level'.$val['id']]['money2'];
            
            $cumulative_fanhuan['level']['level'.$val['id']]['money1'] = round($cumulative_fanhuan['level']['level'.$val['id']]['money1'],2);
            $cumulative_fanhuan['level']['level'.$val['id']]['money2'] = round($cumulative_fanhuan['level']['level'.$val['id']]['money2'],2);

            $plugin['cumulative_fanhuan']['rule']['level']['level'.$val['id']]['money1'] = round($plugin['cumulative_fanhuan']['rule']['level']['level'.$val['id']]['money1'],2);
            $plugin['cumulative_fanhuan']['rule']['level']['level'.$val['id']]['money2'] = round($plugin['cumulative_fanhuan']['rule']['level']['level'.$val['id']]['money2'],2);
            $plugin['cumulative_fanhuan']['score_rule']['level']['level'.$val['id']]['money1'] = round($plugin['cumulative_fanhuan']['score_rule']['level']['level'.$val['id']]['money1'],2);
            $plugin['cumulative_fanhuan']['score_rule']['level']['level'.$val['id']]['money2'] = round($plugin['cumulative_fanhuan']['score_rule']['level']['level'.$val['id']]['money2'],2);
            
            $cloud_warehouse['level']['level'.$val['id']]['money1'] = round($cloud_warehouse['level']['level'.$val['id']]['money1'],2);
            $cloud_warehouse['level']['level'.$val['id']]['money2'] = round($cloud_warehouse['level']['level'.$val['id']]['money2'],2);
            
            $cloud_warehouse_chuhuo['level']['level'.$val['id']]['money1'] = round($cloud_warehouse_chuhuo['level']['level'.$val['id']]['money1'],2);
            $cloud_warehouse_chuhuo['level']['level'.$val['id']]['money2'] = round($cloud_warehouse_chuhuo['level']['level'.$val['id']]['money2'],2);
            
            $ladder_push['level']['level'.$val['id']] = (array)$ladder_push['level']['level'.$val['id']];
            
            
            $chaoyue_reward['level'.$val['id']]['money1'] = round($chaoyue_reward['level'.$val['id']]['money1'],2);
            $chaoyue_reward['level'.$val['id']]['money2'] = round($chaoyue_reward['level'.$val['id']]['money2'],2);
            
            $npingji_reward['level'.$val['id']]['money1'] = round($npingji_reward['level'.$val['id']]['money1'],2);
            $npingji_reward['level'.$val['id']]['money2'] = round($npingji_reward['level'.$val['id']]['money2'],2);

            $plugin['sreturn']['rule']['level']['level'.$val['id']]['money1'] = round($plugin['sreturn']['rule']['level']['level'.$val['id']]['money1'],2);
            $plugin['sreturn']['rule']['level']['level'.$val['id']]['money2'] = round($plugin['sreturn']['rule']['level']['level'.$val['id']]['money2'],2);
            $plugin['sreturn']['score_rule']['level']['level'.$val['id']]['money1'] = round($plugin['sreturn']['score_rule']['level']['level'.$val['id']]['money1'],2);
            $plugin['sreturn']['score_rule']['level']['level'.$val['id']]['money2'] = round($plugin['sreturn']['score_rule']['level']['level'.$val['id']]['money2'],2);
            
        }
        if(empty($commission1)){
            $commission1 = ['level'=>['money1'=>'','money2'=>'','money1_score'=>'','money2_score'=>''],'grade'=>$grade1,'grade_rate'=>$grade1_rate,'grade_score'=>$grade1_score];
        }
        else{
            $commission1['grade'] = $grade1;
            $commission1['grade_rate'] = $grade1_rate;
            $commission1['grade_score'] = $grade1_score;
        }
        if(empty($commission2)){
            $commission2 = ['level'=>['money1'=>'','money2'=>'','money1_score'=>'','money2_score'=>''],'grade'=>$grade2,'grade_rate'=>$grade2_rate,'grade_score'=>$grade2_score];
        }
        else{
            $commission2['grade'] = $grade2;
            $commission2['grade_rate'] = $grade2_rate;
            $commission2['grade_score'] = $grade2_score;
        }
        if(empty($commission3)){
            $commission3 = ['level'=>['money1'=>'','money2'=>'','money1_score'=>'','money2_score'=>''],'grade'=>$grade3,'grade_score'=>$grade3_score];
        }
        else{
            $commission3['grade'] = $grade3;
            $commission3['grade_score'] = $grade3_score;
        }
        
        if(empty($repurchase_reward1)){
            $repurchase_reward1 = ['level'=>['money1'=>'','money2'=>''],'grade'=>$repurchase_grade1,'grade_rate'=>$repurchase_grade1_rate];
        }
        else{
            $repurchase_reward1['grade'] = $repurchase_grade1;
            $repurchase_reward1['grade_rate'] = $repurchase_grade1_rate;
        }
        if(empty($repurchase_reward2)){
            $repurchase_reward2 = ['level'=>['money1'=>'','money2'=>''],'grade'=>$repurchase_grade2,'grade_rate'=>$repurchase_grade2_rate];
        }
        else{
            $repurchase_reward2['grade'] = $repurchase_grade2;
            $repurchase_reward2['grade_rate'] = $repurchase_grade2_rate;
        }
        if(empty($repurchase_reward3)){
            $repurchase_reward3 = ['level'=>['money1'=>'','money2'=>''],'grade'=>$repurchase_grade3];
        }
        else{
            $repurchase_reward3['grade'] = $repurchase_grade3;
        }
        
        $plugin['chaoyue']['reward'] = $chaoyue_reward;
        $plugin['npingji']['reward'] = $npingji_reward;
        $plugin['jiandan']['jd_pingji_reward'] = $jd_pingji_reward;
        
        if(empty($agent)){
            $agent = array(
                "1"=>array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>$agent_grade1
                ),
                "2"=>array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>$agent_grade2
                ),
                "3"=>array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>$agent_grade3
                ),
                "4"=>array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>$agent_grade4
                ),
                "8"=>array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>$agent_grade8
                ),
            );
        }
        else{
            $agent[1]['grade'] = $agent_grade1;
            $agent[2]['grade'] = $agent_grade2;
            $agent[3]['grade'] = $agent_grade3;
            $agent[4]['grade'] = $agent_grade4;
            if(empty($agent[8])) $agent[8] = array(
                    'level'=>['money1'=>'','money2'=>''],
                    'grade'=>[]
                );
            $agent[8]['grade'] = $agent_grade8;
        }
        
        
        // $returns['type'] = 1;
         
        
        if(!empty($goods)){
            $goods['plugin'] = $plugin;
            
            
            
            $goods['commission1'] = $commission1;
            $goods['commission2'] = $commission2;
            $goods['commission3'] = $commission3;
            
            $goods['jc_reward'] = $jc_reward;
            $goods['jd_reward'] = $jd_reward;
            $goods['pj_reward'] = $pj_reward;
            $goods['ab_reward'] = $ab_reward;
            
            $goods['repurchase_reward1'] = $repurchase_reward1;
            $goods['repurchase_reward2'] = $repurchase_reward2;
            $goods['repurchase_reward3'] = $repurchase_reward3;
            
            $goods['cumulative_fanhuan'] = $cumulative_fanhuan;
            $goods['cloud_warehouse'] = $cloud_warehouse;
            $goods['cloud_warehouse_chuhuo'] = $cloud_warehouse_chuhuo;
            
            $goods['ladder_push'] = $ladder_push;
            
            $goods['level_discount'] = $level_discount;
            
            $goods['returns'] = $returns;
            
            $goods['agent'] = $agent;
            $goods['give'] = $give;
            $goods['repurchase_pingji_reward'] = $repurchase_pingji_reward;
            
            $spec = Db::name("bbfx_goods_spec")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->field("id,name")->order("id asc")->select();
            $spec_item_list = [];
            if(!empty($spec)){
                foreach ($spec as $key => &$val){
                    $spec_items = Db::name("bbfx_goods_spec_item")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id'],'pid'=>$val['id']])->field("id,name")->select();
                    foreach ($spec_items as $k => &$v){
                        $v['pindex'] = $key;
                        $v['index'] = $k;
                        $spec_item_list[$v['id']] = $key."_".$k;
                    }
                    unset($v);
                    $val['spec_item'] = $spec_items;
                }
                unset($val);
                
                $sku_list = Db::name("bbfx_goods_spec_sku")->where(['uniacid'=>UNIACID,'goods_id'=>$goods['id']])->select();
                $sku_data = [];
                foreach ($sku_list as $key => $val){
                    $sku = explode(",",$val['sku']);
                    $sku_key = [];
                    foreach ($sku as $k => $v){
                        
                        $sku_key[] = $spec_item_list[$v];
                    }
                    $sku_key = implode(",",$sku_key);
                    $sku_data['price'][$sku_key] = $val['price']; 
                    $sku_data['profit_amount'][$sku_key] = $val['profit_amount']; 
                    $sku_data['original_price'][$sku_key] = $val['original_price'];
                    $sku_data['volume'][$sku_key] = $val['volume'];
                    $sku_data['weight'][$sku_key] = $val['weight'];
                    $sku_data['stock'][$sku_key] = $val['stock'];
                    $sku_data['cost_price'][$sku_key] = $val['cost_price'];
                    $sku_data['thumb'][$sku_key] = cdnurl($val['thumb']);
                    $sku_data['dish'][$sku_key] = iunserializer($val['dish']);
                    $sku_data['product_code'][$sku_key] = trim($val['product_code']);
                }
                $goods['sku_data'] = $sku_data;
            }
            else{
                $goods['is_spec'] = 0;
            }
            
            $goods['spec'] = $spec;
            
            $result['goods'] = $goods;
        }

        $result['plugin'] = $plugin;
        
        $result['commission1'] = $commission1;
        $result['commission2'] = $commission2;
        $result['commission3'] = $commission3;
        
        $result['jc_reward'] = $jc_reward;
        $result['jd_reward'] = $jd_reward;
        $result['pj_reward'] = $pj_reward;
        $result['ab_reward'] = $ab_reward;
        $result['repurchase_reward1'] = $repurchase_reward1;
        $result['repurchase_reward2'] = $repurchase_reward2;
        $result['repurchase_reward3'] = $repurchase_reward3;
        $result['level_discount'] = $level_discount;
        $result['returns'] = $returns;
        $result['agent'] = $agent;
        $result['give'] = $give;
        $result['repurchase_pingji_reward'] = $repurchase_pingji_reward;
        $result['cumulative_fanhuan'] = $cumulative_fanhuan;
        $result['cloud_warehouse'] = $cloud_warehouse;
        $result['cloud_warehouse_chuhuo'] = $cloud_warehouse_chuhuo;
        
        $result['ladder_push'] = $ladder_push;
        $contract_list = Db::name("bbfx_contract")->where(['uniacid'=>UNIACID,'type'=>1])->field("id,name")->select();
        $result['contract_list'] = $contract_list;
        
        $coupon_list = Db::name("bbfx_coupon")->where(['uniacid'=>UNIACID,'status'=>1])->select();
        $result['coupon_list'] = $coupon_list;
        
        $result['goods_category'] = $goods_category;

        return __json(1,"操作成功",$result);
    }
}