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
package org.jeecg.modules.dnc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.dnc.entity.GuideCardBatch;
import org.jeecg.modules.dnc.mapper.GuideCardBatchMapper;
import org.jeecg.modules.dnc.service.IGuideCardBatchService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.List;
 
/**
 * @Description: nc文件对应数控程序加工确认表
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class GuideCardBatchServiceImpl extends ServiceImpl<GuideCardBatchMapper, GuideCardBatch> implements IGuideCardBatchService {
 
    @Autowired
    private ISysDictService sysDictService;
 
    /**
     * 生成流水号
     * @param code
     * @return
     */
    @Override
    public String getSerialNumber(String code) {
        // 事件编号格式:年份后两位 + 单位编码 + 操作工账号 + 流水号(4位)
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
 
        // 获取当前年份后两位
        String yearSuffix = DateUtils.formatDate(new Date(), "yy");
 
        // 查询当年所有记录
        QueryWrapper<GuideCardBatch> wrapper = new QueryWrapper<>();
        wrapper.likeRight("create_time", DateUtils.formatDate(new Date(), "yyyy"));
        wrapper.isNotNull("serial_number");
        wrapper.orderByDesc("SUBSTR(serial_number, -4)");
 
        List<GuideCardBatch> list = this.list(wrapper);
 
        // 生成流水号逻辑
        String serialSuffix;
        if (!list.isEmpty()) {
            // 提取最新流水号的后四位
            String lastSerial = list.get(0).getSerialNumber();
            String lastSuffix = lastSerial.substring(lastSerial.length() - 4);
 
            // 流水号自增(处理9999溢出)
            int nextNum = Integer.parseInt(lastSuffix) + 1;
            serialSuffix = String.format("%04d", nextNum > 9999 ? 1 : nextNum); // 超过9999则重置为0001
        } else {
            serialSuffix = "0001"; // 当年无记录则初始化
        }
 
        // 获取单位编码
        String unitCode = sysDictService.queryDictTextByKey("unit_code", code);
 
        // 拼接完整编号
        return yearSuffix + unitCode + user.getUsername() + serialSuffix;
    }
}