From 103f52ade0f77e420dec306ea3b51d0a3c0ac3ee Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期三, 19 三月 2025 13:23:12 +0800
Subject: [PATCH] art: 设备管理-系统业务编码生成,工艺参数维护功能修改

---
 lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysBusinessCodeRuleServiceImpl.java |  111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 1 deletions(-)

diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysBusinessCodeRuleServiceImpl.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysBusinessCodeRuleServiceImpl.java
index 3a3505e..ea9e380 100644
--- a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysBusinessCodeRuleServiceImpl.java
+++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysBusinessCodeRuleServiceImpl.java
@@ -1,19 +1,128 @@
 package org.jeecg.modules.system.service.impl;
 
+import cn.hutool.core.collection.CollectionUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import org.apache.commons.lang3.StringUtils;
+import org.jeecg.common.constant.CommonConstant;
+import org.jeecg.common.exception.JeecgBootException;
 import org.jeecg.modules.system.entity.SysBusinessCodeRule;
+import org.jeecg.modules.system.entity.SysBusinessCodeSeq;
 import org.jeecg.modules.system.mapper.SysBusinessCodeRuleMapper;
 import org.jeecg.modules.system.service.ISysBusinessCodeRuleService;
+import org.jeecg.modules.system.service.ISysBusinessCodeSeqService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
 
 /**
  * @Description: 涓氬姟缂栫爜瑙勫垯
  * @Author: jeecg-boot
- * @Date:   2025-03-17
+ * @Date: 2025-03-17
  * @Version: V1.0
  */
 @Service
 public class SysBusinessCodeRuleServiceImpl extends ServiceImpl<SysBusinessCodeRuleMapper, SysBusinessCodeRule> implements ISysBusinessCodeRuleService {
 
+    @Resource
+    private SysBusinessCodeRuleMapper businessCodeRuleMapper;
+
+    @Autowired
+    private ISysBusinessCodeSeqService businessCodeSeqService;
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public String generateBusinessCodeSeq(String businessCode) {
+        //鏌ヨ鐢熸垚瑙勫垯
+        LambdaQueryWrapper<SysBusinessCodeRule> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SysBusinessCodeRule::getBusinessCode, businessCode);
+        queryWrapper.eq(SysBusinessCodeRule::getDelFlag, CommonConstant.DEL_FLAG_0);
+        List<SysBusinessCodeRule> list = businessCodeRuleMapper.selectList(queryWrapper);
+        if (CollectionUtil.isEmpty(list)) {
+            throw new JeecgBootException("鏈煡璇㈠埌閰嶇疆鐨勪笟鍔$紪鐮佺敓鎴愯鍒欙紝璇疯仈绯荤鐞嗗憳妫�鏌ワ紒");
+        }
+        if (list.size() > 1) {
+            throw new JeecgBootException("鏌ヨ鍒板涓厤缃殑涓氬姟缂栫爜鐢熸垚瑙勫垯锛岃鑱旂郴绠$悊鍛樻鏌ワ紒");
+        }
+        SysBusinessCodeRule businessCodeRule = list.get(0);
+        String prefix = businessCodeRule.getPrefix();
+        String yearFormat = businessCodeRule.getYearFormat() == null ? "" : businessCodeRule.getYearFormat().trim();
+        String monthFormat = businessCodeRule.getMonthFormat() == null ? "" : businessCodeRule.getMonthFormat().trim();
+        String dayFormat = businessCodeRule.getDayFormat() == null ? "" : businessCodeRule.getDayFormat().trim();
+        Integer seqLength = businessCodeRule.getSeqLength();
+        String formatter = yearFormat + "-" + monthFormat + "-" + dayFormat;
+        //杞崲褰撳墠鏃ユ湡瀵瑰簲鐨勭敓鎴愬簭鍒�
+        String[] currentDate = LocalDate.now().format(DateTimeFormatter.ofPattern(formatter)).split("-");
+        int i = 0;
+        if (StringUtils.isNotBlank(yearFormat)) {
+            yearFormat = currentDate[i];
+            i++;
+        }else {
+            yearFormat = null;
+        }
+        if (StringUtils.isNotBlank(monthFormat)) {
+            monthFormat = currentDate[i];
+            i++;
+        }else {
+            monthFormat = null;
+        }
+        if (StringUtils.isNotBlank(dayFormat)) {
+            dayFormat = currentDate[i];
+        }else {
+            dayFormat = null;
+        }
+        //鏌ヨ鐢熸垚搴忓垪
+        SysBusinessCodeSeq sysBusinessCodeSeq = businessCodeSeqService.queryByParams(businessCodeRule.getId(), prefix, yearFormat, monthFormat, dayFormat);
+        if(sysBusinessCodeSeq == null){
+            //鏂板鐢熸垚搴忓垪
+            sysBusinessCodeSeq = new SysBusinessCodeSeq();
+            sysBusinessCodeSeq.setRuleId(businessCodeRule.getId());
+            sysBusinessCodeSeq.setPrefix(prefix);
+            sysBusinessCodeSeq.setCurrentYear(yearFormat);
+            sysBusinessCodeSeq.setCurrentMonth(monthFormat);
+            sysBusinessCodeSeq.setCurrentDay(dayFormat);
+            sysBusinessCodeSeq.setCurrentSeq(1);
+            businessCodeSeqService.save(sysBusinessCodeSeq);
+        }else {
+            //鏇存柊鐢熸垚搴忓垪
+            sysBusinessCodeSeq.setCurrentSeq(sysBusinessCodeSeq.getCurrentSeq() + 1);
+            businessCodeSeqService.updateById(sysBusinessCodeSeq);
+        }
+        //杩斿洖鐢熸垚鐨勫簭鍒�
+        return appendPrefix(prefix, yearFormat, monthFormat, dayFormat, seqLength, sysBusinessCodeSeq.getCurrentSeq());
+    }
+
+    /**
+     * 鎷兼帴涓氬姟娴佹按鍙�
+     * @param prefix 鍓嶇紑
+     * @param yearFormat 骞翠唤
+     * @param monthFormat 鏈堜唤
+     * @param dayFormat 澶�
+     * @param seqLength 搴忓彿闀垮害
+     * @param currentSeq 褰撳墠搴忓彿
+     * @return
+     */
+    private String appendPrefix(String prefix, String yearFormat, String monthFormat, String dayFormat, Integer seqLength, Integer currentSeq) {
+        StringBuilder sb = new StringBuilder();
+        if(StringUtils.isNotBlank(prefix)){
+            sb.append(prefix);
+        }
+        if(StringUtils.isNotBlank(yearFormat)){
+            sb.append(yearFormat);
+        }
+        if(StringUtils.isNotBlank(monthFormat)){
+            sb.append(monthFormat);
+        }
+        if(StringUtils.isNotBlank(dayFormat)){
+            sb.append(dayFormat);
+        }
+        sb.append(StringUtils.leftPad(currentSeq.toString(), seqLength, '0'));
+        return sb.toString();
+    }
 }

--
Gitblit v1.9.3