package com.yonge.cooleshow.biz.dal.enums;

import com.yonge.cooleshow.common.constant.CommonConstants;
import com.yonge.toolset.base.enums.BaseEnum;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;

/**
 * redis缓存枚举
 *
 * @Author: liweifan
 * @Data: 2022/3/28 16:24
 */
public enum CacheNameEnum implements BaseEnum<String, CacheNameEnum> {
    TEACHER_TOTAL("老师统计缓存"),
    STUDENT_TOTAL("学生统计缓存"),

    LOCK_EXECUTE_ORDER("用户下单/取消订单锁"),
    LOCK_PAY_ORDER("用户付款锁"),
    LOCK_CHANGE_ACCOUNT("账户变更锁"),
    ;
    /***
     * 缓存描述
     * @author liweifan
     * @updateTime 2022/3/28 16:31
     */
    private String msg;
    /***
     * 缓存时长
     * @author liweifan
     * @updateTime 2022/3/28 16:31
     */
    private Duration duration;

    CacheNameEnum(String msg) {
        this.msg = msg;
    }

    CacheNameEnum(String msg, Duration duration) {
        this.msg = msg;
        this.duration = duration;
    }

    @Override
    public String getCode() {
        return this.name();
    }

    public String getMsg() {
        return msg;
    }

    public String getRedisKey(Object... detils) {
        String key = this.getCode();
        for (Object detil : detils) {
            try {
                if (!CommonConstants.REDIS_SPLIT.equals(key.substring(key.length() - 1))) {
                    key += CommonConstants.REDIS_SPLIT;
                }
                if (null != detil && StringUtils.isNotBlank(detil.toString())) {
                    key += detil.toString();
                }
            } catch (Exception e) {
            }
        }
        if (CommonConstants.REDIS_SPLIT.equals(key.substring(key.length() - 1))) {
            key = key.substring(0, key.length() - 1);
        }
        return key;
    }
}
