autoTask.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/gogf/gf/v2/util/gconv"
  6. "log"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/date"
  10. )
  11. func autoTask() {
  12. log.Println("超时未跟进定时任务开始")
  13. t := time.Now()
  14. nowTime := time.Now().Format(date.Date_Full_Layout)
  15. statusMap := map[string]int{
  16. "07": 2, //待签署客户
  17. "06": 2, //高意向客户
  18. "05": 3, //意向客户
  19. "04": 7, //潜在客户
  20. }
  21. //判断节假日
  22. for status, statusInt := range statusMap {
  23. count, counts := 0, 0
  24. for {
  25. count++
  26. currentTime := t.AddDate(0, 0, -count)
  27. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  28. isok := false
  29. for k, v := range DateMap {
  30. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  31. isok = true
  32. }
  33. }
  34. if isok {
  35. counts++
  36. }
  37. } else {
  38. isok := true
  39. for k, v := range DateMap {
  40. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  41. isok = false
  42. }
  43. }
  44. if isok {
  45. counts++
  46. }
  47. }
  48. if counts >= statusInt {
  49. break
  50. }
  51. }
  52. statusMap[status] = count
  53. }
  54. log.Println(statusMap)
  55. for trailstatus := range map[string]string{
  56. "07": "", //待签署客户
  57. "06": "", //高意向客户
  58. "05": "", //意向客户
  59. "04": "", //潜在客户
  60. } {
  61. sql := `SELECT id,position_id,seatNumber,out_task_status FROM dwd_f_crm_clue_info
  62. WHERE trailstatus = ?`
  63. argsSelect := []interface{}{trailstatus}
  64. intime := ""
  65. sql += " AND comeintime <?"
  66. // nt := nexttime.(time.Time)
  67. nt := t.AddDate(0, 0, -statusMap[trailstatus])
  68. intime = nt.Format(date.Date_Full_Layout)
  69. argsSelect = append(argsSelect, intime)
  70. //
  71. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  72. for _, v := range *l {
  73. clueId := common.Int64All(v["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, position_id)
  83. args2 = append(args2, intime)
  84. }
  85. //保留未跟进线索
  86. // if c1, c2 := TiDb.CountBySql(sql1, clueId), TiDb.CountBySql(sql2, args2...); (c1 != 0 && c2 > 0) || out_task_status == 1 {
  87. // log.Println("不满足线索过滤", clueId)
  88. // continue
  89. // }
  90. log.Println("intime ", clueId, intime, sql2)
  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_clue_info", map[string]interface{}{"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["id"])
  125. position_id := common.Int64All(v["position_id"])
  126. out_task_status := common.IntAll(v["out_task_status"])
  127. if position_id > 0 {
  128. if out_task_status != 1 && position_id > 0 {
  129. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  130. "is_task": 1,
  131. "task_time": nowTime2,
  132. "tasktime": nowTime2,
  133. "taskstatus": 0,
  134. "tasksource": "即将到达下次跟进时间",
  135. }) {
  136. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  137. "clue_id": clueId,
  138. "position_id": position_id,
  139. "change_type": "加入任务车",
  140. "new_value": "即将到达下次跟进时间",
  141. "createtime": nowTime2,
  142. "BCPCID": common.GetRandom(32),
  143. "operator_id": -1,
  144. })
  145. }
  146. }
  147. }
  148. }
  149. return true
  150. }, `SELECT a.id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_clue_info a
  151. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=a.id
  152. WHERE c.next_time >= "`+nowTime2+`" and c.next_time <= "`+nextTime+`"`)
  153. log.Println("按照跟进时间提前一天进入任务车定时任务结束")
  154. }
  155. // 所有人达上限退公海处理
  156. func UpperLimitAutoExitSea(upperLimit int64) {
  157. countData := TiDb.SelectBySql(`SELECT
  158. COUNT(b.ID) AS count ,a.position_id
  159. FROM
  160. dwd_f_crm_clue_info b
  161. right JOIN
  162. ( select position_id from dwd_f_crm_personnel_management where assign_type = 1
  163. AND resign = 0 ) a on a.position_id=b.position_id
  164. GROUP BY
  165. a.position_id HAVING count<?`, upperLimit)
  166. if countData == nil || len(*countData) > 0 {
  167. return
  168. }
  169. log.Println("所有人达上限退公海处理任务开始")
  170. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where (trailstatus = "04" or trailstatus = "03" or trailstatus = "01" ) and (is_assign = 1)`)
  171. if data != nil && len(*data) > 0 {
  172. fool := false
  173. for _, v := range *data {
  174. trailstatus := gconv.String(v["trailstatus"])
  175. //坐席信息查询
  176. clueId := common.Int64All(v["id"])
  177. positionId := common.Int64All(v["position_id"])
  178. seatNumber := common.ObjToString(v["seatNumber"])
  179. if trailstatus == "01" {
  180. //该线索最近一次分配给该电销人员后存在通话记录
  181. uid := gconv.String(v["uid"])
  182. if !PhoneState(uid, seatNumber) {
  183. continue
  184. }
  185. }
  186. //退私海处理
  187. nowTime := time.Now().Format(date.Date_Full_Layout)
  188. positionMap := map[int64]interface{}{}
  189. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  190. positionMap[positionId] = true
  191. recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=0,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
  192. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  193. "clue_id": clueId,
  194. "position_id": positionId,
  195. "change_type": "退回公海",
  196. "new_value": "所有人员私海已达上限",
  197. "createtime": nowTime,
  198. "BCPCID": common.GetRandom(32),
  199. "operator_id": -1,
  200. })
  201. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  202. "clue_id": clueId,
  203. "position_id": positionId,
  204. "change_field": "position_id",
  205. "change_type": "所属人变更",
  206. "old_value": GetPositionName(seatNumber),
  207. "new_value": "/",
  208. "createtime": nowTime,
  209. "BCPCID": common.GetRandom(32),
  210. "operator_id": -1,
  211. })
  212. return recordId > -1 && recordId1 > -1 && recordId2 > -1
  213. }) {
  214. //发送消息,找wjh
  215. fool = true
  216. log.Println("达上限自动退出线索成功")
  217. } else {
  218. log.Println("达上限自动退出线索失败")
  219. }
  220. }
  221. //释放邮件
  222. //发送信息
  223. if fool {
  224. AutoReleaseNots()
  225. } else {
  226. CantBeAssignedNots()
  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. }
  363. func Thaw() {
  364. //3个工作日查询
  365. k := 3
  366. nowTime := time.Now()
  367. for {
  368. if k == 0 {
  369. break
  370. }
  371. nowTime = nowTime.AddDate(0, 0, -1)
  372. if !IsBreak(nowTime) {
  373. continue
  374. }
  375. k--
  376. fmt.Println(nowTime.Format("20060102"))
  377. }
  378. nameMap := map[int64]string{}
  379. nameList := TiDb.SelectBySql("select name,position_id from dwd_f_crm_personnel_management")
  380. if nameList != nil && len(*nameList) > 0 {
  381. for _, v := range *nameList {
  382. nameMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
  383. }
  384. }
  385. //查询
  386. fmt.Println()
  387. layout := "2006-01-02 15:04:05"
  388. //三天以后退公海处理
  389. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  390. for _, v := range *l {
  391. clueId := gconv.Int64(v["id"])
  392. positionId := gconv.Int64(v["position_id"])
  393. freezeInt64 := int64(0)
  394. freezeStr := gconv.String(v["FREEZE_TIME"])
  395. if freezeStr != "" {
  396. t, _ := time.ParseInLocation(layout, freezeStr, time.Local)
  397. freezeInt64 = t.Unix()
  398. }
  399. if freezeInt64 < nowTime.Unix() {
  400. //超三天处理
  401. //分配状态改改
  402. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  403. "is_assign": 0,
  404. "position_id": 0,
  405. }) {
  406. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  407. "clue_id": clueId,
  408. "position_id": 0,
  409. "change_field": "position_id",
  410. "change_type": "所属人变更",
  411. "old_value": nameMap[positionId],
  412. "new_value": "/",
  413. "createtime": time.Now().Format(date.Date_Full_Layout),
  414. "BCPCID": common.GetRandom(32),
  415. "operator_id": -1,
  416. })
  417. }
  418. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  419. "clue_id": clueId,
  420. "position_id": 0,
  421. "change_field": "position_id",
  422. "change_type": "线索解冻",
  423. "new_value": "自动退回公海",
  424. "createtime": time.Now().Format(date.Date_Full_Layout),
  425. "BCPCID": common.GetRandom(32),
  426. "operator_id": -1,
  427. })
  428. } else {
  429. fmt.Println(FindUpperLimit(gconv.String(positionId), "", false))
  430. if !FindUpperLimit(gconv.String(positionId), "", false) {
  431. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  432. "is_assign": 1,
  433. }) {
  434. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  435. "clue_id": clueId,
  436. "position_id": positionId,
  437. "change_field": "position_id",
  438. "change_type": "所属人变更",
  439. "old_value": "/",
  440. "new_value": nameMap[positionId],
  441. "createtime": time.Now().Format(date.Date_Full_Layout),
  442. "BCPCID": common.GetRandom(32),
  443. "operator_id": -1,
  444. })
  445. }
  446. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  447. "clue_id": clueId,
  448. "position_id": positionId,
  449. "change_field": "position_id",
  450. "change_type": "线索解冻",
  451. "new_value": "销售人员私海线索已释放",
  452. "createtime": time.Now().Format(date.Date_Full_Layout),
  453. "BCPCID": common.GetRandom(32),
  454. "operator_id": -1,
  455. })
  456. }
  457. }
  458. }
  459. return true
  460. }, `select id,position_id,FREEZE_TIME from dwd_f_crm_clue_info where is_assign = -3 `)
  461. }
  462. func IsBreak(currentTime time.Time) bool {
  463. if currentTime.Weekday() == time.Sunday {
  464. isok := false
  465. for k, v := range DateMap {
  466. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  467. isok = true
  468. }
  469. }
  470. return isok
  471. } else {
  472. isok := true
  473. for k, v := range DateMap {
  474. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  475. isok = false
  476. }
  477. }
  478. return isok
  479. }
  480. }
  481. func PhoneState(uid, seatNumber string) bool {
  482. phoneData := TiDb.SelectBySql("select DISTINCT phone from dwd_f_userbase_contacts where baseinfo_id= ?", uid)
  483. if phoneData == nil || len(*phoneData) == 0 {
  484. //数据不对跳过去
  485. return false
  486. }
  487. aa := false
  488. for _, v1 := range *phoneData {
  489. phone := gconv.String(v1["phone"])
  490. callData := TiDb.FindOne("Call_Accounting.voice_record", map[string]interface{}{
  491. "CalledNo": phone,
  492. }, "Exten", "createTime")
  493. if callData == nil || len(*callData) == 0 {
  494. //手机号没有通话记录
  495. continue
  496. }
  497. exten := gconv.String((*callData)["Exten"])
  498. if seatNumber != exten {
  499. continue
  500. } else {
  501. aa = true
  502. break
  503. }
  504. }
  505. return aa
  506. }