autoTask.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 and b.is_transfer != 1
  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. createtime := gconv.String(v["comeintime"])
  178. positionId := common.Int64All(v["position_id"])
  179. seatNumber := common.ObjToString(v["seatNumber"])
  180. if trailstatus == "01" {
  181. //该线索最近一次分配给该电销人员后存在通话记录
  182. uid := gconv.String(v["uid"])
  183. if createtime == "" {
  184. continue
  185. }
  186. if !PhoneState(uid, seatNumber, createtime) {
  187. continue
  188. }
  189. }
  190. //退私海处理
  191. nowTime := time.Now().Format(date.Date_Full_Layout)
  192. positionMap := map[int64]interface{}{}
  193. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  194. positionMap[positionId] = true
  195. 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
  196. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  197. "clue_id": clueId,
  198. "position_id": positionId,
  199. "change_type": "退回公海",
  200. "new_value": "所有人员私海已达上限",
  201. "createtime": nowTime,
  202. "BCPCID": common.GetRandom(32),
  203. "operator_id": -1,
  204. })
  205. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  206. "clue_id": clueId,
  207. "position_id": positionId,
  208. "change_field": "position_id",
  209. "change_type": "所属人变更",
  210. "old_value": GetPositionName(seatNumber),
  211. "new_value": "/",
  212. "createtime": nowTime,
  213. "BCPCID": common.GetRandom(32),
  214. "operator_id": -1,
  215. })
  216. return recordId > -1 && recordId1 > -1 && recordId2 > -1
  217. }) {
  218. //发送消息,找wjh
  219. fool = true
  220. log.Println("达上限自动退出线索成功")
  221. } else {
  222. log.Println("达上限自动退出线索失败")
  223. }
  224. }
  225. //释放邮件
  226. //发送信息
  227. if fool {
  228. AutoReleaseNots()
  229. } else {
  230. CantBeAssignedNots()
  231. }
  232. }
  233. }
  234. func autoExitSea() {
  235. log.Println("自动退海任务开始")
  236. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where trailstatus = "02" and (is_assign = 1 or is_assign = 0)`)
  237. if data != nil && len(*data) > 0 {
  238. for _, v := range *data {
  239. clueId := common.Int64All(v["id"])
  240. positionId := common.Int64All(v["position_id"])
  241. seatNumber := common.ObjToString(v["seatNumber"])
  242. nowTime := time.Now().Format(date.Date_Full_Layout)
  243. // is_assign := common.IntAll(v["is_assign"])
  244. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  245. 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
  246. // ok1 := true
  247. // if is_assign == 1 {
  248. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  249. // } else if is_assign == 0 {
  250. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  251. // }
  252. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  253. "clue_id": clueId,
  254. "position_id": positionId,
  255. "change_field": "position_id",
  256. "change_type": "所属人变更",
  257. "old_value": GetPositionName(seatNumber),
  258. "new_value": "/",
  259. "createtime": nowTime,
  260. "BCPCID": common.GetRandom(32),
  261. "operator_id": -1,
  262. })
  263. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  264. "clue_id": clueId,
  265. "position_id": positionId,
  266. "change_field": "trailstatus",
  267. "change_type": "基本信息变更",
  268. "old_value": "空号停机",
  269. "new_value": "流失",
  270. "createtime": nowTime,
  271. "BCPCID": common.GetRandom(32),
  272. "operator_id": -1,
  273. })
  274. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  275. "clue_id": clueId,
  276. "position_id": positionId,
  277. "change_type": "退出任务车",
  278. "new_value": "空号停机自动从线索池删除",
  279. "createtime": nowTime,
  280. "BCPCID": common.GetRandom(32),
  281. "operator_id": -1,
  282. })
  283. recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  284. "clue_id": clueId,
  285. "position_id": positionId,
  286. "change_type": "退出公海",
  287. "new_value": "空号停机自动从线索池删除",
  288. "createtime": nowTime,
  289. "BCPCID": common.GetRandom(32),
  290. "operator_id": -1,
  291. })
  292. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  293. }) {
  294. log.Println("自动退出线索成功")
  295. } else {
  296. log.Println("自动退出线索失败")
  297. }
  298. }
  299. }
  300. datas := TiDb.SelectBySql("select * from dwd_f_crm_clue_info where (trailstatus='09' or trailstatus='00') and is_assign=1")
  301. if datas != nil && len(*datas) > 0 {
  302. for _, v := range *datas {
  303. trailstatus := gconv.String(v["trailstatus"])
  304. clueId := common.Int64All(v["id"])
  305. positionId := common.Int64All(v["position_id"])
  306. seatNumber := common.ObjToString(v["seatNumber"])
  307. nowTime := time.Now().Format(date.Date_Full_Layout)
  308. // is_assign := common.IntAll(v["is_assign"])
  309. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  310. recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=0,position_id=null,seatNumber=null,updatetime = ?,comeintime_open = ?,comeinsource_open=?,level_open=4,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null WHERE id = ?`, nowTime, nowTime, common.If(trailstatus == "00", 8, 11), clueId) //,content=null,start_trail_time=null
  311. recordId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  312. "clue_id": clueId,
  313. "position_id": positionId,
  314. "change_field": "position_id",
  315. "change_type": "所属人变更",
  316. "old_value": GetPositionName(seatNumber),
  317. "new_value": "/",
  318. "createtime": nowTime,
  319. "BCPCID": common.GetRandom(32),
  320. "operator_id": -1,
  321. })
  322. recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  323. "clue_id": clueId,
  324. "position_id": positionId,
  325. "change_field": "trailstatus",
  326. "change_type": "基本信息变更",
  327. "old_value": common.If(trailstatus == "00", "无意向客户", "拒绝客户"),
  328. "new_value": "流失",
  329. "createtime": nowTime,
  330. "BCPCID": common.GetRandom(32),
  331. "operator_id": -1,
  332. })
  333. recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  334. "clue_id": clueId,
  335. "position_id": positionId,
  336. "change_type": "退出任务车",
  337. "new_value": common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海"),
  338. "createtime": nowTime,
  339. "BCPCID": common.GetRandom(32),
  340. "operator_id": -1,
  341. })
  342. recordId4 := 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": common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海"),
  347. "createtime": nowTime,
  348. "BCPCID": common.GetRandom(32),
  349. "operator_id": -1,
  350. })
  351. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  352. }) {
  353. log.Println("自动退出私海成功")
  354. } else {
  355. log.Println("自动退出私海失败")
  356. }
  357. }
  358. }
  359. log.Println("自动退海任务结束")
  360. }
  361. func GetPositionName(seatNumber string) string {
  362. data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
  363. if data != nil && len(*data) > 0 {
  364. return common.ObjToString((*data)[0]["name"])
  365. }
  366. return ""
  367. }
  368. func Thaw() {
  369. //3个工作日查询
  370. k := db.ThawDay
  371. nowTime := time.Now()
  372. for {
  373. if k == 0 {
  374. break
  375. }
  376. nowTime = nowTime.AddDate(0, 0, -1)
  377. if !IsBreak(nowTime) {
  378. continue
  379. }
  380. k--
  381. }
  382. nameMap := map[int64]string{}
  383. nameList := TiDb.SelectBySql("select name,position_id from dwd_f_crm_personnel_management")
  384. if nameList != nil && len(*nameList) > 0 {
  385. for _, v := range *nameList {
  386. nameMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
  387. }
  388. }
  389. //查询
  390. layout := "2006-01-02 15:04:05"
  391. //三天以后退公海处理
  392. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  393. for _, v := range *l {
  394. clueId := gconv.Int64(v["id"])
  395. positionId := gconv.Int64(v["position_id"])
  396. freezeInt64 := int64(0)
  397. freezeStr := gconv.String(v["FREEZE_TIME"])
  398. if freezeStr != "" {
  399. t, _ := time.ParseInLocation(layout, freezeStr, time.Local)
  400. freezeInt64 = t.Unix()
  401. }
  402. if freezeInt64 < nowTime.Unix() {
  403. //超三天处理
  404. //分配状态改改
  405. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  406. "is_assign": 0,
  407. "position_id": 0,
  408. }) {
  409. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  410. "clue_id": clueId,
  411. "position_id": 0,
  412. "change_field": "position_id",
  413. "change_type": "所属人变更",
  414. "old_value": nameMap[positionId],
  415. "new_value": "/",
  416. "createtime": time.Now().Format(date.Date_Full_Layout),
  417. "BCPCID": common.GetRandom(32),
  418. "operator_id": -1,
  419. })
  420. }
  421. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  422. "clue_id": clueId,
  423. "position_id": 0,
  424. "change_field": "position_id",
  425. "change_type": "线索解冻",
  426. "new_value": "自动退回公海",
  427. "createtime": time.Now().Format(date.Date_Full_Layout),
  428. "BCPCID": common.GetRandom(32),
  429. "operator_id": -1,
  430. })
  431. } else {
  432. if !FindUpperLimit(gconv.String(positionId), "", false) {
  433. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  434. "is_assign": 1,
  435. }) {
  436. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  437. "clue_id": clueId,
  438. "position_id": positionId,
  439. "change_field": "position_id",
  440. "change_type": "所属人变更",
  441. "old_value": "/",
  442. "new_value": nameMap[positionId],
  443. "createtime": time.Now().Format(date.Date_Full_Layout),
  444. "BCPCID": common.GetRandom(32),
  445. "operator_id": -1,
  446. })
  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. "new_value": "销售人员私海线索已释放",
  454. "createtime": time.Now().Format(date.Date_Full_Layout),
  455. "BCPCID": common.GetRandom(32),
  456. "operator_id": -1,
  457. })
  458. }
  459. }
  460. }
  461. return true
  462. }, `select id,position_id,FREEZE_TIME from dwd_f_crm_clue_info where is_assign = -3 `)
  463. }
  464. func IsBreak(currentTime time.Time) bool {
  465. if currentTime.Weekday() == time.Sunday {
  466. isok := false
  467. for k, v := range DateMap {
  468. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  469. isok = true
  470. }
  471. }
  472. return isok
  473. } else {
  474. isok := true
  475. for k, v := range DateMap {
  476. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  477. isok = false
  478. }
  479. }
  480. return isok
  481. }
  482. }
  483. func PhoneState(uid, seatNumber, createtime string) bool {
  484. phoneData := TiDb.SelectBySql("select DISTINCT phone from dwd_f_userbase_contacts where baseinfo_id= ?", uid)
  485. if phoneData == nil || len(*phoneData) == 0 {
  486. //数据不对跳过去
  487. return false
  488. }
  489. for _, v1 := range *phoneData {
  490. phone := gconv.String(v1["phone"])
  491. callData := TiDb.SelectBySql("select DISTINCT Exten from Call_Accounting.voice_record where CalledNo=? and createtime>=? ORDER BY createTime", phone, createtime)
  492. if callData == nil || len(*callData) == 0 {
  493. //手机号没有通话记录
  494. continue
  495. }
  496. for _, v2 := range *callData {
  497. exten := gconv.String(v2["Exten"])
  498. if seatNumber == exten {
  499. return true
  500. }
  501. }
  502. }
  503. return false
  504. }
  505. func LockHandle() {
  506. time.Now()
  507. log.Println("线索锁定定时任务开始")
  508. timeData := -1 * (db.CornExp8 + 5)
  509. timeDate := time.Now().Add(time.Duration(timeData) * time.Second)
  510. timeStr := timeDate.Format("2006-01-02 15:04:05")
  511. a := 1
  512. fool := false
  513. page := 1
  514. pageSize := 1000
  515. for !fool {
  516. sqlStr := fmt.Sprintf(`select id,cluename,position_id,updatetime,company_nature,company_verification,lock_status from dwd_f_crm_clue_info where updatetime>="%s" order by updatetime limit %d OFFSET %d`, timeStr, pageSize, (page-1)*pageSize)
  517. data := TiDb.SelectBySql(sqlStr)
  518. if len(*data) == 0 {
  519. fool = true
  520. break
  521. }
  522. for _, v := range *data {
  523. log.Println(a, gconv.String(v["updatetime"]))
  524. a++
  525. clue_id := gconv.Int64(v["id"])
  526. cluename := gconv.String(v["cluename"])
  527. position_id := gconv.String(v["position_id"])
  528. company_nature := gconv.Int64(v["company_nature"])
  529. company_verification := gconv.Int64(v["company_verification"])
  530. lock_status := gconv.Int64(v["lock_status"])
  531. //非集团在工商需要查询其他数据是否分配的有人
  532. if company_nature == 0 && company_verification == 1 {
  533. status4Data := TiDb.SelectBySql(fmt.Sprintf(`select GROUP_CONCAT(DISTINCT position_id) as position_id from dwd_f_crm_clue_info where cluename="%s" and position_id>0 `, cluename))
  534. if len(*status4Data) > 0 || status4Data != nil {
  535. position_id = gconv.String((*status4Data)[0]["position_id"])
  536. }
  537. }
  538. if company_nature == 1 && lock_status != 3 {
  539. //无需锁定处理
  540. if lock_status != 3 {
  541. go UpdateData(3, map[string]interface{}{
  542. "id": clue_id,
  543. }, 3, "")
  544. }
  545. } else if company_nature == 0 && company_verification == 1 {
  546. //已锁定处理
  547. if position_id != "" {
  548. go UpdateData(1, map[string]interface{}{
  549. "cluename": cluename,
  550. "company_verification": 1,
  551. "company_nature": 0,
  552. }, 1, position_id)
  553. } else {
  554. go UpdateData(2, map[string]interface{}{
  555. "cluename": cluename,
  556. "company_verification": 1,
  557. "company_nature": 0,
  558. }, 2, "")
  559. }
  560. } else {
  561. //未锁定处理
  562. if lock_status != 2 {
  563. go UpdateData(2, map[string]interface{}{
  564. "id": clue_id,
  565. }, 2, "")
  566. }
  567. }
  568. }
  569. page++
  570. }
  571. log.Println("线索锁定定时任务结束")
  572. }
  573. func UpdateData(lockStatus int64, query map[string]interface{}, status int, positionId string) {
  574. TiDb.Update("dwd_f_crm_clue_info", query, map[string]interface{}{
  575. "lock_status": lockStatus,
  576. "lock_position_id": positionId,
  577. })
  578. }