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

namespace app\admin\controller\bbfxshop\contribution;

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\Contribution;
    }
    
    public function set(){
        $params = $this->param();
        
        $config = Db::name("bbfx_contribution")->where(["uniacid"=>UNIACID])->find();
        
        $data = array(
            "uniacid"=>UNIACID,
            "status"=>intval($params['status']),
            "score_convert"=>intval($params['score_convert']),
            "score_convert_rate"=>round($params['score_convert_rate'],2)
        ); 
        $source = $params['source'];
        if($data['status'] == 1){
            if(empty($source['option'])){
                __error("请选择贡献值来源");
            }
            $source['rate1'] = round($source['rate1'],2);
            $source['rate2'] = round($source['rate2'],2);
            $source['rate3'] = round($source['rate3'],2);
            $source['rate4'] = round($source['rate4'],2);
            if(in_array(1, $source['option']) && $source['rate1'] <= 0){
                __error("请输入消费金额贡献值比例");
            }
            if(in_array(2, $source['option']) && $source['rate2'] <= 0){
                __error("请输入直推业绩金额贡献值比例");
            }
            if(in_array(3, $source['option']) && $source['rate3'] <= 0){
                __error("请输入团队业绩金额贡献值比例");
            }
            if(in_array(4, $source['option']) && $source['rate4'] <= 0){
                __error("请输入推荐用户贡献值");
            }
            if($data['score_convert'] == 1 && $data['score_convert_rate'] <= 0){
                __error("请输入贡献值转换积分比例");
            }
        }
        $data['source'] = iserializer($source);
        
    	if(!empty($config)){
          	$res = Db::name("bbfx_contribution")->where(["id"=>$config['id']])->update($data);;
        }
      	else{
        	$res = Db::name("bbfx_contribution")->insert($data);
        }
        
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_contribution");
            __success("编辑成功",$data);
        }
      	else{
      	    __success("操作成功",$data);
      	}
    }
}