🐛 修改定时任务相关bug

master
loser 6 months ago
parent 389d7c463f
commit b2eb4edbd5

@ -10,6 +10,7 @@ import (
"miniapp/model/app/request"
"miniapp/model/app/response"
"miniapp/model/common"
"strconv"
"strings"
"time"
)
@ -76,22 +77,64 @@ func (UserService) UpdateUserInfo(e *app.User) (err error) {
if len(hospital.Todos) != 0 {
var userTodos []common.UserTodo
for _, todo := range hospital.Todos {
//手术时间
parse, _ := time.Parse("2006-01-02", e.SurgeryTime)
//提醒时间
split := strings.Split(todo.RemindDay, "-")
a1, _ := strconv.Atoi(split[0])
a2, _ := strconv.Atoi(split[1])
//手术前
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
if err != nil {
global.GVA_LOG.Error("创建用户Todo列表失败", zap.Error(err))
return
}
}
}
return err

@ -2,6 +2,8 @@ package task
import (
"github.com/go-co-op/gocron"
"go.uber.org/zap"
"miniapp/global"
"time"
)
@ -12,7 +14,9 @@ func InitTask() {
// 每天凌晨1点执行
_, _ = WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
_, _ = WxTask.Every(1).Day().At("01:00").Do(SendMsg2User)
// 开启定时任务
WxTask.StartAsync()
global.GVA_LOG.Info("定时任务已开启", zap.Any("任务数量:", WxTask.Len()))
}

@ -1,8 +1,10 @@
package task
import (
"go.uber.org/zap"
"miniapp/global"
"miniapp/model/app"
"miniapp/model/common"
"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,7 +54,28 @@ func SendMsg(userId int) {
func MiniappSendMsg(openId string, context string, frequency string, remindTime string) {
sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
var msgDatas []msg.SendRequest
//判断context是否超过20个字符
if len(context)/3 >= 19 {
text := strings.Split(context, "药品")
for i := 1; i < len(text); i++ {
msgData := msg.SendRequest{
ToUser: openId,
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
Page: "/page/index/todo",
MiniprogramState: msg.MiniprogramStateTrial,
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",
@ -78,6 +99,21 @@ func MiniappSendMsg(openId string, context string, frequency string, remindTime
fmt.Printf("微信返回错误: %#v", err)
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
}
for i := 0; i < len(msgDatas); i++ {
send, err := sdk.NewSubscribeMessage().Send(&msgDatas[i])
if err != nil {
return
}
err = send.GetResponseError()
if err != nil {
fmt.Printf("微信返回错误: %#v", err)
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
}
fmt.Printf("返回结果: %#v", send)
}

Loading…
Cancel
Save