package jnpf.util;


import cn.hutool.core.collection.CollUtil;
import jnpf.model.enums.SelfrowingEnum;
import jnpf.util.context.SpringContext;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;

/**
 * @Title: SelfGrowthUtil 自增长工具类
 * @Author: peng.hao
 * @create:  2023/12/1818:07
 */
public class SelfGrowthUtil {

    /**
     * 提供基于模块自定义 ID
     * 提供基于模块自定义 ID
     *
     * @param selfrowingEnum SelfRowing 枚举
     * @return {@link String}
     */
    public static String  provideACustomIDBasedOnTheModule(SelfrowingEnum selfrowingEnum,Integer... args){
        RedisTemplate redisTemplate = SpringContext.getBean("redisTemplate");
        String key = UserProvider.getUser().getTenantId() + ":" + selfrowingEnum.getRedisKey();
        boolean temp = false;
        Long result;
        synchronized (SelfGrowthUtil.class){
            //只会为key设置一次过期时间
            if (!redisTemplate.hasKey(key)) {
                temp = true;
            }
            result = redisTemplate.opsForValue().increment(key);
        }
        // 设置键的过期时间为当天23:59:59
        Date expirationDate = Date.from(LocalDate.now().atTime(23, 59, 59).atZone(ZoneOffset.of("+8")).toInstant());
        if (temp) {
            redisTemplate.expireAt(key, expirationDate);
        }
        //向前补齐默认为3位,可以动态设置补位
        String perfix = "%03d";
        if (CollUtil.isNotEmpty(Arrays.asList(args))) perfix = "%0" + args[0] + "d";
        String format= String.format(perfix, result);
        SimpleDateFormat spd =new SimpleDateFormat("yyyyMMdd");
        //当前年月日
        String nowDate = spd.format(expirationDate);
        return selfrowingEnum.getReturnsThePrefix() + nowDate + format;
    }

    public static String  providePersonnelsCustomIDBasedOnTheModule(SelfrowingEnum selfrowingEnum){
        RedisTemplate redisTemplate = SpringContext.getBean("redisTemplate");
        String key = UserProvider.getUser().getTenantId() + ":" + selfrowingEnum.getRedisKey();
        boolean temp = false;
        Long result;
        synchronized (SelfGrowthUtil.class){
            //只会为key设置一次过期时间
            if (!redisTemplate.hasKey(key)) {
                temp = true;
            }
            result = redisTemplate.opsForValue().increment(key);
        }
        // 设置键的过期时间为当天23:59:59
        Date expirationDate = Date.from(LocalDate.now().atTime(23, 59, 59).atZone(ZoneOffset.of("+8")).toInstant());
        if (temp) {
            redisTemplate.expireAt(key, expirationDate);
        }
        //向前补齐0
        String format= String.format("%03d", result);
        SimpleDateFormat spd =new SimpleDateFormat("yyMMdd");
        //当前年月日
        String nowDate = spd.format(expirationDate);
        return selfrowingEnum.getReturnsThePrefix() + nowDate + format;
    }


    public static String provideACustomIDBasedOnTheModule(SelfrowingEnum selfrowingEnum, String tenantId) {
        RedisTemplate redisTemplate = SpringContext.getBean("redisTemplate");
        String key = tenantId + ":" + selfrowingEnum.getRedisKey();
        boolean temp = false;
        Long result;
        synchronized (SelfGrowthUtil.class) {
            //只会为key设置一次过期时间
            if (!redisTemplate.hasKey(key)) {
                temp = true;
            }
            result = redisTemplate.opsForValue().increment(key);
        }
        // 设置键的过期时间为当天23:59:59
        Date expirationDate = Date.from(LocalDate.now().atTime(23, 59, 59).atZone(ZoneOffset.of("+8")).toInstant());
        if (temp) {
            redisTemplate.expireAt(key, expirationDate);
        }
        //向前补齐0
        String format = String.format("%03d", result);
        SimpleDateFormat spd = new SimpleDateFormat("yyyyMMdd");
        //当前年月日
        String nowDate = spd.format(expirationDate);
        return selfrowingEnum.getReturnsThePrefix() + nowDate  + format;
    }


}
