|
|
@@ -0,0 +1,203 @@
|
|
|
+package plat
|
|
|
+
|
|
|
+import (
|
|
|
+ model "dashoo.cn/micro/app/model/plat"
|
|
|
+ "dashoo.cn/micro/app/service"
|
|
|
+ "database/sql"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/frame/g"
|
|
|
+ "github.com/gogf/gf/os/glog"
|
|
|
+ "github.com/gogf/gf/os/gtime"
|
|
|
+ "github.com/gogf/gf/util/gconv"
|
|
|
+ "github.com/robfig/cron"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 初始化,创建每10分钟执行一次的定时任务
|
|
|
+func init() {
|
|
|
+ // 定时任务
|
|
|
+ c := cron.New()
|
|
|
+ spec := "1 0/10 * * * ?" // 每天10分钟执行一次
|
|
|
+
|
|
|
+ if err := c.AddJob(spec, taskCron{}); err != nil {
|
|
|
+ glog.Error(err)
|
|
|
+ }
|
|
|
+ c.Start()
|
|
|
+}
|
|
|
+
|
|
|
+// 督办任务定时任务定义
|
|
|
+type taskCron struct {
|
|
|
+}
|
|
|
+
|
|
|
+// Run 督办定时任务逻辑
|
|
|
+func (c taskCron) Run() {
|
|
|
+ tenant := g.Config().GetString("setting.task-cron-tenant")
|
|
|
+ if tenant == "" {
|
|
|
+ glog.Error("督办定时任务租户码未设置,请前往配置")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 从配置中获取消息提醒设置
|
|
|
+ configs, err := g.DB(tenant).Model("sys_config").Where("config_key IN ('TaskOverdueBefore','TaskOverdueAfter')").FindAll()
|
|
|
+ if err != nil && err != sql.ErrNoRows {
|
|
|
+ glog.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ before := -1
|
|
|
+ after := -1
|
|
|
+ // 获取
|
|
|
+ for _, config := range configs {
|
|
|
+ if config["config_key"].String() == "TaskOverdueBefore" {
|
|
|
+ before = gconv.Int(config["config_value"].String())
|
|
|
+ } else if config["config_key"].String() == "TaskOverdueAfter" {
|
|
|
+ after = gconv.Int(config["config_value"].String())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 校验
|
|
|
+ if before == -1 || after == -1 {
|
|
|
+ glog.Error("督办定时任务超期提醒参数未配置,请前往配置")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 查询数据,启用定时任务
|
|
|
+ var tasks []*model.PlatTask
|
|
|
+ err = g.DB(tenant).Model("plat_task").Where("task_status='10' OR task_status='20'").Scan(&tasks)
|
|
|
+ if err != nil && err != sql.ErrNoRows {
|
|
|
+ glog.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 当前时间
|
|
|
+ now := gtime.Now()
|
|
|
+ // 生成提醒数据
|
|
|
+ for _, task := range tasks {
|
|
|
+ // 固定日期提醒
|
|
|
+ if task.ReminderRule != "" {
|
|
|
+ rules := strings.Split(task.ReminderRule, " ")
|
|
|
+ if len(rules) != 5 {
|
|
|
+ glog.Error(fmt.Sprintf("%v督办Id为%v提醒规则格式不正确", task.TaskTitle, task.Id))
|
|
|
+ } else {
|
|
|
+ if rules[3] == "*" { // 每天提醒
|
|
|
+ // 校验当前时间
|
|
|
+ remindTime := gtime.NewFromStr(fmt.Sprintf("%v %v:%v:%v", now.Format("Y-m-d"), rules[2], rules[1], rules[0]))
|
|
|
+ // 10分钟一次定时循环,两者相差在10分钟之内(纳秒转换1e9)
|
|
|
+ if (remindTime.Nanosecond()-now.Nanosecond())/(1*60*1e9) <= 10 {
|
|
|
+ taskNotifyMessage(task.MainUserId, task.OwnerUserId, task.TaskTitle+"督办需要处理,请前往执行")
|
|
|
+ }
|
|
|
+ } else if rules[3] == "?" { // 每周提醒
|
|
|
+ // 校验周选项是否匹配
|
|
|
+ weekDays := strings.Split(rules[4], ",")
|
|
|
+ isMatch := false
|
|
|
+ for _, day := range weekDays {
|
|
|
+ if day == transferWeekday(now.Weekday()) {
|
|
|
+ isMatch = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isMatch {
|
|
|
+ // 校验当前时间
|
|
|
+ remindTime := gtime.NewFromStr(fmt.Sprintf("%v %v:%v:%v", now.Format("Y-m-d"), rules[2], rules[1], rules[0]))
|
|
|
+ // 10分钟一次定时循环,两者相差在10分钟之内(纳秒转换1e9)
|
|
|
+ if (remindTime.Nanosecond()-now.Nanosecond())/(1*60*1e9) <= 10 {
|
|
|
+ taskNotifyMessage(task.MainUserId, task.OwnerUserId, task.TaskTitle+"督办需要处理,请前往执行")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else { // 每月提醒
|
|
|
+ monthDays := strings.Split(rules[3], ",")
|
|
|
+ isMatch := false
|
|
|
+ for _, day := range monthDays {
|
|
|
+ if gconv.Int(day) == now.Day() {
|
|
|
+ isMatch = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isMatch {
|
|
|
+ // 校验当前时间
|
|
|
+ remindTime := gtime.NewFromStr(fmt.Sprintf("%v %v:%v:%v", now.Format("Y-m-d"), rules[2], rules[1], rules[0]))
|
|
|
+ // 10分钟一次定时循环,两者相差在10分钟之内(纳秒转换1e9)
|
|
|
+ if (remindTime.Nanosecond()-now.Nanosecond())/(1*60*1e9) <= 10 {
|
|
|
+ taskNotifyMessage(task.MainUserId, task.OwnerUserId, task.TaskTitle+"督办需要处理,请前往执行")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 超期提醒,差10分(定时任务每10分钟执行一次)一天之时进行提醒
|
|
|
+ if task.TaskEndDate != nil {
|
|
|
+ // 超期前提醒
|
|
|
+ beforeDate := task.TaskEndDate.AddDate(0, 0, before)
|
|
|
+ if beforeDate.After(now) {
|
|
|
+ // 10分钟一次定时循环,两者相差在10分钟之内(纳秒转换1e9)
|
|
|
+ if (beforeDate.UnixNano()-now.UnixNano())/(1*60*1e9) <= 10 {
|
|
|
+ taskNotifyMessage(task.MainUserId, task.OwnerUserId, task.TaskTitle+"督办即将超期,请前往执行")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 超期后提醒
|
|
|
+ afterDate := task.TaskEndDate.AddDate(0, 0, -after)
|
|
|
+ if now.After(afterDate) {
|
|
|
+ // 10分钟一次定时循环,两者相差在10分钟之内(纳秒转换1e9)
|
|
|
+ if (now.UnixNano()-afterDate.UnixNano())/(1*60*1e9) <= 10 {
|
|
|
+ taskNotifyMessage(task.MainUserId, task.OwnerUserId, task.TaskTitle+"督办已超期,请确认")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 督办人任务的消息通知
|
|
|
+func taskNotifyMessage(mainId int, ownerIds, message string) {
|
|
|
+ // 协作人包含负责人的情况
|
|
|
+ ids := ownerIds
|
|
|
+ ownerIdArray := strings.Split(ownerIds, ",")
|
|
|
+ isCon := false
|
|
|
+ for _, id := range ownerIdArray {
|
|
|
+ if id == gconv.String(mainId) {
|
|
|
+ isCon = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !isCon {
|
|
|
+ if ids == "" {
|
|
|
+ ids = gconv.String(mainId)
|
|
|
+ } else {
|
|
|
+ ids += "," + gconv.String(mainId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 调用统一的消息通知方式
|
|
|
+ notifyMessage(ids, message)
|
|
|
+}
|
|
|
+
|
|
|
+// notifyMessage 发送消息通知
|
|
|
+func notifyMessage(ids, message string) {
|
|
|
+ msg := g.MapStrStr{
|
|
|
+ "msgTitle": "督办任务提醒",
|
|
|
+ "msgContent": fmt.Sprintf("<p>%v</p>", message),
|
|
|
+ "msgType": "20",
|
|
|
+ "recvUserIds": ids,
|
|
|
+ "msgStatus": "10",
|
|
|
+ "sendType": "10",
|
|
|
+ }
|
|
|
+ if err := service.CreateSystemMessage(msg); err != nil {
|
|
|
+ glog.Error("消息提醒异常:", err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 英文周转换为中文周
|
|
|
+func transferWeekday(day time.Weekday) string {
|
|
|
+ switch day {
|
|
|
+ case time.Monday:
|
|
|
+ return "1"
|
|
|
+ case time.Tuesday:
|
|
|
+ return "2"
|
|
|
+ case time.Wednesday:
|
|
|
+ return "3"
|
|
|
+ case time.Thursday:
|
|
|
+ return "4"
|
|
|
+ case time.Friday:
|
|
|
+ return "5"
|
|
|
+ case time.Saturday:
|
|
|
+ return "6"
|
|
|
+ case time.Sunday:
|
|
|
+ return "7"
|
|
|
+ default:
|
|
|
+ return "-1"
|
|
|
+ }
|
|
|
+}
|