From 01eae19a4870033e879a3faa6749eece92926cab Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期五, 26 一月 2024 17:10:55 +0800
Subject: [PATCH] 1. 新增多项yml配置文件 2. 新增导入静安监测数据功能模块(未完成)

---
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |  382 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 360 insertions(+), 22 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
index 0872846..54815f4 100644
--- a/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
+++ b/src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt
@@ -1,47 +1,385 @@
 package com.flightfeather.uav.lightshare.service.impl
 
+import com.flightfeather.uav.common.scaleMap
+import com.flightfeather.uav.common.utils.DateUtil
+import com.flightfeather.uav.common.utils.ExcelUtil
+import com.flightfeather.uav.common.utils.FileExchange
 import com.flightfeather.uav.common.utils.GsonUtils
-import com.flightfeather.uav.domain.entity.RealTimeData
-import com.flightfeather.uav.domain.mapper.RealTimeDataMapper
-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.dataprocess.AverageUtil
+import com.flightfeather.uav.domain.entity.*
+import com.flightfeather.uav.domain.mapper.*
+import com.flightfeather.uav.lightshare.bean.*
 import com.flightfeather.uav.lightshare.service.RealTimeDataService
+import com.flightfeather.uav.model.epw.EPWDataPrep
+import com.flightfeather.uav.repository.AirDataRepository
 import com.flightfeather.uav.socket.bean.AirData
+import com.flightfeather.uav.socket.eunm.UWDeviceType
 import com.github.pagehelper.PageHelper
+import org.apache.poi.xssf.streaming.SXSSFWorkbook
+import org.springframework.beans.BeanUtils
 import org.springframework.stereotype.Service
+import org.springframework.web.multipart.MultipartFile
 import tk.mybatis.mapper.entity.Example
-import java.text.DateFormat
+import java.io.ByteArrayInputStream
 import java.text.SimpleDateFormat
+import java.time.LocalDateTime
+import java.time.ZoneId
+import java.time.format.DateTimeFormatter
+import java.util.*
+import javax.servlet.http.HttpServletResponse
+import javax.swing.text.DateFormatter
+import kotlin.math.sqrt
 
 @Service
-class RealTimeDataServiceImpl(val realTimeDataMapper: RealTimeDataMapper) : RealTimeDataService {
+class RealTimeDataServiceImpl(
+    private val realTimeDataMapper: RealTimeDataMapper,
+    private val airDataRepository: AirDataRepository,
+    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
+    private val realTimeDataUavMapper: RealTimeDataUavMapper,
+    private val realTimeDataGridMapper: RealTimeDataGridMapper,
+    private val realTimeDataGridOptMapper: RealTimeDataGridOptMapper,
+    private val realTimeDataGridMinMapper: RealTimeDataGridMinMapper
+) : RealTimeDataService {
 
     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?, 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) }
         val eTime = endTime?.let { dateFormatter.parse(it) }
+//        var pageInfo = PageHelper.startPage<BaseRealTimeData>(_page, _perPage)
+        var pageNum = 1
+        var pages = 0
+        val result = mutableListOf<DataVo>()
+
+        when (UWDeviceType.getType(deviceCode)) {
+            UWDeviceType.VEHICLE -> {
+                val pageInfo = PageHelper.startPage<RealTimeDataVehicle>(_page, _perPage)
+                realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach { result.add(it.toDataVo()) }
+                pageNum = pageInfo.pageNum
+                pages = pageInfo.pages
+            }
+            UWDeviceType.UAV -> {
+                val pageInfo = PageHelper.startPage<RealTimeDataUav>(_page, _perPage)
+                realTimeDataUavMapper.selectByExample(Example(RealTimeDataUav::class.java).apply {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach { result.add(it.toDataVo()) }
+                pageNum = pageInfo.pageNum
+                pages = pageInfo.pages
+            }
+            UWDeviceType.GRID -> {
+                // 缃戞牸鍖栫洃娴嬬绾у��
+                if (type == null || type == 0) {
+                    val pageInfo = PageHelper.startPage<RealTimeDataGrid>(_page, _perPage)
+                    realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
+                        getSecondDataExample(this, deviceCode, sTime, eTime)
+                    }).forEach { result.add(it.toDataVo()) }
+                    pageNum = pageInfo.pageNum
+                    pages = pageInfo.pages
+                }
+                // 缃戞牸鍖栫洃娴嬪垎閽熷��
+                else if (type == 1) {
+                    val pageInfo = PageHelper.startPage<RealTimeDataGridMin>(_page, _perPage)
+                    realTimeDataGridMinMapper.selectByExample(Example(RealTimeDataGridMin::class.java).apply {
+                        getSecondDataExample(this, deviceCode, sTime, eTime)
+                    }).forEach { result.add(it.toDataVo()) }
+                    pageNum = pageInfo.pageNum
+                    pages = pageInfo.pages
+                }
+            }
+            else -> {
+                // 浠庡師濮嬫暟鎹〃涓幏鍙栨暟鎹�
+                val pageInfo = PageHelper.startPage<RealTimeData>(_page, _perPage)
+                realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach {
+                    if (it.longitude == null || it.latitude == null) {
+                        return@forEach
+                    }
+                    result.add(it.toDataVo())
+                }
+                pageNum = pageInfo.pageNum
+                pages = pageInfo.pages
+            }
+        }
+
+        if (startTime == null && endTime == null) {
+            result.reverse()
+        }
+        return BaseResponse(true, head = DataHead(pageNum, pages), data = result)
+    }
+
+    private fun getSecondDataExample(example: Example, deviceCode: String?, sTime: Date?, eTime: Date?) {
+        example.createCriteria().apply {
+            deviceCode?.let { andEqualTo("deviceCode", it) }
+            sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
+            eTime?.let { andLessThanOrEqualTo("dataTime", it) }
+        }
+        example.orderBy("dataTime").apply {
+            // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁
+            if (sTime == null && eTime == null) {
+                desc()
+            }
+        }
+    }
+
+    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)
         val result = mutableListOf<DataVo>()
-        realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
-            createCriteria().apply {
-                deviceCode?.let { andEqualTo("deviceCode", it) }
-                sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
-                eTime?.let { andLessThanOrEqualTo("dataTime", it) }
+
+        when (UWDeviceType.getType(deviceCode)) {
+            UWDeviceType.VEHICLE -> {
+                realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply {
+                    getNextDataExample(this, deviceCode, updateTime)
+                }).forEach { result.add(it.toDataVo()) }
             }
-            orderBy("dataTime").desc()
-        }).forEach {
-            result.add(DataVo(
-                    dateFormatter.format(it.dataTime),
-                    it.deviceCode,
-                    GsonUtils.parserJsonToArrayBeans(it.factors, AirData::class.java),
-                    it.longitude.toDouble(), it.latitude.toDouble()
-            ))
+            UWDeviceType.UAV -> {
+                realTimeDataUavMapper.selectByExample(Example(RealTimeDataUav::class.java).apply {
+                    getNextDataExample(this, deviceCode, updateTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
+            UWDeviceType.GRID -> {
+                realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
+                    getNextDataExample(this, deviceCode, updateTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
+            else -> {
+                realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+                    getNextDataExample(this, deviceCode, updateTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
         }
-//        result.reverse()
         return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
     }
+
+    private fun getNextDataExample(example: Example, deviceCode: String, updateTime: String) {
+        example.createCriteria().andEqualTo("deviceCode", deviceCode)
+            .andGreaterThan("dataTime", updateTime)
+        example.orderBy("dataTime")
+    }
+
+    override fun importData(file: MultipartFile): BaseResponse<DataImportResult> {
+        val f = ByteArrayInputStream(file.bytes)
+        fileExchange.exchangeBoatData("0c0000000001", f).forEach {
+            realTimeDataMapper.insert(it)
+        }
+        return BaseResponse(true, data = DataImportResult(""))
+    }
+
+    override fun importJinanData(file: MultipartFile): BaseResponse<DataImportResult> {
+        TODO("Not yet implemented")
+    }
+
+    override fun outToWorkbook(deviceCode: String, startTime: String, endTime: String): SXSSFWorkbook {
+        val sTime = dateFormatter.parse(startTime)
+        val eTime = dateFormatter.parse(endTime)
+        var page = 1
+        var totalPage = 1
+        val pageSize = 10000
+        val workbook = SXSSFWorkbook()
+        var rowIndex = 0
+        while (page <= totalPage) {
+            val pageInfo = PageHelper.startPage<RealTimeData>(page, pageSize)
+            val r = realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+                createCriteria().andEqualTo("deviceCode", deviceCode)
+                    .apply {
+                        sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
+                        eTime?.let { andLessThanOrEqualTo("dataTime", it) }
+                    }
+            })
+            if (r.isNotEmpty()) {
+                val heads = if (page == 1) {
+                    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}......")
+                rowIndex = ExcelUtil.write(heads, contents, workbook, row = rowIndex)
+                println("output done")
+            }
+            totalPage = pageInfo.pages
+            page++
+        }
+        return workbook
+    }
+
+    override fun outToExcel(deviceCode: String, startTime: String, endTime: String, response: HttpServletResponse): HttpServletResponse {
+        val workbook = outToWorkbook(deviceCode, startTime, endTime)
+
+        val out = response.outputStream
+        workbook.write(out)
+        workbook.close()
+        out.flush()
+        out.close()
+
+        return response
+    }
+
+    fun getTableTitle(d: RealTimeData): List<Array<String>> {
+        return listOf(d.toRowTitle())
+    }
+
+    fun getTableContents(list: List<RealTimeData>): List<Array<Any>> {
+        val contents = mutableListOf<Array<Any>>()
+        list.forEach {
+            contents.add(it.toRowContent())
+        }
+        return contents
+    }
+
+    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) }
+        val eTime = endTime?.let { dateFormatter.parse(it) }
+//        var pageInfo = PageHelper.startPage<BaseRealTimeData>(_page, _perPage)
+        var pageNum = 1
+        var pages = 0
+        val result = mutableListOf<DataVo>()
+
+        // 浠庡師濮嬫暟鎹〃涓幏鍙栨暟鎹�
+        val pageInfo = PageHelper.startPage<RealTimeData>(_page, _perPage)
+        realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+            getSecondDataExample(this, deviceCode, sTime, eTime)
+        }).forEach {
+            if (it.longitude == null || it.latitude == null) {
+                return@forEach
+            }
+            result.add(it.toDataVo())
+        }
+        pageNum = pageInfo.pageNum
+        pages = pageInfo.pages
+
+        if (startTime == null && endTime == null) {
+            result.reverse()
+        }
+        return BaseResponse(true, head = DataHead(pageNum, pages), data = result)
+    }
+
+    override fun dataPreprocessing(): BaseResponse<String> {
+        val epwDataPrep = EPWDataPrep()
+        var page = 1
+        var total = -1
+        var count = 0
+
+        while (total == -1 || page <= total) {
+            println("------鏁版嵁棰勫鐞唖tart------")
+            val res = getOriginData("0d0000000001", "2021-07-05 19:47:01", "2021-11-05 00:00:00", page, 50000)
+            res.head?.let {
+                total = it.totalPage
+            }
+            if (page == 1) {
+                println("鎬婚〉鏁帮細$total")
+            }
+            println("褰撳墠椤垫暟锛�$page")
+            val dataList = res.data ?: emptyList()
+            val result = epwDataPrep.mDataPrep2(dataList)
+            count += airDataRepository.savePrepData2(result)
+            page++
+        }
+
+        return BaseResponse(count > 0, data = "鎻掑叆鏁版嵁: ${count}鏉�")
+    }
+
+    override fun averageData(): BaseResponse<String> {
+        var page = 1
+        var total = -1
+        var count = 0
+
+        val minFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
+        val averageUtil = AverageUtil<RealTimeDataGridOpt, RealTimeDataGridMin>({d ->
+            minFormatter.format(d.dataTime)
+        },{list ->
+            list.avg()
+        })
+
+        while (total == -1 || page <= total) {
+            println("------鍧囧�艰绠梥tart------")
+
+            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")
+            })
+
+            total = p.pages
+
+            if (page == 1) {
+                println("鎬婚〉鏁帮細$total")
+            }
+            println("褰撳墠椤垫暟锛�$page")
+            averageUtil.avg(res).forEach {
+                realTimeDataGridMinMapper.insert(it)
+                count++
+            }
+
+            page++
+        }
+
+        return BaseResponse(count > 0, data = "鎻掑叆鏁版嵁: ${count}鏉�")
+    }
+
+    override fun dataCalibration(): BaseResponse<String> {
+        var page = 1
+        var total = -1
+        var count = 0
+        val sT = "2021-06-19 00:00:00"
+        val eT = "2021-10-21 00:00:00"
+
+        println("------鏁版嵁浼樺寲start------")
+        while (total == -1 || page <= total) {
+            // 棰勫鐞嗗悗鐨勭綉鏍煎寲鏁版嵁
+            val p = PageHelper.startPage<RealTimeDataGrid>(page, 50000)
+            val dataList1 = realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
+                createCriteria().andBetween("dataTime", sT, eT)
+            })
+            // 鍘熷鏁版嵁锛圚2S锛�
+            val res = getOriginData("0d0000000001", sT, eT, page, 50000)
+            val dataList2 = res.data ?: emptyList()
+
+            total = p.pages
+
+            if (page == 1) {
+                println("鎬婚〉鏁帮細$total")
+            }
+            println("褰撳墠椤垫暟锛�$page")
+
+            for (i in dataList1.indices) {
+                if (i >= dataList2.size) break
+                val d1 = dataList1[i]
+                val d2 = dataList2[i]
+                // 鍒ゆ柇灏忔椂锛岃幏鍙栧搴旀椂娈电殑浼樺寲绯绘暟
+                val h = LocalDateTime.ofInstant(d1.dataTime?.toInstant(), ZoneId.systemDefault()).hour
+                val scaleList = scaleMap[h] ?: continue
+                d1.apply {
+                    // 鍚勯澶勭悊鍚庣殑鍥犲瓙锛堥櫎H2S锛変箻绯绘暟
+                    no2 = no2?.times(scaleList[0])
+                    co = co?.times(scaleList[1])
+                    so2 = so2?.times(scaleList[3])
+                    o3 = o3?.times(scaleList[4])
+                    voc = voc?.times(scaleList[7])
+
+                    // 鍗曠嫭鎶婂師濮嬬殑H2S涔樹互绯绘暟
+                    h2s = d2.values?.get(2)?.factorData?.toFloat()?.times(scaleList[2])
+                    h2s = h2s?.let { sqrt(it) * 2 }
+                }
+
+                // 鏇存柊绉掔骇鍊�
+                val opt = RealTimeDataGridOpt()
+                BeanUtils.copyProperties(d1, opt)
+                count += realTimeDataGridOptMapper.insert(opt)
+            }
+            page++
+        }
+
+        return BaseResponse(count > 0, data = "鎻掑叆鏁版嵁: ${count}鏉�")
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3