package com.backendsys.entity.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义注解 (逻辑实现)
 */
public class RangeArrayValidator implements ConstraintValidator<RangeArray, Integer> {
    private List<String> value;

    @Override
    public void initialize(RangeArray constrainAnnotation) {
        value = Arrays.asList(constrainAnnotation.value());
        ConstraintValidator.super.initialize(constrainAnnotation);
    }

    @Override
    public boolean isValid(Integer validValue, ConstraintValidatorContext constraintValidatorContext) {
        if (validValue == null || validValue == 0) { return true;  }
        if (value != null) {
            if (!value.contains(String.valueOf(validValue))) {
                return false;
            }
        }
        return true;
    }

    //@Override
    //public boolean isValid(String validValue, ConstraintValidatorContext constraintValidatorContext) {
    //    if (validValue == null || validValue.isEmpty()) { return true; }
    //    if (value != null) {
    //        if (!value.contains(validValue)) {
    //            return false;
    //        }
    //    }
    //    return true;
    //}
}
