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

namespace app\admin\controller\bbfxshop\queue_award;

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\QueueAward;
    }
    
    public function set(){
        $params = $this->param();
        
        $config = Db::name("bbfx_queue_award")->where(["uniacid"=>UNIACID])->find();
        
        $transfer_num = 2;
        
        $data = array(
            "uniacid"=>UNIACID,
            "status"=>$params['status'],
            // "queue_goods"=>$params['queue_goods'],
            // "queue_award_num"=>$params['queue_award_num'],
            // "queue_award_rate"=>$params['queue_award_rate'],
            "is_multiple_queue"=>$params['is_multiple_queue'],
            "rule"=>iserializer($params['rule']),
        );

        $goods = [];
        foreach($params['rule'] as $key=>$val){
            $i = $key+1;
            if(empty($val['goods'])){
                __error("第{$i}条规则排队商品不能为空");
            }
            if(intval($val['num']) <= 0){
                __error("第{$i}条规则排队人数不能为空");
            }
            if(intval($val['reward_rate']) <= 0){
                __error("第{$i}条规则奖励比例不能为空");
            }
            if(intval($val['strikeout_multiple']) <= 0){
                // __error("第{$i}条规则出局倍数不能为空");
            }

            if(!empty($goods)){
                foreach($val['goods'] as $k=>$v){
                    if(in_array($v,$goods)){
                        __error("第{$i}条规则排队商品重复");
                    }
                }
            }
            
            $goods = array_merge($goods,$val['goods']);
        }
        
    	if(!empty($config)){
          	$res = Db::name("bbfx_queue_award")->where(["id"=>$config['id']])->update($data);;
        }
      	else{
        	$res = Db::name("bbfx_queue_award")->insert($data);
        }
        
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_queue_award");
            __success("编辑成功",$data);
        }
      	else{
      	    __success("操作成功",$data);
      	}
    }
}