package com.ruoyi.jarvis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.jarvis.service.IOuterIdGeneratorService;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 商家编码自动生成服务实现类
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class OuterIdGeneratorServiceImpl implements IOuterIdGeneratorService 
{
    private static final Logger log = LoggerFactory.getLogger(OuterIdGeneratorServiceImpl.class);
    
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    
    // Redis key前缀
    private static final String REDIS_KEY_PREFIX = "outer_id_counter:";
    
    // 默认编号长度
    private static final int DEFAULT_NUMBER_LENGTH = 3;
    
    // 默认编号格式
    private static final String DEFAULT_NUMBER_FORMAT = "%03d";

    /**
     * 根据SKUID生成商家编码
     * 
     * @param skuid SKUID
     * @return 生成的商家编码
     */
    @Override
    public String generateOuterId(String skuid) {
        if (StringUtils.isEmpty(skuid)) {
            log.warn("SKUID为空，无法生成商家编码");
            return null;
        }
        
        return generateOuterId(skuid, skuid);
    }

    /**
     * 根据SKUID和自定义前缀生成商家编码
     * 
     * @param skuid SKUID
     * @param prefix 自定义前缀
     * @return 生成的商家编码
     */
    @Override
    public String generateOuterId(String skuid, String prefix) {
        if (StringUtils.isEmpty(skuid) || StringUtils.isEmpty(prefix)) {
            log.warn("SKUID或前缀为空，无法生成商家编码");
            return null;
        }
        
        try {
            // 生成Redis key
            String redisKey = REDIS_KEY_PREFIX + skuid;
            
            // 递增计数器
            Long counter = redisTemplate.opsForValue().increment(redisKey);
            
            // 格式化编号
            String formattedNumber = String.format(DEFAULT_NUMBER_FORMAT, counter);
            
            // 生成商家编码
            String outerId = prefix + formattedNumber;
            
            log.info("成功生成商家编码: skuid={}, counter={}, outerId={}", skuid, counter, outerId);
            
            return outerId;
        } catch (Exception e) {
            log.error("生成商家编码失败: skuid={}, prefix={}", skuid, prefix, e);
            return null;
        }
    }

    /**
     * 重置SKUID的计数器
     * 
     * @param skuid SKUID
     * @return 是否成功
     */
    @Override
    public boolean resetCounter(String skuid) {
        if (StringUtils.isEmpty(skuid)) {
            log.warn("SKUID为空，无法重置计数器");
            return false;
        }
        
        try {
            String redisKey = REDIS_KEY_PREFIX + skuid;
            redisTemplate.delete(redisKey);
            log.info("成功重置计数器: skuid={}", skuid);
            return true;
        } catch (Exception e) {
            log.error("重置计数器失败: skuid={}", skuid, e);
            return false;
        }
    }

    /**
     * 获取SKUID的当前计数器值
     * 
     * @param skuid SKUID
     * @return 当前计数器值
     */
    @Override
    public Long getCurrentCounter(String skuid) {
        if (StringUtils.isEmpty(skuid)) {
            log.warn("SKUID为空，无法获取计数器值");
            return 0L;
        }
        
        try {
            String redisKey = REDIS_KEY_PREFIX + skuid;
            Object value = redisTemplate.opsForValue().get(redisKey);
            
            if (value == null) {
                return 0L;
            }
            
            if (value instanceof Number) {
                return ((Number) value).longValue();
            }
            
            return Long.valueOf(value.toString());
        } catch (Exception e) {
            log.error("获取计数器值失败: skuid={}", skuid, e);
            return 0L;
        }
    }
}
