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

namespace app\admin\controller\bbfxshop\salequota;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Config extends Base
{

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Salequota;
    }
    
    public function set(){
        $params = $this->param();
        
        $config = Db::name("bbfx_salequota")->where(["uniacid"=>UNIACID])->find();
        
        $data = array(
            "uniacid"=>UNIACID, 
            "status"=>intval($params['status']),
            "level"=>iserializer($params['level']),
      	    "stock"=>iserializer($params['stock']),
      	    "association_stock"=>iserializer($params['association_stock']),
        );

        $association_stock = $params['association_stock'];
        $zhu_stock = [];
        foreach($association_stock as $key=>$val){
            $stock = $val['stock'];
            if(!is_numeric($stock)){
                __error("请选择主库存");
            }
            if(in_array($stock,$zhu_stock)){
                __error("多个主库存不能选择一样");
            }
            if(in_array($stock,$val['goal'])){
                __error("主库存不能和关联库存一起选择");
            }
            if($val['deduct_stock'] <= 0){
                __error("请输入关联库存扣除数量");
            }
        }
        
    	if(!empty($config)){
          	$res = Db::name("bbfx_salequota")->where(["id"=>$config['id']])->update($data);;
        }
      	else{
        	$res = Db::name("bbfx_salequota")->insert($data);
        }
        
        if($res){
            
            \app\admin\model\AdminLog::record("","",$data,"bbfx_salequota");
            
            __success("编辑成功",$data);
        }
      	else{
      	    __success("操作成功",$data);
      	}
    }
}