feiyu02
2024-02-02 83455446544f89b0663a3f520744331ad8259289
src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
@@ -1,5 +1,6 @@
package com.flightfeather.uav.lightshare.service.impl
import com.flightfeather.uav.common.exception.ResponseErrorException
import com.flightfeather.uav.common.scaleMap
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.common.utils.ExcelUtil
@@ -17,10 +18,12 @@
import com.github.pagehelper.PageHelper
import org.apache.poi.xssf.streaming.SXSSFWorkbook
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.io.ByteArrayInputStream
import java.io.File
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.ZoneId
@@ -28,6 +31,7 @@
import java.util.*
import javax.servlet.http.HttpServletResponse
import javax.swing.text.DateFormatter
import kotlin.math.sqrt
@Service
class RealTimeDataServiceImpl(
@@ -37,14 +41,25 @@
    private val realTimeDataUavMapper: RealTimeDataUavMapper,
    private val realTimeDataGridMapper: RealTimeDataGridMapper,
    private val realTimeDataGridOptMapper: RealTimeDataGridOptMapper,
    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper
    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper,
    private val missionMapper: MissionMapper,
) : RealTimeDataService {
    @Value("\${filePath}")
    lateinit var filePath: String
    private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    private var dateFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
    private val fileExchange = FileExchange()
    override fun getSecondData(deviceCode: String?, startTime: String?, endTime: String?, type: Int?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
    override fun getSecondData(
        deviceCode: String?,
        startTime: String?,
        endTime: String?,
        type: Int?,
        page: Int?,
        perPage: Int?,
    ): BaseResponse<List<DataVo>> {
        val _perPage = perPage ?: 60
        val _page = page ?: 1
        val sTime = startTime?.let { dateFormatter.parse(it) }
@@ -127,7 +142,12 @@
        }
    }
    override fun getNextData(deviceCode: String, updateTime: String, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
    override fun getNextData(
        deviceCode: String,
        updateTime: String,
        page: Int?,
        perPage: Int?,
    ): BaseResponse<List<DataVo>> {
        val _perPage = perPage ?: 60
        val _page = page ?: 1
        val pageInfo = PageHelper.startPage<RealTimeData>(_page, _perPage)
@@ -169,7 +189,52 @@
        fileExchange.exchangeBoatData("0c0000000001", f).forEach {
            realTimeDataMapper.insert(it)
        }
        return BaseResponse(true, data = DataImportResult(""))
        return BaseResponse(true, data = DataImportResult("成功"))
    }
    override fun importJinanData(code: String, file: MultipartFile): DataImportResult {
        val f = ByteArrayInputStream(file.bytes)
        val result = fileExchange.exchangeJinanData(code, f)
        if (result.isNotEmpty()) {
            val first = result.first()
            val t = DateUtil.instance.dateToString(first.dataTime, DateUtil.DateStyle.YYYY_MM_DD)
            val last = result.last()
            val mission = Mission().apply {
                val tag = code.substring(0, 2) + "-" + code.substring(code.length - 2, code.length)
                missionCode = "SH-JA-${tag}-${t}"
                deviceType = UWDeviceType.getType(code)?.value
                deviceCode = code
            }
            missionMapper.selectOne(mission)?.run { throw ResponseErrorException("该设备该时段任务已存在,无法重复导入") }
            mission.apply {
                startTime = first.dataTime
                endTime = last.dataTime
            }
            missionMapper.insert(mission)
            realTimeDataVehicleMapper.insertList(result)
        }
        return DataImportResult("成功")
    }
    override fun downloadTemplate(response: HttpServletResponse): Boolean {
        val fileName = "JinAn-Template.xlsx"
        val path = (Thread.currentThread().contextClassLoader?.getResource("/")?.path
            ?: "src/main/resources") + "/templates/" + fileName
        val file = File(path)
        if (file.exists()) {
            val fName = Base64.getEncoder().encodeToString(fileName.toByteArray())
            response.apply {
                setHeader("Content-Disposition", "attachment;filename=$fName")
                setHeader("fileName", fName)
                addHeader("Access-Control-Expose-Headers", "fileName")
                contentType = "application/vnd.ms-excel;charset=UTF-8"
                setHeader("Pragma", "no-cache")
                setHeader("Cache-Control", "no-cache")
                setDateHeader("Expires", 0)
            }
            response.outputStream.write(file.readBytes())
        }
        return true
    }
    override fun outToWorkbook(deviceCode: String, startTime: String, endTime: String): SXSSFWorkbook {
@@ -191,13 +256,19 @@
            })
            if (r.isNotEmpty()) {
                val heads = if (page == 1) {
                    println("[${DateUtil.instance.dateToString(Date(), DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)}] totalPage: ${pageInfo.pages}")
                    println("[${
                        DateUtil.instance.dateToString(Date(),
                            DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
                    }] totalPage: ${pageInfo.pages}")
                    getTableTitle(r[0])
                } else {
                    emptyList()
                }
                val contents = getTableContents(r)
                print("[${DateUtil.instance.dateToString(Date(), DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)}] currentPage: ${pageInfo.pageNum}......")
                print("[${
                    DateUtil.instance.dateToString(Date(),
                        DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
                }] currentPage: ${pageInfo.pageNum}......")
                rowIndex = ExcelUtil.write(heads, contents, workbook, row = rowIndex)
                println("output done")
            }
@@ -207,7 +278,12 @@
        return workbook
    }
    override fun outToExcel(deviceCode: String, startTime: String, endTime: String, response: HttpServletResponse): HttpServletResponse {
    override fun outToExcel(
        deviceCode: String,
        startTime: String,
        endTime: String,
        response: HttpServletResponse,
    ): HttpServletResponse {
        val workbook = outToWorkbook(deviceCode, startTime, endTime)
        val out = response.outputStream
@@ -231,7 +307,13 @@
        return contents
    }
    private fun getOriginData(deviceCode: String?, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
    override fun getOriginData(
        deviceCode: String?,
        startTime: String?,
        endTime: String?,
        page: Int?,
        perPage: Int?,
    ): BaseResponse<List<DataVo>> {
        val _perPage = perPage ?: 60
        val _page = page ?: 1
        val sTime = startTime?.let { dateFormatter.parse(it) }
@@ -291,17 +373,17 @@
        var count = 0
        val minFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
        val averageUtil = AverageUtil<RealTimeDataGrid, RealTimeDataGridMin>({d ->
        val averageUtil = AverageUtil<RealTimeDataGridOpt, RealTimeDataGridMin>({ d ->
            minFormatter.format(d.dataTime)
        },{list ->
        }, { list ->
            list.avg()
        })
        while (total == -1 || page <= total) {
            println("------均值计算start------")
            val p = PageHelper.startPage<RealTimeDataGrid>(page, 50000)
            val res = realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
            val p = PageHelper.startPage<RealTimeDataGridOpt>(page, 50000)
            val res = realTimeDataGridOptMapper.selectByExample(Example(RealTimeDataGridOpt::class.java).apply {
                createCriteria().andBetween("dataTime", "2021-06-01 00:00:00", "2021-11-05 00:00:00")
            })
@@ -364,6 +446,7 @@
                    // 单独把原始的H2S乘以系数
                    h2s = d2.values?.get(2)?.factorData?.toFloat()?.times(scaleList[2])
                    h2s = h2s?.let { sqrt(it) * 2 }
                }
                // 更新秒级值