package com.cf.imes.framework.common.util.validation.statistics;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.enums.StatisticsUnit;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 统计维度单位入参校验器
 *
 * @author Gqr
 * @since 2024/7/17 9:33
 */
public class StatisticsUnitInEnumValidator implements ConstraintValidator<StatisticsUnitInEnum, Integer> {

    @Override
    public void initialize(StatisticsUnitInEnum constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (ObjectUtil.isNull(value)) {
            return true;
        }
        StatisticsUnit unit = StatisticsUnit.fromValue(value);
        if (ObjectUtil.isNotNull(unit)) {
            return true;
        } else {
            return false;
        }
    }

}

