<?php



namespace app\validate\admin;


use app\common\repositories\user\UserBrokerageRepository;
use think\Validate;

class UserBrokerageValidate extends Validate
{
    protected $failException = true;

    protected $rule = [
        'brokerage_level|会员等级' => 'require|integer|>:0',
        'brokerage_name|会员名称' => 'require|max:16',
        'brokerage_icon|会员图标' => 'require',
        'brokerage_rule|会员升级规则' => 'require|array|checkBrokerageRule',
        'extension_one|一级佣金比例' => 'require|float|>=:0|<=:100',
        'extension_two|二级佣金比例' => 'require|float|>=:0|<=:100',
    ];

    public function checkBrokerageRule($data)
    {
        $types = UserBrokerageRepository::BROKERAGE_RULE_TYPE;
        if (count($types) != count($data)) {
            return '请输入正确的升级任务';
        }
        $flag = 0;
        foreach ($types as $type) {
            $val = $data[$type] ?? '';
            if (!is_array($val) || !isset($val['name'], $val['num'], $val['info']) || count($val) != 3) return '请输入正确的升级任务';
            if ($val['num'] < 0)
                return '请输入正确的任务数量';
            if ($val['num'] > 0 && !$val['name']) return '请输入任务名称';
            if ($val['num'] > 0) $flag++;
        }
        if (!$flag) return '请至少设置一个升级任务';
        return true;
    }
}