From 01ac08186355e85cab4a7c6f4403180184894f23 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期六, 09 十月 2021 14:38:44 +0800
Subject: [PATCH] 1. 新增网格化数据统计功能

---
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |  128 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 120 insertions(+), 8 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 182ac66..75a581e 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,40 +1,152 @@
 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.toRowContent
+import com.flightfeather.uav.domain.entity.toRowTitle
 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.lightshare.bean.*
 import com.flightfeather.uav.lightshare.service.RealTimeDataService
 import com.flightfeather.uav.socket.bean.AirData
 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.text.SimpleDateFormat
+import java.util.*
+import javax.servlet.http.HttpServletResponse
 
 @Service
 class RealTimeDataServiceImpl(val realTimeDataMapper: RealTimeDataMapper) : RealTimeDataService {
 
-    override fun getSecondData(deviceCode: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
+    private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
+    private val fileExchange = FileExchange()
+
+    override fun getSecondData(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) }
         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) }
             }
-            orderBy("dataTime").desc()
+            orderBy("dataTime").apply {
+                // 褰撹姹傛帴鍙d笉浼犻�掕捣濮嬫椂闂达紝榛樿鑾峰彇鏈�鏂扮殑鏁版嵁
+                if (startTime == null && endTime == null) {
+                    desc()
+                }
+            }
         }).forEach {
+            if (it.longitude == null || it.latitude == null) {
+                return@forEach
+            }
             result.add(DataVo(
-                    SimpleDateFormat.getDateTimeInstance().format(it.dataTime),
+                    dateFormatter.format(it.dataTime),
                     it.deviceCode,
                     GsonUtils.parserJsonToArrayBeans(it.factors, AirData::class.java),
-                    it.longitude.toDouble(), it.latitude.toDouble()
+                    it.longitude?.toDouble(), it.latitude?.toDouble()
             ))
         }
-        result.reverse()
+        if (startTime == null && endTime == null) {
+            result.reverse()
+        }
         return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
     }
+
+    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().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()
+            ))
+        }
+        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
+    }
+
+    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 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
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3