package com.yonge.cooleshow.common.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> {
    QR_LOGIN_CODE("二维码登录code"),

    TEACHER_TOTAL("老师统计缓存"),
    STUDENT_TOTAL("学生统计缓存"),
    SUBJECT_ITEM("声部下拉缓存", Duration.ofMinutes(1L)),
    TEACHER_SUBJECT_ITEM("老师声部下拉缓存", Duration.ofMinutes(1L)),
    STUDENT_SUBJECT_ITEM("学生声部下拉缓存", Duration.ofMinutes(1L)),

    LOCK_EXECUTE_ORDER("用户下单/付款/取消订单锁"),
    LOCK_RECORD_ACCOUNT("账户记录锁"),
    LOCK_CHANGE_ACCOUNT("账户变更锁"),
    LOCK_CHANGE_PIANO_ROOM_ACCOUNT("琴房账户变更锁"),
    LOCK_WITHDRAWAL("锁用户结算"),
    LOCK_REWARD_STOCK("锁奖品库存变更"),
    LOCK_ACTIVITY_REWARD_STOCK("锁活动奖品变更"),
    LOCK_ACTIVITY_STOCK("锁活动变更"),
    LOCK_STANDARD_GIFT_CRON("达标活动定时任务锁"),
    LOCK_EVALUATION_CRON("评测活动定时任务锁"),
    ;
    /***
     * 缓存描述
     * @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 Duration getDuration() {
        return duration;
    }

    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;
    }
}
