timetask.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package timetask
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/date"
  9. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  10. "bp.jydev.jianyu360.cn/CRM/application/service"
  11. "github.com/gogf/gf/v2/os/gcron"
  12. "github.com/gogf/gf/v2/os/gctx"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. )
  15. func Run() {
  16. if cm.Push.TimeTaskSwitch {
  17. sendMess()
  18. }
  19. }
  20. func getTime() (string, string) {
  21. today := time.Now()
  22. // 计算两天前的时间
  23. twoDaysAgo := today.AddDate(0, 0, -cm.Push.DueDay)
  24. // 设置时间为 0 点
  25. startTime := time.Date(twoDaysAgo.Year(), twoDaysAgo.Month(), twoDaysAgo.Day(), 0, 0, 0, 0, twoDaysAgo.Location())
  26. // 设置时间为 24 点
  27. endTime := startTime.Add(24 * time.Hour)
  28. return startTime.Format(date.Date_Full_Layout), endTime.Format(date.Date_Full_Layout)
  29. }
  30. //发送推送 站内信和微信
  31. func sendMess() {
  32. ctx := gctx.New()
  33. _, err := gcron.Add(ctx, cm.Push.PushTime, func(ctx context.Context) {
  34. log.Println("start cron")
  35. //获取倒计时时间
  36. st, et := getTime()
  37. cm.CrmMysql.SelectByBath(500, func(l *[]map[string]interface{}) bool {
  38. for _, v := range *l {
  39. name := gconv.String(v["name"])
  40. taskId := gconv.Int(v["id"])
  41. positionId := gconv.Int64(v["position_id"])
  42. source := gconv.Int(v["source"]) //1:线索 2:机会 3:客户
  43. sourceId := gconv.Int(v["source_id"])
  44. title := cm.Push.SaleClue.Due.Title
  45. content := cm.Push.SaleClue.Due.Content
  46. pcHref := cm.Push.SaleClue.Due.PcHref
  47. mobileHref := cm.Push.SaleClue.Due.MobileHref
  48. if source == 2 {
  49. title = cm.Push.SaleChance.Due.Title
  50. content = cm.Push.SaleChance.Due.Content
  51. pcHref = cm.Push.SaleChance.Due.PcHref
  52. mobileHref = cm.Push.SaleChance.Due.MobileHref
  53. } else if source == 3 {
  54. title = cm.Push.Custom.Due.Title
  55. content = cm.Push.Custom.Due.Content
  56. pcHref = cm.Push.Custom.Due.PcHref
  57. mobileHref = cm.Push.Custom.Due.MobileHref
  58. }
  59. u := &service.User{}
  60. resp := u.IdentityByPositionId(positionId)
  61. if resp == nil {
  62. continue
  63. }
  64. user := &service.User{
  65. BaseUserIds: []int64{resp.UserId},
  66. }
  67. m := user.GetUserId(resp.EntId)
  68. for k, v := range m {
  69. 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")
  70. }
  71. }
  72. return true
  73. }, `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)
  74. where a.status!=3 and a.next_follow_time >= ? and a.next_follow_time < ?`, st, et)
  75. log.Println("end cron")
  76. }, "sendMsg")
  77. if err != nil {
  78. log.Println("cron err:", err)
  79. }
  80. }