Browse Source

增加日期检查健壮性

master
巴林闲侠 3 years ago
parent
commit
1841109101
  1. 4
      api/app/lib/schedule/attendance.js
  2. 18
      api/app/lib/utils/days.js

4
api/app/lib/schedule/attendance.js

@ -264,7 +264,7 @@ module.exports = function (app, opts) {
let lunchBreakDuration = 3600 * 1 let lunchBreakDuration = 3600 * 1
while (curTime.isBefore(endTime_) && durationDayAddDay < durationSec) { while (curTime.isBefore(endTime_) && durationDayAddDay < durationSec) {
let curDayStr = curTime.format('YYYY-MM-DD') let curDayStr = curTime.format('YYYY-MM-DD')
const holidayRes = await judgeHoliday(curDayStr) const holidayRes = await judgeHoliday(curDayStr, true)
if (holidayRes && holidayRes.workday) { if (holidayRes && holidayRes.workday) {
// 只有上班的日子才计算 // 只有上班的日子才计算
@ -451,7 +451,7 @@ module.exports = function (app, opts) {
day: curDayStr, day: curDayStr,
duration: duration_ duration: duration_
}) })
const holidayRes = await judgeHoliday(curDayStr) const holidayRes = await judgeHoliday(curDayStr, true)
if (holidayRes) { if (holidayRes) {
if (compensate.value && compensate.value.indexOf('调休') >= 0) { if (compensate.value && compensate.value.indexOf('调休') >= 0) {
if (holidayRes.workday) { if (holidayRes.workday) {

18
api/app/lib/utils/days.js

@ -4,7 +4,7 @@ const request = require('superagent');
module.exports = function (app, opts) { module.exports = function (app, opts) {
async function judgeHoliday (dayStr) { async function judgeHoliday (dayStr, coolDown) {
const { timorApiUrl } = opts const { timorApiUrl } = opts
const { models } = app.fs.dc const { models } = app.fs.dc
let dayStr_ = dayStr || moment().format('YYYY-MM-DD') let dayStr_ = dayStr || moment().format('YYYY-MM-DD')
@ -14,13 +14,18 @@ module.exports = function (app, opts) {
day: dayStr_ day: dayStr_
} }
}) })
let needCreate = false
if (dbRes) { if (dbRes) {
holidayData = dbRes.dataValues.holiday holidayData = dbRes.dataValues.holiday
} else { } else {
needCreate = true
console.log("提莫 HOLIDAY 请求" + dayStr_); console.log("提莫 HOLIDAY 请求" + dayStr_);
let holidayRes = await request.get( let holidayRes = await request.get(
timorApiUrl + `holiday/info/${dayStr_}` timorApiUrl + `holiday/info/${dayStr_}`
) )
// 冷却一下
if (coolDown)
await new Promise(resolve => setTimeout(() => resolve(), 3000));
holidayData = holidayRes.body || {} holidayData = holidayRes.body || {}
} }
@ -31,6 +36,7 @@ module.exports = function (app, opts) {
festivals: false, festivals: false,
params: holidayData.type params: holidayData.type
} }
dayType = null
let holidayType = holidayData.type.type let holidayType = holidayData.type.type
if ( if (
holidayType == 2 holidayType == 2
@ -40,6 +46,7 @@ module.exports = function (app, opts) {
// 正经节假日 3倍工资那种 // 正经节假日 3倍工资那种
// festivals // festivals
returnD.festivals = true returnD.festivals = true
dayType = 'festivals'
} else if ( } else if (
holidayType == 1 holidayType == 1
|| ( || (
@ -50,9 +57,18 @@ module.exports = function (app, opts) {
) { ) {
// 普假 休息日非节假日 // 普假 休息日非节假日
returnD.dayoff = true returnD.dayoff = true
dayType = 'dayoff'
} else if (holidayType == 0 || holidayType == 3) { } else if (holidayType == 0 || holidayType == 3) {
// 工作日或补班 // 工作日或补班
returnD.workday = true returnD.workday = true
dayType = 'workday'
}
if (needCreate && dayType) {
await models.Holiday.create({
day: dayStr_,
holiday: holidayData,
type: dayType
})
} }
return returnD return returnD
} else { } else {

Loading…
Cancel
Save