<?php
/**
 * CardBatchValidate.php
 * User: ZhouBenXu
 * Date: 2024/6/27
 * Time: 上午9:54
 * Notes:
 */

namespace app\admin\validate\card;

use support\Validate;
use app\model\CardBatch;

class CardBatchValidate extends Validate
{
    protected $rule = [
        'card_batch_id' => 'require', // 批次id
        'join_card_batch_user_id' => 'require', // 批次创建用户 user_id
        'join_card_batch_category_id' => 'require', // 批次卡分类ID
        'card_batch_status' => 'require', // 批次状态 CREATE=已创建|PRODUCTED=制作完成|ENABLED=启用|DISABLED=禁用
        'card_batch_name' => 'require', // 卡批次名称
        'card_batch_prefix' => 'require', // 卡批次前缀
        'card_batch_suffix' => 'require', // 卡批次后缀
        'card_batch_serial_begin' => 'require', // 卡批次序号起始
        'card_batch_serial_end' => 'require', // 卡批次序号结束
        'card_batch_amount' => 'require', // 卡批次面额
        'card_batch_validtimes' => 'require', // 卡批次有效期时间戳 0=永久 天
        'validtimes_status' => 'require', // 修改时有效期状态  0 永久 1增加 2减少
        'page' => 'require', // 页码
        'pageSize' => 'require', // 分页条数
        'card_batch_ids' => 'require', // 批次ids
    ];

    protected $message = [];

    protected $scene = [
        'add' => ['join_card_batch_user_id', 'join_card_batch_category_id', 'card_batch_status', 'card_batch_name', 'card_batch_prefix', 'card_batch_suffix', 'card_batch_serial_begin', 'card_batch_serial_end', 'card_batch_amount', 'card_batch_validtimes'],
        'save' => ['join_card_batch_user_id', 'card_batch_id', 'validtimes_status', 'card_batch_name'],
        'delete' => ['card_batch_ids'],
        'list' => ['page', 'pageSize'],
        'info' => ['card_batch_id']
    ];


    /**
     * Notes: 卡号区间校验不能存在交集
     * User: ZhouBenXu
     * DateTime: 2024/7/1 上午11:30
     * @param $params
     * @return bool
     */
    public function checkCardSort($params)
    {
        if ($params['card_batch_serial_begin'] > $params['card_batch_serial_end']) {
            return false;
        }
        // 查询是否存在相同前缀后缀信息
        $cardBatchInfo = CardBatch::where('card_batch_prefix', $params['card_batch_prefix'])
            ->where('card_batch_suffix', $params['card_batch_prefix'])
            ->select()
            ->get()
            ->toArray();
        if (!empty($cardBatchInfo)) {
            foreach ($cardBatchInfo as $key => $value) {
                // 校验序号是否存在交集信息
                if ($value['card_batch_serial_begin'] <= $params['card_batch_serial_end'] && $value['card_batch_serial_end'] >= $params['card_batch_serial_begin']) {
                    return false;
                }
            }
        }
        return true;
    }


}