From ec1d61de19f0fe6c3efc781e5d7a6af2ed65ae5d Mon Sep 17 00:00:00 2001
From: lyh <925863403@qq.com>
Date: 星期二, 10 六月 2025 10:51:49 +0800
Subject: [PATCH] DNC 文档导入、回传文档、解析文档、取消原有设备

---
 lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/CutterServiceImpl.java |  221 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 192 insertions(+), 29 deletions(-)

diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/CutterServiceImpl.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/CutterServiceImpl.java
index d29c8c5..2d1c271 100644
--- a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/CutterServiceImpl.java
+++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/CutterServiceImpl.java
@@ -5,20 +5,42 @@
 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.exception.ExceptionCast;
 import org.jeecg.modules.dnc.mapper.CutterMapper;
 import org.jeecg.modules.dnc.response.CommonCode;
-import org.jeecg.modules.dnc.response.ProcessInfoCode;
 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.utils.ValidateUtil;
+import org.jeecg.modules.dnc.utils.file.FileUtilS;
+import org.jeecg.modules.system.service.ISysDictService;
+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 ISysDictService sysDictService;
 
     /**
      * 鏂板鍒�鍏蜂俊鎭�
@@ -29,11 +51,12 @@
     public Result<?> add(Cutter cutter){
         if(cutter == null)
             ExceptionCast.cast(CommonCode.INVALID_PARAM);
-        if(!ValidateUtil.validateString(cutter.getProcessStepId()))
+        if(!ValidateUtil.validateString(cutter.getAttributionId()))
             Result.error("鏃犳晥鐨勫垁鍏�");
-        boolean b =this.checkCutterNo(cutter);
-        if(!b)
+        List<Cutter> cutterList =this.checkCutterNo(cutter);
+        if (cutterList != null && !cutterList.isEmpty()) {
             return Result.error("宸插瓨鍦ㄧ浉鍚岀殑鍒�鍏风紪鍙�");
+        }
         boolean save = this.save(cutter);
         if(save){
             return Result.OK("娣诲姞鍒�鍏锋垚鍔�");
@@ -48,21 +71,34 @@
      */
     @Override
     public Result<?> edit(Cutter cutter){
-        if(cutter == null)
-            ExceptionCast.cast(CommonCode.INVALID_PARAM);
-        if(!ValidateUtil.validateString(cutter.getCutterName()))
-            ExceptionCast.cast(ProcessInfoCode.PROCESS_NAME_NONE);
-        Cutter en = super.getById(cutter.getId());
-        if(en == null)
-            ExceptionCast.cast(ProcessInfoCode.WORKSTEP_NOT_EXIST);
-        boolean b =this.checkCutterNo(cutter);
-        if(!b)
-            return Result.error("宸插瓨鍦ㄧ浉鍚岀殑鍒�鍏风紪鍙�");
-        boolean save = this.updateById(cutter);
-        if(save){
-            return Result.OK("鍒�鍏蜂俊鎭紪杈戞垚鍔�");
+        // 妫�鏌ヤ紶鍏ョ殑鍒�鍏峰璞℃槸鍚︿负绌�
+        if (cutter == null) {
+           return Result.OK("鍒�鍏峰璞′笉鑳戒负绌�");
         }
-        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("鍒�鍏蜂俊鎭紪杈戝け璐�");
+        }
     }
 
     /**
@@ -89,13 +125,14 @@
      * @param cutter
      * @return
      */
-    public boolean checkCutterNo(Cutter cutter){
+    public List<Cutter> checkCutterNo(Cutter cutter){
         QueryWrapper<Cutter> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getProcessStepId()),"process_step_id",cutter.getProcessStepId());
-        queryWrapper.eq("type",cutter.getType());
-        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getCutterCode()),"craft_code",cutter.getCutterCode());
-        List<Cutter> list = baseMapper.selectList(queryWrapper);
-        return list.isEmpty();
+        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);
     }
 
     /**
@@ -106,15 +143,141 @@
     @Override
     public Result<?> query(Cutter cutter, Integer pageNo, Integer pageSize){
         QueryWrapper<Cutter> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getProcessStepId()),"process_step_id",cutter.getProcessStepId());
-        if (cutter.getType() != null){
-            queryWrapper.eq("type",cutter.getType());
+        queryWrapper.eq(StrUtil.isNotEmpty(cutter.getAttributionId()),"attribution_id",cutter.getAttributionId());
+        if (cutter.getAttributionType() != null){
+            queryWrapper.eq("attribution_type",cutter.getAttributionType());
         }
-        queryWrapper.like(StrUtil.isNotEmpty(cutter.getCutterName()),"craft_code",cutter.getCutterCode());
-        queryWrapper.like(StrUtil.isNotEmpty(cutter.getCutterName()),"craft_name",cutter.getCutterName());
+        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);
     }
+
+    /**
+     * 浠嶯C鏂囦欢鍐呭鎻愬彇鍒�鍏蜂俊鎭苟淇濆瓨
+     */
+    @Override
+    @Transactional
+    public Result<?> extractAndSaveFromContent(String docId,String attributionId,Integer attributionType){
+        DocInfo docInfo=docInfoService.getById(docId);
+        if (docInfo == null) {
+            return Result.error("鏈壘鍒板搴旀枃妗d俊鎭紝鏃犳硶鎻愬彇鍒�鍏蜂俊鎭�");
+        }
+        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);
+            //TODO鍙戦�佸垁鍏风鐞嗘暟鎹�
+            return Result.OK("鎻愬彇鍒�鍏蜂俊鎭垚鍔�");
+        }else {
+            return Result.error("鏈彂鐜板垁鍏风殑鍙傛暟淇℃伅娉ㄩ噴锛屾棤娉曟彁鍙栧垁鍏蜂俊鎭�");
+        }
+    }
+
+    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浠g爜锛堝 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浠g爜鎴栧叾浠栭�昏緫锛�
+                    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浠g爜锛堟敮鎸� 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;
+    }
 }
+

--
Gitblit v1.9.3