From d9a6f3c2503795f074ac602c24467f804417ad76 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期三, 01 十二月 2021 19:41:34 +0800
Subject: [PATCH] 1. 新增用电量日分析功能
---
src/main/kotlin/com/flightfeather/uav/lightshare/service/ElectricityService.kt | 9 +
src/main/kotlin/com/flightfeather/uav/lightshare/bean/ElectricDailyInfo.kt | 92 ++++++++++++++++++
src/main/kotlin/com/flightfeather/uav/lightshare/eunm/ElectricityType.kt | 4
src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt | 8 +
src/main/kotlin/com/flightfeather/uav/domain/mapper/ElectricMinuteValueMapper.kt | 2
src/main/kotlin/com/flightfeather/uav/domain/mapper/CompanyDeviceMapper.kt | 2
src/main/kotlin/com/flightfeather/uav/dataprocess/ElectricDailyAnalysis.kt | 69 +++++++++++++
src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImpl.kt | 61 ++++++++++-
src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt | 6
src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImplTest.kt | 28 +++++
10 files changed, 267 insertions(+), 14 deletions(-)
diff --git a/src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt b/src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt
index 6cc7b95..8c93f62 100644
--- a/src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt
+++ b/src/main/kotlin/com/flightfeather/uav/dataprocess/AverageUtil.kt
@@ -2,7 +2,7 @@
/**
* 鍧囧�艰绠楀伐鍏凤紝灏嗕竴缁勮繛缁暟鎹浆鎹负鑷畾涔夊懆鏈熺殑鍧囧�兼暟鎹�
- * 瑕佹眰浼犲叆鐨勬暟鎹寜鐓ф椂闂撮『搴忔帓鍒楋紝
+ * 瑕佹眰浼犲叆鐨勬暟鎹寜鐓ф椂闂撮『搴忔搴忔帓鍒楋紝
* @param onTag 瀹氫箟鏁版嵁鏍囩鑾峰彇鍥炶皟鍑芥暟锛屽綋褰撳墠鏁版嵁鏍囩涓庝笂涓暟鎹爣绛句笉鍚屾椂锛屽嵆璁や负涓婁竴缁勬暟鎹负鍚屼竴缁勬暟鎹紝闇�姹傚嚭鍧囧��
* @param onAvg 瀹氫箟鍧囧�艰绠楁柟娉�
*/
@@ -26,11 +26,11 @@
// 璁$畻鍧囧��
list.forEach {
val tag = onTag(it)
- // 绗竴鏉℃暟鎹拰tag鐩稿悓鏃讹紝灏嗘暟鎹斁鍏ヤ复鏃剁紦瀛樺垪琛�
+ // 鏄涓�鏉℃暟鎹垨tag鐩稿悓鏃讹紝灏嗘暟鎹斁鍏ヤ复鏃剁紦瀛樺垪琛�
if (lastTag == null || tag == lastTag) {
dataSet.add(it)
}
- // 褰搕ag涓嶅悓鏃讹紝璁$畻涔嬪墠鏁版嵁鐨勫潎鍊硷紝鍚屾椂鎯呭喌涓存椂鏁版嵁缂撳瓨锛屾坊鍔犲綋鍓嶇殑鏂版暟鎹�
+ // 褰搕ag涓嶅悓鏃讹紝璁$畻涔嬪墠鏁版嵁鐨勫潎鍊硷紝鍚屾椂娓呯┖涓存椂鏁版嵁缂撳瓨锛屾坊鍔犲綋鍓嶇殑鏂版暟鎹�
else {
result.add(onAvg(dataSet))
dataSet.clear()
diff --git a/src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt b/src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt
index 6267a69..aa27311 100644
--- a/src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt
+++ b/src/main/kotlin/com/flightfeather/uav/dataprocess/AvgPair.kt
@@ -2,9 +2,17 @@
import kotlin.math.round
+/**
+ * 鍧囧�艰绠椾腑闂撮噺
+ * @param t 鎬诲拰
+ * @param c 鎬绘暟
+ */
data class AvgPair(
var t: Float, var c: Int
){
+ /**
+ * 璁$畻缁撴灉
+ */
fun avg(): Float = if (c == 0) {
0f
} else {
diff --git a/src/main/kotlin/com/flightfeather/uav/dataprocess/ElectricDailyAnalysis.kt b/src/main/kotlin/com/flightfeather/uav/dataprocess/ElectricDailyAnalysis.kt
new file mode 100644
index 0000000..aca6e91
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/dataprocess/ElectricDailyAnalysis.kt
@@ -0,0 +1,69 @@
+package com.flightfeather.uav.dataprocess
+
+import com.flightfeather.uav.common.utils.DateUtil
+import com.flightfeather.uav.domain.entity.CompanyDevice
+import com.flightfeather.uav.domain.entity.ElectricMinuteValue
+import com.flightfeather.uav.lightshare.bean.ElectricDailyInfo
+import java.time.LocalDateTime
+import java.time.ZoneId
+
+/**
+ * 鐢ㄧ數閲忔棩鍒嗘瀽
+ * Date 2021/12/1 14:53
+ * Created by feiyu
+ */
+object ElectricDailyAnalysis {
+
+ fun analysis(deviceList: List<CompanyDevice>, dataList: List<ElectricMinuteValue>): List<ElectricDailyInfo> {
+ // 杩斿洖缁撴灉
+ val result = mutableListOf<ElectricDailyInfo>()
+ // 姣忔棩缁熻淇℃伅
+ val dailyInfoMap = mutableMapOf<String, ElectricDailyInfo>()
+
+ // 1.鏁版嵁鍑嗗
+ val deviceMap = mutableMapOf<String, CompanyDevice>()
+ val dataMap = mutableMapOf<String, MutableMap<String, MutableList<ElectricMinuteValue>>>()
+ deviceList.forEach {
+ // 鐩戞祴鏁版嵁
+ if (!dataMap.containsKey(it.cdDeviceCode)) {
+ dataMap[it.cdDeviceCode] = mutableMapOf()
+ }
+ // 璁惧淇℃伅
+ deviceMap[it.cdDeviceCode] = it
+ }
+ // 2.杞鏁版嵁锛岀粺璁℃瘡鏃ョ殑鍚勯」鐗瑰緛
+ dataList.forEach {
+ // 鑾峰彇鏃ユ湡
+ val day = DateUtil.instance.dateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD) ?: ""
+ // 姣忔棩瀵瑰簲涓�缁勬暟鎹�
+ if (dataMap[it.mvStatCode]?.containsKey(day) != true) dataMap[it.mvStatCode]?.put(day, mutableListOf())
+ val dayList = dataMap[it.mvStatCode]?.get(day)!!
+ dayList.add(it)
+
+ // 2.1 鏍规嵁鏁版嵁鍒囨崲褰撳墠璁惧绫诲瀷
+ if (!dailyInfoMap.containsKey(day)) dailyInfoMap[day] = ElectricDailyInfo()
+ val dayResult = dailyInfoMap[day]!!
+ dayResult.day = day
+ dayResult.changeType(deviceMap[it.mvStatCode])
+ // 2.2 姣忔棩鑾峰彇鐨勯涓暟鎹紝鍗充负褰撴棩璇ヨ澶囧紑鍚椂闂�
+ if (dayList.size == 1) dayResult.setStartTime(it.mvDataTime)
+ // 2.3 姣忎釜鏁版嵁涓哄垎閽熷潎鍊硷紝鎬绘暟浠h〃寮�鍚椂闀�
+ dayResult.addRunTime(1)
+ // 2.4 缁熻褰撴棩璁惧杩愯鏃舵锛堝皬鏃讹級
+ val hour = LocalDateTime.ofInstant(it.mvDataTime.toInstant(), ZoneId.systemDefault()).hour
+ dayResult.addRunPeriod(hour)
+ }
+ // 2.5 缁熻鍚勫彴璁惧姣忔棩缁撴潫鏃堕棿鍙婂垎鏋愮粨鏋�
+ dataMap.forEach { (dCode, dayMap) ->
+ dayMap.forEach { (day, list) ->
+ dailyInfoMap[day]?.apply {
+ changeType(deviceMap[dCode])
+ setEndTime(list.last().mvDataTime)
+ }
+ }
+ }
+ dailyInfoMap.forEach { (_,v)-> result += v }
+
+ return result
+ }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/domain/mapper/CompanyDeviceMapper.kt b/src/main/kotlin/com/flightfeather/uav/domain/mapper/CompanyDeviceMapper.kt
index 6da2270..813fc2d 100644
--- a/src/main/kotlin/com/flightfeather/uav/domain/mapper/CompanyDeviceMapper.kt
+++ b/src/main/kotlin/com/flightfeather/uav/domain/mapper/CompanyDeviceMapper.kt
@@ -5,4 +5,4 @@
import org.apache.ibatis.annotations.Mapper
@Mapper
-interface CompanyDeviceMapper : MyMapper<CompanyDevice?>
\ No newline at end of file
+interface CompanyDeviceMapper : MyMapper<CompanyDevice>
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/domain/mapper/ElectricMinuteValueMapper.kt b/src/main/kotlin/com/flightfeather/uav/domain/mapper/ElectricMinuteValueMapper.kt
index c91ad62..0ff7af0 100644
--- a/src/main/kotlin/com/flightfeather/uav/domain/mapper/ElectricMinuteValueMapper.kt
+++ b/src/main/kotlin/com/flightfeather/uav/domain/mapper/ElectricMinuteValueMapper.kt
@@ -5,4 +5,4 @@
import org.apache.ibatis.annotations.Mapper
@Mapper
-interface ElectricMinuteValueMapper : MyMapper<ElectricMinuteValue?>
\ No newline at end of file
+interface ElectricMinuteValueMapper : MyMapper<ElectricMinuteValue>
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/bean/ElectricDailyInfo.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/ElectricDailyInfo.kt
new file mode 100644
index 0000000..cde575e
--- /dev/null
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/bean/ElectricDailyInfo.kt
@@ -0,0 +1,92 @@
+package com.flightfeather.uav.lightshare.bean
+
+import com.flightfeather.uav.domain.entity.CompanyDevice
+import com.flightfeather.uav.lightshare.eunm.ElectricityType
+import java.util.*
+
+/**
+ * 鐢ㄧ數閲忔棩鍒嗘瀽缁撴灉
+ * Date 2021/12/1 15:23
+ * Created by feiyu
+ */
+class ElectricDailyInfo {
+ var day: String = ""
+
+ /***********浜х嚎璁惧ProductionLine, pl************/
+ private var plDCode: String? = null // 璁惧缂栧彿
+ private var plSTime: Date? = null // 寮�鍚椂闂�
+ private var plETime: Date? = null // 鍏抽棴鏃堕棿
+ private var plRunTime: Int = 0 // 杩愯鏃堕暱锛堝垎閽燂級
+ private var plRunPeriod = mutableListOf<Int>()// 杩愯鏃舵锛堝皬鏃讹級
+ /***********鍑�鍖栬澶嘝urify, pf********************/
+ private var pfDCode: String? = null
+ private var pfSTime: Date? = null
+ private var pfETime: Date? = null
+ private var pfRunTime: Int = 0
+ private var pfRunPeriod = mutableListOf<Int>()
+
+ private var dailyResult: String = ""// 褰撴棩鍒嗘瀽缁撴灉鎻忚堪
+
+ // 褰撳墠璁惧绫诲瀷锛岀敤浜庢彃鍏ユ暟鎹椂鍐冲畾璧嬪�煎瓧娈�
+ private var deviceType: ElectricityType = ElectricityType.ProductionLine
+
+ fun changeType(d: CompanyDevice?) {
+ deviceType = when (d?.cdType) {
+ ElectricityType.ProductionLine.value -> {
+ plDCode = d.cdDeviceCode
+ ElectricityType.ProductionLine
+ }
+ ElectricityType.Purify.value -> {
+ pfDCode = d.cdDeviceCode
+ ElectricityType.Purify
+ }
+ else -> ElectricityType.UnKnow
+ }
+ }
+
+ fun setStartTime(date: Date) {
+ when (deviceType) {
+ ElectricityType.ProductionLine -> plSTime = date
+ ElectricityType.Purify -> pfSTime = date
+ else -> Unit
+ }
+ }
+
+ fun setEndTime(date: Date) {
+ when (deviceType) {
+ ElectricityType.ProductionLine -> plETime = date
+ ElectricityType.Purify -> pfETime = date
+ else -> Unit
+ }
+ }
+
+ /**
+ * 澧炲姞杩愯鏃堕暱
+ */
+ fun addRunTime(min: Int) {
+ when (deviceType) {
+ ElectricityType.ProductionLine -> plRunTime += min
+ ElectricityType.Purify -> pfRunTime += min
+ else -> Unit
+ }
+ }
+
+ /**
+ * 娣诲姞杩愯鏃舵(灏忔椂)
+ */
+ fun addRunPeriod(hour: Int) {
+ when (deviceType) {
+ ElectricityType.ProductionLine -> if (!plRunPeriod.contains(hour)) plRunPeriod += hour
+ ElectricityType.Purify -> if (!pfRunPeriod.contains(hour)) pfRunPeriod += hour
+ else -> Unit
+ }
+ }
+
+ /**
+ * 缁熻寰楀嚭褰撴棩鍒嗘瀽缁撴灉
+ */
+ fun getResult() {
+ // 1. 璁惧寮�鍚叧闂槸鍚﹀悎瑙�
+// val timeDiff =
+ }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/eunm/ElectricityType.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/eunm/ElectricityType.kt
index 64eb3a1..1cdb0b4 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/eunm/ElectricityType.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/eunm/ElectricityType.kt
@@ -8,5 +8,7 @@
//浜х嚎璁惧
ProductionLine(0),
//鍑�鍖栬澶�
- Purify(1)
+ Purify(1),
+ //鏈煡璁惧
+ UnKnow(99),
}
\ No newline at end of file
diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/ElectricityService.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/ElectricityService.kt
index 3022c55..f961ca2 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/ElectricityService.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/ElectricityService.kt
@@ -3,6 +3,7 @@
import com.flightfeather.uav.domain.entity.ElectricMinuteValue
import com.flightfeather.uav.lightshare.bean.BaseResponse
import com.flightfeather.uav.lightshare.bean.DataVo
+import com.flightfeather.uav.lightshare.bean.ElectricDailyInfo
import com.flightfeather.uav.lightshare.bean.ElectricVo
interface ElectricityService {
@@ -13,5 +14,13 @@
fun getByCompany(cId:String):BaseResponse<List<ElectricMinuteValue>>
+ /**
+ * 鑾峰彇浼佷笟鐢ㄧ數閲忔暣鍚堟暟鎹�
+ */
fun getElectricityInfo(cId: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<ElectricVo>>
+
+ /**
+ * 浼佷笟鐢ㄧ數閲忔棩缁熻淇℃伅
+ */
+ fun dailyStatistics(cId: String, startTime: String?, endTime: String?): BaseResponse<List<ElectricDailyInfo>>
}
\ No newline at end of file
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 01496d2..d055539 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
@@ -1,25 +1,26 @@
package com.flightfeather.uav.lightshare.service.impl
import com.flightfeather.uav.common.utils.DateUtil
+import com.flightfeather.uav.dataprocess.ElectricDailyAnalysis
import com.flightfeather.uav.domain.entity.CompanyDevice
import com.flightfeather.uav.domain.entity.ElectricMinuteValue
import com.flightfeather.uav.domain.entity.toAirData
import com.flightfeather.uav.domain.mapper.CompanyDeviceMapper
import com.flightfeather.uav.domain.mapper.ElectricMinuteValueMapper
-import com.flightfeather.uav.lightshare.bean.BaseResponse
-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.bean.*
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
+import org.springframework.format.annotation.DateTimeFormat
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
+import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
+import java.time.format.DateTimeParseException
import kotlin.math.round
@Service
@@ -29,11 +30,9 @@
private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
private var dateFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
+ private var dateFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
- override fun getMinuteData(
- deviceCode: String, startTime: String?,
- endTime: String?, page: Int?, perPage: Int?
- ): BaseResponse<List<ElectricMinuteValue>> {
+ override fun getMinuteData(deviceCode: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<ElectricMinuteValue>> {
val perP = perPage ?: 60
val p = page ?: 1
val sTime = startTime?.let { dateFormatter.parse(it) }
@@ -277,6 +276,12 @@
return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
}
+ /**
+ * 鑾峰彇璁惧褰撳墠杩愯鐘舵��
+ * @param e 璁惧鐢ㄧ數閲忕洃娴嬫暟鎹�
+ * @param d 浼佷笟璁惧淇℃伅
+ * @return 鏁版嵁瀵瑰簲缁撴灉淇℃伅锛�<鐘舵�佺紪鍙凤紝鐘舵�佹弿杩帮紝鐢垫祦鍧囧��>
+ */
private fun getStatus(e: ElectricMinuteValue?, d: CompanyDevice?): Triple<String, String, Double> {
var values = mutableListOf<Int>().apply {
d?.cdLimits?.split(";")?.forEach {
@@ -305,4 +310,44 @@
}
return Triple(status.last(), statusNames.last(), avg)
}
+
+ override fun dailyStatistics(cId: String, startTime: String?, endTime: String?): BaseResponse<List<ElectricDailyInfo>> {
+ // 鏍规嵁浼佷笟id鑾峰彇瀵瑰簲璁惧
+ val devices = companyDeviceMapper.selectByExample(Example(CompanyDevice::class.java).apply {
+ createCriteria().andEqualTo("cdCompanyId", cId)
+ })
+ val deviceCodeList = mutableListOf<String>()
+ devices.forEach { it?.let { deviceCodeList.add(it.cdDeviceCode) }}
+
+ val st:LocalDateTime
+ val et:LocalDateTime
+ // 褰撴病鏈夊紑濮嬫垨缁撴潫鏃堕棿鏃讹紝浣跨敤鏈�鏂颁竴鏉℃暟鎹椂闂寸殑褰撳ぉ浣滀负缁熻鏃堕棿
+ if (startTime == null || endTime == null) {
+ val em = electricMinuteValueMapper.selectOneByExample(Example(ElectricMinuteValue::class.java).apply {
+ createCriteria().andIn("mvStatCode", deviceCodeList)
+ orderBy("mvDataTime").desc()
+ })
+ val dataTime = LocalDateTime.ofInstant(em?.mvDataTime?.toInstant(), ZoneId.systemDefault())
+ st = dataTime.withHour(0).withMinute(0).withSecond(0)
+ et = dataTime.withHour(23).withMinute(59).withSecond(59)
+ }
+ // 褰撴湁寮�濮嬬粨鏉熸椂闂存椂锛屽垽鏂牸寮忔槸鍚︽纭�
+ else {
+ try {
+ st = LocalDateTime.parse(startTime, dateFormatter3).withHour(0).withMinute(0).withSecond(0)
+ et = LocalDateTime.parse(endTime, dateFormatter3).withHour(23).withMinute(59).withSecond(59)
+ } catch (e: DateTimeParseException) {
+ return BaseResponse(false, "鏃堕棿鏍煎紡閿欒锛屽簲涓簓yyy-MM-dd hh:mm:dd")
+ }
+ }
+
+ val dataList = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
+ createCriteria().andIn("mvStatCode", deviceCodeList)
+ .andBetween("mvDataTime", st, et)
+ orderBy("mvDataTime")
+ })
+ val result = ElectricDailyAnalysis.analysis(devices, dataList)
+
+ return BaseResponse(true, data = result)
+ }
}
\ No newline at end of file
diff --git a/src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImplTest.kt b/src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImplTest.kt
new file mode 100644
index 0000000..3be2cb2
--- /dev/null
+++ b/src/test/kotlin/com/flightfeather/uav/lightshare/service/impl/ElectricityServiceImplTest.kt
@@ -0,0 +1,28 @@
+package com.flightfeather.uav.lightshare.service.impl
+
+import com.flightfeather.uav.lightshare.service.ElectricityService
+import org.junit.Test
+
+import org.junit.Assert.*
+import org.junit.runner.RunWith
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.test.context.junit4.SpringRunner
+
+/**
+ * Date 2021/12/1 16:49
+ * Created by feiyu
+ */
+@RunWith(SpringRunner::class)
+@SpringBootTest
+class ElectricityServiceImplTest {
+
+ @Autowired
+ lateinit var electricityService: ElectricityService
+
+ @Test
+ fun dailyStatistics() {
+ val r = electricityService.dailyStatistics("J3euwNl19WZvH7iE", "2021-07-16 00:00:00", "2021-07-16 00:00:00")
+ println(r)
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3