From d978297ae85b2d7453054e616bbbe87bfabe9cbe Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期一, 12 七月 2021 17:38:09 +0800
Subject: [PATCH] 1. 新增用电量数据查询接口 2. 调整污染权重算法

---
 src/main/kotlin/com/flightfeather/uav/lightshare/service/impl/RealTimeDataServiceImpl.kt |   67 ++++++++++++++++++++++++++++++++-
 1 files changed, 64 insertions(+), 3 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 a22bee6..c7843aa 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,23 +1,32 @@
 package com.flightfeather.uav.lightshare.service.impl
 
+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.hssf.usermodel.HSSFWorkbook
 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 javax.servlet.http.HttpServletResponse
 
 @Service
 class RealTimeDataServiceImpl(val realTimeDataMapper: RealTimeDataMapper) : RealTimeDataService {
 
     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
@@ -71,4 +80,56 @@
         }
         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): HSSFWorkbook {
+        val sTime = dateFormatter.parse(startTime)
+        val eTime = dateFormatter.parse(endTime)
+        val r = realTimeDataMapper.selectByExample(Example(RealTimeData::class.java).apply {
+            createCriteria().andEqualTo("deviceCode", deviceCode)
+                .apply {
+                    sTime?.let { andGreaterThanOrEqualTo("dataTime", it) }
+                    eTime?.let { andLessThanOrEqualTo("dataTime", it) }
+                }
+        })
+        val workbook = HSSFWorkbook()
+        if (r.isNotEmpty()) {
+            val heads = getTableTitle(r[0])
+            val contents = getTableContents(r)
+
+            ExcelUtil.write(heads, contents, workbook)
+        }
+        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