autoTask.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. phoneData := TiDb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{
  183. "baseinfo_id": uid,
  184. }, "phone", "")
  185. if phoneData == nil || len(*phoneData) == 0 {
  186. //数据不对跳过去
  187. continue
  188. }
  189. phone := gconv.String((*phoneData)["phone"])
  190. callData := TiDb.FindOne("Call_Accounting.voice_record", map[string]interface{}{
  191. "CalledNo": phone,
  192. }, "Exten", "createTime")
  193. if callData == nil || len(*callData) == 0 {
  194. //手机号没有通话记录
  195. continue
  196. }
  197. exten := gconv.String((*callData)["Exten"])
  198. if seatNumber != exten {
  199. continue
  200. }
  201. }
  202. //退私海处理
  203. nowTime := time.Now().Format(date.Date_Full_Layout)
  204. positionMap := map[int64]interface{}{}
  205. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  206. positionMap[positionId] = true
  207. 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
  208. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  209. "clue_id": clueId,
  210. "position_id": positionId,
  211. "change_type": "退回公海",
  212. "new_value": "所有人员私海已达上限",
  213. "createtime": nowTime,
  214. "BCPCID": common.GetRandom(32),
  215. "operator_id": -1,
  216. })
  217. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  218. "clue_id": clueId,
  219. "position_id": positionId,
  220. "change_field": "position_id",
  221. "change_type": "所属人变更",
  222. "old_value": GetPositionName(seatNumber),
  223. "new_value": "/",
  224. "createtime": nowTime,
  225. "BCPCID": common.GetRandom(32),
  226. "operator_id": -1,
  227. })
  228. return recordId > -1 && recordId1 > -1 && recordId2 > -1
  229. }) {
  230. //发送消息,找wjh
  231. fool = true
  232. log.Println("达上限自动退出线索成功")
  233. } else {
  234. log.Println("达上限自动退出线索失败")
  235. }
  236. }
  237. //释放邮件
  238. //发送信息
  239. if fool {
  240. AutoReleaseNots()
  241. }
  242. }
  243. }
  244. func autoExitSea() {
  245. log.Println("自动退海任务开始")
  246. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where trailstatus = "02" and (is_assign = 1 or is_assign = 0)`)
  247. if data != nil && len(*data) > 0 {
  248. for _, v := range *data {
  249. clueId := common.Int64All(v["id"])
  250. positionId := common.Int64All(v["position_id"])
  251. seatNumber := common.ObjToString(v["seatNumber"])
  252. nowTime := time.Now().Format(date.Date_Full_Layout)
  253. // is_assign := common.IntAll(v["is_assign"])
  254. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  255. 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
  256. // ok1 := true
  257. // if is_assign == 1 {
  258. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  259. // } else if is_assign == 0 {
  260. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  261. // }
  262. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  263. "clue_id": clueId,
  264. "position_id": positionId,
  265. "change_field": "position_id",
  266. "change_type": "所属人变更",
  267. "old_value": GetPositionName(seatNumber),
  268. "new_value": "/",
  269. "createtime": nowTime,
  270. "BCPCID": common.GetRandom(32),
  271. "operator_id": -1,
  272. })
  273. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  274. "clue_id": clueId,
  275. "position_id": positionId,
  276. "change_field": "trailstatus",
  277. "change_type": "基本信息变更",
  278. "old_value": "空号停机",
  279. "new_value": "流失",
  280. "createtime": nowTime,
  281. "BCPCID": common.GetRandom(32),
  282. "operator_id": -1,
  283. })
  284. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  285. "clue_id": clueId,
  286. "position_id": positionId,
  287. "change_type": "退出任务车",
  288. "new_value": "空号停机自动从线索池删除",
  289. "createtime": nowTime,
  290. "BCPCID": common.GetRandom(32),
  291. "operator_id": -1,
  292. })
  293. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  294. "clue_id": clueId,
  295. "position_id": positionId,
  296. "change_type": "退出公海",
  297. "new_value": "空号停机自动从线索池删除",
  298. "createtime": nowTime,
  299. "BCPCID": common.GetRandom(32),
  300. "operator_id": -1,
  301. })
  302. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  303. }) {
  304. log.Println("自动退出线索成功")
  305. } else {
  306. log.Println("自动退出线索失败")
  307. }
  308. }
  309. }
  310. datas := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"trailstatus": "00", "is_assign": 1}, "", "", -1, -1)
  311. if datas != nil && len(*datas) > 0 {
  312. for _, v := range *datas {
  313. clueId := common.Int64All(v["id"])
  314. positionId := common.Int64All(v["position_id"])
  315. seatNumber := common.ObjToString(v["seatNumber"])
  316. nowTime := time.Now().Format(date.Date_Full_Layout)
  317. // is_assign := common.IntAll(v["is_assign"])
  318. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  319. 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
  320. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  321. "clue_id": clueId,
  322. "position_id": positionId,
  323. "change_field": "position_id",
  324. "change_type": "所属人变更",
  325. "old_value": GetPositionName(seatNumber),
  326. "new_value": "/",
  327. "createtime": nowTime,
  328. "BCPCID": common.GetRandom(32),
  329. "operator_id": -1,
  330. })
  331. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  332. "clue_id": clueId,
  333. "position_id": positionId,
  334. "change_field": "trailstatus",
  335. "change_type": "基本信息变更",
  336. "old_value": "无意向客户",
  337. "new_value": "流失",
  338. "createtime": nowTime,
  339. "BCPCID": common.GetRandom(32),
  340. "operator_id": -1,
  341. })
  342. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  343. "clue_id": clueId,
  344. "position_id": positionId,
  345. "change_type": "退出任务车",
  346. "new_value": "无意向客户自动退回公海",
  347. "createtime": nowTime,
  348. "BCPCID": common.GetRandom(32),
  349. "operator_id": -1,
  350. })
  351. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  352. "clue_id": clueId,
  353. "position_id": positionId,
  354. "change_type": "退回公海",
  355. "new_value": "无意向客户自动退回公海",
  356. "createtime": nowTime,
  357. "BCPCID": common.GetRandom(32),
  358. "operator_id": -1,
  359. })
  360. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  361. }) {
  362. log.Println("自动退出私海成功")
  363. } else {
  364. log.Println("自动退出私海失败")
  365. }
  366. }
  367. }
  368. log.Println("自动退海任务结束")
  369. }
  370. func GetPositionName(seatNumber string) string {
  371. data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
  372. if data != nil && len(*data) > 0 {
  373. return common.ObjToString((*data)[0]["name"])
  374. }
  375. return ""
  376. }
  377. func Thaw() {
  378. //3个工作日查询
  379. k := 3
  380. nowTime := time.Now()
  381. for {
  382. if k == 0 {
  383. break
  384. }
  385. nowTime = nowTime.AddDate(0, 0, -1)
  386. if !IsBreak(nowTime) {
  387. continue
  388. }
  389. k--
  390. fmt.Println(nowTime.Format("20060102"))
  391. }
  392. nameMap := map[int64]string{}
  393. nameList := TiDb.SelectBySql("select name,position_id from dwd_f_crm_personnel_management")
  394. if nameList != nil && len(*nameList) > 0 {
  395. for _, v := range *nameList {
  396. nameMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
  397. }
  398. }
  399. //查询
  400. fmt.Println()
  401. layout := "2006-01-02 15:04:05"
  402. //三天以后退公海处理
  403. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  404. for _, v := range *l {
  405. clueId := gconv.Int64(v["id"])
  406. positionId := gconv.Int64(v["position_id"])
  407. freezeInt64 := int64(0)
  408. freezeStr := gconv.String(v["FREEZE_TIME"])
  409. if freezeStr != "" {
  410. t, _ := time.ParseInLocation(layout, freezeStr, time.Local)
  411. freezeInt64 = t.Unix()
  412. }
  413. if freezeInt64 < nowTime.Unix() {
  414. //超三天处理
  415. //分配状态改改
  416. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  417. "is_assign": 0,
  418. "position_id": 0,
  419. }) {
  420. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  421. "clue_id": clueId,
  422. "position_id": 0,
  423. "change_field": "position_id",
  424. "change_type": "所属人变更",
  425. "old_value": nameMap[positionId],
  426. "new_value": "/",
  427. "createtime": time.Now().Format(date.Date_Full_Layout),
  428. "BCPCID": common.GetRandom(32),
  429. "operator_id": -1,
  430. })
  431. }
  432. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  433. "clue_id": clueId,
  434. "position_id": 0,
  435. "change_field": "position_id",
  436. "change_type": "线索解冻",
  437. "new_value": "自动退回公海",
  438. "createtime": time.Now().Format(date.Date_Full_Layout),
  439. "BCPCID": common.GetRandom(32),
  440. "operator_id": -1,
  441. })
  442. } else {
  443. fmt.Println(FindUpperLimit(gconv.String(positionId), "positionId"))
  444. if !FindUpperLimit(gconv.String(positionId), "positionId") {
  445. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  446. "is_assign": 1,
  447. }) {
  448. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  449. "clue_id": clueId,
  450. "position_id": positionId,
  451. "change_field": "position_id",
  452. "change_type": "所属人变更",
  453. "old_value": "/",
  454. "new_value": nameMap[positionId],
  455. "createtime": time.Now().Format(date.Date_Full_Layout),
  456. "BCPCID": common.GetRandom(32),
  457. "operator_id": -1,
  458. })
  459. }
  460. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  461. "clue_id": clueId,
  462. "position_id": positionId,
  463. "change_field": "position_id",
  464. "change_type": "线索解冻",
  465. "new_value": "销售人员私海线索已释放",
  466. "createtime": time.Now().Format(date.Date_Full_Layout),
  467. "BCPCID": common.GetRandom(32),
  468. "operator_id": -1,
  469. })
  470. }
  471. }
  472. }
  473. return true
  474. }, `select id,position_id,FREEZE_TIME from dwd_f_crm_clue_info where is_assign = -3 `)
  475. }
  476. func IsBreak(currentTime time.Time) bool {
  477. if currentTime.Weekday() == time.Sunday {
  478. isok := false
  479. for k, v := range DateMap {
  480. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  481. isok = true
  482. }
  483. }
  484. return isok
  485. } else {
  486. isok := true
  487. for k, v := range DateMap {
  488. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  489. isok = false
  490. }
  491. }
  492. return isok
  493. }
  494. }