feiyu02
2022-10-21 f22c4b9230808fed4fec80c435eccb4c833349a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cn.flightfeather.supervision.timingtask
 
import cn.flightfeather.supervision.common.net.VOCHttpService
import cn.flightfeather.supervision.domain.entity.DeviceInfo
import cn.flightfeather.supervision.domain.entity.VOCHourValue
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.VOCHourValueMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.util.*
import javax.annotation.PostConstruct
 
/**
 * 获取voc监测数据
 */
@Component
class TaskFetchVOC : BaseTimingTask() {
    companion object {
        private lateinit var instance: TaskFetchVOC
    }
 
    @Autowired
    lateinit var deviceInfoMapper: DeviceInfoMapper
 
    @Autowired
    lateinit var vocHourValueMapper: VOCHourValueMapper
 
    @PostConstruct
    fun init() {
        instance = this
    }
 
    override val period: Long
        get() = 15L
 
    override fun doTask(localtime:LocalDateTime) {
        getVOCData()
    }
 
    private fun getVOCData() {
        val deviceCodeList = mutableListOf<String>()
        deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
            createCriteria().andEqualTo("diProvinceCode", "31")
                .andEqualTo("diCityCode", "3100")
                .andEqualTo("diDistrictCode", DistrictType.XuHui.code)
                .andEqualTo("diSceneTypeId", SceneType.VehicleRepair.value)
                .andEqualTo("diDeviceTypeId", 1)
        }).forEach {
            it?.let {
                deviceCodeList.add(it.diCode)
            }
        }
 
        val cal = Calendar.getInstance(Locale.CHINA).apply {
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
        }
        val endTime = DateUtil.DateToString(cal.time, "yyyy-MM-dd HH:mm:ss") ?: ""
        cal.add(Calendar.HOUR_OF_DAY, -1)
        val startTime = DateUtil.DateToString(cal.time, "yyyy-MM-dd HH:mm:ss") ?: ""
 
        deviceCodeList.forEach {
            val deviceInfo = VOCHttpService.DeviceInfo(listOf(it), startTime, endTime)
//            threadPoo?.execute {
                VOCHttpService.getVOCData(deviceInfo)?.run {
                    try {
                        if (this["code"].asInt == 200) {
                            val data = this["data"].asJsonObject
                            data["rtdMinuteHourDayBeans"].asJsonArray.forEach {e ->
                                e.asJsonObject.let { o->
                                    val hourValue = VOCHourValue()
 
                                    hourValue.vocStatCode = o["deviceCode"].asString
 
                                    val collectTime = o["collectTime"].asString
                                    hourValue.vocDataTime = DateUtil.StringToDate(collectTime)
 
                                    o["minuteHourDayValueBeans"].asJsonArray.forEach {e1 ->
                                        e1.asJsonObject.let {o1 ->
                                            when (o1["factorCode"].asString) {
                                                "e70201" -> hourValue.vocFanElectricity1 = o1["factorAvg"].asDouble
                                                "e70202" -> hourValue.vocFanElectricity2 = o1["factorAvg"].asDouble
                                                "e70203" -> hourValue.vocFanElectricity3 = o1["factorAvg"].asDouble
                                                "e70204" -> hourValue.vocFanElectricity4 = o1["factorAvg"].asDouble
                                                "e70205" -> hourValue.vocFanElectricity5 = o1["factorAvg"].asDouble
                                                "e70206" -> hourValue.vocFanElectricity6 = o1["factorAvg"].asDouble
                                                "g29001" -> hourValue.vocValue = o1["factorAvg"].asDouble
                                            }
                                        }
                                    }
                                    val r = vocHourValueMapper.selectByExample(Example(VOCHourValue::class.java).apply {
                                        createCriteria().andEqualTo("vocDataTime", hourValue.vocDataTime)
                                                .andEqualTo("vocStatCode", it)
                                    })
                                    if (r.isEmpty()) {
                                        vocHourValueMapper.insertSelective(hourValue)
                                    }
                                }
                            }
                        }
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    }
                }
//            }
        }
    }
}