From 027bf7da7a012fa36f8835b2419c74da8b2f1c28 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 15 十一月 2022 11:00:21 +0800
Subject: [PATCH] 2022.11.15

---
 src/main/kotlin/cn/flightfeather/supervision/timingtask/PushFume.kt |  163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/timingtask/PushFume.kt b/src/main/kotlin/cn/flightfeather/supervision/timingtask/PushFume.kt
new file mode 100644
index 0000000..b2e4007
--- /dev/null
+++ b/src/main/kotlin/cn/flightfeather/supervision/timingtask/PushFume.kt
@@ -0,0 +1,163 @@
+package cn.flightfeather.supervision.timingtask
+
+import cn.flightfeather.supervision.common.net.FumeHttpService
+import cn.flightfeather.supervision.domain.entity.DeviceInfo
+import cn.flightfeather.supervision.domain.entity.FumeMinuteValue
+import cn.flightfeather.supervision.domain.enumeration.DistrictType
+import cn.flightfeather.supervision.domain.enumeration.SceneType
+import cn.flightfeather.supervision.domain.mapper.DeviceInfoMapper
+import cn.flightfeather.supervision.domain.mapper.FumeMinuteValueMapper
+import cn.flightfeather.supervision.infrastructure.utils.DateUtil
+import com.github.pagehelper.PageHelper
+import org.slf4j.LoggerFactory
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Component
+import tk.mybatis.mapper.entity.Example
+import java.time.LocalDateTime
+import java.time.LocalTime
+import java.time.ZoneId
+import java.time.ZoneOffset
+import java.time.format.DateTimeFormatter
+import java.util.*
+import javax.annotation.PostConstruct
+import kotlin.math.round
+
+/**
+ * 涓婁紶娌圭儫鐩戞祴鏁版嵁
+ */
+@Component
+class PushFume : BaseTimingTask() {
+
+    companion object {
+//        private lateinit var instance: PushFume
+        val LOGGER = LoggerFactory.getLogger(PushFume::class.java)
+    }
+
+    @Autowired
+    lateinit var deviceInfoMapper: DeviceInfoMapper
+
+    @Autowired
+    lateinit var fumeMinuteValueMapper: FumeMinuteValueMapper
+
+//    @PostConstruct
+//    fun init() {
+//        instance = this
+//    }
+
+    private val deviceCodeList = mutableListOf<String>()
+
+    override val period: Long
+        get() = 1L
+
+    override fun doTask(localtime: LocalDateTime) {
+        LOGGER.info("===========寮�濮嬫墽琛屾补鐑熸暟鎹笂浼犱换鍔�===============")
+        // 鍒锋柊鐩戞祴鐐圭紪鍙�
+        refreshDeviceCode()
+
+        //姣�10鍒嗛挓璁$畻涓�娆″钩鍧囧�煎苟涓婁紶
+        // FIXME: 2021/4/8 鍧囧�肩殑璁$畻閫昏緫涔嬪悗搴旇鏀惧埌鍏朵粬妯″潡
+        val min = localtime.minute
+        if (min != 0 && min != 10 && min != 20 && min != 30 && min != 40 && min != 50) return
+
+        //璁$畻鍙栧�兼椂闂�
+        val endTime = Date.from(localtime.minusMinutes(1).withSecond(59).atZone(ZoneId.systemDefault()).toInstant())
+        val startTime = Date.from(localtime.minusMinutes(10).withSecond(0).atZone(ZoneId.systemDefault()).toInstant())
+
+        //鐢熸垚涓婁紶鏁版嵁缁撴瀯浣�
+        val postData = FumeHttpService.PostData()
+        val allData = mutableListOf<FumeMinuteValue>()
+        deviceCodeList.forEach {
+            //鑾峰彇鍓�10鍒嗛挓鐨勬暟鎹�
+            val dataList = fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
+                createCriteria().andEqualTo("mvStatCode", it)
+                        .andBetween("mvCreateTime", startTime, endTime)
+                and(createCriteria().orIsNull("mvUpload")
+                        .orEqualTo("mvUpload", false))
+            })
+
+            //璁$畻鍧囧��
+            var count = 0
+            var total = 0.0
+            dataList.forEach {
+                total += it.mvFumeConcentration2
+                if (it.mvFumeConcentration2 != 0.0) {
+                    count++
+                }
+            }
+            if (count == 0) {
+                dataUpdate(dataList)
+                return@forEach
+            }
+            val average = round(total / count * 100) / 100
+
+            //鍧囧�肩瓑浜�0锛屼笉涓婃姤锛岀洿鎺ユ洿鏂颁笂浼犵姸鎬�
+            if (average == 0.0) {
+                dataUpdate(dataList)
+                return@forEach
+            }
+
+            //鐢熸垚涓婁紶鏁版嵁缁撴瀯浣�
+            dataList.last().let {
+                postData.data.add(FumeHttpService.FumeData(
+                        "hengzhiyuan_${it.mvStatCode}",
+                    DateUtil.DateToString(startTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS),
+                        0.0, average,
+                        if (it.mvFanStatus) 3 else 2,
+                        it.mvFanElectricity, 0,
+                        if (it.mvPurifierStatus) 3 else 2,
+                        it.mvPurifierElectricity, 0,
+                        0, 0.0
+                ))
+            }
+
+            allData.addAll(dataList)
+        }
+
+        //涓婁紶鏁版嵁骞舵洿鏂版暟鎹姸鎬�
+        LOGGER.info("===========鏁版嵁閲囨牱鏃堕棿锛�$localtime")
+        postData.data.forEach {
+            LOGGER.info("${it.equipmentShowId}   **   ${it.dataTime}")
+        }
+        LOGGER.info("=================================")
+        FumeHttpService.uploadData(postData)?.run {
+            LOGGER.info(this.toString())
+            if (this["code"].asInt == 0 && this["data"].asInt > 0) {
+                dataUpdate(allData)
+            }
+            LOGGER.info("===========娌圭儫鏁版嵁涓婁紶浠诲姟缁撴潫============")
+        }
+    }
+
+    /**
+     * 鍒锋柊鐩戞祴鐐圭紪鍙�
+     */
+    @Synchronized
+    private fun refreshDeviceCode() {
+        val now = LocalDateTime.now()
+        if (deviceCodeList.isEmpty() || (now.hour == 0 && now.minute <= period)) {
+            deviceCodeList.clear()
+            deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
+                createCriteria().andEqualTo("diProvinceCode", "31")
+                        .andEqualTo("diCityCode", "3100")
+                        .andEqualTo("diDistrictCode", DistrictType.XuHui.code)
+                        .andEqualTo("diSceneTypeId", SceneType.Restaurant.value)
+                        .andEqualTo("diDeviceTypeId", 1)
+                        .andEqualTo("diSupplier", "hengzhiyuan")
+            }).forEach {
+                it?.let {
+                    deviceCodeList.add(it.diCode)
+                }
+            }
+        }
+    }
+
+    /**
+     * 鏇存柊鏁版嵁鐘舵��
+     */
+    private fun dataUpdate(dataList: List<FumeMinuteValue>) {
+        dataList.forEach {
+            it.mvUpload = true
+            fumeMinuteValueMapper.updateByPrimaryKey(it)
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3