wuhb 2 месяцев назад
Родитель
Сommit
2d617a4e68

+ 11 - 1
ygtx-system/src/main/java/com/ygtx/system/strategy/AutoCodeUtil.java

@@ -18,6 +18,7 @@ import org.springframework.util.ObjectUtils;
 
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
 @Service
@@ -63,9 +64,13 @@ public class AutoCodeUtil {
         Assert.isTrue(collect.size()<2,"编码规则[{}]流水号方式的组成只能存在一个",ruleCode);
 
         StringBuilder buff = new StringBuilder();
+        AtomicReference<String> lastKey = new AtomicReference<>("");
         parts.forEach(codePart ->{
-            codePart.setInputCharacter(inputCharacter);
+            if(StringUtils.equals(codePart.getPartType(),PartTypeEnum.PART_TYPE_SERIALNO.getCode())){
+                codePart.setInputCharacter(lastKey.get());
+            }
             codePart.setBizData(data);
+
             //根据当前组成部分,获取当前组成部分的结果
             String partStr = partTypeHandler.choiceExecute(codePart);
 
@@ -74,6 +79,9 @@ public class AutoCodeUtil {
                 lastSerialNo = partStr;
             }
             //将获取到的部分组装进整体编码中
+            if(!StringUtils.equals(codePart.getPartType(),PartTypeEnum.PART_TYPE_SERIALNO.getCode())) {
+                lastKey.set(partStr);
+            }
             buff.append(partStr);
 
         });
@@ -86,6 +94,7 @@ public class AutoCodeUtil {
 
         String autoCode = paddingStr(rule,buff);
 
+        inputCharacter = lastKey.get();
         //将生成结果保存到数据库
         saveAutoCodeResult(rule,autoCode,inputCharacter);
         return autoCode;
@@ -143,6 +152,7 @@ public class AutoCodeUtil {
                 //直接更新对应的记录(我们默认非流水号模式下一个RULE_CODE只有一种方式)
                 SysAutoCodeResult bo = new SysAutoCodeResult();
                 bo.setRuleId(rule.getRuleId());
+                bo.setLastInputChar(inputChar);
                 List<SysAutoCodeResult> results = iAutoCodeResultService.list(bo);
                 if(!existData){
                     Assert.notEmpty(results, "未查询到规则{[]}对应的结果记录", rule.getRuleCode());

+ 8 - 4
ygtx-system/src/main/java/com/ygtx/system/strategy/PartTypeSerialNoHandler.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
 import com.ygtx.common.core.domain.entity.SysAutoCodePart;
 import com.ygtx.common.core.domain.entity.SysAutoCodeResult;
 import com.ygtx.common.enums.CycleMethodMnum;
+import com.ygtx.common.utils.StringUtils;
 import com.ygtx.system.service.IAutoCodeResultService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.annotation.Order;
@@ -57,7 +58,7 @@ public class PartTypeSerialNoHandler implements PartTypeTemplate {
             }
         }
 
-        List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart.getRuleId(),param,method);
+        List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart,param,method);
         if(CollectionUtil.isNotEmpty(rs)){
             //如果在编码记录表中有记录,则在最后一个流水号上加上步长,返回新的流水号
             AutoCodeUtil.threadLocal.set(false);
@@ -72,7 +73,7 @@ public class PartTypeSerialNoHandler implements PartTypeTemplate {
 
     //不循环方式
     private String cycleN(SysAutoCodePart sysAutoCodePart){
-        List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart.getRuleId(),"","");
+        List<SysAutoCodeResult> rs = getAutoCodeResult(sysAutoCodePart,"","");
         if(CollectionUtil.isNotEmpty(rs)){
             //存在记录则在当前记录加上步长
             Integer lastSerialNo = rs.get(0).getLastSerialNo();
@@ -89,15 +90,18 @@ public class PartTypeSerialNoHandler implements PartTypeTemplate {
     }
 
     //从编码结果记录表中查找当前指定循环规则的流水号记录
-    private List<SysAutoCodeResult> getAutoCodeResult(Long ruleId,String param,String cycleMethod){
+    private List<SysAutoCodeResult> getAutoCodeResult(SysAutoCodePart sysAutoCodePart,String param,String cycleMethod){
         SysAutoCodeResult queryParam = new SysAutoCodeResult();
-        queryParam.setRuleId(ruleId);//ruleId要一致
+        queryParam.setRuleId(sysAutoCodePart.getRuleId());//ruleId要一致
 
         if(CycleMethodMnum.CYCLE_METHOD_OTHER.getCode().equals(cycleMethod)){
             //如果循环方式是手工输入指定的字符
             queryParam.setLastInputChar(param);
         }else{
             //如果循环方式是按格式化的日期
+            if(StringUtils.isNotEmpty(sysAutoCodePart.getInputCharacter())){
+                queryParam.setLastInputChar(sysAutoCodePart.getInputCharacter());
+            }
             queryParam.setGenDate(param);//这里的param将按照 gen_date like #{param}+'%' 的方式进行模糊查询,数据库中记录的永远都是yyyMMddHHmmss格式的
         }
         return iAutoCodeResultService.list(queryParam);