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/impl/ElectricityServiceImpl.kt | 288 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 280 insertions(+), 8 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 afe80b0..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,20 +1,27 @@ 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.* +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 class ElectricityServiceImpl( @@ -22,11 +29,10 @@ ) : ElectricityService { 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) } @@ -39,9 +45,17 @@ sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) } eTime?.let { andLessThanOrEqualTo("mvDataTime", it) } } - orderBy("mvDataTime") + orderBy("mvDataTime").apply { + // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁 + if (startTime == null && endTime == null) { + desc() + } + } }).forEach { it?.let { result.add(it) } + } + if (startTime == null && endTime == null) { + result.reverse() } return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result) } @@ -78,4 +92,262 @@ return BaseResponse(true, data = result) } + + override fun getElectricityInfo(cId: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<ElectricVo>> { + val perP = perPage ?: 60 + val p = page ?: 1 + val sTime = startTime?.let { dateFormatter.parse(it) } + val eTime = endTime?.let { dateFormatter.parse(it) } + val result = mutableListOf<ElectricVo>() + val deviceCodeList = companyDeviceMapper.selectByExample(Example(CompanyDevice::class.java).apply { + createCriteria().andEqualTo("cdCompanyId", cId) + }) + //浜х嚎椋庢満 + var d1: CompanyDevice? = null + //搴熸皵鍑�鍖栬缃� + var d2: CompanyDevice? = null + + deviceCodeList.forEach { + 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?.cdDeviceCode) + .apply { + sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) } + eTime?.let { andLessThanOrEqualTo("mvDataTime", it) } + } + orderBy("mvDataTime").apply { + // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁 + if (startTime == null && endTime == null) { + desc() + } + } + }) + if (startTime == null && endTime == null) { + dataList1.reverse() + } + pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP) + val dataList2 = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply { + createCriteria().andEqualTo("mvStatCode", d2?.cdDeviceCode) + .apply { + sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) } + eTime?.let { andLessThanOrEqualTo("mvDataTime", it) } + } + orderBy("mvDataTime").apply { + // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁 + if (startTime == null && endTime == null) { + desc() + } + } + }) + if (startTime == null && endTime == null) { + dataList2.reverse() + } + + // FIXME: 2021/11/5 杩斿洖缁撴灉闇�瑕佹牴鎹袱鍙拌澶囩殑鏁版嵁濮嬫湯鏃堕棿锛岄�夊彇鏈�闀跨殑涓や釜濮嬫湯鏃堕棿 + val sT = if (dataList1.isEmpty() && dataList2.isNotEmpty()) { + dataList2[0]?.mvDataTime + }else if (dataList1.isNotEmpty() && dataList2.isEmpty()) { + dataList1[0]?.mvDataTime + } else if (dataList1.isNotEmpty() && dataList2.isNotEmpty()) { + if (dataList1[0]?.mvDataTime?.after(dataList2[0]?.mvDataTime) == true) { + dataList2[0]?.mvDataTime + } else { + dataList1[0]?.mvDataTime + } + } else { + null + } + val eT = if (dataList1.isEmpty() && dataList2.isNotEmpty()) { + dataList2.last()?.mvDataTime + }else if (dataList1.isNotEmpty() && dataList2.isEmpty()) { + dataList1.last()?.mvDataTime + } else if (dataList1.isNotEmpty() && dataList2.isNotEmpty()) { + if (dataList1.last()?.mvDataTime?.after(dataList2.last()?.mvDataTime) == true) { + dataList1.last()?.mvDataTime + } else { + dataList2.last()?.mvDataTime + } + } else { + null + } + + if (sT == null || eT == null) { + return BaseResponse(true, data = result) + } + + 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 = it.d1eA + d1eB = it.d1eB + d1eC = it.d1eC + d1Status = it.d1Status + d1StatusName = it.d1StatusName + d1Avg = it.d1Avg + } + } + } + } else { + result.lastOrNull()?.let { + vo.apply { + d1eA = it.d1eA + d1eB = it.d1eB + d1eC = it.d1eC + d1Status = it.d1Status + d1StatusName = it.d1StatusName + d1Avg = it.d1Avg + } + } + } + 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) + } + + /** + * 鑾峰彇璁惧褰撳墠杩愯鐘舵�� + * @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 { + 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 Triple(status.first(), statusNames.first(), .0) + } + + val electricityList = mutableListOf<Double>() + electricityList.add(e.mvElectricityA) + electricityList.add(e.mvElectricityB) + electricityList.add(e.mvElectricityC) + + val avg = round(electricityList.average() * 100) / 100 + for (i in values.indices) { + if (avg < values[i]) { + return Triple(status[i], statusNames[i], avg) + } + } + 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 -- Gitblit v1.9.3