riku
2021-12-01 d9a6f3c2503795f074ac602c24467f804417ad76
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, "时间格式错误,应为yyyy-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)
    }
}