autoTask.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package main
  2. import (
  3. "database/sql"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. "log"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/date"
  9. )
  10. func autoTask() {
  11. log.Println("超时未跟进定时任务开始")
  12. t := time.Now()
  13. nowTime := time.Now().Format(date.Date_Full_Layout)
  14. statusMap := map[string]int{
  15. "07": 2, //待签署客户
  16. "06": 2, //高意向客户
  17. "05": 3, //意向客户
  18. "04": 7, //潜在客户
  19. }
  20. //判断节假日
  21. for status, statusInt := range statusMap {
  22. count, counts := 0, 0
  23. for {
  24. count++
  25. currentTime := t.AddDate(0, 0, -count)
  26. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  27. isok := false
  28. for k, v := range DateMap {
  29. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  30. isok = true
  31. }
  32. }
  33. if isok {
  34. counts++
  35. }
  36. } else {
  37. isok := true
  38. for k, v := range DateMap {
  39. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  40. isok = false
  41. }
  42. }
  43. if isok {
  44. counts++
  45. }
  46. }
  47. if counts >= statusInt {
  48. break
  49. }
  50. }
  51. statusMap[status] = count
  52. }
  53. log.Println(statusMap)
  54. for trailstatus, _ := range map[string]string{
  55. "07": "", //待签署客户
  56. "06": "", //高意向客户
  57. "05": "", //意向客户
  58. "04": "", //潜在客户
  59. } {
  60. sql := `SELECT id,position_id,seatNumber,out_task_status FROM dwd_f_crm_clue_info
  61. WHERE trailstatus = ?`
  62. argsSelect := []interface{}{trailstatus}
  63. intime := ""
  64. sql += " AND 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["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_clue_info", map[string]interface{}{"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["id"])
  124. position_id := common.Int64All(v["position_id"])
  125. out_task_status := common.IntAll(v["out_task_status"])
  126. if position_id > 0 {
  127. if out_task_status != 1 && position_id > 0 {
  128. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"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. }
  148. return true
  149. }, `SELECT a.id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_clue_info a
  150. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=a.id
  151. WHERE c.next_time >= "`+nowTime2+`" and c.next_time <= "`+nextTime+`"`)
  152. log.Println("按照跟进时间提前一天进入任务车定时任务结束")
  153. }
  154. // 所有人达上限退公海处理
  155. func upperLimitAutoExitSea() {
  156. log.Println("所有人达上限退公海处理任务开始")
  157. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where (trailstatus = "04" or trailstatus = "03" or trailstatus = "01" ) and (is_assign = 1 or is_assign = 0)`)
  158. if data != nil && len(*data) > 0 {
  159. for _, v := range *data {
  160. trailstatus := gconv.String(v["trailstatus"])
  161. //坐席信息查询
  162. clueId := common.Int64All(v["id"])
  163. positionId := common.Int64All(v["position_id"])
  164. seatNumber := common.ObjToString(v["seatNumber"])
  165. if trailstatus == "03" {
  166. //该线索最近一次分配给该电销人员后存在通话记录
  167. uid := gconv.String(v["uid"])
  168. phoneData := TiDb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{
  169. "baseinfo_id": uid,
  170. }, "phone", "")
  171. if phoneData == nil || len(*phoneData) == 0 {
  172. //数据不对跳过去
  173. continue
  174. }
  175. phone := gconv.String((*phoneData)["phone"])
  176. callData := TiDb.FindOne("Call_Accounting.voice_record", map[string]interface{}{
  177. "CalledNo": phone,
  178. }, "Exten", "createTime")
  179. if callData == nil || len(*callData) == 0 {
  180. //手机号没有通话记录
  181. continue
  182. }
  183. exten := gconv.String((*callData)["Exten"])
  184. if seatNumber != exten {
  185. continue
  186. }
  187. }
  188. //退私海处理
  189. nowTime := time.Now().Format(date.Date_Full_Layout)
  190. positionMap := map[int64]interface{}{}
  191. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  192. positionMap[positionId] = true
  193. 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
  194. // ok1 := true
  195. // if is_assign == 1 {
  196. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  197. // } else if is_assign == 0 {
  198. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  199. // }
  200. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  201. "clue_id": clueId,
  202. "position_id": positionId,
  203. "change_field": "position_id",
  204. "change_type": "所属人变更",
  205. "old_value": GetPositionName(seatNumber),
  206. "new_value": "/",
  207. "createtime": nowTime,
  208. "BCPCID": common.GetRandom(32),
  209. "operator_id": -1,
  210. })
  211. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  212. "clue_id": clueId,
  213. "position_id": positionId,
  214. "change_type": "退出公海",
  215. "new_value": "所有人员私海已达上限",
  216. "createtime": nowTime,
  217. "BCPCID": common.GetRandom(32),
  218. "operator_id": -1,
  219. })
  220. return recordId > -1 && recordId1 > -1 && recordId2 > -1
  221. }) {
  222. //发送消息,找wjh
  223. log.Println("达上限自动退出线索成功")
  224. } else {
  225. log.Println("达上限自动退出线索失败")
  226. }
  227. }
  228. }
  229. }
  230. func autoExitSea() {
  231. log.Println("自动退海任务开始")
  232. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where trailstatus = "02" and (is_assign = 1 or is_assign = 0)`)
  233. if data != nil && len(*data) > 0 {
  234. for _, v := range *data {
  235. clueId := common.Int64All(v["id"])
  236. positionId := common.Int64All(v["position_id"])
  237. seatNumber := common.ObjToString(v["seatNumber"])
  238. nowTime := time.Now().Format(date.Date_Full_Layout)
  239. // is_assign := common.IntAll(v["is_assign"])
  240. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  241. 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
  242. // ok1 := true
  243. // if is_assign == 1 {
  244. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  245. // } else if is_assign == 0 {
  246. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  247. // }
  248. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  249. "clue_id": clueId,
  250. "position_id": positionId,
  251. "change_field": "position_id",
  252. "change_type": "所属人变更",
  253. "old_value": GetPositionName(seatNumber),
  254. "new_value": "/",
  255. "createtime": nowTime,
  256. "BCPCID": common.GetRandom(32),
  257. "operator_id": -1,
  258. })
  259. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  260. "clue_id": clueId,
  261. "position_id": positionId,
  262. "change_field": "trailstatus",
  263. "change_type": "基本信息变更",
  264. "old_value": "空号停机",
  265. "new_value": "流失",
  266. "createtime": nowTime,
  267. "BCPCID": common.GetRandom(32),
  268. "operator_id": -1,
  269. })
  270. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  271. "clue_id": clueId,
  272. "position_id": positionId,
  273. "change_type": "退出任务车",
  274. "new_value": "空号停机自动从线索池删除",
  275. "createtime": nowTime,
  276. "BCPCID": common.GetRandom(32),
  277. "operator_id": -1,
  278. })
  279. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  280. "clue_id": clueId,
  281. "position_id": positionId,
  282. "change_type": "退出公海",
  283. "new_value": "空号停机自动从线索池删除",
  284. "createtime": nowTime,
  285. "BCPCID": common.GetRandom(32),
  286. "operator_id": -1,
  287. })
  288. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  289. }) {
  290. log.Println("自动退出线索成功")
  291. } else {
  292. log.Println("自动退出线索失败")
  293. }
  294. }
  295. }
  296. datas := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"trailstatus": "00", "is_assign": 1}, "", "", -1, -1)
  297. if datas != nil && len(*datas) > 0 {
  298. for _, v := range *datas {
  299. clueId := common.Int64All(v["id"])
  300. positionId := common.Int64All(v["position_id"])
  301. seatNumber := common.ObjToString(v["seatNumber"])
  302. nowTime := time.Now().Format(date.Date_Full_Layout)
  303. // is_assign := common.IntAll(v["is_assign"])
  304. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  305. 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
  306. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  307. "clue_id": clueId,
  308. "position_id": positionId,
  309. "change_field": "position_id",
  310. "change_type": "所属人变更",
  311. "old_value": GetPositionName(seatNumber),
  312. "new_value": "/",
  313. "createtime": nowTime,
  314. "BCPCID": common.GetRandom(32),
  315. "operator_id": -1,
  316. })
  317. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  318. "clue_id": clueId,
  319. "position_id": positionId,
  320. "change_field": "trailstatus",
  321. "change_type": "基本信息变更",
  322. "old_value": "无意向客户",
  323. "new_value": "流失",
  324. "createtime": nowTime,
  325. "BCPCID": common.GetRandom(32),
  326. "operator_id": -1,
  327. })
  328. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  329. "clue_id": clueId,
  330. "position_id": positionId,
  331. "change_type": "退出任务车",
  332. "new_value": "无意向客户自动退回公海",
  333. "createtime": nowTime,
  334. "BCPCID": common.GetRandom(32),
  335. "operator_id": -1,
  336. })
  337. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  338. "clue_id": clueId,
  339. "position_id": positionId,
  340. "change_type": "退回公海",
  341. "new_value": "无意向客户自动退回公海",
  342. "createtime": nowTime,
  343. "BCPCID": common.GetRandom(32),
  344. "operator_id": -1,
  345. })
  346. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  347. }) {
  348. log.Println("自动退出私海成功")
  349. } else {
  350. log.Println("自动退出私海失败")
  351. }
  352. }
  353. }
  354. log.Println("自动退海任务结束")
  355. }
  356. func GetPositionName(seatNumber string) string {
  357. data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
  358. if data != nil && len(*data) > 0 {
  359. return common.ObjToString((*data)[0]["name"])
  360. }
  361. return ""
  362. }