Переглянути джерело

Merge remote-tracking branch 'origin/master'

wudingsheng 1 тиждень тому
батько
коміт
4a9e06608a

+ 1 - 1
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/cutterdata/service/impl/CutterTestDataServiceImpl.java

@@ -67,7 +67,7 @@ public class CutterTestDataServiceImpl extends SuperServiceImpl<CutterTestDataMa
     @Value("${cutter-shared.file-path}")
     private String CutterSharedPath;
 
-    @Value("${cutter-shared.file-ip:192.168.0.116}")
+    @Value("${cutter-shared.file-ip")
     private String CutterSharedIp;
 
     @Value("${imcs-to-ccs.enabled:true}")

+ 13 - 0
imcs-admin-boot/imcs-business-controller/pom.xml

@@ -92,6 +92,19 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
         </dependency>
+
+        <!-- 远程文件读取(SMB协议) -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-vfs2</artifactId>
+            <version>2.9.0</version>
+        </dependency>
+        <dependency>
+            <groupId>jcifs</groupId>
+            <artifactId>jcifs</artifactId>
+            <version>1.3.17</version>
+        </dependency>
+
     </dependencies>
 
 

+ 167 - 5
imcs-admin-boot/imcs-business-controller/src/main/java/com/github/zuihou/business/controller/cutterdata/CutterTestDataController.java

@@ -1,6 +1,7 @@
 package com.github.zuihou.business.controller.cutterdata;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.zuihou.base.R;
 import com.github.zuihou.base.controller.SuperController;
 import com.github.zuihou.base.request.PageParams;
@@ -10,10 +11,18 @@ import com.github.zuihou.business.cutterdata.service.CutterTestDataService;
 import com.github.zuihou.business.productionReadyCenter.entity.CuttingTool;
 import com.github.zuihou.database.mybatis.conditions.query.LbqWrapper;
 import com.github.zuihou.database.mybatis.conditions.query.QueryWrap;
+import com.github.zuihou.exception.BizException;
 import com.github.zuihou.log.annotation.SysLog;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import jcifs.smb.SmbFile;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.FileSystemManager;
+import org.apache.commons.vfs2.FileSystemOptions;
+import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
+import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -21,6 +30,18 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
 /**
  * @Project: imcs-admin-boot
  * @Package: com.github.zuihou.business.controller.cutterdata
@@ -40,15 +61,15 @@ public class CutterTestDataController extends SuperController<CutterTestDataServ
     @Autowired
     private CutterTestDataService cutterTestDataService;
 
-
     @ApiOperation(value = "获取机外对刀仪数据", notes = "获取机外对刀仪数据")
     @PostMapping("/getCutterPage")
     public R<IPage<CutterTestDataEntity>> getCutterPage(@RequestBody @Validated PageParams<CutterTestDataEntity> params) {
+        // 第一步:读取远程CSV最后两条数据并入库
+        readRemoteCsvAndSave();
 
+        // 第二步:按原有逻辑查询数据库(保持不变)
         IPage<CutterTestDataEntity> page = params.buildPage();
-
         QueryWrap<CutterTestDataEntity> wrap = handlerWrapper(null, params);
-
         LbqWrapper<CutterTestDataEntity> wrapper = wrap.lambda();
 
         wrapper.like(CutterTestDataEntity::getCutterT, params.getModel().getCutterT())
@@ -57,12 +78,153 @@ public class CutterTestDataController extends SuperController<CutterTestDataServ
                 .like(CutterTestDataEntity::getCutterP, params.getModel().getCutterP())
                 .like(CutterTestDataEntity::getCutterPIp, params.getModel().getCutterPIp())
                 .orderByDesc(CutterTestDataEntity::getUpdateTime);
-        //查询弹框异常
-        IPage<CutterTestDataEntity> cutterTestDataDtoIPage = cutterTestDataService.pageList(page, wrapper);
 
+        IPage<CutterTestDataEntity> cutterTestDataDtoIPage = cutterTestDataService.pageList(page, wrapper);
         return R.success(cutterTestDataDtoIPage);
     }
 
+    /**
+     * 读取远程CSV文件最后两条数据并入库(修正版)
+     */
+    private void readRemoteCsvAndSave() {
+        // 远程共享信息
+        String username = "omega";
+        String password = "123456";
+        String host = "Desktop-g07o4f4"; // 主机名或IP(192.168.11.125)
+        String shareName = "p8"; // 共享文件夹名
+        String filePath = "RFIDWrite.csv"; // 文件名
+
+        // 构建SMB路径(无密码时直接省略密码部分或留空)
+        String smbPath;
+        if (password.isEmpty()) {
+            // 无密码格式:smb://用户名@主机/共享名/文件名
+            smbPath = String.format("smb://%s@%s/%s/%s",
+                    username, host, shareName, filePath);
+        } else {
+            // 有密码格式(备用)
+            smbPath = String.format("smb://%s:%s@%s/%s/%s",
+                    username, password, host, shareName, filePath);
+        }
+
+        try {
+            // 连接远程文件
+            SmbFile smbFile = new SmbFile(smbPath);
+            if (!smbFile.exists()) {
+                log.error("文件不存在:{}", smbPath);
+                throw new RuntimeException("文件不存在");
+            }
+
+            // 读取文件内容
+            List<String> allLines = new ArrayList<>();
+            try (InputStream is = smbFile.getInputStream();
+                 BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
+                String line;
+                while ((line = br.readLine()) != null) {
+                    allLines.add(line);
+                }
+            }
+
+            // 处理最后两条数据(逻辑不变)
+            List<String> dataLines = allLines.stream()
+                    .skip(1) // 跳过表头
+                    .filter(line -> !line.trim().isEmpty())
+                    .collect(Collectors.toList());
+
+            if (dataLines.size() >= 2) {
+                saveCutterData(dataLines.get(dataLines.size() - 2));
+                saveCutterData(dataLines.get(dataLines.size() - 1));
+            }
+
+        } catch (Exception e) {
+            log.error("读取SMB文件失败", e);
+            throw new RuntimeException("获取对刀仪数据失败:" + e.getMessage());
+        }
+    }
+
+    /**
+     * 解析CSV行数据并保存到数据库
+     */
+    private void saveCutterData(String csvLine) {
+        String[] fields = csvLine.split(",");
+        if (fields.length < 14) {
+            log.warn("CSV数据格式异常:{}", csvLine);
+            return;
+        }
+
+        // 解析CSV字段(按索引对应)
+        String fileTime = fields[0].trim(); // 创建时间
+        String cutterName = fields[4].trim(); // 刀具名
+        String featureName = fields[7].trim(); // 特征名称(DC1=直径,LF1=长度)
+        String featureValue = fields[11].trim(); // 特征已测量值
+
+        // 1. 格式化测量值为三位小数
+        String formattedValue = formatToThreeDecimals(featureValue);
+
+        // 构建实体类
+        CutterTestDataEntity entity = CutterTestDataEntity.builder()
+                .filePath("\\\\10.0.251.131\\p8\\RFIDWrite.csv")
+                .fileTime(fileTime)
+                .cutterT(cutterName) // 刀柄号=刀具名(根据实际业务调整)
+                .cutterName(cutterName)
+                .cutterTyp("机外对刀仪") // 刀具类型
+                .status(2) // 状态:2-未推送机床(默认值)
+                .build();
+
+        // 区分直径和长度
+        if ("DC1".equals(featureName)) {
+            entity.setCutterXl(formattedValue); // DC1=直径,存入cutterXl
+        } else if ("LF1".equals(featureName)) {
+            entity.setCutterZl(formattedValue); // LF1=长度,存入cutterZl
+        }
+
+        // 保存到数据库(存在则更新,不存在则新增)
+        cutterTestDataService.saveOrUpdate(entity,
+                Wrappers.<CutterTestDataEntity>lambdaQuery()
+                        .eq(CutterTestDataEntity::getCutterName, cutterName)
+                        .eq(CutterTestDataEntity::getFileTime, fileTime)
+                        .eq(CutterTestDataEntity::getCutterTyp, "机外对刀仪")
+        );
+    }
+    /**
+     * 将数值格式化保留三位小数
+     * @param value 原始数值字符串(如 "15.967869252786")
+     * @return 格式化后的字符串(如 "15.968")
+     */
+    private String formatToThreeDecimals(String value) {
+        try {
+            // 转换为Double,保留三位小数(四舍五入)
+            double num = Double.parseDouble(value);
+            // 使用DecimalFormat格式化,确保三位小数(如 15.9 → 15.900)
+            DecimalFormat df = new DecimalFormat("#.000");
+            return df.format(num);
+        } catch (NumberFormatException e) {
+            log.warn("数值格式化失败,原始值:{}", value, e);
+            return value; // 格式化失败时返回原始值
+        }
+    }
+
+//    @ApiOperation(value = "获取机外对刀仪数据", notes = "获取机外对刀仪数据")
+//    @PostMapping("/getCutterPage")
+//    public R<IPage<CutterTestDataEntity>> getCutterPage(@RequestBody @Validated PageParams<CutterTestDataEntity> params) {
+//
+//        IPage<CutterTestDataEntity> page = params.buildPage();
+//
+//        QueryWrap<CutterTestDataEntity> wrap = handlerWrapper(null, params);
+//
+//        LbqWrapper<CutterTestDataEntity> wrapper = wrap.lambda();
+//
+//        wrapper.like(CutterTestDataEntity::getCutterT, params.getModel().getCutterT())
+//                .like(CutterTestDataEntity::getCutterTyp, params.getModel().getCutterTyp())
+//                .like(CutterTestDataEntity::getStatus, params.getModel().getStatus())
+//                .like(CutterTestDataEntity::getCutterP, params.getModel().getCutterP())
+//                .like(CutterTestDataEntity::getCutterPIp, params.getModel().getCutterPIp())
+//                .orderByDesc(CutterTestDataEntity::getUpdateTime);
+//        //查询弹框异常
+//        IPage<CutterTestDataEntity> cutterTestDataDtoIPage = cutterTestDataService.pageList(page, wrapper);
+//
+//        return R.success(cutterTestDataDtoIPage);
+//    }
+
     @ApiOperation(value = "定时查询rfid,根据rfid查询组刀对照的表", notes = "定时查询rfid,根据rfid查询组刀对照的表")
     @PostMapping("/getCutterByRfid")
     public R<CuttingTool> getCutterByRfid(@RequestBody AuthUser authUser) {