package com.yiqi.common.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.yiqi.common.config.SnowFlakeConfig;
import com.yiqi.common.core.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;

@Component
public class OrderNoUtils {

    @Autowired
    RedisUtils redisUtils;

    public final static String cloth_wash_code_header = "1";
    public final static String cloth_wash_adjust_code_header = "2";
    public final static String activity_order_header = "A";
    public final static String cloth_order_header = "C";
    public final static String purchase_order_header = "P";
    public final static String goods_order_header = "G";
    public final static String recharge_order_header = "R";
    public final static String delivery_order_header = "D";


    /**
     * 洗衣水洗唛号生成
     *
     * @return
     */
    public String getClothWashCode() {
        return getOrderNo(cloth_wash_code_header);
    }

    /**
     * 洗衣水洗附件单号
     *
     * @return
     */
    public String getClothWashAdjustCode() {
        return getOrderNo(cloth_wash_adjust_code_header);
    }

    /**
     * 洗衣订单号生成
     *
     * @return
     */
    public String getClothOrderNo() {
        return getOrderNo(cloth_order_header);
    }

    /**
     * 进货订单号生成
     *
     * @return
     */
    public String getPurchaseOrderNo() {
        return getOrderNo(purchase_order_header);
    }

    /**
     * 商品订单号生成
     *
     * @return
     */
    public String getGoodsOrderNo() {
        return getOrderNo(goods_order_header);
    }

    /**
     * 活动订单号生成
     *
     * @return
     */
    public String getActivityOrderNo() {
        return getOrderNo(activity_order_header);
    }

    /**
     * 充值订单号生成
     *
     * @return
     */
    public String getRechargeOrderNo() {
        return getOrderNo(recharge_order_header);
    }

    /**
     * 配送费订单号生成
     *
     * @return
     */
    public String getDeliveryOrderNo() {
        return getOrderNo(delivery_order_header);
    }


    public String getOrderNo(String headerKey) {
        Long days = DateUtil.between(new Date(), DateUtils.parseDate("2022-01-01"), DateUnit.DAY);
        String date = DateUtil.format(new Date(), "HHmmss");
        Long num = getOrderNum(headerKey);
        String str = String.format("%03d", num.intValue());
        return headerKey + days + date + str;
    }

    public String getOrderNo() {
        return getOrderNo("");
    }


    /**
     * REDIS_KEY
     */
    public final static String REDIS_KEY = "ORDER_NUM:";

    /**
     * 获取取货码
     *
     * @return
     */
    public Long getOrderNum(String headerKey) {
        if (StringUtils.isEmpty(headerKey)) {
            headerKey = "O";
        }
        String key = REDIS_KEY + headerKey + ":" + DateUtil.formatDate(new Date());
        Long count = redisUtils.incr(key, 1);
        redisUtils.expire(key, 24 * 3600);
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new OrderNoUtils().getOrderNo());
    }
}
