package com.yonge.cooleshow.enums;

import com.yonge.cooleshow.common.constant.CommonConstants;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;

/**
 * Description
 *
 * @author liujunchi
 * @date 2022-04-24
 */
public enum OrdeerCacherEnmu {
    LOCK_EXECUTE_ORDER("用户下单锁"),
    LOCK_PAY_ORDER("用户付款锁"),
    LOCK_CHANGE_ACCOUNT("账户变更锁"),
    LOCK_REFUND_ORDER_MALL("商城退款锁"),

    ;

    ;
    /***
     * 缓存描述
     * @author liweifan
     * @updateTime 2022/3/28 16:31
     */
    private String msg;
    /***
     * 缓存时长
     * @author liweifan
     * @updateTime 2022/3/28 16:31
     */
    private Duration duration;

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

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

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