|
@@ -0,0 +1,147 @@
|
|
|
+package com.fjhx.tenant.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.fjhx.tenant.entity.dict.po.DictCommonData;
|
|
|
+import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
|
|
|
+import com.fjhx.tenant.service.dict.DictCommonDataService;
|
|
|
+import com.fjhx.tenant.service.dict.DictTenantDataService;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.Setter;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 字典编码规则工具
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class DictCodingService {
|
|
|
+
|
|
|
+ private final DictTenantDataService dictTenantDataService;
|
|
|
+ private final DictCommonDataService dictCommonDataService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public DictCodingService(DictTenantDataService dictTenantDataService, DictCommonDataService dictCommonDataService) {
|
|
|
+ this.dictTenantDataService = dictTenantDataService;
|
|
|
+ this.dictCommonDataService = dictCommonDataService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ public synchronized String createCode(String dictCode, String dictKey) {
|
|
|
+ StringBuilder codeBuilder = new StringBuilder();
|
|
|
+
|
|
|
+ DictTenantDataVo dictTenantDataVo = DictUtils.getDictDataMap(dictCode).get(dictKey);
|
|
|
+ List<RuleVo> ruleVoList = JSON.parseArray(dictTenantDataVo.getRuleJson(), RuleVo.class);
|
|
|
+ if (ruleVoList.size() == 0) {
|
|
|
+ throw new ServiceException("编码生成规则为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (RuleVo ruleVo : ruleVoList) {
|
|
|
+ Integer ruleType = ruleVo.getRuleType();
|
|
|
+ RuleTypeEnum ruleTypeEnum = RuleTypeEnum.getRuleTypeEnum(ruleType);
|
|
|
+
|
|
|
+ switch (ruleTypeEnum) {
|
|
|
+ case CUSTOMIZE:
|
|
|
+ codeBuilder.append(ruleVo.getValue());
|
|
|
+ break;
|
|
|
+ case AUTOINCREMENT:
|
|
|
+ String value = ruleVo.getValue();
|
|
|
+
|
|
|
+ dictTenantDataVo.setRuleNum(dictTenantDataVo.getRuleNum() + 1);
|
|
|
+ if(ObjectUtil.equals(dictTenantDataVo.getType(),1)) {
|
|
|
+ DictCommonData dictCommonData = BeanUtil.copyProperties(dictTenantDataVo, DictCommonData.class);
|
|
|
+ dictCommonDataService.updateById(dictCommonData);
|
|
|
+ }else {
|
|
|
+ dictTenantDataService.updateById(dictTenantDataVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ codeBuilder.append(autoGenericCode(value, dictTenantDataVo.getRuleNum()));
|
|
|
+ break;
|
|
|
+ case DATE_FORMAT:
|
|
|
+ String dataFormat = ruleVo.getValue();
|
|
|
+ try {
|
|
|
+ String format = DateUtil.format(new Date(), dataFormat);
|
|
|
+ codeBuilder.append(format);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("生成编码配置日期生成格式错误");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return codeBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不够位数的在前面补0,保留num的长度位数字
|
|
|
+ */
|
|
|
+ private static String autoGenericCode(Object length, Integer codeNum) {
|
|
|
+ Integer integer = Convert.toInt(length);
|
|
|
+ if (integer == null) {
|
|
|
+ throw new ServiceException("自定义编码长度不为数字");
|
|
|
+ }
|
|
|
+
|
|
|
+ return String.format("%0" + integer + "d", codeNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 规则实体
|
|
|
+ */
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ private class RuleVo {
|
|
|
+ /**
|
|
|
+ * 编码规则
|
|
|
+ */
|
|
|
+ private Integer ruleType;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 值
|
|
|
+ */
|
|
|
+ private String value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 规则枚举
|
|
|
+ */
|
|
|
+ @Getter
|
|
|
+ @AllArgsConstructor
|
|
|
+ private enum RuleTypeEnum {
|
|
|
+
|
|
|
+ CUSTOMIZE(1, "自定义字符"),
|
|
|
+ AUTOINCREMENT(2, "自增编号"),
|
|
|
+ DATE_FORMAT(3, "日期");
|
|
|
+
|
|
|
+ private final Integer type;
|
|
|
+
|
|
|
+ private final String name;
|
|
|
+
|
|
|
+ private static final Map<Integer, RuleTypeEnum> map = new HashMap<>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ for (RuleTypeEnum value : RuleTypeEnum.values()) {
|
|
|
+ map.put(value.getType(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static RuleTypeEnum getRuleTypeEnum(Integer type) {
|
|
|
+ RuleTypeEnum ruleTypeEnum = map.get(type);
|
|
|
+ if (ruleTypeEnum == null) {
|
|
|
+ throw new ServiceException("编码规则类型不存在");
|
|
|
+ }
|
|
|
+ return ruleTypeEnum;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|