package com.bonus.canteen.core.drp.utils;

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

import java.time.LocalDateTime;

public class CustomIdGenerateUtil {
   public static String generatePurchaseOrdId() {
      return generateFixedRuleId("purchaseOrderId:", "CGJH", 3, 3);
   }

   public static String generateIntoId() {
      return generateFixedRuleId("intoId:", "RKD", 3, 3);
   }

   public static String generateOutId() {
      return generateFixedRuleId("outId:", "CKD", 3, 3);
   }

   public static String generateCheckId() {
      return generateFixedRuleId("checkId:", "PDD", 3, 3);
   }

   public static String generateInquiryId() {
      return generateFixedRuleId("inquiryId:", "XJD", 3, 3);
   }

   public static String generateProductionId() {
      return generateFixedRuleId("productionId:", "SCJH", 3, 3);
   }

   public static String generateDeliveryGoodsId() {
      return generateFixedRuleId("deliveryGoodsId:", "SHD", 3, 3);
   }

   public static String generateOrderGoodsId() {
      return generateFixedRuleId("orderGoodsId:", "CGDD", 3, 3);
   }

   public static String generateMaterialInquiryId() {
      return generateFixedRuleId("materialInquiryId:", "HPXJD", 3, 3);
   }

   public static String generateInspectGoodsId() {
      return generateFixedRuleId("inspectGoodsId:", "YHD", 3, 3);
   }

   public static String generateRefundGoodsId() {
      return generateFixedRuleId("refundGoodsId:", "THD", 3, 3);
   }

   public static String generateFetchMaterialId() {
      return generateFixedRuleId("fetchMaterialId:", "LLD", 3, 3);
   }

   public static String generateGoodsAllocationId() {
      return generateFixedRuleId("goodsAllocationId", "HPDB", 3, 3);
   }

   public static String generateContractId() {
      return generateFixedRuleId("drpContractId:", "CGHT", 3, 3);
   }

   public static String generateAdjustmentOrderId() {
      return generateFixedRuleId("drpAdjustmentOrderId:", "TJD", 3, 3);
   }

   public static String generateMarketPriceId() {
      return generateFixedRuleId("drpAdjustmentOrderId:", "SCJ", 3, 3);
   }

   public static String generateFixedRuleId(String redisKey, String idPrefix, Integer randomLength, Integer length) {
      Long var10000 = TenantContextHolder.getTenantId();
      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);
      }
   }
}
