<?php
declare (strict_types = 1);

namespace app\admin\validate\v1;

use app\admin\model\v1\ProductCategoryModel;
use think\facade\Db;
use think\Validate;

class ProductCategoryValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id'                   => 'require|integer',
        'language_id'          => 'require|integer',
        'unique_id'            => 'require',
        'pid'                  => 'integer|different:id|checkPidNotBeChildren',
        'name'                 => 'require|max:64',
        'icon'                 => 'max:125',
        'desc'                 => 'max:255',
        'related_tco_category' => 'string|checkRelatedTcoCategory',
        'sort'                 => 'integer',
        'level'                => 'integer',
        'is_show'              => 'in:0,1',
        'seo_title'            => 'max:255',
        'seo_keywords'         => 'max:255',
        'seo_desc'             => 'max:255',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'id.require'                                   => 'ID不能为空',
        'id.integer'                                   => 'ID必须为整数',
        'language_id.require'                          => '语言ID不能为空',
        'language_id.integer'                          => '语言ID必须为整数',
        'unique_id.require'                            => '唯一标识不能为空',
        'pid.different'                                => '父级ID不能为自身',
        'pid.checkPidNotBeChildren'                    => '父级ID不能为自身的子分类',
        'pid.integer'                                  => '父级ID必须为整数',
        'name.require'                                 => '名称不能为空',
        'name.max'                                     => '名称最多不能超过64个字符',
        'icon.max'                                     => '图标最多不能超过125个字符',
        'desc.max'                                     => '描述最多不能超过255个字符',
        'related_tco_category.string'                  => '关联TCO分类格式错误',
        'related_tco_category.checkRelatedTcoCategory' => '该TCO分类已绑定',
        'sort.integer'                                 => '排序格式错误',
        'level.integer'                                => '级别格式错误',
        'is_show.in'                                   => '是否显示格式错误',
        'seo_title.max'                                => 'SEO标题最多不能超过255个字符',
        'seo_keywords.max'                             => 'SEO关键字最多不能超过255个字符',
        'seo_desc.max'                                 => 'SEO描述最多不能超过255个字符',
    ];

    // 验证pid
    protected function checkPidNotBeChildren($value, $rule, $data = [])
    {
        if ($value == 0) {
            return true;
        }
        // 如果pid和id相同，返回false
        if ($value == $data['id']) {
            return false;
        }
        // 如果pid是id的子分类，返回false
        $children = ProductCategoryModel::whereRaw('FIND_IN_SET(:id, path)', ['id' => $data['id']])->column('id');
        if (!empty($children) && in_array($value, $children)) {
            return false;
        }

        return true;
    }

    // 新增场景
    public function sceneAdd()
    {
        return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
    }

    // 验证related_tco_category
    protected function checkRelatedTcoCategory($value, $rule, $data = [])
    {
        if (empty($value)) {
            return true;
        }

        $val = ProductCategoryModel::where(function($query) use($value) {
            $arr = explode(",", $value);
            foreach ($arr as $v) {
                $query->whereFindInSet('related_tco_category', $v, 'OR');
            }
        })->column('id');
        if (!empty($val)) {
            $size = count($val);
            // 如果存在超过一个，直接验证失败
            if ($size > 1) {
                return false;
            }
            // 如果存在一个，并存在的id不为自身，验证失败（考虑更新场景查到自身情况）
            if ($size == 1 && $val[0] != $data['id']) {
                return false;
            }
        }

        return true;
    }
}
