Houjie
2025-06-30 c34fc83655b146b40802e1fce22db37b7367c83d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
 * @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 synchronized 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();
    }
}