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

namespace app\admin\controller\bbfxshop\transfer_mode;

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\TransferMode;
    }
    
    public function set(){
        $params = $this->param();
        
        $config = Db::name("bbfx_fenxiao")->where(["uniacid"=>UNIACID])->find();
        
        
        $status = intval($params['status']);
        $transfer_num = intval($params['transfer_num']);
        
        $data = array(
            "uniacid"=>UNIACID
        );
        
        if($status == 1){
            $data['transfer_mode'] = 1;
            $data['transfer_num'] = $transfer_num;
        }
        else{
            if($config['transfer_mode'] == 1 && $config['transfer_num'] == $transfer_num){
                $data['transfer_mode'] = 0;
            }
        }
        
        // if($transfer_num == 2){
            $data['zsil_nojumpout'] = intval($params['zsil_nojumpout']);
            $data['freeze_money'] = intval($params['freeze_money']);
            $data['transfer_mode_pinji'] = intval($params['transfer_mode_pinji']);
            // $data['transfer_mode_type'] = intval($params['transfer_mode_type']);
            $data['unlock_fund_num'] = max(0,intval($params['unlock_fund_num']));
            $data['fund_money'] = max(0,intval($params['fund_money']));
            $data['transfer_mode_type'] = 2;
            if($data['freeze_money'] > 100){
                __error("创业基金比例不得超出100%");
            }
            if($data['fund_money'] > 100){
                __error("发展基金比例不得超出100%");
            }
            if($data['fund_money'] + $data['freeze_money'] > 100){
                __error("创业基金和发展基金比例加起来不得超出100%");
            }
        // }
        
    	if(!empty($config)){
    	    
    	    if($data['transfer_mode'] == 1){
                Db::name("bbfx_double_track")->where(["uniacid"=>UNIACID])->update(['status'=>0]);
                Db::name("bbfx_pyramid")->where(["uniacid"=>UNIACID])->update(['status'=>0]);
                Db::name("bbfx_repurchase_jiandan")->where(["uniacid"=>UNIACID])->update(['status'=>0]);
            }
    	    
          	$res = Db::name("bbfx_fenxiao")->where(["id"=>$config['id']])->update($data);;
        }
      	else{
        	$res = Db::name("bbfx_fenxiao")->insert($data);
        }
        
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_fenxiao");
            __success("编辑成功",$data);
        }
      	else{
      	    __success("操作成功",$data);
      	}
    }
}