ly 2 anni fa
parent
commit
8641d53916

+ 11 - 3
master/src/main/java/com/ruoyi/common/utils/file/ExcelUtils.java

@@ -1,11 +1,14 @@
 package com.ruoyi.common.utils.file;
 
+import com.ruoyi.common.core.controller.BaseController;
 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -23,6 +26,7 @@ import java.util.List;
  * Created by Administrator on 2018/7/26 0026.
  */
 public class ExcelUtils {
+    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
 
     public static Workbook getWorkBook(MultipartFile file) {
         //获得文件名
@@ -73,14 +77,14 @@ public class ExcelUtils {
         if (cell == null) {
             return cellValue;
         }
-        System.out.println("Excel格式:"+ cell.getCellType().name());
+        logger.info("Excel格式:"+ cell.getCellType().name());
 
         switch (cell.getCellType().name()) {
             case "STRING":
                 cellValue = cell.getRichStringCellValue().getString();
                 break;
             case "NUMERIC":
-                System.out.println("Excel数据样式:"+ cell.getCellStyle().getDataFormatString());
+                logger.info("Excel数据样式:"+ cell.getCellStyle().getDataFormatString());
                 if("General".equals(cell.getCellStyle().getDataFormatString())){
                     cellValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
                 }else if("m/d/yyyy;@".equals(cell.getCellStyle().getDataFormatString())){
@@ -89,7 +93,11 @@ public class ExcelUtils {
                     cellValue = cell.getNumericCellValue() + "";
                 }else if(cell.getCellStyle().getDataFormatString().indexOf("0_") > -1){
                     //数字非日期
-                    System.out.println("Excel值:"+ cell.getNumericCellValue());
+                    logger.info("Excel值:"+ cell.getNumericCellValue());
+                    cellValue = cell.getNumericCellValue() + "";
+                }else if (cell.getCellStyle().getDataFormatString().indexOf("0.0") > -1) {
+                    //数字非日期
+                    logger.info("Excel值:" + cell.getNumericCellValue());
                     cellValue = cell.getNumericCellValue() + "";
                 }else{
                     try {

+ 158 - 163
master/src/main/java/com/ruoyi/project/base/controller/TBasePointController.java

@@ -5,15 +5,20 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 
 import com.alibaba.fastjson2.JSON;
 import com.ruoyi.common.core.domain.entity.SysDictData;
-import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.project.base.domain.TBaseDevice;
-import com.ruoyi.project.base.domain.TBasePlant;
-import com.ruoyi.project.base.domain.TBaseRegion;
+import com.ruoyi.project.base.mapper.TBaseDeviceMapper;
+import com.ruoyi.project.base.mapper.TBasePointMapper;
 import com.ruoyi.project.base.service.ITBaseDeviceService;
 import com.ruoyi.project.base.service.ITBasePlantService;
 import com.ruoyi.project.base.service.ITBaseRegionService;
@@ -59,6 +64,10 @@ public class TBasePointController extends BaseController {
     @Autowired
     private ISysDictTypeService isysDictTypeService;
 
+    @Resource
+    private TBasePointMapper tBasePointMapper;
+    @Resource
+    private TBaseDeviceMapper tBaseDeviceMapper;
 
     /**
      * 查询密封点列表
@@ -145,183 +154,169 @@ public class TBasePointController extends BaseController {
         //获取操作人员ID
         Long userId = getUserId();
         //报错行数统计
-        List<Integer> failRow = new ArrayList<>();
+        CopyOnWriteArrayList<Integer> failRow = new CopyOnWriteArrayList<>();
         Workbook workbook = ExcelUtils.getWorkBook(file);
         Sheet sheet = workbook.getSheetAt(0);
         List<TBasePoint> list = new ArrayList<>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         int rowNum = sheet.getPhysicalNumberOfRows();
-        int failNumber = 0;
+        //线程池
+        ExecutorService executorService = Executors.newFixedThreadPool(30);
+        final CountDownLatch latch = new CountDownLatch(rowNum -1); //相同线程数量的计数
+        AtomicInteger failNumber = new AtomicInteger();
+        AtomicInteger successNumber = new AtomicInteger();
         for (int i = 1; i < rowNum; i++) {
-            try {
-                logger.info("读取行数:" + i);
-                Row row = sheet.getRow(i);
-                int cellNum = row.getLastCellNum();
-                TBasePoint entity = new TBasePoint();
-                Long plantId = 0L;
-                Long regionId = 0L;
-                String devCode = "";
-                String devName = "";
-                for (int j = 0; j < cellNum; j++) {
-                    Cell cell = row.getCell(j);
-                    if (cell == null) {
-                        continue;
-                    }
-                    String cellValue = ExcelUtils.getCellValue(cell);
-                    logger.info("cellValue:" + cellValue);
-                    if (j == 0) {
-                        //装置名称
-                        TBasePlant tBasePlant = tBasePlantService.selectTBasePlantByName(cellValue);
-                        if (tBasePlant == null) {
-                            return AjaxResult.success("未找到对应装置!请检查装置无误后重新提交!", 0);
-                        }
-                        plantId = tBasePlant.getPlantId();
-                        entity.setPlantId(plantId);
-                    } else if (j == 1 || j == 2) {
-                        if (StringUtils.isEmpty(cellValue))
-                            return AjaxResult.success("未找到对应装置!请检查装置名称无误后重新提交!", 0);
-                    } else if (j == 3) {
-                        //  区域名称
-                        TBaseRegion tBaseRegion = tBaseRegionService.selectTBaseRegionByName(cellValue, plantId);
-                        if (tBaseRegion == null) {
-                            return AjaxResult.success("未找到对应区域!请检查装置与区域无误后重新提交!", 0);
+            int finalI = i;
+            executorService.execute(() -> {
+                try {
+                    logger.info("读取行数:" + finalI);
+                    Row row = sheet.getRow(finalI);
+                    int cellNum = row.getLastCellNum();
+                    TBasePoint entity = new TBasePoint();
+                    for (int j = 0; j < cellNum; j++) {
+                        Cell cell = row.getCell(j);
+                        if (cell == null) {
+                            continue;
                         }
-                        regionId = tBaseRegion.getId();
-                        entity.setRegionId(regionId);
-                    } else if (j == 4) {
-                        if (StringUtils.isEmpty(cellValue))
-                            return AjaxResult.success("未找到对应区域!请检查装置与区域无误后重新提交!", 0);
-                    } else if (j == 5) {
-                        // 平台
-                        entity.setLayer(cellValue);
-                    } else if (j == 6) {
-                        //PID
-                        entity.setPidNo(cellValue);
-                    } else if (j == 7) {
-                        // 设备名称
-                        devName = cellValue;
-                    } else if (j == 8) {
-                        // 设备编号
-                        devCode = cellValue;
-                        TBaseDevice tBaseDevice = new TBaseDevice();
-                        tBaseDevice.setPlantId(plantId);
-                        tBaseDevice.setRegionId(regionId);
-                        tBaseDevice.setDevCode(devCode);
-                        tBaseDevice.setDevDescribe(devName);
-                        TBaseDevice device = tBaseDeviceService.selectDeviceByPlantRegionAndDevice(tBaseDevice);
-                        if (device == null) {
-                            return AjaxResult.success("未找到对应设备!请检查装置、区域、设备无误后重新提交!", 0);
-                        }
-                        entity.setDevId(device.getDevId());// 设备id
-                    } else if (j == 9) {
-                        // 群组位置
-                        entity.setGroupPosition(cellValue);
-                    } else if (j == 10) {
-                        // 密封点位置
-                        entity.setPointPosition(cellValue);
-                    } else if (j == 11) {
-                        // 群组编码
-                        entity.setGroupCode(cellValue);
-                    } else if (j == 12) {
-                        // 密封点扩展号编码
-                        entity.setExtendCode(cellValue);
-                    } else if (j == 13) {
-                        for (SysDictData dictData : isysDictTypeService.selectDictDataByType("point_type")) {
-                            if (dictData.getDictLabel().equals(cellValue)) {
-                                // 密封点类型
-                                entity.setPointType(dictData.getDictValue());
-                                break;
+                        String cellValue = ExcelUtils.getCellValue(cell);
+                        logger.info("cellValue:" + cellValue);
+                        if (j == 0) {
+                            //装置名称
+                            entity.setPlantName(cellValue);
+                        } else if (j == 1 ) {
+                            entity.setPlantCode(cellValue);
+                        } else if (j == 3) {
+                            //  区域名称
+                            entity.setRegionName(cellValue);
+                        } else if (j == 4) {
+                            entity.setRegionCode(cellValue);
+                        } else if (j == 5) {
+                            // 平台
+                            entity.setLayer(cellValue);
+                        } else if (j == 6) {
+                            //PID
+                            entity.setPidNo(cellValue);
+                        } else if (j == 7) {
+                            // 设备名称
+                            entity.setDevName(cellValue);
+                        } else if (j == 8) {
+                            // 设备编号
+                            entity.setDevCode(cellValue);// 设备id
+                        } else if (j == 9) {
+                            // 群组位置
+                            entity.setGroupPosition(cellValue);
+                        } else if (j == 10) {
+                            // 密封点位置
+                            entity.setPointPosition(cellValue);
+                        } else if (j == 11) {
+                            // 群组编码
+                            entity.setGroupCode(cellValue);
+                        } else if (j == 12) {
+                            // 密封点扩展号编码
+                            entity.setExtendCode(cellValue);
+                        } else if (j == 13) {
+                            // 密封点类型
+                            entity.setPointType(cellValue);
+                        } else if (j == 14) {
+                            // 密封点子类型
+                            entity.setSubPointType(cellValue);
+                        } else if (j == 15) {
+                            // 公称直径
+                            entity.setDia(cellValue);
+                        } else if (j == 16) {
+                            // 是否不可达点
+                            entity.setUnarrive(cellValue);
+                        } else if (j == 17) {
+                            // 不可达原因
+                            entity.setUnarriveReason(cellValue);
+                        } else if (j == 18) {
+                            // 是否保温
+                            entity.setKeepWarm(cellValue);
+                        } else if (j == 19) {
+                            // 介质
+                            entity.setMedium(cellValue);
+                        } else if (j == 20) {
+                            for (SysDictData dictData : isysDictTypeService.selectDictDataByType("medium_type")) {
+                                if (dictData.getDictLabel().equals(cellValue)) {
+                                    // 介质状态
+                                    entity.setMediumType(dictData.getDictValue());
+                                    break;
+                                }
                             }
+                        } else if (j == 21) {
+                            // toc分数
+                            entity.setTocMark(cellValue);
+                        } else if (j == 22) {
+                            // 甲烷质量分数
+                            entity.setMethaneMark(cellValue);
+                        } else if (j == 23) {
+                            // vocs分数
+                            entity.setVocsMark(cellValue);
+                        } else if (j == 24) {
+                            // 工艺温度
+                            entity.setTemperature(cellValue);
+                        } else if (j == 25) {
+                            // 工艺压力
+                            entity.setPressure(cellValue);
+                        } else if (j == 26) {
+                            // pic图号
+                            entity.setPicNo(cellValue);
+                        } else if (j == 27) {
+                            // 运行时间
+                            entity.setRunTime(sdf.parse(cellValue));
+                        } else if (j == 28) {
+                            // 备注
+                            entity.setRemarks(cellValue);
                         }
-                    } else if (j == 14) {
-                        // 密封点子类型
-                        entity.setSubPointType(cellValue);
-                    } else if (j == 15) {
-                        // 公称直径
-                        entity.setDia(cellValue);
-                    } else if (j == 16) {
-                        // 是否不可达点
-                        entity.setUnarrive(cellValue.equals("是")?"1":"0");
-                    } else if (j == 17) {
-                        // 不可达原因
-                        entity.setUnarriveReason(cellValue);
-                    } else if (j == 18) {
-                        // 是否保温
-                        entity.setKeepWarm(cellValue.equals("是")?"1":"0");
-                    } else if (j == 19) {
-                        // 介质
-                        entity.setMedium(cellValue);
-                    } else if (j == 20) {
-                        for (SysDictData dictData : isysDictTypeService.selectDictDataByType("medium_type")) {
-                            if (dictData.getDictLabel().equals(cellValue)) {
-                                // 介质状态
-                                entity.setMediumType(dictData.getDictValue());
-                                break;
-                            }
+                    }
+                    entity.setCreaterCode(userId);
+                    entity.setApproveStatus(0L);
+                    TBaseDevice device = tBaseDeviceMapper.selectTBaseDeviceByCode(entity);
+                    if (device == null) {
+                        logger.info("没有设备数据");
+                        failRow.add(finalI + 1);
+                        throw new InterruptedException(); //中止线程
+                    }else {
+                        entity.setDevId(device.getDevId());
+                        entity.setPlantId(device.getPlantId());
+                        entity.setRegionId(device.getRegionId());
+                        logger.info("entity:" + entity);
+                        //进行数据更新
+                        TBasePoint tBasePoint = tBasePointService.selectTBasePointByGroupCodeAndExtendCode(entity);
+                        if (tBasePoint == null) {
+                            entity.setUpdatedate(new Date());
+                            entity.setUpdaterCode(getUserId());
+                            tBasePointService.insertTBasePoint(entity);
+                            successNumber.getAndIncrement();
+                        }else {
+                            logger.info("重复数据");
+                            failNumber.getAndIncrement();
+                            failRow.add(finalI + 1);
+                            throw new InterruptedException();//中止线程
                         }
-                    } else if (j == 21) {
-                        // toc分数
-                        entity.setTocMark(cellValue);
-                    } else if (j == 22) {
-                        // 甲烷质量分数
-                        entity.setMethaneMark(cellValue);
-                    } else if (j == 23) {
-                        // vocs分数
-                        entity.setVocsMark(cellValue);
-                    } else if (j == 24) {
-                        // 工艺温度
-                        entity.setTemperature(cellValue);
-                    } else if (j == 25) {
-                        // 工艺压力
-                        entity.setPressure(cellValue);
-                    } else if (j == 26) {
-                        // pic图号
-                        entity.setPicNo(cellValue);
-                    } else if (j == 27) {
-                        // 运行时间
-                        entity.setRunTime(sdf.parse(cellValue));
-                    } else if (j == 28) {
-                        // 备注
-                        entity.setRemarks(cellValue);
                     }
+                }catch (InterruptedException e) {
+                    logger.info("中止线程"+ Thread.currentThread().getName());
+                }catch (Exception e) {
+                    failNumber.getAndIncrement();
+                    failRow.add(finalI + 1);
+                    e.printStackTrace();
+                } finally {
+                    latch.countDown(); //线程计数
                 }
-                entity.setCreaterCode(userId);
-                entity.setApproveStatus(0L);
-                logger.info("entity:" + entity);
-                list.add(entity);
-            } catch (Exception e) {
-                failNumber++;
-                logger.info("e:" + JSON.toJSONString(e));
-                failRow.add(i + 1);
-            }
+            });
         }
-        int successNumber = 0;
-        int failNum = 0;
-        for (TBasePoint t : list
-        ) {
-            failNum++;
-            try {
-                //进行数据更新
-                TBasePoint tBasePoint = tBasePointService.selectTBasePointByGroupCodeAndExtendCode(t);
-                if (tBasePoint == null) {
-                    t.setUpdatedate(new Date());
-                    t.setUpdaterCode(getUserId());
-                    tBasePointService.insertTBasePoint(t);
-                    successNumber++;
-                }else {
-                    failNumber++;
-                    failRow.add(failNum + 1);
-                }
-            } catch (Exception e) {
-                failNumber++;
-                logger.info("e:" + e);
-                failRow.add(failNum + 1);
-            }
+        try {
+            latch.await(); //线程计数完毕后继续执行
+        } catch (InterruptedException e) {
+            e.printStackTrace();
         }
+        executorService.shutdown();
+
         logger.info("list:" + JSON.toJSONString(list));
         logger.info("successNumber:" + successNumber);
         logger.info("failNumber:" + failNumber);
         logger.info("failRow:" + failRow);
-        return AjaxResult.success(String.valueOf(successNumber), failRow);
+        return AjaxResult.success(String.valueOf(successNumber.get()), failRow);
     }
 }

+ 3 - 0
master/src/main/java/com/ruoyi/project/base/mapper/TBaseDeviceMapper.java

@@ -2,6 +2,7 @@ package com.ruoyi.project.base.mapper;
 
 import java.util.List;
 import com.ruoyi.project.base.domain.TBaseDevice;
+import com.ruoyi.project.base.domain.TBasePoint;
 
 /**
  * 设备/管线Mapper接口
@@ -20,6 +21,8 @@ public interface TBaseDeviceMapper
     public TBaseDevice selectTBaseDeviceById(Long id);
     public TBaseDevice selectDeviceByPlantRegionAndDevice(TBaseDevice tBaseDevice);
 
+    public TBaseDevice selectTBaseDeviceByCode(TBasePoint tBasePoint);
+
     /**
      * 查询设备/管线列表
      *

+ 9 - 0
master/src/main/resources/mybatis/base/TBaseDeviceMapper.xml

@@ -59,10 +59,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         select d.dev_id, concat(d.dev_code, '+',d.dev_describe) `dev_describe` from t_base_device d where d.region_id=#{regionId} and d.del_flag=0
     </select>
 
+    <select id="selectTBaseDeviceByCode" parameterType="TBasePoint" resultMap="TBaseDeviceResult">
+        select d.dev_id, d.plant_id, d.region_id, d.dev_code from t_base_device d
+              LEFT JOIN  t_base_region r on d.region_id = r.region_id
+              LEFT JOIN  t_base_plant p on p.plant_id = d.plant_id
+        where d.dev_code = #{devCode}  and r.region_code = #{regionCode} and p.plant_code = #{plantCode}
+    </select>
+
     <select id="selectTBaseDeviceById" parameterType="Long" resultMap="TBaseDeviceResult">
         <include refid="selectTBaseDeviceVo"/>
         where dev_id = #{devId}
     </select>
+
+
     <select id="selectDeviceByPlantRegionAndDevice" parameterType="TBaseDevice" resultMap="TBaseDeviceResult">
         <include refid="selectTBaseDeviceVo"/>
         where plant_id=#{plantId} and region_id=#{regionId} and `dev_code`=#{devCode} and `dev_describe`=#{devDescribe} and del_flag=0