package com.yc.ship.module.trade.utils;

import com.alibaba.excel.util.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * &#064;Description ：交易相关编号生成组件
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/11 10:33
 */
@Component
public class TradeUUCodeUtils {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final String ORDER_PREFIX = "SP";
    public static final String SUPPLIER_ORDER_PREFIX = "XS";

    @Value("${yudao.suffixCode:LS}")
    public String suffixCode;
    /**
     * 生成订单号  prefix + yyyyMMddHHmmss + 至少5位序列+2位随机+2位分中心后缀
     * @return
     */
    public String generateOrderNo(String prefix){
        Date now = new Date();
        //生成序号
        String key = prefix + DateUtils.format(now,"yyyyMMdd");
        long incrNum =stringRedisTemplate.opsForValue().increment(key,1);
        if (incrNum<2){
            stringRedisTemplate.expire(key,30, TimeUnit.HOURS);
        }
        String strNum = String.format("%05d", incrNum);
        //生成时间标识
        String date = DateUtils.format(now,"yyyyMMddHHmm");
        // 生成随机数
        Random random = new Random();
        int ranNum = random.nextInt(100);
        String strRan = String.format("%02d", ranNum);
//        //获取后缀
//        String suffix=config.getSuffixCode();
        //组合生成订单
        String orderId = prefix + date + strNum + strRan ;
        return orderId;
    }

    /**
     * 生成票号
     * 票号规则 时间+2位校验位+2位分中心后缀
     * @param delta 生成个数
     * @return
     */
    public List<String> generateTicketNo(int delta) {
        List<String> tickes=new ArrayList<>();
        Date now = new Date();
        //生成序号
        String time=DateUtils.format(now,"yyyyMMdd");
        String key = "TK$" +time;
        long incrNum =stringRedisTemplate.opsForValue().increment(key,delta);
        long begin=incrNum-delta;
        if (begin==0) {
            stringRedisTemplate.expire(key, 30, TimeUnit.HOURS);
        }
        //获取后缀
//        String suffix=config.getSuffixCode();
        //时间标识
        for (long i = begin; i < incrNum; i++) {
            //序号位
            String s1 = String.format("%05d", i);

            //校验位
            String src = time + s1;
            long crc = CCRC8Util.FindCRC(src.getBytes());
            String s2 = String.format("%03d", crc);
            //加密
            Long slong = Long.parseLong(time + s2 +s1);
//            String code = CCRC8Util.NumToStr(slong, 32)+suffix;
            String code = CCRC8Util.NumToStr(slong, 32)+suffixCode;

            //时间+序号+后缀
            //String strNum= time + s1;
            //采用自定义加密算法
            //String code=HexUtil.encode(HEX_KEY,strNum)+suffix;
            tickes.add(code);
        }
        return  tickes;
    }
}
