<?php
/*
 * @Author: ZMH
 * @Email: zmhwork@qq.com
 * @Date: 2025-03-13 15:21:41
 * @LastEditTime: 2025-03-13 16:13:45
 * @LastEditors: ZMH
 * @FilePath: \opkpm\app\admin\validate\shop\GoodsRenewValidate.php
 * 
 * @Description: 续费验证
 */
namespace app\admin\validate\shop;


use app\common\basics\Validate;
use app\common\model\shop\ShopGoodsRenew;

class GoodsRenewValidate extends Validate
{
    protected $rule = [
        'id'           => 'require|number',
        'name|套餐名称' => 'require|max:32|checkExist',
        'price|套餐价格' => 'require|number',
        'type_id|套餐类型' => 'require|number|in:0,1',
        'op_count|套餐操作次数' => 'require|number|min:1',
        'status|套餐状态' => 'require|number|in:0,1',
        'sort|排序'      => 'number',
    ];

    protected $message = [
        'id.require' => 'ID不可为空',
        'id.number'  => 'ID必须为数字',
    ];

    protected $scene = [
        'id'    => ['id'],
        'add' => ['name', 'price', 'type_id', 'op_count', 'status'],
        'edit' => ['id', 'name', 'price', 'type_id', 'op_count', 'sort', 'status'],
        'del'   => ['id'],
    ];

    public static function checkExist($value, $rule, $data=[])
    {
        $where = [];
        if (isset($data['id'])) {
            $where[] = ['id', '<>', $data['id']];
        }
        $where[] = ['name', '=', $value];

        $res = ShopGoodsRenew::where($where)->find();
        if ($res) {
            return '该名称已存在';
        }
        return true;
    }
}