feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
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
package cn.flightfeather.supervision.bgtask
 
import cn.flightfeather.supervision.common.net.JinAnLianTongHttpService
import cn.flightfeather.supervision.domain.entity.LampDeviceData
import cn.flightfeather.supervision.domain.mapper.LampDeviceDataMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.lightshare.vo.JinAnLampDeviceData
import com.github.pagehelper.PageHelper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.*
 
/**
 * 获取静安油烟设备数据
 */
@Component
class TaskJinAnLampDeviceData(
    private val lampDeviceDataMapper: LampDeviceDataMapper
) : BaseTimingTask() {
 
    private val LOGGER: Logger? = LoggerFactory.getLogger(TaskJinAnLampDeviceData::class.java)
 
    private var times = 0
 
    //本任务每小时执行一次
    override fun execute(localtime: LocalDateTime) {
        if (localtime.minute == 0) {
            doTask(localtime)
        }
//        println("=================check")
//        if (times == 0) {
//            doTask(localtime)
//            println("=================TaskJinAnLampDeviceData")
//            times++
//        }
    }
 
    //不使用此参数
    override val period: Long
        get() = 1440L
 
    override fun doTask(localtime: LocalDateTime) {
        LOGGER?.info("===========开始执行静安油烟监测数据获取任务===============")
        PageHelper.startPage<LampDeviceData>(1, 1)
        val r = lampDeviceDataMapper.selectByExample(Example(LampDeviceData::class.java).apply {
            orderBy("monitorTime").desc()
        })
        var smDate = if (r.isEmpty()) null else r[0]?.monitorTime
        var emDate: Date? = null
        smDate = Date.from(LocalDateTime.of(2023, 4, 1, 12, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
        emDate = Date.from(LocalDateTime.of(2023, 4, 1, 13, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
        var page = 1
        var hasNextPage: Boolean
        do {
            val res = JinAnLianTongHttpService.getLampDeviceData(page, smDate, emDate)
            println("page: $page, count: ${res.second.size}")
            hasNextPage = res.first
            page++
            saveSiteInfo(res.second)
        } while (hasNextPage)
 
    }
 
    /**
     * 保存油烟监测点数据
     */
    private fun saveSiteInfo(dataList:List<JinAnLampDeviceData>) {
        dataList.forEach {d ->
            if (d.monitorTime == null) return@forEach
            val mStr = d.monitorTime!!.split(".")[0]
            val mt = DateUtil.StringToDate(mStr)
            val info = lampDeviceDataMapper.selectByExample(Example(LampDeviceData::class.java).apply {
                createCriteria().andEqualTo("deviceCode", d.deviceCode)
                    .andEqualTo("monitorTime", mt)
            })
            if (info.isEmpty()) {
                val b = LampDeviceData()
                BeanUtils.copyProperties(d, b)
                b.apply {
                    lampblackValue = d.lampblackValue?.toDouble()
                    monitorTime = mt
                    productionDate = d.productionDate?.let { DateUtil.StringToDate(it) }
                }
                lampDeviceDataMapper.insert(b)
            }
        }
    }
}