🐛 修改定时任务相关bug
This commit is contained in:
		@@ -10,6 +10,7 @@ import (
 | 
				
			|||||||
	"miniapp/model/app/request"
 | 
						"miniapp/model/app/request"
 | 
				
			||||||
	"miniapp/model/app/response"
 | 
						"miniapp/model/app/response"
 | 
				
			||||||
	"miniapp/model/common"
 | 
						"miniapp/model/common"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -76,15 +77,56 @@ func (UserService) UpdateUserInfo(e *app.User) (err error) {
 | 
				
			|||||||
		if len(hospital.Todos) != 0 {
 | 
							if len(hospital.Todos) != 0 {
 | 
				
			||||||
			var userTodos []common.UserTodo
 | 
								var userTodos []common.UserTodo
 | 
				
			||||||
			for _, todo := range hospital.Todos {
 | 
								for _, todo := range hospital.Todos {
 | 
				
			||||||
				for _, s := range strings.Split(todo.RemindTime, ",") {
 | 
									//手术时间
 | 
				
			||||||
					userTodos = append(userTodos, common.UserTodo{
 | 
									parse, _ := time.Parse("2006-01-02", e.SurgeryTime)
 | 
				
			||||||
						Content:      todo.Content,
 | 
									//提醒时间
 | 
				
			||||||
						UserId:       int(e.ID),
 | 
									split := strings.Split(todo.RemindDay, "-")
 | 
				
			||||||
						IsFinish:     0,
 | 
									a1, _ := strconv.Atoi(split[0])
 | 
				
			||||||
						RemindTime:   s,
 | 
									a2, _ := strconv.Atoi(split[1])
 | 
				
			||||||
						RemindPeriod: todo.RemindPeriod,
 | 
					
 | 
				
			||||||
					})
 | 
									//手术前
 | 
				
			||||||
 | 
									if todo.RemindPeriod == 0 {
 | 
				
			||||||
 | 
										t1 := parse.AddDate(0, 0, -a1)
 | 
				
			||||||
 | 
										t2 := parse.AddDate(0, 0, -a2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										days := t1.Sub(t2).Hours() / 24
 | 
				
			||||||
 | 
										for i := int(-days); i <= 0; i++ {
 | 
				
			||||||
 | 
											for _, s := range strings.Split(todo.RemindTime, ",") {
 | 
				
			||||||
 | 
												userTodos = append(userTodos, common.UserTodo{
 | 
				
			||||||
 | 
													Content:      todo.Content,
 | 
				
			||||||
 | 
													UserId:       int(e.ID),
 | 
				
			||||||
 | 
													IsFinish:     0,
 | 
				
			||||||
 | 
													RemindTime:   s,
 | 
				
			||||||
 | 
													Frequency:    todo.Frequency,
 | 
				
			||||||
 | 
													RemindPeriod: todo.RemindPeriod,
 | 
				
			||||||
 | 
													RemindDay:    t1.AddDate(0, 0, i).Format("2006-01-02"),
 | 
				
			||||||
 | 
												})
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									//手术后
 | 
				
			||||||
 | 
									if todo.RemindPeriod == 1 {
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											t1 := parse.AddDate(0, 0, a1)
 | 
				
			||||||
 | 
											t2 := parse.AddDate(0, 0, a2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
											days := t1.Sub(t2).Hours() / 24
 | 
				
			||||||
 | 
											for i := 0; i <= int(days*-1); i++ {
 | 
				
			||||||
 | 
												for _, s := range strings.Split(todo.RemindTime, ",") {
 | 
				
			||||||
 | 
													userTodos = append(userTodos, common.UserTodo{
 | 
				
			||||||
 | 
														Content:      todo.Content,
 | 
				
			||||||
 | 
														UserId:       int(e.ID),
 | 
				
			||||||
 | 
														IsFinish:     0,
 | 
				
			||||||
 | 
														RemindTime:   s,
 | 
				
			||||||
 | 
														Frequency:    todo.Frequency,
 | 
				
			||||||
 | 
														RemindPeriod: todo.RemindPeriod,
 | 
				
			||||||
 | 
														RemindDay:    t1.AddDate(0, 0, i).Format("2006-01-02"),
 | 
				
			||||||
 | 
													})
 | 
				
			||||||
 | 
												}
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			err = global.GVA_DB.Create(&userTodos).Error
 | 
								err = global.GVA_DB.Create(&userTodos).Error
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
@@ -92,6 +134,7 @@ func (UserService) UpdateUserInfo(e *app.User) (err error) {
 | 
				
			|||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,8 @@ package task
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/go-co-op/gocron"
 | 
						"github.com/go-co-op/gocron"
 | 
				
			||||||
 | 
						"go.uber.org/zap"
 | 
				
			||||||
 | 
						"miniapp/global"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -12,7 +14,9 @@ func InitTask() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// 每天凌晨1点执行
 | 
						// 每天凌晨1点执行
 | 
				
			||||||
	_, _ = WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
 | 
						_, _ = WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
 | 
				
			||||||
 | 
						_, _ = WxTask.Every(1).Day().At("01:00").Do(SendMsg2User)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 开启定时任务
 | 
						// 开启定时任务
 | 
				
			||||||
	WxTask.StartAsync()
 | 
						WxTask.StartAsync()
 | 
				
			||||||
 | 
						global.GVA_LOG.Info("定时任务已开启", zap.Any("任务数量:", WxTask.Len()))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										38
									
								
								task/user.go
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								task/user.go
									
									
									
									
									
								
							@@ -1,8 +1,10 @@
 | 
				
			|||||||
package task
 | 
					package task
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"go.uber.org/zap"
 | 
				
			||||||
	"miniapp/global"
 | 
						"miniapp/global"
 | 
				
			||||||
	"miniapp/model/app"
 | 
						"miniapp/model/app"
 | 
				
			||||||
 | 
						"miniapp/model/common"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,3 +19,39 @@ func CheckUserSurgeryDate() {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SendMsg2User() {
 | 
				
			||||||
 | 
						WxTask.Clear()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var ut []common.UserTodo
 | 
				
			||||||
 | 
						err := global.GVA_DB.Model(&common.UserTodo{}).Find(&ut).Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							global.GVA_LOG.Error("定时任务获取用户Todo列表失败:", zap.Error(err))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, todo := range ut {
 | 
				
			||||||
 | 
							parse, _ := time.Parse("2006-01-02", todo.RemindDay)
 | 
				
			||||||
 | 
							if time.Now().Sub(parse).Hours()/24 == 0 {
 | 
				
			||||||
 | 
								// 创建定时任务
 | 
				
			||||||
 | 
								//WxTask := gocron.NewScheduler(time.Local)
 | 
				
			||||||
 | 
								//Send(todo.UserId, todo.Content, todo.RemindTime, todo.Frequency)
 | 
				
			||||||
 | 
								user := app.User{}
 | 
				
			||||||
 | 
								err := global.GVA_DB.Model(&app.User{}).Where("id = ?", todo.UserId).Find(&user).Error
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									global.GVA_LOG.Error("定时任务获取用户信息失败:%s", zap.Error(err))
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 根据用户手术信息 发送提醒消息
 | 
				
			||||||
 | 
								if user.IsSurgery == 0 && todo.RemindPeriod == 0 && todo.IsFinish == 0 {
 | 
				
			||||||
 | 
									// 根据年月日时发送消息
 | 
				
			||||||
 | 
									_, err := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, *user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										global.GVA_LOG.Error("定时任务创建失败:%s", zap.Error(err))
 | 
				
			||||||
 | 
										return
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,30 +54,66 @@ func SendMsg(userId int) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func MiniappSendMsg(openId string, context string, frequency string, remindTime string) {
 | 
					func MiniappSendMsg(openId string, context string, frequency string, remindTime string) {
 | 
				
			||||||
	sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
 | 
						sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
 | 
				
			||||||
 | 
						var msgDatas []msg.SendRequest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	msgData := msg.SendRequest{
 | 
						//判断context是否超过20个字符
 | 
				
			||||||
		ToUser:           openId,
 | 
						if len(context)/3 >= 19 {
 | 
				
			||||||
		TemplateID:       "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
 | 
							text := strings.Split(context, "药品")
 | 
				
			||||||
		Page:             "/page/index/todo",
 | 
							for i := 1; i < len(text); i++ {
 | 
				
			||||||
		MiniprogramState: msg.MiniprogramStateTrial,
 | 
								msgData := msg.SendRequest{
 | 
				
			||||||
		Data: msg.SendData{
 | 
									ToUser:           openId,
 | 
				
			||||||
			"thing1":        msg.SendValue{Value: context},
 | 
									TemplateID:       "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
 | 
				
			||||||
			"time2":         msg.SendValue{Value: remindTime},
 | 
									Page:             "/page/index/todo",
 | 
				
			||||||
			"short_thing17": msg.SendValue{Value: frequency},
 | 
									MiniprogramState: msg.MiniprogramStateTrial,
 | 
				
			||||||
			"time15":        msg.SendValue{Value: time.DateTime},
 | 
									Data: msg.SendData{
 | 
				
			||||||
		},
 | 
										"thing1":        msg.SendValue{Value: text[0] + text[i]},
 | 
				
			||||||
 | 
										"time2":         msg.SendValue{Value: remindTime},
 | 
				
			||||||
 | 
										"short_thing17": msg.SendValue{Value: frequency},
 | 
				
			||||||
 | 
										"time15":        msg.SendValue{Value: time.DateTime},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								msgDatas = append(msgDatas, msgData)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							msgData := msg.SendRequest{
 | 
				
			||||||
 | 
								ToUser:           openId,
 | 
				
			||||||
 | 
								TemplateID:       "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
 | 
				
			||||||
 | 
								Page:             "/page/index/todo",
 | 
				
			||||||
 | 
								MiniprogramState: msg.MiniprogramStateTrial,
 | 
				
			||||||
 | 
								Data: msg.SendData{
 | 
				
			||||||
 | 
									"thing1":        msg.SendValue{Value: context},
 | 
				
			||||||
 | 
									"time2":         msg.SendValue{Value: remindTime},
 | 
				
			||||||
 | 
									"short_thing17": msg.SendValue{Value: frequency},
 | 
				
			||||||
 | 
									"time15":        msg.SendValue{Value: time.DateTime},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							send, err := sdk.NewSubscribeMessage().Send(&msgData)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = send.GetResponseError()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("微信返回错误: %#v", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	send, err := sdk.NewSubscribeMessage().Send(&msgData)
 | 
						for i := 0; i < len(msgDatas); i++ {
 | 
				
			||||||
	if err != nil {
 | 
							send, err := sdk.NewSubscribeMessage().Send(&msgDatas[i])
 | 
				
			||||||
		return
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = send.GetResponseError()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("微信返回错误: %#v", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = send.GetResponseError()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		fmt.Printf("微信返回错误: %#v", err)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	fmt.Printf("返回结果: %#v", send)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user