You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.8 KiB
59 lines
1.8 KiB
'use strict';
|
|
const moment = require('moment');
|
|
const request = require('superagent');
|
|
|
|
module.exports = function (app, opts) {
|
|
const timorHoliday = app.fs.scheduleInit(
|
|
{
|
|
interval: '54 42 4 */7 * * *',
|
|
// immediate: true,
|
|
proRun: true,
|
|
},
|
|
async () => {
|
|
const { timorApiUrl } = opts
|
|
const { models } = app.fs.dc
|
|
|
|
const existCount = await models.Holiday.count()
|
|
|
|
let begainDay = existCount ? moment() : moment('2022-01-01')
|
|
let endDay = moment().add(3, 'months').endOf('month')
|
|
let checkDay = begainDay.clone()
|
|
while (checkDay.isSameOrBefore(endDay)) {
|
|
try {
|
|
const checkDayStr = checkDay.format('YYYY-MM-DD')
|
|
let holidayRes = await request.get(
|
|
timorApiUrl + `holiday/info/${checkDayStr}`,
|
|
)
|
|
let holidayData = holidayRes.body || {}
|
|
if (holidayData.code == 0) {
|
|
const dbRes = await models.Holiday.findOne({
|
|
where: {
|
|
day: checkDayStr
|
|
}
|
|
})
|
|
if (dbRes) {
|
|
await models.Holiday.update({ holiady: holidayData }, {
|
|
where: {
|
|
day: checkDayStr
|
|
}
|
|
})
|
|
} else {
|
|
await models.Holiday.create({
|
|
day: checkDayStr,
|
|
holiady: holidayData
|
|
})
|
|
}
|
|
}
|
|
checkDay = checkDay.add(1, 'day')
|
|
} catch (error) {
|
|
console.error(error);
|
|
// TODO 发邮件
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
return {
|
|
timorHoliday
|
|
}
|
|
}
|