autoTask.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package main
  2. import (
  3. "database/sql"
  4. "log"
  5. "time"
  6. "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/date"
  8. )
  9. func autoTask() {
  10. log.Println("超时未跟进定时任务开始")
  11. t := time.Now()
  12. nowTime := time.Now().Format(date.Date_Full_Layout)
  13. statusMap := map[string]int{
  14. "07": 2, //待签署客户
  15. "06": 2, //高意向客户
  16. "05": 3, //意向客户
  17. "04": 7, //潜在客户
  18. }
  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 id,position_id,seatNumber,out_task_status FROM dwd_f_crm_clue_info
  60. WHERE trailstatus = ?`
  61. argsSelect := []interface{}{trailstatus}
  62. intime := ""
  63. sql += " AND comeintime <?"
  64. // nt := nexttime.(time.Time)
  65. nt := t.AddDate(0, 0, -statusMap[trailstatus])
  66. intime = nt.Format(date.Date_Full_Layout)
  67. argsSelect = append(argsSelect, intime)
  68. //
  69. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  70. for _, v := range *l {
  71. clueId := common.Int64All(v["id"])
  72. position_id := common.Int64All(v["position_id"])
  73. out_task_status := common.IntAll(v["out_task_status"])
  74. args2 := []interface{}{clueId}
  75. //获取跟进内容
  76. // sql1 := `select COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id =?;`
  77. sql2 := `SELECT COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id = ? and position_id = ?`
  78. if intime != "" {
  79. sql2 += ` and createtime > ?`
  80. args2 = append(args2, position_id)
  81. args2 = append(args2, intime)
  82. }
  83. //保留未跟进线索
  84. // if c1, c2 := TiDb.CountBySql(sql1, clueId), TiDb.CountBySql(sql2, args2...); (c1 != 0 && c2 > 0) || out_task_status == 1 {
  85. // log.Println("不满足线索过滤", clueId)
  86. // continue
  87. // }
  88. log.Println("intime ", clueId, intime, sql2)
  89. if c2 := TiDb.CountBySql(sql2, args2...); c2 > 0 || out_task_status == 1 {
  90. log.Println("不满足线索过滤", clueId)
  91. continue
  92. }
  93. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  94. "is_task": 1,
  95. "task_time": nowTime,
  96. "tasktime": time.Now().Format(date.Date_Short_Layout) + " 10:00:00",
  97. "taskstatus": 0,
  98. "tasksource": "超时未跟进自动加车",
  99. }) {
  100. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  101. "clue_id": clueId,
  102. "position_id": position_id,
  103. "change_type": "加入任务车",
  104. "new_value": "超时未跟进自动加车",
  105. "createtime": nowTime,
  106. "BCPCID": common.GetRandom(32),
  107. "operator_id": -1,
  108. })
  109. }
  110. }
  111. return true
  112. }, sql, argsSelect...)
  113. }
  114. log.Println("超时未跟进定时任务结束")
  115. }
  116. func autoTasks() {
  117. log.Println("按照跟进时间提前一天进入任务车定时任务开始")
  118. nowTime2 := time.Now().Format(date.Date_Full_Layout)
  119. nextTime := time.Now().AddDate(0, 0, 1).Format(date.Date_Full_Layout)
  120. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  121. for _, v := range *l {
  122. clueId := common.Int64All(v["id"])
  123. position_id := common.Int64All(v["position_id"])
  124. out_task_status := common.IntAll(v["out_task_status"])
  125. if position_id > 0 {
  126. if out_task_status != 1 && position_id > 0 {
  127. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  128. "is_task": 1,
  129. "task_time": nowTime2,
  130. "tasktime": nowTime2,
  131. "taskstatus": 0,
  132. "tasksource": "即将到达下次跟进时间",
  133. }) {
  134. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  135. "clue_id": clueId,
  136. "position_id": position_id,
  137. "change_type": "加入任务车",
  138. "new_value": "即将到达下次跟进时间",
  139. "createtime": nowTime2,
  140. "BCPCID": common.GetRandom(32),
  141. "operator_id": -1,
  142. })
  143. }
  144. }
  145. }
  146. }
  147. return true
  148. }, `SELECT a.id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_clue_info a
  149. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=a.id
  150. WHERE c.next_time >= "`+nowTime2+`" and c.next_time <= "`+nextTime+`"`)
  151. log.Println("按照跟进时间提前一天进入任务车定时任务结束")
  152. }
  153. func autoExitSea() {
  154. log.Println("自动退海任务开始")
  155. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where trailstatus = "02" and (is_assign = 1 or is_assign = 0)`)
  156. if data != nil && len(*data) > 0 {
  157. for _, v := range *data {
  158. clueId := common.Int64All(v["id"])
  159. positionId := common.Int64All(v["position_id"])
  160. seatNumber := common.ObjToString(v["seatNumber"])
  161. nowTime := time.Now().Format(date.Date_Full_Layout)
  162. // is_assign := common.IntAll(v["is_assign"])
  163. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  164. recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=-1,position_id=null,seatNumber=null,updatetime = ?,comeinsource_open=null,level_open=null,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null WHERE id = ?`, nowTime, clueId) //,start_trail_time=null,content=null
  165. // ok1 := true
  166. // if is_assign == 1 {
  167. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  168. // } else if is_assign == 0 {
  169. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  170. // }
  171. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  172. "clue_id": clueId,
  173. "position_id": positionId,
  174. "change_field": "position_id",
  175. "change_type": "所属人变更",
  176. "old_value": GetPositionName(seatNumber),
  177. "new_value": "/",
  178. "createtime": nowTime,
  179. "BCPCID": common.GetRandom(32),
  180. "operator_id": -1,
  181. })
  182. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  183. "clue_id": clueId,
  184. "position_id": positionId,
  185. "change_field": "trailstatus",
  186. "change_type": "基本信息变更",
  187. "old_value": "空号停机",
  188. "new_value": "流失",
  189. "createtime": nowTime,
  190. "BCPCID": common.GetRandom(32),
  191. "operator_id": -1,
  192. })
  193. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  194. "clue_id": clueId,
  195. "position_id": positionId,
  196. "change_type": "退出任务车",
  197. "new_value": "空号停机自动从线索池删除",
  198. "createtime": nowTime,
  199. "BCPCID": common.GetRandom(32),
  200. "operator_id": -1,
  201. })
  202. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  203. "clue_id": clueId,
  204. "position_id": positionId,
  205. "change_type": "退出公海",
  206. "new_value": "空号停机自动从线索池删除",
  207. "createtime": nowTime,
  208. "BCPCID": common.GetRandom(32),
  209. "operator_id": -1,
  210. })
  211. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  212. }) {
  213. log.Println("自动退出线索成功")
  214. } else {
  215. log.Println("自动退出线索失败")
  216. }
  217. }
  218. }
  219. datas := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"trailstatus": "00", "is_assign": 1}, "", "", -1, -1)
  220. if datas != nil && len(*datas) > 0 {
  221. for _, v := range *datas {
  222. clueId := common.Int64All(v["id"])
  223. positionId := common.Int64All(v["position_id"])
  224. seatNumber := common.ObjToString(v["seatNumber"])
  225. nowTime := time.Now().Format(date.Date_Full_Layout)
  226. // is_assign := common.IntAll(v["is_assign"])
  227. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  228. recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=0,position_id=null,seatNumber=null,updatetime = ?,comeintime_open = ?,comeinsource_open=8,level_open=4,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null WHERE id = ?`, nowTime, nowTime, clueId) //,content=null,start_trail_time=null
  229. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  230. "clue_id": clueId,
  231. "position_id": positionId,
  232. "change_field": "position_id",
  233. "change_type": "所属人变更",
  234. "old_value": GetPositionName(seatNumber),
  235. "new_value": "/",
  236. "createtime": nowTime,
  237. "BCPCID": common.GetRandom(32),
  238. "operator_id": -1,
  239. })
  240. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  241. "clue_id": clueId,
  242. "position_id": positionId,
  243. "change_field": "trailstatus",
  244. "change_type": "基本信息变更",
  245. "old_value": "无意向客户",
  246. "new_value": "流失",
  247. "createtime": nowTime,
  248. "BCPCID": common.GetRandom(32),
  249. "operator_id": -1,
  250. })
  251. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  252. "clue_id": clueId,
  253. "position_id": positionId,
  254. "change_type": "退出任务车",
  255. "new_value": "无意向客户自动退回公海",
  256. "createtime": nowTime,
  257. "BCPCID": common.GetRandom(32),
  258. "operator_id": -1,
  259. })
  260. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  261. "clue_id": clueId,
  262. "position_id": positionId,
  263. "change_type": "退回公海",
  264. "new_value": "无意向客户自动退回公海",
  265. "createtime": nowTime,
  266. "BCPCID": common.GetRandom(32),
  267. "operator_id": -1,
  268. })
  269. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  270. }) {
  271. log.Println("自动退出私海成功")
  272. } else {
  273. log.Println("自动退出私海失败")
  274. }
  275. }
  276. }
  277. log.Println("自动退海任务结束")
  278. }
  279. func GetPositionName(seatNumber string) string {
  280. data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
  281. if data != nil && len(*data) > 0 {
  282. return common.ObjToString((*data)[0]["name"])
  283. }
  284. return ""
  285. }