feat: 完成获取下次提醒时间

This commit is contained in:
zhaoyingbo 2023-08-25 20:58:20 +08:00
parent 201f3b9162
commit 9bf37ce9d3
3 changed files with 165 additions and 4 deletions

View File

@ -1,4 +1,30 @@
const { genCard } = require("../utils/genCard")
const { getCurrRemind } = require("../utils/pb")
const { sendCard } = require("../utils/sendCard")
/**
* 处理提醒包括创建卡片更新下一次提醒时间更新上一个padding状态的卡片至delayed
* @param {Remind} remind 提醒信息
*/
const manageRemind = async (remind) =>{
// 生成卡片
const card = genCard(remind)
// 发送卡片
await sendCard(card)
// 更新下一次提醒时间
await updateNextRemindTime(remind)
// 更新上一个padding状态的卡片至delayed
await updatePrevPaddingCard(remind)
}
/**
* 处理当前分钟需要处理的卡片
* 发送提醒更新上一个padding状态的卡片至delayed
*/
const sendCurrTimeReminds = async () => {
const remindList = await getCurrRemind()
// 没有需要提醒的卡片
if (!remindList.length) return
}

View File

@ -23,14 +23,13 @@ module.exports.getTenantAccessToken = async () => {
}
/**
* 获取当前分钟应该提醒的所有卡片
* 获取当前分钟应该提醒的所有提醒
*/
module.exports.getRemindCards = async () => {
module.exports.getCurrRemind = async () => {
const nowStr = getNowStr()
// TODO: 应该不会同时出现同一时间100个需要提醒的情况等通知系统完善这里加个分页提醒吧
const { items } = await pb.collection('remind').getList(1, 100, {
filter: `nextRemindTime = "${nowStr}"`,
filter: `nextRemindTime = "${nowStr}" && enabled = true`,
})
return items
}

View File

@ -0,0 +1,136 @@
const chineseHoliday = {
'2023-09-29': true,
'2023-09-30': true,
'2023-10-01': true,
'2023-10-02': true,
'2023-10-03': true,
'2023-10-04': true,
'2023-10-05': true,
'2023-10-06': true,
'2023-10-07': false,
'2023-10-08': false,
'2023-12-31': true,
}
/**
* 判断是否是工作日
* @param {string} time 时间
* @returns {boolean} 是否是工作日
*/
const judgeIsWorkDay = (time) => {
const now = time ? new Date(time) : new Date();
const nowDay = now.getDay();
const nowDate = now.getDate();
const nowMonth = now.getMonth();
const nowYear = now.getFullYear();
// 是否是调休
const isHoliday = chineseHoliday[`${nowYear}-${nowMonth + 1}-${nowDate}`]
if (isHoliday) return false;
// 是否是补班
const isMakeUpWorkday = chineseHoliday[`${nowYear}-${nowMonth + 1}-${nowDate + 1}`] === false
if (isMakeUpWorkday) return true;
// 是否是日常工作日
return nowDay !== 0 && nowDay !== 6
}
/**
* 获取下一个工作日或者非工作日日期
* @param {boolean} isWorkday 是否是工作日
* @returns {string} 下一个工作日日期格式yyyy-MM-dd
*/
const getNextday = (isWorkday = true) => {
const now = new Date();
let dayCount = 1;
let nextDay = new Date(now.getTime() + dayCount * 24 * 60 * 60 * 1000)
while ((isWorkday && !judgeIsWorkDay(nextDay)) || (!isWorkday && judgeIsWorkDay(nextDay))) {
dayCount++;
nextDay = new Date(now.getTime() + dayCount * 24 * 60 * 60 * 1000)
}
const nextWorkdayYear = nextDay.getFullYear();
const nextWorkdayMonth = nextDay.getMonth() + 1;
const nextWorkdayDate = nextDay.getDate();
return `${nextWorkdayYear}-${nextWorkdayMonth}-${nextWorkdayDate}`
}
/**
* 获取下次提醒时间
* @param {Remind} remind 提醒信息
* @returns {string} 下次提醒时间, 格式为yyyy-MM-dd HH:mm
*/
const getNextRemindTime = (remind) => {
const { frequency, time, dayOfWeek, dayOfMonth, dayOfYear } = remind;
const now = new Date();
const nowDay = now.getDay();
const nowDate = now.getDate();
const nowMonth = now.getMonth();
const nowYear = now.getFullYear();
const nowHour = now.getHours();
const nowMinute = now.getMinutes();
console.log(nowDay, nowDate, nowMonth, nowYear, nowHour, nowMinute)
if (frequency === 'daily') {
// 判断当前时间是否已经过了今天的提醒时间
if (nowHour > time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute >= time.split(':')[1])) {
// 如果已经过了,那么下次提醒时间为明天的提醒时间
return `${nowYear}-${nowMonth + 1}-${nowDate + 1} ${time}`
} else {
// 否则下次提醒时间为今天的提醒时间
return `${nowYear}-${nowMonth + 1}-${nowDate} ${time}`
}
}
if (frequency === 'weekly') {
// 判断当前时间是否已经过了本周的提醒时间
if (nowDay > dayOfWeek || (nowDay === dayOfWeek && (nowHour > time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute >= time.split(':')[1])))) {
// 如果已经过了,那么下次提醒时间为下周的提醒时间
return `${nowYear}-${nowMonth + 1}-${nowDate + 7 - nowDay + dayOfWeek} ${time}`
} else {
// 否则下次提醒时间为本周的提醒时间
return `${nowYear}-${nowMonth + 1}-${nowDate + dayOfWeek - nowDay} ${time}`
}
}
if (frequency === 'monthly') {
// 判断当前时间是否已经过了本月的提醒时间
if (nowDate > dayOfMonth || (nowDate === dayOfMonth && (nowHour > time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute >= time.split(':')[1])))) {
// 如果已经过了,那么下次提醒时间为下月的提醒时间
return `${nowYear}-${nowMonth + 2}-${dayOfMonth} ${time}`
} else {
// 否则下次提醒时间为本月的提醒时间
return `${nowYear}-${nowMonth + 1}-${dayOfMonth} ${time}`
}
}
if (frequency === 'yearly') {
// 判断当前时间是否已经过了今年的提醒时间
if (nowMonth > parseInt(dayOfYear.split('-')[0]) || (nowMonth === parseInt(dayOfYear.split('-')[0]) && (nowDate > parseInt(dayOfYear.split('-')[1]) || (nowDate === parseInt(dayOfYear.split('-')[1]) && (nowHour > time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute >= time.split(':')[1])))))) {
// 如果已经过了,那么下次提醒时间为明年的提醒时间
return `${nowYear + 1}-${dayOfYear} ${time}`
} else {
// 否则下次提醒时间为今年的提醒时间
return `${nowYear}-${dayOfYear} ${time}`
}
}
if (frequency === 'workday') {
// 获取今天是不是工作日
const isWorkday = judgeIsWorkDay()
// 如果是工作日,且当前时间没过提醒时间,那么下次提醒时间为今天的提醒时间
if (isWorkday && (nowHour < time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute < time.split(':')[1]))) {
return `${nowYear}-${nowMonth + 1}-${nowDate} ${time}`
}
// 非工作日或者已经过了提醒时间,那么下次提醒时间为下一个工作日的提醒时间
return `${getNextday()} ${time}`
}
if (frequency === 'holiday') {
// 获取今天是不是工作日
const isWorkday = judgeIsWorkDay()
// 如果是非工作日,且当前时间没过提醒时间,那么下次提醒时间为今天的提醒时间
if (!isWorkday && (nowHour < time.split(':')[0] || (nowHour === time.split(':')[0] && nowMinute < time.split(':')[1]))) {
return `${nowYear}-${nowMonth + 1}-${nowDate} ${time}`
}
// 工作日或者已经过了提醒时间,那么下次提醒时间为下一个非工作日的提醒时间
return `${getNextday(false)} ${time}`
}
}