package com.bonus.canteen.core.supply.purchase.utils;

import cn.hutool.core.date.DateUtil;
import com.bonus.canteen.core.common.utils.RedisUtil;
import com.bonus.canteen.core.common.utils.SpringContextHolder;
import com.bonus.common.houqin.constant.GlobalConstants;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDateTime;

public class CustomIdGenerateUtil {
   public static String generateInspectGoodsId() {
      return generateFixedRuleId("inspectGoodsId:", "YHD", 3, 3);
   }
   public static String generateProductionId() {
      return generateFixedRuleId("productionId:", "SCJH", 3, 3);
   }
   public static String generatePurchaseOrdId() {
      return generateFixedRuleId("purchaseOrderId:", "CGJH", 3, 3);
   }
   public static String generateContractId() {
      return generateFixedRuleId("drpContractId:", "CGHT", 3, 3);
   }
   public static String generateMarketPriceId() {
      return generateFixedRuleId("drpAdjustmentOrderId:", "SCJ", 3, 3);
   }
   public static String generateOrderGoodsId() {
      return generateFixedRuleId("orderGoodsId:", "CGDD", 3, 3);
   }
   public static String generateInquiryId() {
      return generateFixedRuleId("inquiryId:", "XJD", 3, 3);
   }

   public static String generateFixedRuleId(String redisKey, String idPrefix, Integer randomLength, Integer length) {
      Long var10000 = GlobalConstants.TENANT_ID;
      redisKey = "ystKey:" + var10000 + ":" + redisKey;
      Long incr = RedisUtil.getIncr(redisTemplate(), redisKey);
      return idPrefix + DateUtil.format(LocalDateTime.now(), "yyyyMMdd") + getRandomDigit(randomLength) + (length <= 0 ? "" : String.format("%0" + length + "d", incr));
   }

   private static RedisTemplate<Object, Object> redisTemplate() {
      return SpringContextHolder.getBean("redisTemplate");
   }

   public static String getRandomDigit(int length) {
      if (length <= 0) {
         return "";
      } else {
         int num = (int)(Math.random() * Math.pow(10.0, (double)length));
         return String.format("%0" + length + "d", num);
      }
   }
}
