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)}")
|
}
|
}
|
}
|