package com.yunfeiyun.agmp.common.enums;

import com.yunfeiyun.agmp.common.constant.Constants;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;

public enum RedisCacheKey {
    SYS_CONFIG(ModuleType.SYS,"config","系统配置","COMMON"),
    SYS_DICT(ModuleType.SYS,"dict","系统字典","COMMON"),
    SYS_MENU(ModuleType.SYS,"menu","权限字典","COMMON"),
    SYS_RES(ModuleType.SYS,"res","系统资源","COMMON"),
    SYS_RES_EXPIRE_LOCK(ModuleType.SYS,"res_expire_lock","资源数据过期锁","COMMON"),
    SYS_LOGIN_TOKEN(ModuleType.SYS,"login:token","登录用户token","COMMON"),
    SYS_REPEAT_SUBMIT(ModuleType.SYS,"repeat_submit","重复提交校验","COMMON"),
    SYS_PWD_ERR_CNT_KEY(ModuleType.SYS,"pwd_err_cnt","登录账户密码错误次数缓存键名","COMMON"),
    CAPTCHA_CODE_KEY(ModuleType.SYS,"captcha_codes","验证码缓存","COMMON"),
    FMS_NO_GENERATOR(ModuleType.FMS,"no_generator","农事业务编号生成","FMS"),
    WMS_STOCK_CHANGE_LOCK(ModuleType.WMS,"stock_change_lock","库存变动锁","FMS")
    ;

    private final ModuleType moduleType;
    private final String name;
    private final String content;
    private final String prefix;

    RedisCacheKey(ModuleType moduleType, String name, String content,String prefix)
    {
        this.moduleType = moduleType;
        this.name = name;
        this.content = content;
        this.prefix = prefix;
    }


    public String getCacheKey(){
        String appName = SpringUtils.getRequiredProperty("application.name");
        if( this.moduleType == ModuleType.SYS ){
            return this.prefix + ":" + this.moduleType + ":" + this.name;
        }else if( this.moduleType == ModuleType.FMS || this.moduleType == ModuleType.WMS ){
            if(appName.toUpperCase().contains(Constants.FMS)){ //包含FMS
                return appName + ":" + this.moduleType + ":" + this.name;
            }
            return this.prefix + ":" + this.moduleType + ":" + this.name;
        }
        return appName + ":" + this.moduleType + ":" + this.name;
    }

    public String formatter(String value){
        if(StringUtils.isEmpty(value)){
            return this.getCacheKey();
        }
        return this.getCacheKey()+":"+value;
    }


}
