BrokerUserConfigRequest.php 4.01 KB
<?php

namespace App\Http\Container\AdminSection\Requests\System;

use App\Models\BrokerPushConfig;
use App\Models\BrokerUserConfig;
use Arr;
use Hikoon\LaravelApi\Exceptions\ValidationException;
use Hikoon\LaravelApi\Support\ApiCode;
use Hikoon\LaravelApi\Support\ApiRequest;
use Illuminate\Database\Eloquent\Builder;

class BrokerUserConfigRequest extends ApiRequest
{
    /**
     * @return void
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     */
    protected function passedValidation(): void
    {
        $itemIdentifier = array_unique(Arr::pluck($this->input('items', []), 'identifier'));

        if (BrokerPushConfig::query()->whereIn('identifier', $itemIdentifier)->count() !== count($itemIdentifier)) {
            throw new ValidationException(ApiCode::VALIDATION_ERROR, '无法匹配到分类对应关系,请检查分类配置');
        }

        if (BrokerUserConfig::query()->whereNot(fn(Builder $builder) => $builder->where('begin_at', '>=', $this->input('end_at'))->orWhere('end_at', '<=', $this->input('begin_at')))->where('id', '!=', $this->route('user_config.id'))->exists()) {
            throw new ValidationException(ApiCode::VALIDATION_ERROR, '时间区间内,已有生效配置');
        }
    }


    public function rules(): array
    {
        $rules = [
            'title'              => 'required|string|max:100',
            'begin_at'           => 'required|date_format:Y-m-d H:i:s|before:end_at',
            'end_at'             => 'required|date_format:Y-m-d H:i:s|after:begin_at',
            'push_type'          => 'required|in:0,1,2',
            'push_at'            => 'required_unless:push_type,0|nullable|date_format:Y-m-d H:i:s',
            'items'              => 'required|array',
            'items.*.user_id'    => 'required|int|min:0',
            'items.*.identifier' => 'required|string',
            'items.*.singer_num' => 'integer|min:0|max:1000'
        ];

        if ($this->getMethod() === 'PUT') {
            unset($rules['push_type'], $rules['push_at']);
        }

        return $rules;
    }

    public function messages(): array
    {
        return [
            'title.required'              => '请输入名称',
            'title.max'                   => '名称长度超过最大长度',
            'begin_at.required'           => '请选择生效时间',
            'begin_at.date_format'        => '生效开始时间格式不正确',
            'begin_at.before'             => '生效开始时间不能晚于结束时间',
            'end_at.required'             => '请选择生效时间',
            'end_at.date_format'          => '生效结束时间格式不正确',
            'end_at.after'                => '生效结束时间不能早于开始时间',
            'push_type.required'          => '请选择通知方式',
            'push_type.in'                => '通知方式不在指定范围内',
            'push_at.required_unless'     => '请选择推送时间',
            'push_at.date_format'         => '推送时间格式不正确',
            'push_at.before'              => '推送时间不能晚于生效结束时间',
            'push_at.after'               => '推送时间不能早于生效开始时间',
            'items.required'              => '请上传经纪人配置文件',
            'items.*.user_id.required'    => '请录入经纪人配置-用户ID',
            'items.*.identifier.required' => '请录入经纪人配置-用户分类',
            'items.*.singer_num.required' => '请录入经纪人配置-用户团队歌手额度',
            'items.*.user_id.int'         => '经纪人配置-用户ID类型错误',
            'items.*.user_id.min'         => '经纪人配置-用户ID类型错误',
            'items.*.singer_num.int'      => '经纪人配置-用户团队歌手额度类型错误',
            'items.*.singer_num.min'      => '经纪人配置-用户团队歌手额度不能低于:min',
            'items.*.singer_num.max'      => '经纪人配置-用户团队歌手额度不能高于:max',
        ];
    }
}