hyingbo
2025-07-10 7feb2d0ebb7c6899cc654ca4ca88416b7d5ffdd0
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package org.jeecg.modules.dnc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.dnc.entity.Cutter;
import org.jeecg.modules.dnc.entity.DocFile;
import org.jeecg.modules.dnc.entity.DocInfo;
import org.jeecg.modules.dnc.entity.GuideCardBatch;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.CutterMapper;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.service.ICutterService;
import org.jeecg.modules.dnc.service.IDocFileService;
import org.jeecg.modules.dnc.service.IDocInfoService;
import org.jeecg.modules.dnc.service.IGuideCardBatchService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.dnc.utils.file.FileUtilS;
import org.jeecg.modules.tms.entity.PreparationOrderDetail;
import org.jeecg.modules.tms.entity.dto.PreparationOrderAndDetailDto;
import org.jeecg.modules.tms.service.IPreparationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class CutterServiceImpl extends ServiceImpl<CutterMapper, Cutter> implements ICutterService {
 
    @Autowired
    private IDocInfoService docInfoService;
 
    @Autowired
    private IDocFileService docFileService;
 
    @Autowired
    private IGuideCardBatchService guideCardBatchService;
 
    @Autowired
    private IPreparationOrderService preparationOrderService;
    /**
     * 新增刀具信息
     * @param cutter
     * @return
     */
    @Override
    public Result<?> add(Cutter cutter){
        if(cutter == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(cutter.getAttributionId()))
            Result.error("无效的刀具");
        List<Cutter> cutterList =this.checkCutterNo(cutter);
        if (cutterList != null && !cutterList.isEmpty()) {
            return Result.error("已存在相同的刀具编号");
        }
        boolean save = this.save(cutter);
        if(save){
            return Result.OK("添加刀具成功");
        }
        return Result.error("新增刀具失败");
    }
 
    /**
     * 编辑刀具信息
     * @param cutter
     * @return
     */
    @Override
    public Result<?> edit(Cutter cutter){
        // 检查传入的刀具对象是否为空
        if (cutter == null) {
           return Result.OK("刀具对象不能为空");
        }
        // 检查刀具名称是否有效
        if (!ValidateUtil.validateString(cutter.getCutterName())) {
            return Result.OK("刀具名称无效");
        }
        // 根据刀具 ID 获取刀具信息
        Cutter existingCutter = super.getById(cutter.getId());
        if (existingCutter == null) {
            return Result.OK("刀具不存在");
        }
        // 过滤掉当前要编辑的刀具,检查是否有其他刀具存在相同编号
        List<Cutter> otherCuttersWithSameNo = this.checkCutterNo(cutter).stream()
                .filter(cut -> !cut.getId().equals(cutter.getId()))
                .collect(Collectors.toList());
        if (!otherCuttersWithSameNo.isEmpty()) {
            // 如果存在除当前刀具外的其他刀具编号重复
            return Result.error("已存在相同的刀具编号");
        }
        // 尝试更新刀具信息
        boolean updated = this.updateById(cutter);
        if (updated) {
            return Result.OK("刀具信息编辑成功");
        } else {
            return Result.error("刀具信息编辑失败");
        }
    }
 
    /**
     * 删除刀具信息
     * @param id
     * @return
     */
    @Override
    public Result<?> delete(String id){
        if(!ValidateUtil.validateString(id))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        Cutter en = super.getById(id);
        if(en == null)
            return Result.error("无效的刀具");
        boolean b=super.removeById(id);
        if(!b) {
            return Result.error("删除刀具失败");
        }
        return Result.OK("删除刀具成功");
    }
 
    /**
     * 验证同结构下刀具编号是否重复
     * @param cutter
     * @return
     */
    public List<Cutter> checkCutterNo(Cutter cutter){
        QueryWrapper<Cutter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getAttributionId()),"attribution_id",cutter.getAttributionId());
        queryWrapper.eq("attribution_type",cutter.getAttributionType());
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getCutterCode()),"cutter_code",cutter.getCutterCode());
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getCutterType()),"cutter_type",cutter.getCutterType());
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getCutterSpacing()),"cutter_spacing",cutter.getCutterSpacing());
        return baseMapper.selectList(queryWrapper);
    }
 
    /**
     * 获取业务id下的刀具列表
     * @param cutter
     * @return
     */
    @Override
    public Result<?> query(Cutter cutter, Integer pageNo, Integer pageSize){
        QueryWrapper<Cutter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getAttributionId()),"attribution_id",cutter.getAttributionId());
        if (cutter.getAttributionType() != null){
            queryWrapper.eq("attribution_type",cutter.getAttributionType());
        }
        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getDocId()),"doc_id",cutter.getDocId());
        queryWrapper.like(StrUtil.isNotEmpty(cutter.getCutterCode()),"cutter_code",cutter.getCutterCode());
        queryWrapper.like(StrUtil.isNotEmpty(cutter.getCutterName()),"cutter_name",cutter.getCutterName());
        queryWrapper.orderByDesc("create_time");
        Page<Cutter> page = new Page<>(pageNo,pageSize);
        IPage<Cutter> cutterIPage = baseMapper.selectPage(page, queryWrapper);
        return Result.OK(cutterIPage);
    }
 
    /**
     * 从NC文件内容提取刀具信息并保存
     */
    @Override
    @Transactional
    public Result<?> extractAndSaveFromContent(String docId,String attributionId,Integer attributionType){
        DocInfo docInfo=docInfoService.getById(docId);
        if (docInfo == null) {
            return Result.error("未找到对应文档信息,无法提取刀具信息");
        }
        docInfo.setAttributionId(attributionId);
        docInfo.setAttributionType(attributionType);
        DocFile docFile=docFileService.getById(docInfo.getPublishFileId());
        if (docFile == null) {
            return Result.error("未找到对应文件信息,无法提取刀具信息");
        }
        String filePath = docFile.getFilePath();
        String fileEncodeName = docFile.getFileEncodeName();
        //文档内容
        List<String> list = FileUtilS.readFile(fileEncodeName, filePath);
        if (list == null || list.isEmpty()) {
            return Result.error("文档内容为空,无法提取刀具信息");
        }
        List<Cutter> cutterList = extractToolAfterM6(docInfo,list);
        // 保存刀具
        if (!cutterList.isEmpty()) {
            List<Cutter> newCutterList = new ArrayList<>();
            //验证刀具是否已经存在
            cutterList.forEach(item -> {
                List<Cutter> otherCuttersWithSameNo = checkCutterNo(item);
                if (otherCuttersWithSameNo == null || otherCuttersWithSameNo.isEmpty()) {
                    newCutterList.add(item);
                }
            });
            if (newCutterList.isEmpty()) {
                return Result.error("未发现刀具的参数信息注释,无法提取刀具信息");
            }
            this.saveBatch(newCutterList);
            return Result.OK("提取刀具信息成功");
        }else {
            return Result.error("未发现刀具的参数信息注释,无法提取刀具信息");
        }
    }
 
    @Override
    public Result<?> sendToCutterSystem(String docId,String attributionId,Integer attributionType){
        List<Cutter> cutterList = this.list(new QueryWrapper<Cutter>()
                .eq("doc_id", docId)
                .eq(StrUtil.isNotEmpty(attributionId),"attribution_id",attributionId)
                .eq("attribution_type",attributionType));
        if (cutterList == null || cutterList.isEmpty()) {
            return Result.error("未发现刀具信息,无法发送到刀具系统");
        }
        if (cutterList.stream().anyMatch(item -> item.getCutterCode() == null)) {
            return Result.error("未发现刀具编号信息,无法发送到刀具系统");
        }
        //获取最新数控程序加工确认表
        List<GuideCardBatch> guideCardBatchList = guideCardBatchService.list(new QueryWrapper<GuideCardBatch>()
                .eq("doc_id", docId)
                .isNotNull("serial_number")
                .orderByDesc("SUBSTRING(serial_number, LEN(serial_number)-3, 4)"));
        if (guideCardBatchList == null || guideCardBatchList.isEmpty()) {
            return Result.error("未发现程序加工确认表信息,无法发送到刀具系统");
        }
        GuideCardBatch guideCardBatch = guideCardBatchList.get(0);
        PreparationOrderAndDetailDto dto = new PreparationOrderAndDetailDto();
        dto.setPartDrawingNo(guideCardBatch.getPartsCode());
        dto.setPartName(guideCardBatch.getPartsName());
        dto.setPartMaterial(guideCardBatch.getMaterielDesp());
        dto.setProductionProcessesNo(guideCardBatch.getProcessWorkCode());
        dto.setBatchCode(guideCardBatch.getProcessingBatch());
        dto.setMachiningCount(guideCardBatch.getProcessingQuantity());
        dto.setEquipmentCode(guideCardBatch.getProcessingEquipment());
        dto.setNcName(guideCardBatch.getDocName());
        List<PreparationOrderDetail> detailList = new ArrayList<>();
        cutterList.forEach(item -> {
            PreparationOrderDetail detail = new PreparationOrderDetail();
            detail.setToolCode(item.getToolsId());
            detailList.add(detail);
        });
        dto.setPreparationOrderDetailList(detailList);
        preparationOrderService.addPreparationOrderFromDnc(dto);
        return Result.OK("发送到刀具系统成功");
    }
 
    public List<Cutter> extractToolAfterM6(DocInfo docInfo, List<String> ncLines) {
        List<Cutter> cutterList = new ArrayList<>();
        String currentToolCode = null; // 用于追踪当前换刀指令的刀具号
 
        for (String line : ncLines) {
            String trimmedLine = line.trim();
 
            // 1. 匹配 M6 换刀指令,提取 T代码(如 T01 M06 或 T 02 M06)
            if (trimmedLine.contains("M6")||trimmedLine.contains("M06")) {
                currentToolCode = extractToolCodeFromM6Line(trimmedLine);
            }
 
            // 2. 匹配刀具参数注释(紧跟在 M6 后的括号内容)
            if (currentToolCode != null && trimmedLine.startsWith("(") && trimmedLine.endsWith(")")) {
                String toolDescription = trimmedLine.substring(1, trimmedLine.length() - 1).trim();
                if (!toolDescription.isEmpty()) {
                    Cutter cutter = new Cutter();
                    cutter.setDocId(docInfo.getDocId());
                    cutter.setAttributionId(docInfo.getAttributionId());
                    cutter.setAttributionType(docInfo.getAttributionType());
                    cutter.setDescription(toolDescription);
 
                    // 从刀具描述中提取 cutterCode (例如从 "90E-10A" 中提取 "E")
                    extractToolInfoFromDescription(toolDescription, cutter);
 
                    // 设置刀具间距(使用T代码或其他逻辑)
                    cutter.setCutterSpacing(currentToolCode);
 
                    // 拆分刀具名称与规格(简单按空格分割,前部分为名称,后部分为规格)
                    String[] parts = toolDescription.split(" ", 2);
                    if (parts.length >= 1) {
                        cutter.setCutterName(parts[0]);
                    }
                    cutterList.add(cutter);
                    currentToolCode = null; // 重置,避免重复匹配
                }
            }
        }
 
        return cutterList;
    }
 
    /**
     * 从刀具描述中提取 cutterType 和 cutterCode
     * 例如: "8CH-90A" -> cutterType="CH", cutterCode="90A"
     */
    private void extractToolInfoFromDescription(String description, Cutter cutter) {
        // 提取刀具型号(一个或多个连续的大写字母)
        String cutterType = extractCutterType(description);
        cutter.setCutterType(cutterType);
        // 提取破折号后的规格部分
        String cutterSpec = "";
        int dashIndex = description.indexOf('-');
        if (dashIndex != -1 && dashIndex < description.length() - 1) {
            cutterSpec = description.substring(dashIndex + 1).trim();
            cutter.setCutterSpec(cutterSpec);
        }
    }
 
    /**
     * 提取刀具型号(一个或多个连续的大写字母)
     */
    private String extractCutterType(String description) {
        Pattern pattern = Pattern.compile("[A-Z]+");
        Matcher matcher = pattern.matcher(description);
 
        if (matcher.find()) {
            return matcher.group();
        }
 
        return description;
    }
 
    // 辅助方法:从 M6 行提取 T代码(支持 T01 或 T 01 格式)
    private String extractToolCodeFromM6Line(String line) {
        Matcher matcher = Pattern.compile("T(\\d+)").matcher(line);
        return matcher.find() ? "T" + matcher.group(1).trim() : null;
    }
}