timetask.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. positionId := gconv.Int64(v["position_id"])
  41. source := gconv.Int(v["source"]) //1:线索 2:机会 3:客户
  42. sourceId := gconv.Int(v["source_id"])
  43. title := cm.Push.SaleClue.Due.Title
  44. content := cm.Push.SaleClue.Due.Content
  45. pcHref := cm.Push.SaleClue.Due.PcHref
  46. mobileHref := cm.Push.SaleClue.Due.MobileHref
  47. if source == 2 {
  48. title = cm.Push.SaleChance.Due.Title
  49. content = cm.Push.SaleChance.Due.Content
  50. pcHref = cm.Push.SaleChance.Due.PcHref
  51. mobileHref = cm.Push.SaleChance.Due.MobileHref
  52. } else if source == 3 {
  53. title = cm.Push.Custom.Due.Title
  54. content = cm.Push.Custom.Due.Content
  55. pcHref = cm.Push.Custom.Due.PcHref
  56. mobileHref = cm.Push.Custom.Due.MobileHref
  57. }
  58. u := &service.User{}
  59. resp := u.IdentityByPositionId(positionId)
  60. if resp == nil {
  61. continue
  62. }
  63. user := &service.User{
  64. BaseUserIds: []int64{resp.UserId},
  65. }
  66. m := user.GetUserId(resp.EntId)
  67. for k, v := range m {
  68. service.StationMailPush(k, gconv.String(v), strings.Replace(title, " ", "%20", -1), strings.Replace(fmt.Sprintf(content, name), " ", "%20", -1), fmt.Sprintf(pcHref, sourceId), mobileHref)
  69. }
  70. }
  71. return true
  72. }, `select 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)
  73. where a.status!=3 and a.next_follow_time >= ? and a.next_follow_time < ?`, st, et)
  74. log.Println("end cron")
  75. }, "sendMsg")
  76. if err != nil {
  77. log.Println("cron err:", err)
  78. }
  79. }