package models import ( "fmt" "spaceofcheng/services/models/model" "spaceofcheng/services/utils" "time" "github.com/astaxie/beego" "github.com/astaxie/beego/config" ) func InitTask() { kernel := utils.NewKernel() kernel.SetTask("sendmessage", SendMessageToCustomer) now := time.Now().Local() timeStr := now.Format("2006-01-02") + " " + beego.AppConfig.String("job::sendmessagetime") ten, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local) var tomorrow time.Time if now.Before(ten) { tomorrow = ten } else { tomorrow = ten.AddDate(0, 0, 1) } dur := tomorrow.Sub(now) beego.Error(dur) kernel.StartTask("sendmessage", dur) } func SendMessageToCustomer() time.Duration { // do something type TimeRecordList struct { model.TaBookBorrowRecord `xorm:"extends"` BookName string CaseName string Openid string } appRoot := utils.GetAppRoot() wechatconf, _ := config.NewConfig("ini", appRoot+"/conf/wechat.conf") week := wechatconf.String("bookReturnAlertTplID") late := wechatconf.String("bookLateAlertTplID") var weekList []TimeRecordList sql := `SELECT a.*, b.book_name, c.case_name, d.openid FROM ta_book_borrow_record a inner join ta_book b on a.book_id = b.book_id inner join sys_case c on a.case_id = c.case_id inner join ta_user_mapping d on a.customer_id = d.user_id WHERE DATE_FORMAT( a.end_date, "%Y-%m-%d" ) = DATE_FORMAT( ( SELECT date_add( now( ), INTERVAL 1 WEEK ) ), "%Y-%m-%d" ) and a.borrow_status = '1'` DBEngine.Sql(sql).Find(&weekList) var lateList []TimeRecordList sqlLate := `SELECT a.*, b.book_name, c.case_name, d.openid FROM ta_book_borrow_record a inner join ta_book b on a.book_id = b.book_id inner join sys_case c on a.case_id = c.case_id inner join ta_user_mapping d on a.customer_id = d.user_id WHERE DATE_FORMAT( now(), "%Y-%m-%d" ) = DATE_FORMAT( ( SELECT date_add( a.end_date, INTERVAL 2 DAY ) ), "%Y-%m-%d" ) and a.borrow_status = '0'` DBEngine.Sql(sqlLate).Find(&lateList) // 发消息 if len(weekList) > 0 { for i := 0; i < len(weekList); i++ { message := utils.Message{ To: utils.ClientID{ID: weekList[i].Openid}, Data: map[string]interface{}{ "orgid": weekList[i].OrgId, "tplid": week, "link": "", "data": map[string]string{ "first": "您好,您借阅图书即将逾期,请及时归还。", "keyword1": weekList[i].BookName, "keyword2": weekList[i].BorrowDate.Format("2006-01-02"), "keyword3": weekList[i].EndDate.Format("2006-01-02"), "remark": fmt.Sprintf("避免影响您下次借阅,请及时归还至%s", weekList[i].CaseName), }, }, } cert, err := GetWeChatConfig(weekList[i].OrgId, WECHAT_WX) if err != nil { utils.LogError("初始化微信失败: " + err.Error()) continue } utils.WxClientSingleton(weekList[i].OrgId, cert) time.Sleep(5 * time.Second) go utils.SendWechat(message) } } if len(lateList) > 0 { for i := 0; i < len(lateList); i++ { message := utils.Message{ To: utils.ClientID{ID: lateList[i].Openid}, Data: map[string]interface{}{ "orgid": lateList[i].OrgId, "tplid": late, "link": "", "data": map[string]string{ "first": "您好,您借阅图书的时间已经逾期2天", "keyword1": lateList[i].BookName, "keyword2": weekList[i].BorrowDate.Format("2006-01-02"), "keyword3": weekList[i].EndDate.Format("2006-01-02"), "remark": fmt.Sprintf("避免影响您下次借阅,请及时归还至%s", lateList[i].CaseName), }, }, } cert, err := GetWeChatConfig(weekList[i].OrgId, WECHAT_WX) if err != nil { utils.LogError("初始化微信失败: " + err.Error()) continue } utils.WxClientSingleton(weekList[i].OrgId, cert) time.Sleep(5 * time.Second) go utils.SendWechat(message) } } timeNext, _ := time.ParseDuration("24h") return timeNext }