init: initial commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package task
|
||||
|
||||
//
|
||||
//import (
|
||||
// "bytes"
|
||||
// "encoding/json"
|
||||
// "fmt"
|
||||
// "io"
|
||||
// "sundynix-go/global"
|
||||
// "sundynix-go/model/plant"
|
||||
// "sundynix-go/model/system"
|
||||
// "sundynix-go/pkg/httpclient"
|
||||
// "sundynix-go/utils/wechat"
|
||||
// "time"
|
||||
//)
|
||||
//
|
||||
//type TemplateDataItem struct {
|
||||
// Value string `json:"value"`
|
||||
//}
|
||||
//
|
||||
//// SendMessagePayload 是我们发送给微信服务器的消息体
|
||||
//type SendMessagePayload struct {
|
||||
// TemplateID string `json:"template_id"`
|
||||
// Page string `json:"page"`
|
||||
// Touser string `json:"touser"`
|
||||
// Data map[string]TemplateDataItem `json:"data"`
|
||||
// MiniProgramState string `json:"miniprogram_state"` //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
||||
// Lang string `json:"lang"`
|
||||
//}
|
||||
//
|
||||
//// SendMessageResponse 是发送消息的响应体
|
||||
//type SendMessageResponse struct {
|
||||
// Errcode int `json:"errcode"`
|
||||
// Errmsg string `json:"errmsg"`
|
||||
//}
|
||||
//
|
||||
//// SendCareMsg 发送提醒消息
|
||||
//func SendCareMsg() error {
|
||||
// httpUrl := "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + wechat.GetMiniAccessToken()
|
||||
//
|
||||
// //1.查询出今日的养护任务
|
||||
// now := time.Now()
|
||||
// today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
// var tasks []*plant.TodayCare
|
||||
// err := global.DB.Where("expected_date = ? and status in (1,4)", today).Find(&tasks).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if len(tasks) > 0 {
|
||||
// // 将tasks分组,key为用户id 保证无论用户有多少植物,只给用户发送一条消息
|
||||
// tasksMap := make(map[string][]*plant.TodayCare)
|
||||
// for _, task := range tasks {
|
||||
// tasksMap[task.UserId] = append(tasksMap[task.UserId], task)
|
||||
// }
|
||||
// for userId, cares := range tasksMap {
|
||||
// //1.查询用户
|
||||
// var user system.User
|
||||
// err = global.DB.Where("id = ?", userId).First(&user).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if user.MiniOpenId != "" {
|
||||
// //2.用户该养护的植物
|
||||
// plantId := cares[0].PlantId
|
||||
// var myPlant plant.MyPlant
|
||||
// err = global.DB.Where("id = ?", plantId).First(&myPlant).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// //3.构造请求参数 发送订阅消息
|
||||
// payload := SendMessagePayload{
|
||||
// TemplateID: "inVOG9qy5NylOivO4Xb9H1db6PQlfv5doNNVhh_3iFE",
|
||||
// Page: "pages/garden/index",
|
||||
// Touser: user.MiniOpenId,
|
||||
// Data: map[string]TemplateDataItem{
|
||||
// "thing2": {
|
||||
// Value: myPlant.Name + "等",
|
||||
// },
|
||||
// "time3": {
|
||||
// // 今天的九点
|
||||
// Value: time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.Local).Format("2006-01-02"),
|
||||
// },
|
||||
// },
|
||||
// MiniProgramState: "formal",
|
||||
// //MiniProgramState: "trial",
|
||||
// Lang: "zh_CN",
|
||||
// }
|
||||
// payloadBytes, err := json.Marshal(payload)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// myHttpClient := httpclient.GetClient()
|
||||
// resp, err := myHttpClient.Post(httpUrl, "application/json", bytes.NewBuffer(payloadBytes))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer resp.Body.Close()
|
||||
// body, err := io.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("读取订阅消息响应失败: %v", err)
|
||||
// }
|
||||
//
|
||||
// var smr SendMessageResponse
|
||||
// err = json.Unmarshal(body, &smr)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("解析订阅消息响应失败: %v, body: %s", err, string(body))
|
||||
// }
|
||||
//
|
||||
// if smr.Errcode != 0 {
|
||||
// return fmt.Errorf("微信服务器返回错误: errcode=%d, errmsg=%s", smr.Errcode, smr.Errmsg)
|
||||
// }
|
||||
// global.Logger.Info("订阅消息发送成功!")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
@@ -0,0 +1,117 @@
|
||||
package task
|
||||
|
||||
//
|
||||
//import (
|
||||
// "sundynix-go/global"
|
||||
// "sundynix-go/model/plant"
|
||||
// "time"
|
||||
//
|
||||
// "go.uber.org/zap"
|
||||
//)
|
||||
//
|
||||
//// GeneratorTodayCare 生成今日养护任务
|
||||
//func GeneratorTodayCare() error {
|
||||
// now := time.Now()
|
||||
// today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
//
|
||||
// //1.获取所有的养护计划
|
||||
// var plans []*plant.CarePlan
|
||||
// if err := global.DB.Find(&plans).Error; err != nil {
|
||||
// global.Logger.Error("获取所有的养护计划失败", zap.Error(err))
|
||||
// return err
|
||||
// }
|
||||
// for _, plan := range plans {
|
||||
// if plan.Period > 0 {
|
||||
// var todayCare plant.TodayCare
|
||||
// err := global.DB.Where("care_id = ?", plan.Id).First(&todayCare).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// // 如果今日日期满足周期循环
|
||||
// if plan.ShouldTriggerOn() {
|
||||
// // 如果不是逾期的任务 就把ExpectedDate改为今日
|
||||
// if todayCare.Status != 4 {
|
||||
// todayCare.Status = 1
|
||||
// todayCare.ExpectedDate = today
|
||||
// if err = global.DB.Save(&todayCare).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//// UpdateExpireCare 更新过期的养护任务
|
||||
//func UpdateExpireCare() error {
|
||||
// now := time.Now()
|
||||
// // 归一化到当天的0点(本地时区)
|
||||
// today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
// var expiredCares []*plant.TodayCare
|
||||
// // 1.查询所有未完成且预计日期在今天之前的养护任务
|
||||
// if err := global.DB.Where("status = ? and expected_date < ?", 1, today).
|
||||
// Or("status = ?", 4).Find(&expiredCares).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// //2.计算过期天数并更新状态为逾期
|
||||
// for _, care := range expiredCares {
|
||||
// expireDays := int(today.Sub(care.ExpectedDate).Hours() / 24)
|
||||
// updateMap := map[string]interface{}{
|
||||
// "is_expired": 1,
|
||||
// "expire_days": expireDays,
|
||||
// "status": 4,
|
||||
// "expected_date": today,
|
||||
// }
|
||||
// if err := global.DB.Model(care).Updates(updateMap).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//// GenerateUserCenter 更新用户中心数据
|
||||
//func GenerateUserCenter() error {
|
||||
// //1.所有的用户
|
||||
// var users []plant.Personal
|
||||
// if err := global.DB.Find(&users).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// now := time.Now()
|
||||
// // 归一化到当天的0点(本地时区)
|
||||
// now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
// for _, user := range users {
|
||||
// joinDate := user.JoinDate
|
||||
// //1.加入多少天
|
||||
// joinDays := int(now.Sub(joinDate).Hours() / 24)
|
||||
// //2.植物数量
|
||||
// var plantCount int64
|
||||
// err := global.DB.Model(&plant.MyPlant{}).Where("user_id = ?", user.Id).Count(&plantCount).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// //3.养护次数
|
||||
// var careCount int64
|
||||
// err = global.DB.Model(&plant.CareRecord{}).Where("user_id = ?", user.Id).Count(&careCount).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// //4.徽章数量 todo
|
||||
// var badgeCount int64
|
||||
// err = global.DB.Model(&plant.MyBadge{}).Where("user_id = ?", user.Id).Count(&badgeCount).Error
|
||||
// user.BadgeCount = int(badgeCount)
|
||||
// // 5. 使用 Updates 方法进行精确、安全的更新
|
||||
// // 只更新我们刚刚计算和查询出的这四个字段
|
||||
// updateData := map[string]interface{}{
|
||||
// "join_days": joinDays,
|
||||
// "plant_count": plantCount,
|
||||
// "care_count": careCount,
|
||||
// "badge_count": badgeCount,
|
||||
// }
|
||||
// if err := global.DB.Model(&plant.Personal{}).Where("id = ?", user.Id).Updates(updateData).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
Reference in New Issue
Block a user