package com.cool.bang.common.tools.number;

import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 根据规则生成编号
 *
 * @author PeiZhi Guo
 * 2022-8-2 8:42
 */
@Component
public class IncrUtil {

    private static final String YEAR_FORMAT = "yyyyMMdd";

    private static final String DATE_FORMAT = "yyyyMMdd";

    private static final String DATE_TIME_FORMAT = "yyyyMMddHHmmss";

    private static final String KEY_PREFIX = "incr:";

    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    public IncrUtil(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}

    /**
     * 编号增量 (年月日 + 6位自增)
     */
    public String incr(String businessType) {
        Integer currentYear = Calendar.getInstance().get(Calendar.YEAR);
        RedisAtomicLong counter = new RedisAtomicLong(KEY_PREFIX + businessType + ":" + currentYear,
                Objects.requireNonNull(stringRedisTemplate.getConnectionFactory()));
        long redisId = counter.incrementAndGet();
        String redisIdStr = StrUtil.fillBefore(String.valueOf(redisId), '0',4);
        return nowDate().substring(2) + redisIdStr;
    }

    /**
     * 编号增量 (年 + 6位自增)
     */
    public String incrYear(String businessType) {
        Integer currentYear = Calendar.getInstance().get(Calendar.YEAR);
        RedisAtomicLong counter = new RedisAtomicLong(KEY_PREFIX + businessType + ":" + currentYear,
                Objects.requireNonNull(stringRedisTemplate.getConnectionFactory()));
        long redisId = counter.incrementAndGet();
        String redisIdStr = StrUtil.fillBefore(String.valueOf(redisId), '0',6);
        return currentYear + redisIdStr;
    }

    /**
     * 获取审批流程编号 (年月日时分秒 + 4位随机数)
     */
    public static String flowNumber() {
        return nowDateTime() + randomNumber(4);
    }

    /**
     * 获取审批节点id (年月日时分秒 + 5位随机数)
     */
    public static Long approvalNodeId() {
        return Long.valueOf(nowDateTime() + randomNumber(4));// 当前时间
    }

    /**
     * 获取当前年月日字符串
     */
    public static String nowDate() {
        return formatDate(DATE_FORMAT);
    }

    /**
     * 获取当前年月日时分秒字符串
     */
    public static String nowDateTime() {
        return formatDate(DATE_TIME_FORMAT);
    }

    /**
     * 根据传入参数格式化时间
     */
    public static String formatDate(String format) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 根据传入长度生成随机数
     */
    public static String randomNumber(Integer number) {
        StringBuilder randomNumber = new StringBuilder();
        for (int i = 1; i <= number; i++) {
            randomNumber.append((int) (Math.random() * 9));
        }
        return randomNumber.toString();
    }

}
