package com.mosty.base.annot.validation;

import com.mosty.base.annot.IsDict;
import com.mosty.base.utils.DictUtils;
import com.mosty.common.base.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author zhangzhao
 * @description 校验是否是字典 实现类
 * @modifier zhangzhao
 * @modifiedTime 2023/3/17 17:40
 * @since 2023/3/17 17:40
 */
public class DictValidation implements ConstraintValidator<IsDict, Object> {

    private IsDict annotation;

    @Override
    public void initialize(IsDict constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if(value == null || StringUtils.isBlank(value + "")){
            //如果需要校验的值为空，并且该项为必填项，则返回错误
            return !annotation.isNotBlank();
        }else{
            //使用字典工具类中的校验方法
            return DictUtils.isDictContainValue(annotation.zdbh(), value + "");
        }
    }
}
