zhangherong
2025-03-19 103f52ade0f77e420dec306ea3b51d0a3c0ac3ee
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();
    }
}