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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cn.flightfeather.supervision.bgtask
 
import cn.flightfeather.supervision.common.net.JinAnLianTongHttpService
import cn.flightfeather.supervision.domain.entity.HourDustData
import cn.flightfeather.supervision.domain.mapper.HourDustDataMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.lightshare.vo.JinAnHourDustData
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 TaskJinAnHourlyDustData(
    private val hourDustDataMapper: HourDustDataMapper
) : BaseTimingTask() {
 
    private val log: Logger? = LoggerFactory.getLogger(TaskJinAnHourlyDustData::class.java)
 
    private var times = 0
 
    private var isFirst = true
 
    //本任务每小时整点执行一次
    override fun execute(localtime: LocalDateTime) {
        if (isFirst || localtime.minute == 0) {
            isFirst = false
            doTask(localtime)
        }
//        println("=================check")
//        if (times == 0) {
//            doTask(localtime)
//            println("=================TaskJinAnHourlyDustData")
//            times++
//        }
    }
 
    //不使用此参数
    override val period: Long
        get() = 1440L
 
    override fun doTask(localtime: LocalDateTime) {
        log?.info("===========开始执行静安扬尘监测数据获取任务===============")
        PageHelper.startPage<HourDustData>(1, 1)
        val r = hourDustDataMapper.selectByExample(Example(HourDustData::class.java).apply {
            orderBy("lst").desc()
        })
        var sDate = if (r.isEmpty()) null else r[0]?.lst
//        val sDate = null
        var eDate: Date? = null
        if (sDate != null) {
            val lastHour = LocalDateTime.ofInstant(sDate.toInstant(), ZoneId.systemDefault()).minusHours(12)
            sDate = Date.from(lastHour.atZone(ZoneId.systemDefault()).toInstant())
        }
//        sDate = Date.from(LocalDateTime.of(2023, 8, 1, 0, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
//        eDate = Date.from(LocalDateTime.of(2023, 8, 23, 0, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
        var page = 1
        var hasNextPage: Boolean = false
        do {
            try {
                val res = JinAnLianTongHttpService.getHourlyDustData(page, sDate, eDate)
                hasNextPage = res.first
                page++
                saveSiteInfo(res.second)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } while (hasNextPage)
 
    }
 
    /**
     * 保存监测点小时数据
     */
    fun saveSiteInfo(dataList:List<JinAnHourDustData>) {
        dataList.forEach {d ->
            val info = hourDustDataMapper.selectByPrimaryKey(d.id)
            if (info == null) {
                val b = HourDustData()
                BeanUtils.copyProperties(d, b)
//                d.createTime?.let { b.inserttime = Date(it) }
//                d.stTime?.let { b.lst = Date(it) }
//                d.etTime?.let { b.lstEnd = Date(it) }
                b.inserttime = d.createTime
                b.lst = d.stTime
                b.lstEnd = d.etTime
                hourDustDataMapper.insert(b)
            }
        }
    }
 
    fun debugDoTask() {
        val sDate = Date.from(LocalDateTime.of(2024, 6, 18, 0, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
        val eDate = Date.from(LocalDateTime.of(2024, 6, 19, 0, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant())
        val res = JinAnLianTongHttpService.getHourlyDustData(1, sDate, eDate, 10)
        res.second.forEach {
            println("${it.mncode}: ${DateUtil.DateToString(it.stTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)}")
        }
    }
}