From d04e699fcd11aa715439d8ab87827a3984450ccd Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期一, 22 十一月 2021 17:39:13 +0800
Subject: [PATCH] 1. 用电量数据平滑处理缺失的分钟值

---
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt |  174 +++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 111 insertions(+), 63 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt
index 1a696eb..01496d2 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt
@@ -10,6 +10,7 @@
 import com.flightfeather.uav.lightshare.bean.DataHead
 import com.flightfeather.uav.lightshare.bean.DataVo
 import com.flightfeather.uav.lightshare.bean.ElectricVo
+import com.flightfeather.uav.lightshare.eunm.ElectricityType
 import com.flightfeather.uav.lightshare.service.ElectricityService
 import com.flightfeather.uav.socket.bean.AirData
 import com.github.pagehelper.PageHelper
@@ -19,6 +20,7 @@
 import java.time.LocalDateTime
 import java.time.ZoneId
 import java.time.format.DateTimeFormatter
+import kotlin.math.round
 
 @Service
 class ElectricityServiceImpl(
@@ -102,22 +104,21 @@
             createCriteria().andEqualTo("cdCompanyId", cId)
         })
         //浜х嚎椋庢満
-        var d1: String? = null
+        var d1: CompanyDevice? = null
         //搴熸皵鍑�鍖栬缃�
-        var d2: String? = null
+        var d2: CompanyDevice? = null
 
         deviceCodeList.forEach {
-            // FIXME: 2021/11/5 姝ゅ鏆傛椂鍐欐锛屽悗缁慨鏀�
-            if (it?.cdDeviceCode == "31011020210602" || it?.cdDeviceCode == "31011020210603") {
-                d1 = it.cdDeviceCode
-            }else if (it?.cdDeviceCode == "31011020210601" || it?.cdDeviceCode == "31011020210604") {
-                d2 = it.cdDeviceCode
+            if (it?.cdType == ElectricityType.ProductionLine.value) {
+                d1 = it
+            }else if (it?.cdType == ElectricityType.Purify.value) {
+                d2 = it
             }
         }
 
         var pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
         val dataList1 = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
-            createCriteria().andEqualTo("mvStatCode", d1)
+            createCriteria().andEqualTo("mvStatCode", d1?.cdDeviceCode)
                 .apply {
                     sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                     eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
@@ -134,7 +135,7 @@
         }
         pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
         val dataList2 = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
-            createCriteria().andEqualTo("mvStatCode", d2)
+            createCriteria().andEqualTo("mvStatCode", d2?.cdDeviceCode)
                 .apply {
                     sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                     eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
@@ -178,83 +179,130 @@
             null
         }
 
-        var lsT = LocalDateTime.ofInstant(sT?.toInstant(), ZoneId.systemDefault()).withSecond(0)
-        val leT = LocalDateTime.ofInstant(eT?.toInstant(), ZoneId.systemDefault()).withSecond(0)
+        if (sT == null || eT == null) {
+            return BaseResponse(true, data = result)
+        }
 
-        if (sT != null && eT != null) {
-            while (!lsT.isAfter(leT)) {
-                val vo = ElectricVo(lsT.format(dateFormatter2))
-                if (dataList1.isNotEmpty()) {
-                    val d = dataList1[0]
-                    val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
-                    if (lsT.isEqual(t)) {
+        var lsT = LocalDateTime.ofInstant(sT.toInstant(), ZoneId.systemDefault()).withSecond(0)
+        val leT = LocalDateTime.ofInstant(eT.toInstant(), ZoneId.systemDefault()).withSecond(0)
+
+        // 鏍规嵁涓ゅ彴璁惧鐨勬渶闀胯捣濮嬫椂闂达紝璁$畻鍏朵腑姣忎竴鍒嗛挓鐨勫搴斿潎鍊�
+        while (!lsT.isAfter(leT)) {
+            // 涓ゅ彴璁惧鐨勬暟鎹牴鎹椂闂村悎骞朵负涓�涓粨鏋勪綋
+            val vo = ElectricVo(lsT.format(dateFormatter2))
+
+            // FIXME: 2021/11/22 姝ゅ鐢变簬鍓嶇璁惧鐨勯噰鏍锋椂闂翠笉鏍囧噯 锛岄噰鏍峰懆鏈熷苟涓嶆槸涓ユ牸鐨�1鍒嗛挓锛屽鑷撮噰鏍锋椂闂存湁鏃朵細缂哄皯1鍒嗛挓鐨勬暟鎹�
+            // FIXME: 2021/11/22 鍥犳锛屽綋鏌愪竴鍒嗛挓璇ヨ澶囨暟鎹疆绌烘椂锛岄噰鐢ㄥ墠涓�涓暟鎹綔涓哄~鍏�
+            if (dataList1.isNotEmpty()) {
+                val d = dataList1[0]
+                val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
+                if (lsT.isEqual(t)) {
+                    vo.apply {
+                        d1eA = d?.mvElectricityA ?: .0
+                        d1eB = d?.mvElectricityB ?: .0
+                        d1eC = d?.mvElectricityC ?: .0
+                        val s = getStatus(d, d1)
+                        d1Status = s.first
+                        d1StatusName = s.second
+                        d1Avg = s.third
+                    }
+                    dataList1.removeAt(0)
+                } else {
+                    result.lastOrNull()?.let {
                         vo.apply {
-                            d1eA = d?.mvElectricityA ?: .0
-                            d1eB = d?.mvElectricityB ?: .0
-                            d1eC = d?.mvElectricityC ?: .0
-                            d1Status = getStatus(d)
+                            d1eA = it.d1eA
+                            d1eB = it.d1eB
+                            d1eC = it.d1eC
+                            d1Status = it.d1Status
+                            d1StatusName = it.d1StatusName
+                            d1Avg = it.d1Avg
                         }
-                        dataList1.removeAt(0)
                     }
                 }
-                if (dataList2.isNotEmpty()) {
-                    val d = dataList2[0]
-                    val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
-                    if (lsT.isEqual(t)) {
-                        vo.apply {
-                            d2eA = d?.mvElectricityA ?: .0
-                            d2eB = d?.mvElectricityB ?: .0
-                            d2eC = d?.mvElectricityC ?: .0
-                            d2Status = getStatus(d)
-                        }
-                        dataList2.removeAt(0)
+            } else {
+                result.lastOrNull()?.let {
+                    vo.apply {
+                        d1eA = it.d1eA
+                        d1eB = it.d1eB
+                        d1eC = it.d1eC
+                        d1Status = it.d1Status
+                        d1StatusName = it.d1StatusName
+                        d1Avg = it.d1Avg
                     }
                 }
-                result.add(vo)
-
-                lsT = lsT.plusMinutes(1)
             }
+            if (dataList2.isNotEmpty()) {
+                val d = dataList2[0]
+                val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
+                if (lsT.isEqual(t)) {
+                    vo.apply {
+                        d2eA = d?.mvElectricityA ?: .0
+                        d2eB = d?.mvElectricityB ?: .0
+                        d2eC = d?.mvElectricityC ?: .0
+                        val s = getStatus(d, d2)
+                        d2Status = s.first
+                        d2StatusName = s.second
+                        d2Avg = s.third
+                    }
+                    dataList2.removeAt(0)
+                } else {
+                    result.lastOrNull()?.let {
+                        vo.apply {
+                            d2eA = it.d2eA
+                            d2eB = it.d2eB
+                            d2eC = it.d2eC
+                            d2Status = it.d2Status
+                            d2StatusName = it.d2StatusName
+                            d2Avg = it.d2Avg
+                        }
+                    }
+                }
+            } else {
+                result.lastOrNull()?.let {
+                    vo.apply {
+                        d2eA = it.d2eA
+                        d2eB = it.d2eB
+                        d2eC = it.d2eC
+                        d2Status = it.d2Status
+                        d2StatusName = it.d2StatusName
+                        d2Avg = it.d2Avg
+                    }
+                }
+            }
+            result.add(vo)
+
+            lsT = lsT.plusMinutes(1)
         }
 
         return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
     }
 
-    // FIXME: 2021/11/5 姝ゅ鏆傛椂鍐欐锛屽悗缁慨鏀�
-    private fun getStatus(e: ElectricMinuteValue?): String {
-        var values = listOf(1, 100)
-        var status = listOf("0", "2", "3")
+    private fun getStatus(e: ElectricMinuteValue?, d: CompanyDevice?): Triple<String, String, Double> {
+        var values = mutableListOf<Int>().apply {
+            d?.cdLimits?.split(";")?.forEach {
+                it.toIntOrNull()?.let { i -> add(i) }
+            }
+        }
+        var status = d?.cdStatus?.split(";") ?: emptyList()
+        var statusNames = d?.cdStatusName?.split(";") ?: emptyList()
+        if (values.isEmpty()) values = mutableListOf(1, 100)
+        if (status.isEmpty()) status = listOf("0", "2", "3")
+        if (statusNames.isEmpty()) statusNames = listOf("寰呮満", "杩愯", "瓒呰礋鑽�")
         if (e == null) {
-            return status.first()
+            return Triple(status.first(), statusNames.first(), .0)
         }
-        when (e.mvStatCode) {
-            "31011020210601" -> {
-                values = listOf(1, 100)
-                status = listOf("0", "2", "3")
-            }
-            "31011020210602" -> {
-                values = listOf(13, 30)
-                status = listOf("0", "2", "3")
-            }
-            "31011020210603" -> {
-                values = listOf(1, 50, 80)
-                status = listOf("0", "1", "2", "3")
-            }
-            "31011020210604" -> {
-                values = listOf(15, 90, 125)
-                status = listOf("0", "1", "2", "3")
-            }
-        }
+
         val electricityList = mutableListOf<Double>()
         electricityList.add(e.mvElectricityA)
         electricityList.add(e.mvElectricityB)
         electricityList.add(e.mvElectricityC)
 
-        val avg = electricityList.average()
+        val avg = round(electricityList.average() * 100) / 100
         for (i in values.indices) {
             if (avg < values[i]) {
-                return status[i]
+                return Triple(status[i], statusNames[i], avg)
             }
         }
-        return status.last()
+        return Triple(status.last(), statusNames.last(), avg)
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3