package com.hzs.member.pub.tool;

import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.util.TransactionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: sui q
 * @Time: 2023/3/21 10:04
 * @Classname: MemberToolsHandler
 * @PackageName: com.hzs.member.pub.tool
 */
@Component
@Slf4j
public class MemberToolsHandler {

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * @param orderPrefix 订单前缀
     * @return 订单号
     */
    public String createTradeCode(String orderPrefix) {
        for (int i = 0; i < MagicNumberConstants.RETRY_COUNT; i++) {
            String orderCode = CommonUtil.createSerialNumber(orderPrefix);
            if (redisService.lockKeyOnce(orderCode)) {
                return orderCode;
            }
        }
        throw new RuntimeException(TransactionUtils.getContent(MemberMsgConstants.MEMBER_IS_OPERATION));
    }
}
