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/domain/repository/SatelliteGridRep.kt                    |   65 ++++++++++++++++++++-
 /dev/null                                                                                      |   12 ----
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt |   17 ++---
 src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt                             |   31 +++++-----
 src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt          |    5 -
 src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt           |    4 
 src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt                    |    4 -
 src/main/resources/mapper/GridDataDetailMapper.xml                                             |   11 ---
 8 files changed, 89 insertions(+), 60 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 423644f..13ffed5 100644
--- a/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
+++ b/src/main/kotlin/com/flightfeather/uav/common/utils/FileExchange.kt
@@ -8,7 +8,6 @@
 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.Cell
 import org.apache.poi.ss.usermodel.CellType
 import org.apache.poi.ss.util.CellAddress
 import org.apache.poi.xssf.streaming.SXSSFWorkbook
@@ -166,23 +165,24 @@
                                 this.pm25 = it.getCell(1)?.numericCellValue?.toFloat()
                             }
                             else -> {
-                                throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷")
+                                throw BizException("鍗曞厓鏍糩${pm25CellPosStr}]涓嶆槸鏁板瓧绫诲瀷鎴栬�呮枃鏈被鍨�")
                             }
                         }
                     }
                     result.add(data)
                 })
-        } catch (e: BizException) {
-            throw e
+        } catch (e: Exception) {
+            e.printStackTrace()
+            throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e)
         }
         // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴
-        val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet()
+        val resultCellIdSet = result.map { it.cellId ?: -1 }.toSet()
         // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸
-        val shortCells = correctRangeGridCells - resultCellIdSet
+        val lackCells = correctRangeGridCells - resultCellIdSet
         // excel 涓鍑虹殑鐨勫崟鍏冩牸
         val outCells = resultCellIdSet - correctRangeGridCells
-        if (shortCells.isNotEmpty()) {
-            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${shortCells.joinToString(",")}")
+        if (lackCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${lackCells.joinToString(",")}")
         }
         if (outCells.isNotEmpty()) {
             throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}")
@@ -265,17 +265,18 @@
                     }
                     result.add(data)
                 })
-        } catch (e: BizException) {
-            throw e
+        } catch (e: Exception) {
+            e.printStackTrace()
+            throw BizException("excel鏂囦欢鍐呭閿欒锛屾暟鎹浆鎹㈠け璐ワ紒", e)
         }
         // 鍒ゆ柇缃戞牸鍗曞厓鏍肩紪鍙锋槸鍚﹀湪姝g‘鐨勮寖鍥村唴
-        val resultCellIdSet = result.asSequence().map { it.cellId ?: -1 }.toSet()
+        val resultCellIdSet = result.map { it.cellId ?: -1 }.toSet()
         // 鐢ㄦ埛瀵煎叆涓己灏戠殑鐨勫崟鍏冩牸
-        val shortCells = correctRangeGridCells - resultCellIdSet
+        val lackCells = correctRangeGridCells - resultCellIdSet
         // excel 涓鍑虹殑鐨勫崟鍏冩牸
         val outCells = resultCellIdSet - correctRangeGridCells
-        if (shortCells.isNotEmpty()) {
-            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${shortCells.joinToString(",")}")
+        if (lackCells.isNotEmpty()) {
+            throw BizException("瀵煎叆鏁版嵁涓己灏戜互涓嬬綉鏍煎崟鍏冩牸锛�${lackCells.joinToString(",")}")
         }
         if (outCells.isNotEmpty()) {
             throw BizException("瀵煎叆鏁版嵁涓湁澶氫綑缃戞牸鍗曞厓鏍硷細${outCells.joinToString(",")}")
@@ -619,4 +620,4 @@
         out.flush()
         out.close()
     }
-}
\ No newline at end of file
+}
diff --git a/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt b/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt
index cf4437e..1a6f152 100644
--- a/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt
+++ b/src/main/kotlin/com/flightfeather/uav/domain/mapper/GridDataDetailMapper.kt
@@ -5,6 +5,4 @@
 import org.apache.ibatis.annotations.Mapper
 
 @Mapper
-interface GridDataDetailMapper : MyMapper<GridDataDetail?> {
-    fun updatePM25Batch(gridDataDetails: List<GridDataDetail>)
-}
\ No newline at end of file
+interface GridDataDetailMapper : MyMapper<GridDataDetail?>
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt b/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt
index b88bc35..3293a6f 100644
--- a/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt
+++ b/src/main/kotlin/com/flightfeather/uav/domain/repository/SatelliteGridRep.kt
@@ -9,6 +9,7 @@
 import com.flightfeather.uav.domain.mapper.GridGroupMapper
 import com.flightfeather.uav.lightshare.bean.AreaVo
 import org.springframework.stereotype.Repository
+import org.springframework.transaction.annotation.Transactional
 import tk.mybatis.mapper.entity.Example
 import java.time.LocalDateTime
 
@@ -61,6 +62,14 @@
         })
     }
 
+    /**
+     * 鎻掑叆缃戞牸缁凱M2.5鏁版嵁绱㈠紩鍜岃缁嗘暟鎹��
+     *
+     * @param data 缃戞牸缁凱M2.5鏁版嵁绱㈠紩銆�
+     * @param gridDataDetails 缃戞牸缁凱M2.5璇︾粏鏁版嵁鍒楄〃銆�
+     * @author hc
+     * @date 2024-12-29
+     */
     fun insertGridDataAndDetail(data: GridData, gridDataDetails: List<GridDataDetail>) {
         gridDataMapper.insert(data)
         gridDataDetails.forEach {
@@ -70,12 +79,33 @@
         gridDataDetailMapper.insertList(gridDataDetails)
     }
 
+    /**
+     * 鏇存柊缃戞牸缁勮缁嗘暟鎹��
+     *
+     * @param gridDataDetails 闇�瑕佹洿鏂扮殑缃戞牸缁勮缁嗘暟鎹垪琛ㄣ��
+     * @author hc
+     * @date 2024-12-29
+     */
+    @Transactional(rollbackFor = [Exception::class])
     fun updatePM25Batch(gridDataDetails: List<GridDataDetail>) {
-        gridDataDetailMapper.updatePM25Batch(gridDataDetails)
+        gridDataDetails.forEach {
+            gridDataDetailMapper.updateByExample(it, Example(GridDataDetail::class.java).apply {
+                createCriteria().andEqualTo("dataId", it.dataId)
+                    .andEqualTo("groupId", it.groupId)
+                    .andEqualTo("cellId", it.cellId)
+            })
+        }
     }
 
-
-    //    aod 鐩稿叧鎿嶄綔
+    /**
+     * 鏍规嵁缁処D鍜屾暟鎹椂闂磋幏鍙栫綉鏍肩粍aod鏁版嵁绱㈠紩銆�
+     *
+     * @param groupId 缃戞牸缁処D銆�
+     * @param dataTime 鏁版嵁鏃堕棿銆�
+     * @return 缃戞牸缁刟od鏁版嵁绱㈠紩鍒楄〃銆�
+     * @author hc
+     * @date 2024-12-29
+     */
     fun fetchGridAod(groupId: Int, dataTime: LocalDateTime?): List<GridAod?> {
         return gridAodMapper.selectByExample(Example(GridAod::class.java).apply {
             createCriteria().andEqualTo("groupId", groupId)
@@ -83,6 +113,16 @@
         })
     }
 
+    /**
+     * 鑾峰彇缃戞牸缁刟od鏁版嵁璇︽儏銆�
+     *
+     * @param aodId aod鏁版嵁ID銆�
+     * @param groupId 缃戞牸缁処D銆�
+     * @param cellId 鍗曞厓鏍糏D銆�
+     * @return 缃戞牸缁刟od鏁版嵁璇︽儏鍒楄〃銆�
+     * @author hc
+     * @date 2024-12-29
+     */
     fun fetchGridAodDetail(aodId: Int, groupId: Int?, cellId: Int?): List<GridAodDetail?> {
         return gridAodDetailMapper.selectByExample(Example(GridAodDetail::class.java).apply {
             createCriteria().andEqualTo("aodId", aodId)
@@ -92,6 +132,14 @@
         })
     }
 
+    /**
+     * 鎻掑叆缃戞牸缁刟od鏁版嵁绱㈠紩鍜岃缁嗘暟鎹��
+     *
+     * @param aod 缃戞牸缁刟od鏁版嵁绱㈠紩銆�
+     * @param gridAodDetails 缃戞牸缁刟od璇︾粏鏁版嵁鍒楄〃銆�
+     * @author hc
+     * @date 2024-12-29
+     */
     fun insertGridAodAndDetail(aod: GridAod, gridAodDetails: List<GridAodDetail>) {
         gridAodMapper.insert(aod)
         gridAodDetails.forEach {
@@ -101,6 +149,14 @@
         gridAodDetailMapper.insertList(gridAodDetails)
     }
 
+    /**
+     * 鏇存柊缃戞牸缁刟od鏁版嵁璇︽儏銆�
+     *
+     * @param gridDataDetails 闇�瑕佹洿鏂扮殑缃戞牸缁刟od璇︾粏鏁版嵁鍒楄〃銆�
+     * @author hc
+     * @date 2024-12-29
+     */
+    @Transactional(rollbackFor = [Exception::class])
     fun updateGridAodBatch(gridDataDetails: List<GridAodDetail>) {
         gridDataDetails.forEach {
             gridAodDetailMapper.updateByExample(it, Example(GridAodDetail::class.java).apply {
@@ -109,7 +165,6 @@
                     .andEqualTo("cellId", it.cellId)
             })
         }
-
     }
 
-}
\ No newline at end of file
+}
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt
deleted file mode 100644
index 7afad90..0000000
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/GridDataImportResult.kt
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.flightfeather.uav.lightshare.bean
-
-import com.fasterxml.jackson.annotation.JsonInclude
-
-/**
- * 缃戞牸鏁版嵁瀵煎叆澶勭悊缁撴灉
- */
-@JsonInclude(JsonInclude.Include.NON_NULL)
-data class GridDataImportResult(
-    val success: Boolean,
-    val result: String
-)
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt
index 97014cb..d4cf0b2 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/SatelliteTelemetryService.kt
@@ -8,7 +8,6 @@
 import com.flightfeather.uav.domain.entity.GridGroup
 import com.flightfeather.uav.lightshare.bean.AreaVo
 import com.flightfeather.uav.lightshare.bean.DataHead
-import com.flightfeather.uav.lightshare.bean.GridDataImportResult
 import org.springframework.web.multipart.MultipartFile
 import java.time.LocalDateTime
 import javax.servlet.http.HttpServletResponse
@@ -29,14 +28,14 @@
     fun fetchGridDataDetail(dataId: Int, groupId: Int?, cellId: Int?): List<GridDataDetail?>
 
     @Throws(BizException::class)
-    fun importGridData(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): GridDataImportResult?
+    fun importGridData(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): Boolean?
 
     fun downloadTemplate(response: HttpServletResponse): Boolean
 
     fun fetchGridAod(groupId: Int, dataTime: LocalDateTime?): List<GridAod?>
 
     @Throws(BizException::class)
-    fun importGridAOD(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): GridDataImportResult?
+    fun importGridAOD(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): Boolean?
 
     fun downloadAODTemplate(response: HttpServletResponse): Boolean
 }
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt
index db1be82..6ae9e79 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/SatelliteTelemetryServiceImpl.kt
@@ -10,7 +10,6 @@
 import com.flightfeather.uav.domain.repository.SatelliteGridRep
 import com.flightfeather.uav.lightshare.bean.AreaVo
 import com.flightfeather.uav.lightshare.bean.DataHead
-import com.flightfeather.uav.lightshare.bean.GridDataImportResult
 import com.flightfeather.uav.lightshare.service.SatelliteTelemetryService
 import com.github.pagehelper.PageHelper
 import org.springframework.stereotype.Service
@@ -53,7 +52,7 @@
         return satelliteGridRep.fetchGridDataDetail(dataId, groupId, cellId)
     }
 
-    override fun importGridData(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): GridDataImportResult? {
+    override fun importGridData(groupId: Int, dataTime: LocalDateTime?, update: Boolean, file: MultipartFile): Boolean? {
         // 鍥犱负鏄鍏ュ崼鏄熼仴娴嬫暟鎹� type濮嬬粓涓�0
         val type = 0
         // 棣栧厛鍒ゆ柇鏂囦欢绫诲瀷锛屾枃浠剁被鍨嬩笉鏄痻lsx鐩存帴鎶ラ敊
@@ -65,7 +64,7 @@
         val gridData = satelliteGridRep.fetchGridData(groupId, dataTime, type)
         // 灏嗙敤鎴峰鍏ョ殑鏂囦欢杞崲涓烘暟鎹�
         // 杞崲鐨勮繃绋嬮渶瑕佺綉鏍煎崟鍏冩牸鏁版嵁 浠ュ垽鏂綉鏍煎崟鍏冩牸鏄惁鍦ㄦ纭殑鑼冨洿鍐�
-        val gridCellsInDBSet = satelliteGridRep.fetchGridCell(groupId).asSequence().map { it?.id ?: -1 }.toSet()
+        val gridCellsInDBSet = satelliteGridRep.fetchGridCell(groupId).map { it?.id ?: -1 }.toSet()
         val importData
                 = fileExchange.exchangeGridData(ByteArrayInputStream(file.bytes), gridCellsInDBSet)
         if (update) {
@@ -78,7 +77,7 @@
                 it.groupId = gridData[0]?.groupId
             }
             satelliteGridRep.updatePM25Batch(importData)
-            return GridDataImportResult(true, "瑕嗙洊鎴愬姛")
+            return true
         }else {
             // 鐢ㄦ埛闇�瑕佹墽琛屾彃鍏�
             if (gridData.isNotEmpty()) {
@@ -90,7 +89,7 @@
                 ?.let { Date(it) }
             gridDataEntity.type = type.toByte()
             satelliteGridRep.insertGridDataAndDetail(gridDataEntity, importData)
-            return GridDataImportResult(true, "瀵煎叆鎴愬姛")
+            return true
         }
     }
 
@@ -120,7 +119,7 @@
         dataTime: LocalDateTime?,
         update: Boolean,
         file: MultipartFile
-    ): GridDataImportResult? {
+    ): Boolean? {
         // 棣栧厛鍒ゆ柇鏂囦欢绫诲瀷锛屾枃浠剁被鍨嬩笉鏄痻lsx鐩存帴鎶ラ敊
         if (!file.contentType!!.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
             throw BizException("鏂囦欢绫诲瀷閿欒锛岃涓婁紶xlsx绫诲瀷鏂囦欢")
@@ -130,7 +129,7 @@
         val gridAod = satelliteGridRep.fetchGridAod(groupId, dataTime)
         // 灏嗙敤鎴峰鍏ョ殑鏂囦欢杞崲涓烘暟鎹�
         // 杞崲鐨勮繃绋嬮渶瑕佺綉鏍煎崟鍏冩牸鏁版嵁 浠ュ垽鏂綉鏍煎崟鍏冩牸鏄惁鍦ㄦ纭殑鑼冨洿鍐�
-        val gridCellsInDBSet = satelliteGridRep.fetchGridCell(groupId).asSequence().map { it?.id ?: -1 }.toSet()
+        val gridCellsInDBSet = satelliteGridRep.fetchGridCell(groupId).map { it?.id ?: -1 }.toSet()
         val importAod
                 = fileExchange.exchangeGridAod(ByteArrayInputStream(file.bytes), gridCellsInDBSet)
         if (update) {
@@ -143,7 +142,7 @@
                 it.groupId = gridAod[0]?.groupId
             }
             satelliteGridRep.updateGridAodBatch(importAod)
-            return GridDataImportResult(true, "瑕嗙洊鎴愬姛")
+            return true
         }else {
             // 鐢ㄦ埛闇�瑕佹墽琛屾彃鍏�
             if (gridAod.isNotEmpty()) {
@@ -154,7 +153,7 @@
             gridAodEntity.dataTime = dataTime?.atZone(ZoneId.systemDefault())?.toInstant()?.toEpochMilli()
                 ?.let { Date(it) }
             satelliteGridRep.insertGridAodAndDetail(gridAodEntity, importAod)
-            return GridDataImportResult(true, "瀵煎叆鎴愬姛")
+            return true
         }
     }
 
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt
index f5b6175..af1615b 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/web/SatelliteTelemetryController.kt
@@ -61,7 +61,7 @@
         @ApiParam("缃戞牸缁刬d") @RequestParam groupId: Int,
         @ApiParam("閬ユ祴鏁版嵁鏃堕棿")
         @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") dateTime: LocalDateTime,
-        @ApiParam("瑕嗙洊鏃ф暟鎹� 0: 涓嶈鐩� 1: 瑕嗙洊") @RequestParam update: Boolean,
+        @ApiParam("鏄惁瑕嗙洊鏃ф暟鎹�") @RequestParam update: Boolean,
         @RequestParam("excel") file: MultipartFile,
     ) = resPack {
         satelliteTelemetryService.importGridData(groupId, dateTime, update, file)
@@ -85,7 +85,7 @@
         @ApiParam("缃戞牸缁刬d") @RequestParam groupId: Int,
         @ApiParam("閬ユ祴鏁版嵁鏃堕棿")
         @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") dateTime: LocalDateTime,
-        @ApiParam("瑕嗙洊鏃ф暟鎹� 0: 涓嶈鐩� 1: 瑕嗙洊") @RequestParam update: Boolean,
+        @ApiParam("鏄惁瑕嗙洊鏃ф暟鎹�") @RequestParam update: Boolean,
         @RequestParam("excel") file: MultipartFile,
     ) = resPack {
         satelliteTelemetryService.importGridAOD(groupId, dateTime, update, file)
diff --git a/src/main/resources/mapper/GridDataDetailMapper.xml b/src/main/resources/mapper/GridDataDetailMapper.xml
index 7574d75..6312fe3 100644
--- a/src/main/resources/mapper/GridDataDetailMapper.xml
+++ b/src/main/resources/mapper/GridDataDetailMapper.xml
@@ -18,15 +18,4 @@
     -->
     id, data_id, group_id, cell_id, PM25, rank
   </sql>
-
-  <!-- 娣诲姞鎵归噺鏇存柊PM25鐨勫嚱鏁� -->
-  <update id="updatePM25Batch" parameterType="java.util.List">
-    <foreach collection="list" item="item" separator=";">
-      UPDATE grid_data_detail
-      SET PM25 = #{item.pm25}
-      WHERE data_id = #{item.dataId}
-      AND group_id = #{item.groupId}
-      AND cell_id = #{item.cellId}
-    </foreach>
-  </update>
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.3