autoTask.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/mongodb"
  4. "database/sql"
  5. "fmt"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "log"
  8. "time"
  9. "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/date"
  11. )
  12. func autoTask() {
  13. log.Println("超时未跟进定时任务开始")
  14. t := time.Now()
  15. nowTime := time.Now().Format(date.Date_Full_Layout)
  16. statusMap := map[string]int{
  17. "07": 2, //待签署客户
  18. "06": 2, //高意向客户
  19. "05": 3, //意向客户
  20. "04": 7, //潜在客户
  21. }
  22. //判断节假日
  23. for status, statusInt := range statusMap {
  24. count, counts := 0, 0
  25. for {
  26. count++
  27. currentTime := t.AddDate(0, 0, -count)
  28. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  29. isok := false
  30. for k, v := range DateMap {
  31. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  32. isok = true
  33. }
  34. }
  35. if isok {
  36. counts++
  37. }
  38. } else {
  39. isok := true
  40. for k, v := range DateMap {
  41. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  42. isok = false
  43. }
  44. }
  45. if isok {
  46. counts++
  47. }
  48. }
  49. if counts >= statusInt {
  50. break
  51. }
  52. }
  53. statusMap[status] = count
  54. }
  55. log.Println(statusMap)
  56. for trailstatus := range map[string]string{
  57. "07": "", //待签署客户
  58. "06": "", //高意向客户
  59. "05": "", //意向客户
  60. "04": "", //潜在客户
  61. } {
  62. sql := `SELECT id,position_id,seatNumber,out_task_status FROM dwd_f_crm_clue_info
  63. WHERE trailstatus = ?`
  64. argsSelect := []interface{}{trailstatus}
  65. intime := ""
  66. sql += " AND comeintime <?"
  67. // nt := nexttime.(time.Time)
  68. nt := t.AddDate(0, 0, -statusMap[trailstatus])
  69. intime = nt.Format(date.Date_Full_Layout)
  70. argsSelect = append(argsSelect, intime)
  71. //
  72. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  73. for _, v := range *l {
  74. clueId := common.Int64All(v["id"])
  75. position_id := common.Int64All(v["position_id"])
  76. out_task_status := common.IntAll(v["out_task_status"])
  77. args2 := []interface{}{clueId}
  78. //获取跟进内容
  79. // sql1 := `select COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id =?;`
  80. sql2 := `SELECT COUNT(1) FROM dwd_f_crm_trail_content WHERE clue_id = ? and position_id = ?`
  81. if intime != "" {
  82. sql2 += ` and createtime > ?`
  83. args2 = append(args2, position_id)
  84. args2 = append(args2, intime)
  85. }
  86. //保留未跟进线索
  87. // if c1, c2 := TiDb.CountBySql(sql1, clueId), TiDb.CountBySql(sql2, args2...); (c1 != 0 && c2 > 0) || out_task_status == 1 {
  88. // log.Println("不满足线索过滤", clueId)
  89. // continue
  90. // }
  91. log.Println("intime ", clueId, intime, sql2)
  92. if c2 := TiDb.CountBySql(sql2, args2...); c2 > 0 || out_task_status == 1 {
  93. log.Println("不满足线索过滤", clueId)
  94. continue
  95. }
  96. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  97. "is_task": 1,
  98. "task_time": nowTime,
  99. "tasktime": time.Now().Format(date.Date_Short_Layout) + " 10:00:00",
  100. "taskstatus": 0,
  101. "tasksource": "超时未跟进自动加车",
  102. }) {
  103. AddChangeRecord(nil, clueId, position_id, "", "加入任务车", "超时未跟进自动加车", "", nowTime, "", -1)
  104. }
  105. }
  106. return true
  107. }, sql, argsSelect...)
  108. }
  109. log.Println("超时未跟进定时任务结束")
  110. }
  111. func autoTasks() {
  112. log.Println("按照跟进时间提前一天进入任务车定时任务开始")
  113. nowTime2 := time.Now().Format(date.Date_Full_Layout)
  114. nextTime := time.Now().AddDate(0, 0, 1).Format(date.Date_Full_Layout)
  115. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  116. for _, v := range *l {
  117. clueId := common.Int64All(v["id"])
  118. position_id := common.Int64All(v["position_id"])
  119. out_task_status := common.IntAll(v["out_task_status"])
  120. if position_id > 0 {
  121. if out_task_status != 1 && position_id > 0 {
  122. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  123. "is_task": 1,
  124. "task_time": nowTime2,
  125. "tasktime": nowTime2,
  126. "taskstatus": 0,
  127. "tasksource": "即将到达下次跟进时间",
  128. }) {
  129. AddChangeRecord(nil, clueId, position_id, "", "加入任务车", "即将到达下次跟进时间", "", nowTime2, "", -1)
  130. }
  131. }
  132. }
  133. }
  134. return true
  135. }, `SELECT a.id,a.position_id,a.seatNumber,a.out_task_status FROM dwd_f_crm_clue_info a
  136. LEFT JOIN dwd_f_crm_trail_content c ON c.clue_id=a.id
  137. WHERE c.next_time >= "`+nowTime2+`" and c.next_time <= "`+nextTime+`"`)
  138. log.Println("按照跟进时间提前一天进入任务车定时任务结束")
  139. }
  140. // 所有人达上限退公海处理
  141. func UpperLimitAutoExitSea(upperLimit int64) {
  142. countData := TiDb.SelectBySql(`SELECT
  143. COUNT(b.ID) AS count ,a.position_id
  144. FROM
  145. dwd_f_crm_clue_info b
  146. right JOIN
  147. ( select position_id from dwd_f_crm_personnel_management where assign_type = 1
  148. AND resign = 0 ) a on a.position_id=b.position_id and b.is_transfer != 1
  149. GROUP BY
  150. a.position_id HAVING count<?`, upperLimit)
  151. if countData == nil || len(*countData) > 0 {
  152. return
  153. }
  154. log.Println("所有人达上限退公海处理任务开始")
  155. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where (trailstatus = "04" or trailstatus = "03" or trailstatus = "01" ) and (is_assign = 1)`)
  156. if data != nil && len(*data) > 0 {
  157. fool := false
  158. for _, v := range *data {
  159. trailstatus := gconv.String(v["trailstatus"])
  160. //坐席信息查询
  161. clueId := common.Int64All(v["id"])
  162. createtime := gconv.String(v["comeintime"])
  163. clueName := gconv.String(v["cluename"])
  164. positionId := common.Int64All(v["position_id"])
  165. lockStatus := common.Int64All(v["lock_status"])
  166. seatNumber := common.ObjToString(v["seatNumber"])
  167. isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
  168. if trailstatus == "01" {
  169. //该线索最近一次分配给该电销人员后存在通话记录
  170. uid := gconv.String(v["uid"])
  171. if createtime == "" {
  172. continue
  173. }
  174. if !PhoneState(uid, seatNumber, createtime) {
  175. continue
  176. }
  177. }
  178. //退私海处理
  179. nowTime := time.Now().Format(date.Date_Full_Layout)
  180. positionMap := map[int64]interface{}{}
  181. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  182. positionMap[positionId] = true
  183. 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
  184. recordId2 := AddChangeRecord(tx, clueId, positionId, "", "退回公海", "所有人员私海已达上限", "", nowTime, "", -1)
  185. recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", FindSeatNumberToName(seatNumber), nowTime, "", -1)
  186. AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
  187. return recordId > -1 && recordId1 > -1 && recordId2 > -1
  188. }) {
  189. //发送消息,找wjh
  190. fool = true
  191. log.Println("达上限自动退出线索成功")
  192. } else {
  193. log.Println("达上限自动退出线索失败")
  194. }
  195. }
  196. //释放邮件
  197. //发送信息
  198. if fool {
  199. AutoReleaseNots()
  200. } else {
  201. CantBeAssignedNots()
  202. }
  203. }
  204. }
  205. func autoExitSea() {
  206. log.Println("自动退海任务开始")
  207. data := TiDb.SelectBySql(`select * from dwd_f_crm_clue_info where trailstatus = "02" and (is_assign = 1 or is_assign = 0)`)
  208. if data != nil && len(*data) > 0 {
  209. for _, v := range *data {
  210. clueId := common.Int64All(v["id"])
  211. positionId := common.Int64All(v["position_id"])
  212. seatNumber := common.ObjToString(v["seatNumber"])
  213. clueName := gconv.String(v["cluename"])
  214. lockStatus := common.Int64All(v["lock_status"])
  215. isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
  216. nowTime := time.Now().Format(date.Date_Full_Layout)
  217. // is_assign := common.IntAll(v["is_assign"])
  218. if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
  219. 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
  220. // ok1 := true
  221. // if is_assign == 1 {
  222. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId})
  223. // } else if is_assign == 0 {
  224. // ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  225. // }
  226. recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", FindSeatNumberToName(seatNumber), "/", nowTime, "", -1)
  227. recordId2 := AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "流失", "空号停机", nowTime, "", -1)
  228. recordId3 := AddChangeRecord(tx, clueId, positionId, "", "退出任务车", "空号停机自动从线索池删除", "", nowTime, "", -1)
  229. recordId4 := AddChangeRecord(tx, clueId, positionId, "", "退出公海", "空号停机自动从线索池删除", "", nowTime, "", -1)
  230. AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
  231. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  232. }) {
  233. log.Println("自动退出线索成功")
  234. } else {
  235. log.Println("自动退出线索失败")
  236. }
  237. }
  238. }
  239. datas := TiDb.SelectBySql("select * from dwd_f_crm_clue_info where (trailstatus='09' or trailstatus='00') and is_assign=1")
  240. if datas != nil && len(*datas) > 0 {
  241. for _, v := range *datas {
  242. trailstatus := gconv.String(v["trailstatus"])
  243. clueId := common.Int64All(v["id"])
  244. positionId := common.Int64All(v["position_id"])
  245. seatNumber := common.ObjToString(v["seatNumber"])
  246. nowTime := time.Now().Format(date.Date_Full_Layout)
  247. clueName := gconv.String(v["cluename"])
  248. lockStatus := common.Int64All(v["lock_status"])
  249. isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
  250. // is_assign := common.IntAll(v["is_assign"])
  251. if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
  252. 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
  253. recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", FindSeatNumberToName(seatNumber), nowTime, "", -1)
  254. recordId2 := AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "流失", gconv.String(common.If(trailstatus == "00", "无意向客户", "拒绝客户")), nowTime, "", -1)
  255. recordId3 := AddChangeRecord(tx, clueId, positionId, "", "退出任务车", gconv.String(common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海")), "", nowTime, "", -1)
  256. recordId4 := AddChangeRecord(tx, clueId, positionId, "", "退回公海", gconv.String(common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海")), "", nowTime, "", -1)
  257. AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
  258. return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
  259. }) {
  260. log.Println("自动退出私海成功")
  261. } else {
  262. log.Println("自动退出私海失败")
  263. }
  264. }
  265. }
  266. log.Println("自动退海任务结束")
  267. }
  268. func Thaw() {
  269. //3个工作日查询
  270. k := db.ThawDay
  271. nowTime := time.Now()
  272. for {
  273. if k == 0 {
  274. break
  275. }
  276. nowTime = nowTime.AddDate(0, 0, -1)
  277. if !IsBreak(nowTime) {
  278. continue
  279. }
  280. k--
  281. }
  282. nameMap := map[int64]string{}
  283. nameList := TiDb.SelectBySql("select name,position_id from dwd_f_crm_personnel_management")
  284. if nameList != nil && len(*nameList) > 0 {
  285. for _, v := range *nameList {
  286. nameMap[gconv.Int64(v["position_id"])] = gconv.String(v["name"])
  287. }
  288. }
  289. //查询
  290. layout := "2006-01-02 15:04:05"
  291. //三天以后退公海处理
  292. TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
  293. for _, v := range *l {
  294. clueId := gconv.Int64(v["id"])
  295. positionId := gconv.Int64(v["position_id"])
  296. freezeInt64 := int64(0)
  297. clueName := gconv.String(v["cluename"])
  298. lockStatus := common.Int64All(v["lock_status"])
  299. isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
  300. freezeStr := gconv.String(v["FREEZE_TIME"])
  301. if freezeStr != "" {
  302. t, _ := time.ParseInLocation(layout, freezeStr, time.Local)
  303. freezeInt64 = t.Unix()
  304. }
  305. if freezeInt64 < nowTime.Unix() {
  306. nowTime := time.Now().Format(date.Date_Full_Layout)
  307. //超三天处理
  308. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  309. "is_assign": 0,
  310. "position_id": 0,
  311. "seatNumber": nil,
  312. }) {
  313. AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", "/", nameMap[positionId], nowTime, "", -1)
  314. }
  315. AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "自动退回公海", "", nowTime, "", -1)
  316. AddLockeStatusChange(nil, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
  317. } else {
  318. //分配状态改改
  319. if !FindUpperLimit(gconv.String(positionId), "", false) {
  320. nowTime := time.Now().Format(date.Date_Full_Layout)
  321. if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  322. "is_assign": 1,
  323. }) {
  324. AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", nameMap[positionId], "/", nowTime, "", -1)
  325. }
  326. AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "销售人员私海线索已释放", "", nowTime, "", -1)
  327. AddLockeStatusChange(nil, clueId, positionId, positionId, lockStatus, isGroup, isCommerce, clueName, nowTime, "add")
  328. }
  329. }
  330. }
  331. return true
  332. }, `select * from dwd_f_crm_clue_info where is_assign = -3 `)
  333. }
  334. func IsBreak(currentTime time.Time) bool {
  335. if currentTime.Weekday() == time.Sunday {
  336. isok := false
  337. for k, v := range DateMap {
  338. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  339. isok = true
  340. }
  341. }
  342. return isok
  343. } else {
  344. isok := true
  345. for k, v := range DateMap {
  346. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  347. isok = false
  348. }
  349. }
  350. return isok
  351. }
  352. }
  353. func PhoneState(uid, seatNumber, createtime string) bool {
  354. phoneData := TiDb.SelectBySql("select DISTINCT phone from dwd_f_userbase_contacts where baseinfo_id= ?", uid)
  355. if phoneData == nil || len(*phoneData) == 0 {
  356. //数据不对跳过去
  357. return false
  358. }
  359. for _, v1 := range *phoneData {
  360. phone := gconv.String(v1["phone"])
  361. callData := TiDb.SelectBySql("select DISTINCT Exten from Call_Accounting.voice_record where CalledNo=? and createtime>=? ORDER BY createTime", phone, createtime)
  362. if callData == nil || len(*callData) == 0 {
  363. //手机号没有通话记录
  364. continue
  365. }
  366. for _, v2 := range *callData {
  367. exten := gconv.String(v2["Exten"])
  368. if seatNumber == exten {
  369. return true
  370. }
  371. }
  372. }
  373. return false
  374. }
  375. func LockHandle() {
  376. time.Now()
  377. log.Println("线索锁定定时任务开始")
  378. timeData := -1 * (db.CornExp8 + 5)
  379. timeDate := time.Now().Add(time.Duration(timeData) * time.Second)
  380. timeStr := timeDate.Format("2006-01-02 15:04:05")
  381. a := 1
  382. fool := false
  383. page := 1
  384. pageSize := 1000
  385. for !fool {
  386. 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)
  387. data := TiDb.SelectBySql(sqlStr)
  388. if len(*data) == 0 {
  389. fool = true
  390. break
  391. }
  392. for _, v := range *data {
  393. log.Println(a, gconv.String(v["updatetime"]))
  394. a++
  395. clue_id := gconv.Int64(v["id"])
  396. cluename := gconv.String(v["cluename"])
  397. position_id := gconv.String(v["position_id"])
  398. company_nature := gconv.Int64(v["company_nature"])
  399. company_verification := gconv.Int64(v["company_verification"])
  400. lock_status := gconv.Int64(v["lock_status"])
  401. //非集团在工商需要查询其他数据是否分配的有人
  402. if company_nature == 0 && company_verification == 1 {
  403. 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))
  404. if len(*status4Data) > 0 || status4Data != nil {
  405. position_id = gconv.String((*status4Data)[0]["position_id"])
  406. }
  407. }
  408. if company_nature == 1 && lock_status != 3 {
  409. //无需锁定处理
  410. if lock_status != 3 {
  411. go UpdateData(3, map[string]interface{}{
  412. "id": clue_id,
  413. }, 3, "")
  414. }
  415. } else if company_nature == 0 && company_verification == 1 {
  416. //已锁定处理
  417. if position_id != "" {
  418. go UpdateData(1, map[string]interface{}{
  419. "cluename": cluename,
  420. "company_verification": 1,
  421. "company_nature": 0,
  422. }, 1, position_id)
  423. } else {
  424. go UpdateData(2, map[string]interface{}{
  425. "cluename": cluename,
  426. "company_verification": 1,
  427. "company_nature": 0,
  428. }, 2, "")
  429. }
  430. } else {
  431. //未锁定处理
  432. if lock_status != 2 {
  433. go UpdateData(2, map[string]interface{}{
  434. "id": clue_id,
  435. }, 2, "")
  436. }
  437. }
  438. }
  439. page++
  440. }
  441. log.Println("线索锁定定时任务结束")
  442. }
  443. func UpdateData(lockStatus int64, query map[string]interface{}, status int, positionId string) {
  444. TiDb.Update("dwd_f_crm_clue_info", query, map[string]interface{}{
  445. "lock_status": lockStatus,
  446. "lock_position_id": positionId,
  447. })
  448. }
  449. // 大会员子账号不作为电销线索
  450. func ClueToDxTask() {
  451. //把所-5有的用户刷至公海
  452. ok := TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"is_assign": -5}, map[string]interface{}{"is_assign": 0, "seatNumber": nil, "position_id": 0})
  453. if !ok {
  454. log.Println("更新dwd_f_crm_clue_info处理子账号出错")
  455. return
  456. }
  457. //查询在客成的用户
  458. res := TiDb.Query("SELECT clue_id,ent_id FROM `dwd_f_csm_customer_info` WHERE is_transfer = 0")
  459. if res == nil || len(*res) <= 0 {
  460. log.Println("查dwd_f_csm_customer_info 客成用户异常")
  461. return
  462. }
  463. log.Println("客成用户量:", len(*res))
  464. for _, val := range *res {
  465. //log.Println("ent_id", val["ent_id"])
  466. clue_id := gconv.Int(val["clue_id"])
  467. childIds := []string{}
  468. if gconv.String(val["ent_id"]) == "" || gconv.String(val["ent_id"]) == "0" {
  469. continue
  470. }
  471. if mongodb.IsObjectIdHex(gconv.String(val["ent_id"])) { //mongodb 个人身份大会员
  472. //查询个人大会员下的子账号
  473. query := map[string]interface{}{"s_member_mainid": gconv.String(val["ent_id"])}
  474. childAccount, _ := Mgo.Find("user", query, "", `"_id":1`, false, -1, -1)
  475. if childAccount != nil && len(*childAccount) > 0 {
  476. log.Println("个人大会员子账号数量:", gconv.String(val["ent_id"]), len(*childAccount))
  477. for _, v := range *childAccount {
  478. childIds = append(childIds, mongodb.BsonIdToSId(v["_id"]))
  479. }
  480. }
  481. } else { //企业大会员
  482. //根据企业id查询企业下的员工
  483. if gconv.Int(val["ent_id"]) == 0 {
  484. continue
  485. }
  486. data := TiDb.Query("SELECT userid,uid FROM dwd_f_userbase_id_mapping WHERE ent_id = ?", gconv.Int(val["ent_id"]))
  487. if data != nil && len(*data) > 0 {
  488. log.Println("企业下员工数:", gconv.Int(val["ent_id"]), len(*data))
  489. for _, vv := range *data {
  490. childIds = append(childIds, gconv.String(vv["userid"]))
  491. }
  492. }
  493. }
  494. //更新线索信息
  495. if len(childIds) > 0 {
  496. ids := ""
  497. for k, vv := range childIds {
  498. if k == 0 {
  499. ids += fmt.Sprintf("'%s'", vv)
  500. } else {
  501. ids += fmt.Sprintf(",'%s'", vv)
  502. }
  503. }
  504. //log.Println("线索ids:", ids)
  505. sql := fmt.Sprintf("UPDATE dwd_f_crm_clue_info SET is_assign = -5 WHERE id != %d and userid IN (%s)", clue_id, ids)
  506. log.Println("sql:", sql)
  507. up := TiDb.UpdateOrDeleteBySql(sql)
  508. if up < 0 {
  509. log.Println("更新线索为-5出错", childIds)
  510. return
  511. }
  512. }
  513. }
  514. log.Println("定时任务完成")
  515. }