chejianzheng hace 3 meses
padre
commit
2f762adc2a

+ 7 - 0
pom.xml

@@ -239,6 +239,13 @@
             <artifactId>spring-integration-mqtt</artifactId>
         </dependency>
         <!-- image-combiner -->
+
+        <!-- EasyExcel 核心依赖 -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>3.3.2</version> <!-- 请检查最新版本 -->
+        </dependency>
     </dependencies>
 
     <build>

+ 40 - 0
src/main/java/com/wxxcx/index/DevInfoImportVo.java

@@ -0,0 +1,40 @@
+package com.wxxcx.index;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+
+public class DevInfoImportVo {
+
+    @ExcelProperty("固件版本")
+    private String software;
+
+    @ExcelProperty("设备ID")
+    private String dev_id;
+
+    @ExcelProperty("设备类型")
+    private String dev_type;
+
+
+    public String getSoftware() {
+        return software;
+    }
+
+    public void setSoftware(String software) {
+        this.software = software;
+    }
+
+    public String getDev_id() {
+        return dev_id;
+    }
+
+    public void setDev_id(String dev_id) {
+        this.dev_id = dev_id;
+    }
+
+    public String getDev_type() {
+        return dev_type;
+    }
+
+    public void setDev_type(String dev_type) {
+        this.dev_type = dev_type;
+    }
+}

+ 95 - 0
src/main/java/com/wxxcx/index/QueryListParam.java

@@ -0,0 +1,95 @@
+package com.wxxcx.index;
+
+
+public class QueryListParam {
+
+    private String software;
+
+    private String devId;
+
+    private String devType;
+
+    private String devName;
+
+    private Integer online;
+
+    private String createTimeStart;
+
+    private String createTimeEnd;
+
+    private int pageSize = 10;
+
+    private int pageNum = 1;
+
+    public String getCreateTimeStart() {
+        return createTimeStart;
+    }
+
+    public void setCreateTimeStart(String createTimeStart) {
+        this.createTimeStart = createTimeStart;
+    }
+
+    public String getCreateTimeEnd() {
+        return createTimeEnd;
+    }
+
+    public void setCreateTimeEnd(String createTimeEnd) {
+        this.createTimeEnd = createTimeEnd;
+    }
+
+    public int getPageNum() {
+        return pageNum;
+    }
+
+    public void setPageNum(int pageNum) {
+        this.pageNum = pageNum;
+    }
+
+    public int getPageSize() {
+        return pageSize;
+    }
+
+    public void setPageSize(int pageSize) {
+        this.pageSize = pageSize;
+    }
+
+    public String getSoftware() {
+        return software;
+    }
+
+    public void setSoftware(String software) {
+        this.software = software;
+    }
+
+    public String getDevId() {
+        return devId;
+    }
+
+    public void setDevId(String devId) {
+        this.devId = devId;
+    }
+
+    public String getDevType() {
+        return devType;
+    }
+
+    public void setDevType(String devType) {
+        this.devType = devType;
+    }
+
+    public String getDevName() {
+        return devName;
+    }
+
+    public void setDevName(String devName) {
+        this.devName = devName;
+    }
+
+    public Integer getOnline() {
+        return online;
+    }
+
+    public void setOnline(Integer online) {
+        this.online = online;
+    }
+}

+ 122 - 0
src/main/java/com/wxxcx/index/WxxcxIndexController.java

@@ -4,13 +4,17 @@ import java.io.*;
 import java.math.BigDecimal;
 import java.security.MessageDigest;
 import java.sql.Timestamp;
+import java.sql.Types;
 import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.stream.Collectors;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import com.admin.alarm_event.AlarmEvent;
+import com.alibaba.excel.EasyExcel;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -21,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataAccessException;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -2009,6 +2014,123 @@ public class WxxcxIndexController<AesException extends Throwable> {
     }
 
 
+    // 上传导入表
+    @PostMapping("/uploadDevInfo")
+    @ApiOperation(value = "上传导入表")
+    public R uploadDevInfo(@RequestParam("file") MultipartFile file) throws IllegalStateException,
+            IOException {
+        // 校验文件非空
+        if (file.isEmpty()) {
+            return JSONUtil.getErrorMsg("文件为空");
+        }
+        System.out.println("start time :" + System.currentTimeMillis());
+        List<DevInfoImportVo> devInfoImportVoList = EasyExcel.read(file.getInputStream(), DevInfoImportVo.class, null).sheet(0).doReadSync();
+        System.out.println("parse time :" + System.currentTimeMillis());
+        if (CollectionUtils.isEmpty(devInfoImportVoList)) {
+            return JSONUtil.getErrorMsg("数据为空");
+        }
+
+        R r = JSONUtil.getSuccessMsg(null);
+
+        String sql2 = "SELECT * FROM dev_info WHERE dev_id IN (" +
+                devInfoImportVoList.stream()
+                        .map(DevInfoImportVo::getDev_id)
+                        .distinct()
+                        .map(id -> "?")
+                        .collect(Collectors.joining(", ")) +
+                ")";
+        List<String> params2 = devInfoImportVoList.stream()
+                .map(DevInfoImportVo::getDev_id)
+                .distinct()
+                .collect(Collectors.toList());
+
+        List<DevVO> newlist2 = baseDAO.getJdbcTemplate().query(sql2, params2.toArray(), new BeanPropertyRowMapper<>(DevVO.class));
+        if (!CollectionUtils.isEmpty(newlist2)) {
+            List<String> dupDevIds = newlist2.stream().map(DevVO::getDev_id).collect(Collectors.toList());
+            r.data.put("dupIdList", dupDevIds);
+            devInfoImportVoList = devInfoImportVoList.stream().filter(item -> !dupDevIds.contains(item.getDev_id())).collect(Collectors.toList());
+        }
+
+        // 将数据分批插入数据库,每批100条,使用jdbcTemplate
+        // 将devInfoImportVoList按元素每一百个分成多个集合
+        int batchSize = 100;
+        int total = devInfoImportVoList.size();
+        for (int i = 0; i < total; i += batchSize) {
+            int end = Math.min(i + batchSize, total);
+            List<DevInfoImportVo> subList = devInfoImportVoList.subList(i, end);
+            // 使用jdbcTemplate批量插入数据
+            baseDAO.getJdbcTemplate().batchUpdate(
+                    "INSERT INTO dev_info (dev_id, dev_type, software, status_light, online, delete_tag, create_time, update_time, x, y, north_angle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+                    subList.stream().map(devVO -> new Object[]{
+                        devVO.getDev_id(),
+                        devVO.getDev_type(),
+                        devVO.getSoftware(),
+                        1,
+                        0,
+                        0,
+                        new Timestamp(System.currentTimeMillis()),
+                        new Timestamp(System.currentTimeMillis()), 0, 0, 0
+                    }).collect(Collectors.toList()),
+                    new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP}
+            );
+        }
+
+
+        System.out.println("end time :" + System.currentTimeMillis());
+        return r;
+    }
+
+    // 上传导入表
+    @PostMapping("/queryList")
+    @ApiOperation(value = "上传导入表")
+    public R queryList(@RequestBody QueryListParam queryListParam) throws IllegalStateException,
+            IOException {
+
+
+        // 根据queryListParam查询条件是否为空,拼接 jdbc sql
+        StringBuilder sql = new StringBuilder("SELECT * FROM dev_info WHERE 1=1 ");
+        if (!StringUtils.isEmpty(queryListParam.getDevId())) {
+            sql.append(" AND dev_id like '%" + queryListParam.getDevId() + "%'");
+        }
+        if (!StringUtils.isEmpty(queryListParam.getDevType())) {
+            sql.append(" AND dev_type = '" + queryListParam.getDevType() + "'");
+        }
+        if (!StringUtils.isEmpty(queryListParam.getSoftware())) {
+            sql.append(" AND software = '" + queryListParam.getSoftware() + "'");
+        }
+
+        if (!StringUtils.isEmpty(queryListParam.getDevName())) {
+            sql.append(" AND dev_name like '%" + queryListParam.getDevName() + "%'");
+        }
+
+        if (queryListParam.getOnline() != null) {
+            sql.append(" AND online = '" + queryListParam.getOnline() + "'");
+        }
+
+        // 添加创建时间的范围查询 数据库里的create_time字段为timestamp类型,查询条件里的createtimestart和createtimeend为string类型,需要转换成timestamp类型,yyyyMMdd
+        String queryStartTime = queryListParam.getCreateTimeStart();
+        if (!StringUtils.isEmpty(queryStartTime)) {
+            sql.append(" AND create_time >= STR_TO_DATE('" + queryStartTime + "', '%Y%m%d') ");
+        }
+        String queryEndTime = queryListParam.getCreateTimeEnd();
+        if (!StringUtils.isEmpty(queryEndTime)) {
+            LocalDate endDate = LocalDate.parse(queryEndTime, DateTimeFormatter.ofPattern("yyyyMMdd"));
+            endDate = endDate.plusDays(1);
+            sql.append(" AND create_time < STR_TO_DATE('" + DateTimeFormatter.ofPattern("yyyyMMdd").format(endDate) + "', '%Y%m%d') ");
+        }
+
+        // 分页参数
+        int pageSize = queryListParam.getPageSize();
+        int pageNum = queryListParam.getPageNum();
+        int offset = (pageNum - 1) * pageSize;
+
+        sql.append(" ORDER BY create_time DESC LIMIT ").append("" + pageSize).append(" OFFSET ").append("" + offset);
+        List<DevVO> newlist2 = baseDAO.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(DevVO.class));
+        System.out.println("end time :" + System.currentTimeMillis());
+        R r = JSONUtil.getSuccessMsg(null);
+        r.data.put("resList", newlist2);
+        return r;
+    }
 }