autoTask.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "log"
  4. "time"
  5. "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/date"
  7. )
  8. func autoTask() {
  9. log.Println("超时未跟进定时任务开始")
  10. t := time.Now()
  11. nowTime := time.Now().Format(date.Date_Full_Layout)
  12. statusMap := map[string]int{
  13. "07": 5, //待签署客户
  14. "06": 3, //高意向客户
  15. "05": 5, //意向客户
  16. "04": 7, //潜在客户
  17. }
  18. // statusMaps := map[string]int{}
  19. //判断节假日
  20. for status, statusInt := range statusMap {
  21. count, counts := 0, 0
  22. for {
  23. count++
  24. currentTime := t.AddDate(0, 0, -count)
  25. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  26. isok := false
  27. for k, v := range DateMap {
  28. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  29. isok = true
  30. }
  31. }
  32. if isok {
  33. counts++
  34. }
  35. } else {
  36. isok := true
  37. for k, v := range DateMap {
  38. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  39. isok = false
  40. }
  41. }
  42. if isok {
  43. counts++
  44. }
  45. }
  46. if counts >= statusInt {
  47. break
  48. }
  49. }
  50. statusMap[status] = count
  51. }
  52. log.Println(statusMap)
  53. for trailstatus, _ := range map[string]string{
  54. "07": "", //待签署客户
  55. "06": "", //高意向客户
  56. "05": "", //意向客户
  57. "04": "", //潜在客户
  58. } {
  59. sql := `SELECT a.clue_id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_private_sea a
  60. LEFT JOIN dwd_f_crm_clue_info b ON a.clue_id=b.id
  61. WHERE b.trailstatus =?`
  62. argsSelect := []interface{}{trailstatus}
  63. intime := ""
  64. sql += " AND a.comeintime <?"
  65. nt := t.AddDate(0, 0, -statusMap[trailstatus])
  66. intime = nt.Format(date.Date_Full_Layout)
  67. argsSelect = append(argsSelect, intime)
  68. log.Println(sql)
  69. log.Println(argsSelect)
  70. //
  71. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  72. for _, v := range *l {
  73. clueId := common.Int64All(v["clue_id"])
  74. position_id := common.Int64All(v["position_id"])
  75. out_task_status := common.IntAll(v["out_task_status"])
  76. args2 := []interface{}{clueId}
  77. //获取跟进内容
  78. // sql1 := `select COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id =?;`
  79. sql2 := `SELECT COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id = ? and position_id = ?`
  80. if intime != "" {
  81. sql2 += ` and createtime > ?`
  82. args2 = append(args2, intime)
  83. args2 = append(args2, position_id)
  84. }
  85. log.Println("intime", intime, clueId, sql2)
  86. //保留未跟进线索
  87. // if c1, c2 := TiDb.CountBySql(sql1, clueId), TiDb.CountBySql(sql2, args2...); (c1 != 0 && c2 > 0) || out_task_status == 1 {
  88. // log.Println("不满足线索过滤", clueId)
  89. // continue
  90. // }
  91. if c2 := TiDb.CountBySql(sql2, args2...); c2 > 0 || out_task_status == 1 {
  92. log.Println("不满足线索过滤", clueId)
  93. continue
  94. }
  95. if TiDb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  96. "is_task": 1,
  97. "task_time": nowTime,
  98. "tasktime": time.Now().Format(date.Date_Short_Layout) + " 10:00:00",
  99. "taskstatus": 0,
  100. "tasksource": "超时未跟进自动加车",
  101. }) {
  102. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  103. "clue_id": clueId,
  104. "position_id": position_id,
  105. "change_type": "加入任务车",
  106. "new_value": "超时未跟进自动加车",
  107. "createtime": nowTime,
  108. "BCPCID": common.GetRandom(32),
  109. "operator_id": -1,
  110. })
  111. }
  112. }
  113. return true
  114. }, sql, argsSelect...)
  115. }
  116. log.Println("超时未跟进定时任务结束")
  117. }
  118. func autoTasks() {
  119. log.Println("按照跟进时间提前一天进入任务车定时任务开始")
  120. nowTime2 := time.Now().Format(date.Date_Full_Layout)
  121. nextTime := time.Now().AddDate(0, 0, 1).Format(date.Date_Full_Layout)
  122. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  123. for _, v := range *l {
  124. clueId := common.Int64All(v["clue_id"])
  125. position_id := common.Int64All(v["position_id"])
  126. out_task_status := common.IntAll(v["out_task_status"])
  127. if out_task_status != 1 {
  128. if TiDb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  129. "is_task": 1,
  130. "task_time": nowTime2,
  131. "tasktime": nowTime2,
  132. "taskstatus": 0,
  133. "tasksource": "即将到达下次跟进时间",
  134. }) {
  135. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  136. "clue_id": clueId,
  137. "position_id": position_id,
  138. "change_type": "加入任务车",
  139. "new_value": "即将到达下次跟进时间",
  140. "createtime": nowTime2,
  141. "BCPCID": common.GetRandom(32),
  142. "operator_id": -1,
  143. })
  144. }
  145. }
  146. }
  147. return true
  148. }, `SELECT a.clue_id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_private_sea a
  149. LEFT JOIN dwd_f_crm_clue_info b ON a.clue_id=b.id
  150. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=b.id
  151. WHERE c.next_time >= "`+nowTime2+`" and c.next_time <= "`+nextTime+`"`)
  152. log.Println("按照跟进时间提前一天进入任务车定时任务结束")
  153. }