package cn.lihongjie.coal.sysconfig.entity;

import cn.lihongjie.coal.base.entity.CommonEntity;

import jakarta.persistence.Entity;

import lombok.Data;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;

@Data
@Entity
@Comment("系统配置")
public class SysConfigEntity extends CommonEntity {

    @Comment("配置类型 1 字符串 2 数字 3 数据字典")
    private String type;

    @Comment("配置类型如果是数字, 最小值")
    private Integer minValue;

    @Comment("配置类型如果是数字, 最小值")
    private Integer maxValue;

    @Comment("配置类型如果是字符串, 正则表达式校验")
    private String regexValidator;

    @Comment("配置类型如果是数据字典, 字典编码")
    private String dictCode;

    @Comment("值")
    private String configVal;

    public void validate(String configVal) {

        switch (type) {
            case "1":
                if (StringUtils.isNotEmpty(regexValidator) && !configVal.matches(regexValidator)) {
                    throw new RuntimeException("字符串不符合正则表达式");
                }
                break;
            case "2":
                Integer integer = Integer.valueOf(configVal);
                if (integer < minValue || integer > maxValue) {
                    throw new RuntimeException("数字不符合要求");
                }
                break;
        }
    }
}
