From 275ad59f206d9bc1801b28daeb31b4207f609ce3 Mon Sep 17 00:00:00 2001
From: hcong <1050828145@qq.com>
Date: 星期四, 02 一月 2025 09:10:09 +0800
Subject: [PATCH] 1. 修改网格组数据导入接口返回值为bool 2. 修改网格组数据批量更新 使用Transactional注解

---
 src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt |  196 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 194 insertions(+), 2 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
index ea62b09..13ffed5 100644
--- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
+++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
@@ -2,10 +2,14 @@
 
 import com.alibaba.fastjson.JSONObject
 import com.flightfeather.uav.common.exception.BizException
+import com.flightfeather.uav.domain.entity.GridAodDetail
+import com.flightfeather.uav.domain.entity.GridDataDetail
 import com.flightfeather.uav.domain.entity.RealTimeData
 import com.flightfeather.uav.domain.entity.RealTimeDataVehicle
 import com.flightfeather.uav.socket.bean.AirData
 import org.apache.poi.hssf.usermodel.HSSFWorkbook
+import org.apache.poi.ss.usermodel.CellType
+import org.apache.poi.ss.util.CellAddress
 import org.apache.poi.xssf.streaming.SXSSFWorkbook
 import java.io.File
 import java.io.FileInputStream
@@ -53,7 +57,7 @@
                     if (cellIterator.hasNext()) {
                         val cellText = cellIterator.next().stringCellValue
                         if (!cellText.equals(head)) {
-                            throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]搴旇涓篬${cellText}]")
+                            throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]")
                         }
                     } else {
                         throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け")
@@ -88,6 +92,194 @@
         } catch (e: Exception) {
             e.printStackTrace()
             throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e)
+        }
+        return result
+    }
+
+    /**
+     * 杞崲PM2.5琛ㄦ牸鏁版嵁
+     */
+    fun exchangeGridData(file: InputStream, correctRangeGridCells: Set<Int>): List<GridDataDetail> {
+        // 妯′豢宸叉湁浠g爜鍒ゆ柇琛ㄥご姝g‘鎬� 骞跺湪閿欒鏃舵姏鍑哄紓甯�
+        val headers = listOf(
+            "pointid",
+            "PM2.5"
+        )
+        val result = mutableListOf<GridDataDetail>()
+        try {
+            ExcelUtil.readXLXS(file, headerCheck = {
+                val cellIterator = it.cellIterator()
+                val headIterator = headers.iterator()
+                while (headIterator.hasNext()) {
+                    val head = headIterator.next()
+                    if (cellIterator.hasNext()) {
+                        val cellText = cellIterator.next().stringCellValue
+                        if (!cellText.equals(head)) {
+                            throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]")
+                        }
+                    } else {
+                        throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け")
+                    }
+                }
+                true
+            },
+                // 鍒ゆ柇鍗曞厓鏍兼暟鎹纭�э紝骞跺湪鎶ラ敊鍚庢姏鍑哄紓甯�
+                onRow = {
+                    val data = GridDataDetail().apply {
+                        // 杞崲缃戞牸缁勫崟鍏冩牸id
+                        // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛�
+                        val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString()
+                        // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨�
+                        val cellIdCellType = it.getCell(0).cellType
+                        when (cellIdCellType) {
+                            CellType.NUMERIC -> {
+                                // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负缃戞牸鍗曞厓鏍糹d
+                                this.cellId = it.getCell(0)?.numericCellValue?.toInt()
+                            }
+                            else -> {
+                                throw BizException("鍗曞厓鏍糩${cellIdCellPosStr}]涓嶆槸鏁板瓧绫诲瀷")
+                            }
+                        }
+
+                        // 杞崲PM2.5
+                        // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛�
+                        val pm25CellPosStr = CellAddress(it.rowNum, 1).formatAsString()
+                        // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨�
+                        val pm25CellType = it.getCell(1).cellType
+                        when (pm25CellType) {
+                            CellType.STRING -> {
+                                // 褰撳�间负瀛楃涓叉椂鏈変袱绉嶆儏鍐�
+                                // 绌虹櫧鎴栬�呮槸鈥�/鈥欏瓧绗� 杞寲涓虹┖鍊�
+                                // 鍚﹀垯鎶ラ敊
+                                val stringValue = it.getCell(1)?.stringCellValue?.trim()
+                                if (stringValue != null) {
+                                    if (stringValue == "/" || stringValue.isBlank()) {
+                                        this.pm25 = null
+                                    }
+                                }else {
+                                    throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�")
+                                }
+                            }
+                            CellType.NUMERIC -> {
+                                // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负pm2.5鏁版嵁
+                                this.pm25 = it.getCell(1)?.numericCellValue?.toFloat()
+                            }
+                            else -> {
+                                throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�")
+                            }
+                        }
+                    }
+                    result.add(data)
+                })
+        } catch (e: Exception) {
+            e.printStackTrace()
+            throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e)
+        }
+        // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴
+        val resultCellIdSet = result.map { it.cellId ?: -1 }.toSet()
+        // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸
+        val lackCells = correctRangeGridCells - resultCellIdSet
+        // excel 涓鍑虹殑鐨勫崟鍏冩牸
+        val outCells = resultCellIdSet - correctRangeGridCells
+        if (lackCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${lackCells.joinToString(",")}")
+        }
+        if (outCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}")
+        }
+        return result
+    }
+
+    /**
+     * 杞崲AOD琛ㄦ牸鏁版嵁
+     */
+    fun exchangeGridAod(file: InputStream, correctRangeGridCells: Set<Int>): List<GridAodDetail> {
+        // 妯′豢宸叉湁浠g爜鍒ゆ柇琛ㄥご姝g‘鎬� 骞跺湪閿欒鏃舵姏鍑哄紓甯�
+        val headers = listOf(
+            "pointid",
+            "AOD"
+        )
+        val result = mutableListOf<GridAodDetail>()
+        try {
+            ExcelUtil.readXLXS(file, headerCheck = {
+                val cellIterator = it.cellIterator()
+                val headIterator = headers.iterator()
+                while (headIterator.hasNext()) {
+                    val head = headIterator.next()
+                    if (cellIterator.hasNext()) {
+                        val cellText = cellIterator.next().stringCellValue
+                        if (!cellText.equals(head)) {
+                            throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${cellText}]搴旇涓篬${head}]")
+                        }
+                    } else {
+                        throw BizException("鏂囦欢鏍煎紡閿欒, 琛ㄥご[${head}]缂哄け")
+                    }
+                }
+                true
+            },
+                // 鍒ゆ柇鍗曞厓鏍兼暟鎹纭�э紝骞跺湪鎶ラ敊鍚庢姏鍑哄紓甯�
+                onRow = {
+                    val data = GridAodDetail().apply {
+                        // 杞崲缃戞牸缁勫崟鍏冩牸id
+                        // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛�
+                        val cellIdCellPosStr = CellAddress(it.rowNum, 0).formatAsString()
+                        // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨�
+                        val cellIdCellType = it.getCell(0).cellType
+                        when (cellIdCellType) {
+                            CellType.NUMERIC -> {
+                                // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负缃戞牸鍗曞厓鏍糹d
+                                this.cellId = it.getCell(0)?.numericCellValue?.toInt()
+                            }
+                            else -> {
+                                throw BizException("鍗曞厓鏍糩${cellIdCellPosStr}]涓嶆槸鏁板瓧绫诲瀷")
+                            }
+                        }
+
+                        // 杞崲AOD
+                        // 寰楀埌褰撳墠鍗曞厓鏍肩殑瀹氫綅淇℃伅 锛坋g: B3,BA3...锛�
+                        val aodCellPosStr = CellAddress(it.rowNum, 1).formatAsString()
+                        // 寰楀埌褰撳墠鍗曞厓鏍兼暟鎹被鍨�
+                        val aodCellType = it.getCell(1).cellType
+                        when (aodCellType) {
+                            CellType.STRING -> {
+                                // 褰撳�间负瀛楃涓叉椂鏈変袱绉嶆儏鍐�
+                                // 绌虹櫧鎴栬�呮槸鈥�/鈥欏瓧绗� 杞寲涓虹┖鍊�
+                                // 鍚﹀垯鎶ラ敊
+                                val stringValue = it.getCell(1)?.stringCellValue?.trim()
+                                if (stringValue != null) {
+                                    if (stringValue == "/" || stringValue.isBlank()) {
+                                        this.aod = null
+                                    }
+                                }else {
+                                    throw BizException("鍗曞厓鏍糩${aodCellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�")
+                                }
+                            }
+                            CellType.NUMERIC -> {
+                                // 褰撳�间负鏁板瓧绫诲瀷鏃惰浆鍖栦负pm2.5鏁版嵁
+                                this.aod = it.getCell(1)?.numericCellValue?.toFloat()
+                            }
+                            else -> {
+                                throw BizException("鍗曞厓鏍糩${aodCellPosStr}]涓嶆槸鏁板瓧绫诲瀷")
+                            }
+                        }
+                    }
+                    result.add(data)
+                })
+        } catch (e: Exception) {
+            e.printStackTrace()
+            throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e)
+        }
+        // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴
+        val resultCellIdSet = result.map { it.cellId ?: -1 }.toSet()
+        // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸
+        val lackCells = correctRangeGridCells - resultCellIdSet
+        // excel 涓鍑虹殑鐨勫崟鍏冩牸
+        val outCells = resultCellIdSet - correctRangeGridCells
+        if (lackCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${lackCells.joinToString(",")}")
+        }
+        if (outCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}")
         }
         return result
     }
@@ -428,4 +620,4 @@
         out.flush()
         out.close()
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3