package com.yonge.cooleshow.mall.common.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 OrderCacheEnum {
    LOCK_EXECUTE_ORDER("用户下单锁"),
    LOCK_PAY_ORDER("用户付款锁"),
    LOCK_CHANGE_ACCOUNT("账户变更锁"),
    LOCK_REFUND_ORDER_MALL("商城退款锁"),
    LOCK_ORDER_NO_MALL("订单号锁"),
    LOCK_STOCK_MALL("商品库存锁"),

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

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

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