timetask.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(time.Duration(1) * 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 := ""
  47. mobileHref := cm.Push.SaleClue.Due.MobileHref
  48. responsible_id := gconv.Int64(v["responsible_id"])
  49. //判断是自办还是转办
  50. if responsible_id > 0 {
  51. //转办
  52. if source == 1 {
  53. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.TransferPcHref, responsible_id)
  54. } else if source == 2 {
  55. pcHref = fmt.Sprintf(cm.Push.SaleChance.Create.TransferPcHref, responsible_id)
  56. } else if source == 3 {
  57. pcHref = fmt.Sprintf(cm.Push.Custom.Create.TransferPcHref, responsible_id)
  58. }
  59. } else {
  60. //自办
  61. if source == 1 {
  62. ///succbi/crm_system/app/crm.app/task_follow/task_follow_up.spg?E_position_id=%v&E_control_editing=1&E_task_id=%v&E_cust_oppo_lead=%v
  63. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, sourceId)
  64. } else if source == 2 {
  65. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, sourceId)
  66. } else if source == 3 {
  67. pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, sourceId)
  68. }
  69. }
  70. if source == 2 {
  71. title = cm.Push.SaleChance.Due.Title
  72. content = cm.Push.SaleChance.Due.Content
  73. mobileHref = cm.Push.SaleChance.Due.MobileHref
  74. } else if source == 3 {
  75. title = cm.Push.Custom.Due.Title
  76. content = cm.Push.Custom.Due.Content
  77. mobileHref = cm.Push.Custom.Due.MobileHref
  78. }
  79. u := &service.User{}
  80. resp := u.IdentityByPositionId(positionId)
  81. if resp == nil {
  82. log.Println("%v未找到身份", positionId)
  83. continue
  84. }
  85. user := &service.User{
  86. BaseUserIds: []int64{resp.UserId},
  87. }
  88. m := user.GetUserId(resp.EntId)
  89. for k, v := range m {
  90. service.StationMailPush(k, gconv.String(v), strings.Replace(title, " ", "%20", -1), strings.Replace(fmt.Sprintf(content, name), " ", "%20", -1), pcHref, mobileHref, "11")
  91. }
  92. }
  93. return true
  94. }, `SELECT a.id,a.name,b.position_id,a.source,a.source_id,c.responsible_id FROM task a
  95. LEFT JOIN task_team b ON (a.id =b.task_id AND b.role=1)
  96. LEFT JOIN task_transfer c ON(a.id =c.task_id AND c.is_transfer =1)
  97. WHERE a.status!=3 AND a.next_follow_time >= ? AND a.next_follow_time < ?`, st, et)
  98. log.Println("end cron", st, et)
  99. }, "sendMsg")
  100. if err != nil {
  101. log.Println("cron err:", err)
  102. }
  103. }