autoTask.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 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 := nexttime.(time.Time)
  66. nt := t.AddDate(0, 0, -statusMap[trailstatus])
  67. intime = nt.Format(date.Date_Full_Layout)
  68. argsSelect = append(argsSelect, intime)
  69. //
  70. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  71. for _, v := range *l {
  72. clueId := common.Int64All(v["clue_id"])
  73. position_id := common.Int64All(v["position_id"])
  74. out_task_status := common.IntAll(v["out_task_status"])
  75. args2 := []interface{}{clueId}
  76. //获取跟进内容
  77. // sql1 := `select COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id =?;`
  78. sql2 := `SELECT COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id = ? and position_id = ?`
  79. if intime != "" {
  80. sql2 += ` and createtime > ?`
  81. args2 = append(args2, position_id)
  82. args2 = append(args2, intime)
  83. }
  84. //保留未跟进线索
  85. // if c1, c2 := TiDb.CountBySql(sql1, clueId), TiDb.CountBySql(sql2, args2...); (c1 != 0 && c2 > 0) || out_task_status == 1 {
  86. // log.Println("不满足线索过滤", clueId)
  87. // continue
  88. // }
  89. log.Println("intime ", clueId, intime, sql2)
  90. if c2 := TiDb.CountBySql(sql2, args2...); c2 > 0 || out_task_status == 1 {
  91. log.Println("不满足线索过滤", clueId)
  92. continue
  93. }
  94. if TiDb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  95. "is_task": 1,
  96. "task_time": nowTime,
  97. "tasktime": time.Now().Format(date.Date_Short_Layout) + " 10:00:00",
  98. "taskstatus": 0,
  99. "tasksource": "超时未跟进自动加车",
  100. }) {
  101. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  102. "clue_id": clueId,
  103. "position_id": position_id,
  104. "change_type": "加入任务车",
  105. "new_value": "超时未跟进自动加车",
  106. "createtime": nowTime,
  107. "BCPCID": common.GetRandom(32),
  108. "operator_id": -1,
  109. })
  110. }
  111. }
  112. return true
  113. }, sql, argsSelect...)
  114. }
  115. log.Println("超时未跟进定时任务结束")
  116. }
  117. func autoTasks() {
  118. log.Println("按照跟进时间提前一天进入任务车定时任务开始")
  119. nowTime2 := time.Now().Format(date.Date_Full_Layout)
  120. nextTime := time.Now().AddDate(0, 0, 1).Format(date.Date_Full_Layout)
  121. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  122. for _, v := range *l {
  123. clueId := common.Int64All(v["clue_id"])
  124. position_id := common.Int64All(v["position_id"])
  125. out_task_status := common.IntAll(v["out_task_status"])
  126. if out_task_status != 1 {
  127. if TiDb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_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. return true
  147. }, `SELECT a.clue_id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_private_sea a
  148. LEFT JOIN dwd_f_crm_clue_info b ON a.clue_id=b.id
  149. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=b.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 WHERE id = ?`, clueId)
  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. }
  169. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  170. "clue_id": clueId,
  171. "position_id": positionId,
  172. "change_field": "position_id",
  173. "change_type": "所属人变更",
  174. "old_value": GetPositionName(seatNumber),
  175. "new_value": "/",
  176. "createtime": nowTime,
  177. "BCPCID": common.GetRandom(32),
  178. "operator_id": -1,
  179. })
  180. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  181. "clue_id": clueId,
  182. "position_id": positionId,
  183. "change_field": "trailstatus",
  184. "change_type": "基本信息变更",
  185. "old_value": "空号停机",
  186. "new_value": "流失",
  187. "createtime": nowTime,
  188. "BCPCID": common.GetRandom(32),
  189. "operator_id": -1,
  190. })
  191. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  192. "clue_id": clueId,
  193. "position_id": positionId,
  194. "change_type": "退出任务车",
  195. "new_value": "空号停机自动从线索池删除",
  196. "createtime": nowTime,
  197. "BCPCID": common.GetRandom(32),
  198. "operator_id": -1,
  199. })
  200. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  201. "clue_id": clueId,
  202. "position_id": positionId,
  203. "change_type": "退出公海",
  204. "new_value": "空号停机自动从线索池删除",
  205. "createtime": nowTime,
  206. "BCPCID": common.GetRandom(32),
  207. "operator_id": -1,
  208. })
  209. log.Println(ok1, recordId, recordId1, recordId2, recordId3, recordId4)
  210. return ok1 && recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  211. }) {
  212. log.Println("自动退出线索成功")
  213. } else {
  214. log.Println("自动退出线索失败")
  215. }
  216. }
  217. }
  218. datas := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"trailstatus": "00", "is_assign": 1}, "", "", -1, -1)
  219. if datas != nil && len(*datas) > 0 {
  220. for _, v := range *datas {
  221. clueId := common.Int64All(v["id"])
  222. positionId := common.Int64All(v["position_id"])
  223. seatNumber := common.ObjToString(v["seatNumber"])
  224. nowTime := time.Now().Format(date.Date_Full_Layout)
  225. is_assign := common.IntAll(v["is_assign"])
  226. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  227. recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign = 0,position_id = null,seatNumber = null WHERE id = ?`, clueId)
  228. ok1 := true
  229. if is_assign == 1 {
  230. ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  231. }
  232. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  233. "clue_id": clueId,
  234. "position_id": positionId,
  235. "change_field": "position_id",
  236. "change_type": "所属人变更",
  237. "old_value": GetPositionName(seatNumber),
  238. "new_value": "/",
  239. "createtime": nowTime,
  240. "BCPCID": common.GetRandom(32),
  241. "operator_id": -1,
  242. })
  243. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  244. "clue_id": clueId,
  245. "position_id": positionId,
  246. "change_field": "trailstatus",
  247. "change_type": "基本信息变更",
  248. "old_value": "无意向客户",
  249. "new_value": "流失",
  250. "createtime": nowTime,
  251. "BCPCID": common.GetRandom(32),
  252. "operator_id": -1,
  253. })
  254. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  255. "clue_id": clueId,
  256. "position_id": positionId,
  257. "change_type": "退出任务车",
  258. "new_value": "无意向客户自动退回公海",
  259. "createtime": nowTime,
  260. "BCPCID": common.GetRandom(32),
  261. "operator_id": -1,
  262. })
  263. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  264. "clue_id": clueId,
  265. "position_id": positionId,
  266. "change_type": "退回公海",
  267. "new_value": "无意向客户自动退回公海",
  268. "createtime": nowTime,
  269. "BCPCID": common.GetRandom(32),
  270. "operator_id": -1,
  271. })
  272. recordId5 := TiDb.InsertByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{
  273. "clue_id": clueId,
  274. "comeintime": nowTime,
  275. "comeinsource": 2,
  276. })
  277. return ok1 && recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1 && recordId5 > -1
  278. }) {
  279. log.Println("自动退出私海成功")
  280. } else {
  281. log.Println("自动退出私海失败")
  282. }
  283. }
  284. }
  285. log.Println("自动退海任务结束")
  286. }
  287. func GetPositionName(seatNumber string) string {
  288. data := TiDb.SelectBySql(`select name from jy_salesperson_info where status = 0 and position != 0 and seatNumber = ? limit 1`, seatNumber)
  289. if data != nil && len(*data) > 0 {
  290. return common.ObjToString((*data)[0]["name"])
  291. }
  292. return ""
  293. }