From ef9692dd7a43e0620632e48ef295da738da50f90 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期三, 27 十月 2021 16:10:15 +0800
Subject: [PATCH] 1. 监测数据根据设备类型不同分别存储不同数据表 2. 车载数据部分监测因子进行量级调整 3. 无人机监测数据部分监测因子由于偏差较大采用车载数据填充 4. 监测数据存储之前先进行平滑预处理,矫正异常值

---
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |  197 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 161 insertions(+), 36 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 287c2e1..cce2f37 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,27 +1,38 @@
 package com.flightfeather.uav.lightshare.service.impl
 
+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.entity.*
+import com.flightfeather.uav.domain.mapper.RealTimeDataGridMapper
 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.DataImportResult
-import com.flightfeather.uav.lightshare.bean.DataVo
+import com.flightfeather.uav.domain.mapper.RealTimeDataUavMapper
+import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
+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.stereotype.Service
 import org.springframework.web.multipart.MultipartFile
 import tk.mybatis.mapper.entity.Example
 import java.io.ByteArrayInputStream
-import java.io.FileInputStream
-import java.io.InputStream
-import java.text.DateFormat
 import java.text.SimpleDateFormat
+import java.util.*
+import javax.servlet.http.HttpServletResponse
 
 @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
+) : RealTimeDataService {
 
     private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
     private val fileExchange = FileExchange()
@@ -33,30 +44,54 @@
         val eTime = endTime?.let { dateFormatter.parse(it) }
         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 {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach { result.add(it.toDataVo()) }
             }
-            orderBy("dataTime").apply {
-                // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁
-                if (startTime == null && endTime == null) {
-                    desc()
+            UWDeviceType.UAV -> {
+                realTimeDataUavMapper.selectByExample(Example(RealTimeDataUav::class.java).apply {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
+            UWDeviceType.GRID -> {
+                realTimeDataGridMapper.selectByExample(Example(RealTimeDataGrid::class.java).apply {
+                    getSecondDataExample(this, deviceCode, sTime, eTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
+            else -> {
+                // 浠庡師濮嬫暟鎹〃涓幏鍙栨暟鎹�
+                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())
                 }
             }
-        }).forEach {
-            result.add(DataVo(
-                    dateFormatter.format(it.dataTime),
-                    it.deviceCode,
-                    GsonUtils.parserJsonToArrayBeans(it.factors, AirData::class.java),
-                    it.longitude.toDouble(), it.latitude.toDouble()
-            ))
         }
+
         if (startTime == null && endTime == null) {
             result.reverse()
         }
         return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.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>> {
@@ -64,19 +99,36 @@
         val _page = page ?: 1
         val pageInfo = PageHelper.startPage<RealTimeData>(_page, _perPage)
         val result = mutableListOf<DataVo>()
-        realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
-            createCriteria().andEqualTo("deviceCode", deviceCode)
-                .andGreaterThan("dataTime", updateTime)
-            orderBy("dataTime")
-        }).forEach {
-            result.add(DataVo(
-                dateFormatter.format(it.dataTime),
-                it.deviceCode,
-                GsonUtils.parserJsonToArrayBeans(it.factors, AirData::class.java),
-                it.longitude.toDouble(), it.latitude.toDouble()
-            ))
+
+        when (UWDeviceType.getType(deviceCode)) {
+            UWDeviceType.VEHICLE -> {
+                realTimeDataVehicleMapper.selectByExample(Example(RealTimeDataVehicle::class.java).apply {
+                    getNextDataExample(this, deviceCode, updateTime)
+                }).forEach { result.add(it.toDataVo()) }
+            }
+            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()) }
+            }
         }
         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> {
@@ -86,4 +138,77 @@
         }
         return BaseResponse(true, data = DataImportResult(""))
     }
+
+    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 dataPreprocessing(): BaseResponse<String> {
+        val epwDataPrep = EPWDataPrep()
+        val res = getSecondData("0b0000000002", "2021-10-23 19:00:00", "2021-10-23 20:00:00", 1, 5000)
+        val dataList = res.data ?: emptyList()
+        var count = 0
+        dataList.forEach {
+            val result = epwDataPrep.mDataPrep2(listOf(it))
+            count += airDataRepository.savePrepData2(result)
+            println("[${it.deviceCode}]: [${it.time}]")
+            Thread.sleep(4000)
+        }
+        return BaseResponse(count > 0, data = "鎻掑叆鏁版嵁: ${count}鏉�")
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3