12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package timetask
- import (
- "context"
- "fmt"
- "log"
- "strings"
- "time"
- "app.yhyue.com/moapp/jybase/date"
- cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/service"
- "github.com/gogf/gf/v2/os/gcron"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/util/gconv"
- )
- func Run() {
- if cm.Push.TimeTaskSwitch {
- sendMess()
- }
- }
- func getTime() (string, string) {
- today := time.Now()
- // 计算两天前的时间
- twoDaysAgo := today.AddDate(0, 0, -cm.Push.DueDay)
- // 设置时间为 0 点
- startTime := time.Date(twoDaysAgo.Year(), twoDaysAgo.Month(), twoDaysAgo.Day(), 0, 0, 0, 0, twoDaysAgo.Location())
- // 设置时间为 24 点
- endTime := startTime.Add(time.Duration(cm.Push.DueDay) * 24 * time.Hour)
- return startTime.Format(date.Date_Full_Layout), endTime.Format(date.Date_Full_Layout)
- }
- //发送推送 站内信和微信
- func sendMess() {
- ctx := gctx.New()
- _, err := gcron.Add(ctx, cm.Push.PushTime, func(ctx context.Context) {
- log.Println("start cron")
- //获取倒计时时间
- st, et := getTime()
- cm.CrmMysql.SelectByBath(500, func(l *[]map[string]interface{}) bool {
- for _, v := range *l {
- name := gconv.String(v["name"])
- taskId := gconv.Int(v["id"])
- positionId := gconv.Int64(v["position_id"])
- source := gconv.Int(v["source"]) //1:线索 2:机会 3:客户
- sourceId := gconv.Int(v["source_id"])
- title := cm.Push.SaleClue.Due.Title
- content := cm.Push.SaleClue.Due.Content
- pcHref := cm.Push.SaleClue.Due.PcHref
- mobileHref := cm.Push.SaleClue.Due.MobileHref
- if source == 2 {
- title = cm.Push.SaleChance.Due.Title
- content = cm.Push.SaleChance.Due.Content
- pcHref = cm.Push.SaleChance.Due.PcHref
- mobileHref = cm.Push.SaleChance.Due.MobileHref
- } else if source == 3 {
- title = cm.Push.Custom.Due.Title
- content = cm.Push.Custom.Due.Content
- pcHref = cm.Push.Custom.Due.PcHref
- mobileHref = cm.Push.Custom.Due.MobileHref
- }
- u := &service.User{}
- resp := u.IdentityByPositionId(positionId)
- if resp == nil {
- log.Println("%v未找到身份", positionId)
- continue
- }
- user := &service.User{
- BaseUserIds: []int64{resp.UserId},
- }
- m := user.GetUserId(resp.EntId)
- for k, v := range m {
- service.StationMailPush(k, gconv.String(v), strings.Replace(title, " ", "%20", -1), strings.Replace(fmt.Sprintf(content, name), " ", "%20", -1), fmt.Sprintf(pcHref, gconv.String(v), taskId, sourceId), mobileHref, "11")
- }
- }
- return true
- }, `select a.id,a.name,b.position_id,a.source,a.source_id from task a left join task_team b on (a.id =b.task_id and b.role=1)
- where a.status!=3 and a.next_follow_time >= ? and a.next_follow_time < ?`, st, et)
- log.Println("end cron", st, et)
- }, "sendMsg")
- if err != nil {
- log.Println("cron err:", err)
- }
- }
|