From 7d74f3fd087d4a8192ed556a6c2e3a2ea3c81cff Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期五, 28 三月 2025 17:43:48 +0800
Subject: [PATCH] 1. 新增扬尘监测数据上传功能

---
 src/main/kotlin/cn/flightfeather/supervision/business/crosstimechange/CrossTimeChangeManager.kt |  190 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 190 insertions(+), 0 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/business/crosstimechange/CrossTimeChangeManager.kt b/src/main/kotlin/cn/flightfeather/supervision/business/crosstimechange/CrossTimeChangeManager.kt
new file mode 100644
index 0000000..759a6b8
--- /dev/null
+++ b/src/main/kotlin/cn/flightfeather/supervision/business/crosstimechange/CrossTimeChangeManager.kt
@@ -0,0 +1,190 @@
+package cn.flightfeather.supervision.business.crosstimechange
+
+import cn.flightfeather.supervision.common.utils.DateUtil
+import cn.flightfeather.supervision.common.utils.ExcelUtil
+import cn.flightfeather.supervision.domain.ds1.entity.Problemlist
+import cn.flightfeather.supervision.domain.ds1.repository.ProblemRep
+import cn.flightfeather.supervision.domain.ds1.repository.SubTaskRep
+import cn.flightfeather.supervision.lightshare.vo.AreaVo
+import org.springframework.stereotype.Component
+import java.io.File
+import java.io.FileOutputStream
+import java.time.format.DateTimeFormatter
+
+/**
+ * 璺ㄦ椂闂磋法鏈堝害鏁存敼
+ * @date 2024/12/20
+ * @author feiyu02
+ */
+@Component
+class CrossTimeChangeManager(private val subTaskRep: SubTaskRep, private val problemRep: ProblemRep) {
+
+    /**
+     * 鎵ц璺ㄦ湀搴﹁法鏃堕棿鏁存敼鍒嗘瀽
+     * @param areaVo 绛涢�夋潯浠讹紝鍖呮嫭鍖哄煙銆佸満鏅被鍨嬨�佹椂闂磋寖鍥�
+     */
+    fun execute(areaVo: AreaVo, fileName: String) {
+        val pbGroupList = searchProblem(areaVo)
+        pbGroupList.forEach { changeAnalysis(it) }
+        formatToExcel(areaVo, pbGroupList, fileName)
+    }
+
+    /**
+     * 鏌ヨ鑼冨洿鍐呯殑鍦烘櫙鍙婂悇鏈堝害闂
+     */
+    fun searchProblem(areaVo: AreaVo): List<ScenePbGroup> {
+        // 鑾峰彇鑼冨洿鍐呮墍鏈夌殑宸℃煡璁板綍鍙婂叿浣撻棶棰�
+        val summary = subTaskRep.findSummary(areaVo)
+        // 鎸夌収鍦烘櫙杩涜褰掔被
+        val sceneMap = mutableMapOf<String?, ScenePbGroup>()
+        summary.forEach {
+            if (!sceneMap.containsKey(it.sceneId)) {
+                sceneMap[it.sceneId] = ScenePbGroup().apply {
+                    scene = it.scene
+                }
+            }
+            sceneMap[it.sceneId]?.apply {
+                pbGroup.add(ProblemInfo().apply {
+                    subtask = it.subtask
+                    val pList = problemRep.find(Problemlist().apply {
+                        stguid = it.stGuid
+                    })
+                    pbList.addAll(pList)
+                })
+            }
+        }
+        // 灏嗗綊绫诲ソ鐨勫悇鍦烘櫙闂鎸夌収宸℃煡鏃堕棿鍗囧簭鎺掑垪骞惰浆鎹负鏁扮粍缁撴瀯
+        val res = mutableListOf<ScenePbGroup>()
+        sceneMap.forEach { (t, u) ->
+            u.pbGroup.sortBy { it?.subtask?.planstarttime }
+            res.add(u)
+        }
+        // 灏嗗満鏅寜鐓у敮涓�缂栧彿鍗囧簭鎺掑垪锛堥潪蹇呰锛�
+        res.sortBy { it.scene?.index }
+
+        return res
+    }
+
+    /**
+     * 鍒嗘瀽鍗曚釜鍦烘櫙鍚勬湀闂鏄惁鍙互鏁存敼
+     */
+    fun changeAnalysis(scenePbGroup: ScenePbGroup) {
+        scenePbGroup.pbGroup.forEachIndexed {i,it ->
+            it?.pbList?.forEach { p ->
+                // 宸叉暣鏀圭殑闂鐣ヨ繃
+                if (p?.ischanged == true) return@forEach
+
+                // 鎵惧埌缁撴灉锛堟寚鏈変竴涓湀鏈嚭鐜版闂锛屾垨鑰呭嚭鐜颁簡姝ら棶棰樹絾宸叉暣鏀癸級锛屽垯鍏佽灏嗘鍘嗗彶闂浣滀负宸叉暣鏀�
+                var found = false
+
+                // 鍚戝悗缁湀搴︽煡鎵炬槸鍚︽湁鐩稿悓鐨勯棶棰樺嚭鐜�
+                var index = i + 1
+                while (!found && index < scenePbGroup.pbGroup.size) {
+                    val nextGroup = scenePbGroup.pbGroup[index]
+                    val result = nextGroup?.pbList?.find { nP ->
+                        nP?.ptguid == p?.ptguid
+                    }
+                    // 璇ユ湀鏈嚭鐜版闂锛屾垨鑰呭嚭鐜颁簡姝ら棶棰樹絾宸叉暣鏀�
+                    if (result == null || result.ischanged == true) {
+                        found = true
+                    }
+                    index++
+                }
+
+                // 鑻ヨ闂绗﹀悎鏁存敼鏉′欢锛屽垯娣诲姞鑷冲彲鏁存敼鍒楄〃
+                if (found) {
+                    it.pbChangeList.add(p)
+                }
+            }
+        }
+    }
+
+    /**
+     * 鏍煎紡鍖栬緭鍑鸿嚦excel鏂囦欢
+     */
+    fun formatToExcel(areaVo: AreaVo, pbGroupList: List<ScenePbGroup>, fileName: String) {
+        if (pbGroupList.isEmpty()) return
+
+        val h = mutableListOf<MutableList<Any>>()
+        val c = mutableListOf<MutableList<Any>>()
+
+        // 鐢熸垚琛ㄥご
+        val h1 = mutableListOf<Any>("鍞竴缂栧彿", "鍦烘櫙")
+        val monHead = mutableListOf<String>()
+        var time = areaVo.starttime?.toLocalDate()
+        val end = areaVo.endtime?.toLocalDate()
+        // 姝ゅ寮�濮嬫椂闂村簲涓烘湀鍒濓紝缁撴潫鏃堕棿搴斾负鏈堟湯锛屽垯鍙互鐩存帴鐢╥sBefore姣旇緝
+        while (time?.isBefore(end) == true) {
+            monHead.add(time.format(DateTimeFormatter.ofPattern("YYYY-MM")))
+            time = time.plusMonths(1)
+        }
+        monHead.forEach {
+            val str = it.split("-")[1] + "鏈�"
+            h1.addAll(listOf(str + "闂", str + "鏈暣鏀�", str + "鏈暣鏀规暟"))
+        }
+        monHead.forEach {
+            val str = it.split("-")[1] + "鏈�"
+            h1.addAll(listOf(str + "鍙暣鏀�", str + "鍘熸暣鏀规暟", str + "鏂板鍙暣鏀规暟"))
+        }
+        h.add(h1)
+
+        // 鐢熸垚鍐呭
+        pbGroupList.forEach {pbg ->
+            val index = pbg.scene?.index ?: ""
+            val name = pbg.scene?.name ?: ""
+
+            val problemContent = mutableListOf<Any>()
+            monHead.forEach {mH ->
+                val stp = pbg.pbGroup.find { pbi ->
+                    DateUtil.DateToString(pbi?.subtask?.planstarttime, DateUtil.DateStyle.YYYY_MM) == mH
+                }
+
+                if (stp == null) {
+                    problemContent.addAll(listOf("/", "/", "/"))
+                } else {
+                    // 闂
+                    problemContent.add(stp.pbList.mapIndexed { i, pb -> "${i+1}銆�${pb?.problemname}" }.joinToString
+                        ("\n"))
+                    val unchanged = stp.pbList.filter { pb-> pb?.ischanged != true }
+                    // 鏈暣鏀�
+                    problemContent.add(unchanged.mapIndexed { i, pb -> "${i+1}銆�${pb?.problemname}" }.joinToString("\n"))
+                    // 鏈暣鏀规暟
+                    problemContent.add(unchanged.size)
+                }
+            }
+
+            val willChangeContent = mutableListOf<Any>()
+            monHead.forEach {mH ->
+                val stp = pbg.pbGroup.find { pbi ->
+                    DateUtil.DateToString(pbi?.subtask?.planstarttime, DateUtil.DateStyle.YYYY_MM) == mH
+                }
+
+                if (stp == null) {
+                    willChangeContent.addAll(listOf("/", "/", "/"))
+                } else {
+                    // 鍙暣鏀�
+                    willChangeContent.add(stp.pbChangeList.mapIndexed { i, pb -> "${i+1}銆�${pb?.problemname}" }
+                        .joinToString("\n"))
+                    val changed = stp.pbList.filter { pb-> pb?.ischanged == true }
+                    // 鍘熸暣鏀规暟
+                    willChangeContent.add(changed.size)
+                    // 鏂板鍙暣鏀规暟
+                    willChangeContent.add(stp.pbChangeList.size)
+                }
+            }
+
+            val row = mutableListOf<Any>()
+            row.add(index)
+            row.add(name)
+            row.addAll(problemContent)
+            row.addAll(willChangeContent)
+
+            c.add(row)
+        }
+
+        // 鐢熸垚鏂囦欢
+        val file = File("target/${fileName}")
+        val out = FileOutputStream(file)
+        ExcelUtil.write2(out, h.map { it.toTypedArray() }, c.map { it.toTypedArray() }.toMutableList())
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3