job.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/date"
  9. "app.yhyue.com/moapp/jybase/redis"
  10. "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/mongodb"
  12. )
  13. // 未支付订单 30分钟一次
  14. func orders() {
  15. //一个小时未支付进入线索 A
  16. log.Println("未支付订单定时任务开始")
  17. lastOrderId := cfg.LastOrderId
  18. selectTimeStart := time.Unix(time.Now().Unix()-7200, 0).Format("2006-01-02 15:04:05")
  19. selectTimeEnd := time.Unix(time.Now().Unix()-3600, 0).Format("2006-01-02 15:04:05")
  20. sql := fmt.Sprintf(`select * from dataexport_order where order_status = 0 and product_type in ("大会员","VIP订阅","数据流量包","历史数据") and create_time <= "%s" and create_time >= "%s" and id > %s`, selectTimeEnd, selectTimeStart, fmt.Sprint(lastOrderId))
  21. data := Mysql.SelectBySql(sql)
  22. if data != nil && *data != nil && len(*data) > 0 {
  23. for _, v := range *data {
  24. // FormatData(v, "orders")
  25. // if k == len(*data)-1 {
  26. // lastOrderId = common.IntAll(v["id"])
  27. // }
  28. ok1, _ := FormatData(v, "orders")
  29. if !ok1 {
  30. common.WriteSysConfig(&cfg)
  31. break
  32. }
  33. cfg.LastOrderId = common.IntAll(v["id"])
  34. }
  35. }
  36. common.WriteSysConfig(&cfg)
  37. log.Println("未支付订单定时任务结束")
  38. }
  39. // 新注册用户 5分钟一次
  40. func users() {
  41. //判断节假日
  42. currentTime, runOk := time.Now(), false
  43. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  44. isok := false
  45. for k, v := range DateMap {
  46. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  47. isok = true
  48. }
  49. }
  50. if isok {
  51. runOk = true
  52. }
  53. } else {
  54. isok := true
  55. for k, v := range DateMap {
  56. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  57. isok = false
  58. }
  59. }
  60. if isok {
  61. runOk = true
  62. }
  63. }
  64. if !runOk {
  65. log.Println("不是工作日,任务暂停")
  66. return
  67. } else {
  68. if currentTime.Hour() == 17 && currentTime.Minute() > 30 {
  69. log.Println("不是工作日,任务暂停")
  70. return
  71. }
  72. }
  73. //新用户注册后5分钟内进入线索 C
  74. log.Println("新注册用户定时任务开始")
  75. selectTimeEnd := time.Unix(time.Now().Unix()-300, 0).Format("2006-01-02 15:04:05")
  76. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where createtime > "%s" and source = 1`, selectTimeEnd)
  77. data := TiDb.SelectBySql(sql)
  78. if data != nil && *data != nil && len(*data) > 0 {
  79. for _, v := range *data {
  80. FormatData(v, "users")
  81. }
  82. }
  83. log.Println("新注册用户定时任务结束")
  84. }
  85. func userbase() {
  86. log.Println("userbase定时任务开始")
  87. selectTimeEnd := time.Unix(time.Now().Unix()-1800, 0).Format("2006-01-02 15:04:05")
  88. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where updatetime > "%s" and source > 0`, selectTimeEnd)
  89. data := TiDb.SelectBySql(sql)
  90. if data != nil && *data != nil && len(*data) > 0 {
  91. for _, v := range *data {
  92. phone := common.ObjToString(v["phone"])
  93. uId := common.ObjToString(v["uid"])
  94. userId := common.ObjToString(v["userid"])
  95. registedate := common.ObjToString(v["l_registedate"])
  96. name := common.ObjToString(v["name"])
  97. nowTime := time.Now().Format(date.Date_Full_Layout)
  98. source := common.IntAll(v["source"])
  99. if phone != "" {
  100. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  101. if contactsData == nil || len(*contactsData) == 0 {
  102. registedates, _ := time.Parse(date.Date_Full_Layout, registedate)
  103. count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where phone = ?", phone)
  104. if time.Now().Unix()-registedates.Unix() > 7*86400 {
  105. if count == 0 {
  106. FormatData(v, "users")
  107. } else {
  108. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  109. "status": 1,
  110. "is_delete": 1,
  111. "createtime": nowTime,
  112. "updatetime": nowTime,
  113. "phone": phone,
  114. "baseinfo_id": uId,
  115. "SOURCE": source,
  116. })
  117. }
  118. } else {
  119. if count == 0 {
  120. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  121. "userid": userId,
  122. "uid": uId,
  123. "is_assign": 0,
  124. "comeintime": nowTime,
  125. "createtime": nowTime,
  126. "updatetime": nowTime,
  127. "cluename": phone,
  128. "top_cluetype": "",
  129. "sub_cluetype": "",
  130. "trailstatus": "01",
  131. "name": name,
  132. "phone": phone,
  133. })
  134. if clueId > 0 {
  135. TiDb.Insert("dwd_f_crm_open_sea", map[string]interface{}{
  136. "clue_id": clueId,
  137. "comeintime": nowTime,
  138. "comeinsource": 2,
  139. })
  140. }
  141. } else {
  142. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  143. "status": 1,
  144. "is_delete": 1,
  145. "createtime": nowTime,
  146. "updatetime": nowTime,
  147. "phone": phone,
  148. "baseinfo_id": uId,
  149. "SOURCE": source,
  150. })
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. log.Println("userbase定时任务结束")
  158. }
  159. // 留资 5分钟一次
  160. func saleLeads() {
  161. //判断节假日
  162. currentTime, runOk := time.Now(), false
  163. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  164. isok := false
  165. for k, v := range DateMap {
  166. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  167. isok = true
  168. }
  169. }
  170. if isok {
  171. runOk = true
  172. }
  173. } else {
  174. isok := true
  175. for k, v := range DateMap {
  176. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  177. isok = false
  178. }
  179. }
  180. if isok {
  181. runOk = true
  182. }
  183. }
  184. if !runOk {
  185. log.Println("不是工作日,任务暂停")
  186. return
  187. } else {
  188. if currentTime.Hour() == 17 && currentTime.Minute() > 30 {
  189. log.Println("不是工作日,任务暂停")
  190. return
  191. }
  192. }
  193. //留资后5分钟内进入线索
  194. //分为免费留资和付费留资 付费B 免费C
  195. log.Println("用户留资定时任务开始")
  196. session := Mgo.GetMgoConn()
  197. lastId := cfg.LastId
  198. defer func() {
  199. Mgo.DestoryMongoConn(session)
  200. }()
  201. query := map[string]interface{}{}
  202. if lastId != "" {
  203. query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastId)}
  204. }
  205. // query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63f6049a4c172a1fd44ed3c8")}
  206. log.Println("query :", query)
  207. iter := session.DB(cfg.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
  208. thisData := map[string]interface{}{}
  209. for {
  210. if !iter.Next(&thisData) {
  211. break
  212. }
  213. ok1, _ := FormatData(thisData, "saleLeads")
  214. if !ok1 {
  215. common.WriteSysConfig(&cfg)
  216. break
  217. }
  218. cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
  219. // FormatData(thisData, "saleLeads")
  220. }
  221. common.WriteSysConfig(&cfg)
  222. log.Println("用户留资定时任务结束")
  223. }
  224. func FormatData(data map[string]interface{}, item string) (bool, bool) {
  225. userId, uId, positionId, source, cluename, phone := common.ObjToString(data["user_id"]), "", "", 0, "", ""
  226. role, industry, department, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", ""
  227. query, topname, subname := map[string]interface{}{}, "", ""
  228. if item == "orders" {
  229. if !mongodb.IsObjectIdHex(userId) {
  230. positionId = userId
  231. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  232. if userMapping != nil && len(*userMapping) > 0 {
  233. userId = common.ObjToString((*userMapping)["userid"])
  234. }
  235. }
  236. query["userid"] = userId
  237. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  238. if userInfo != nil && len(*userInfo) > 0 {
  239. uId = common.ObjToString((*userInfo)["uid"])
  240. source = common.IntAll((*userInfo)["source"])
  241. }
  242. //cluename --> company_name
  243. cluename = common.ObjToString(data["company_name"])
  244. phone = common.ObjToString(data["user_phone"])
  245. } else if item == "users" {
  246. userId = mongodb.BsonIdToSId(data["userid"])
  247. //新用户没有uid、source要等5分钟
  248. cluename = common.ObjToString(data["company_name"])
  249. phone = common.ObjToString(data["phone"])
  250. if phone != "" {
  251. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  252. if contactsData != nil && len(*contactsData) > 0 {
  253. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  254. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  255. query["uid"] = uId
  256. } else {
  257. query["userid"] = userId
  258. }
  259. } else {
  260. query["userid"] = userId
  261. }
  262. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  263. if userInfo != nil && len(*userInfo) > 0 {
  264. uId = common.ObjToString((*userInfo)["uid"])
  265. source = common.IntAll((*userInfo)["source"])
  266. // userId = common.ObjToString((*userInfo)["userid"])
  267. }
  268. }
  269. } else if item == "saleLeads" {
  270. userId = common.ObjToString(data["userid"])
  271. if !mongodb.IsObjectIdHex(userId) {
  272. positionId = userId
  273. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  274. if userMapping != nil && len(*userMapping) > 0 {
  275. userId = common.ObjToString((*userMapping)["userid"])
  276. }
  277. }
  278. cluename = common.ObjToString(data["company"])
  279. phone = common.ObjToString(data["phone"])
  280. role = common.ObjToString(data["companyType"])
  281. industry = common.ObjToString(data["industry"])
  282. department = common.ObjToString(data["branch"])
  283. position = common.ObjToString(data["position"])
  284. name = common.ObjToString(data["name"])
  285. // sourceCode = common.ObjToString(data["source"])
  286. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  287. if contactsData != nil && len(*contactsData) > 0 {
  288. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  289. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  290. query["uid"] = uId
  291. } else {
  292. query["userid"] = userId
  293. }
  294. } else {
  295. query["userid"] = userId
  296. }
  297. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  298. if userInfo != nil && len(*userInfo) > 0 {
  299. uId = common.ObjToString((*userInfo)["uid"])
  300. source = common.IntAll((*userInfo)["source"])
  301. userId = common.ObjToString((*userInfo)["userid"])
  302. }
  303. }
  304. //线索名称为空用手机号代替
  305. if cluename == "" {
  306. cluename = phone
  307. }
  308. //域外用户和内部用户和没有手机号,不存线索
  309. if source == 5 || source == 6 || phone == "" {
  310. log.Println("线索分配失败,线索过滤!!", item, source, phone, userId)
  311. return true, false
  312. }
  313. if uId == "" {
  314. if isExists, _ := redis.Exists("bidx", "bidx_userId_"+userId); isExists {
  315. redisInt := redis.GetInt("bidx", "bidx_userId_"+userId)
  316. if redisInt > 4 {
  317. log.Println("线索分配失败,线索缺少信息,任务已执行超过1次", item, userId, phone)
  318. return true, true
  319. } else {
  320. redis.Incr("bidx", "bidx_userId_"+userId)
  321. }
  322. } else {
  323. redis.Put("bidx", "bidx_userId_"+userId, 1, 3600)
  324. }
  325. log.Println("线索分配失败,线索缺少信息", item, phone, userId)
  326. return false, false
  327. }
  328. //不是留资的要查一遍留资
  329. if item == "orders" {
  330. qid := positionId
  331. if qid == "" {
  332. qid = userId
  333. }
  334. saleLeadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"userid": qid}, map[string]interface{}{"_id": -1}, nil, false, 0, 1)
  335. if ok && saleLeadsData != nil && len(*saleLeadsData) > 0 {
  336. sdata := (*saleLeadsData)[0]
  337. role = common.ObjToString(sdata["companyType"])
  338. industry = common.ObjToString(sdata["industry"])
  339. department = common.ObjToString(sdata["branch"])
  340. position = common.ObjToString(sdata["position"])
  341. name = common.ObjToString(sdata["name"])
  342. }
  343. }
  344. //top_cluetype
  345. top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data)
  346. //follow_project_area --> follow_project_monitor
  347. follow_project_area = getAreaCode(userId)
  348. //seatNumber position_id
  349. log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area)
  350. if top_cluetype == "" || sub_cluetype == "" || level == "" {
  351. log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId)
  352. return true, true
  353. }
  354. position_id, seatNumber, saleName, saleData := autoDraw(level)
  355. log.Println("data -------", position_id, seatNumber, saleName)
  356. if position_id > 0 && seatNumber != "" {
  357. // if TiDb.Count("dwd_f_crm_private_sea", map[string]interface{}{"position_id": position_id}) >= cfg.CountLimit {
  358. // log.Println("线索分配失败,私海线索数据超过限制", item, position_id, seatNumber, uId, userId, phone)
  359. // TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, level)
  360. // return false, false
  361. // // position_id, seatNumber, saleName, saleData = autoDraw(level)
  362. // }
  363. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true
  364. if uCount != nil && len(*uCount) > 0 {
  365. oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id, source)
  366. } else {
  367. oks = SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id, source)
  368. }
  369. if oks {
  370. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, level)
  371. } else {
  372. log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone)
  373. return false, false
  374. }
  375. }
  376. return true, true
  377. }
  378. func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64, source int) bool {
  379. nowTime := time.Now().Format("2006-01-02 15:04:05")
  380. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  381. clueId, seaId, uodateId1, uodateId2, uodateId3, uodateId4 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  382. // BCPCID := common.GetRandom(32)
  383. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  384. //线索
  385. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  386. "userid": userId,
  387. "uid": uId,
  388. "seatNumber": seatNumber,
  389. "position_id": positionId,
  390. "is_assign": 1,
  391. "comeintime": nowTime,
  392. "createtime": nowTime,
  393. "updatetime": nowTime,
  394. "cluename": cluename,
  395. "top_cluetype": top_cluetype,
  396. "sub_cluetype": sub_cluetype,
  397. "trailstatus": "01",
  398. "name": name,
  399. "phone": phone,
  400. "position": position,
  401. "department": department,
  402. "industry": industry,
  403. "follow_project_area": follow_project_area,
  404. "role": role,
  405. })
  406. //私海 --> 任务车
  407. seaId = TiDb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  408. "clue_id": clueId,
  409. "seatNumber": seatNumber,
  410. "position_id": positionId,
  411. "comeintime": nowTime,
  412. "comeinsource": 2,
  413. "is_task": 1,
  414. "task_time": nowTime,
  415. "tasktime": common.If(item == "users", nowTimes, nowTime),
  416. "taskstatus": 0,
  417. "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
  418. })
  419. //变更记录
  420. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  421. "clue_id": clueId,
  422. "position_id": positionId,
  423. "change_type": "创建线索",
  424. "new_value": "系统自动创建",
  425. "createtime": nowTime,
  426. "BCPCID": common.GetRandom(32),
  427. "operator_id": -1,
  428. })
  429. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  430. "clue_id": clueId,
  431. "position_id": positionId,
  432. "change_field": "position_id",
  433. "change_type": "所属人变更",
  434. "old_value": "/",
  435. "new_value": saleName,
  436. "createtime": nowTime,
  437. "BCPCID": common.GetRandom(32),
  438. "operator_id": -1,
  439. })
  440. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  441. "clue_id": clueId,
  442. "position_id": positionId,
  443. "change_field": "trailstatus",
  444. "change_type": "基本信息变更",
  445. "old_value": "商机线索",
  446. "new_value": "新增",
  447. "createtime": nowTime,
  448. "BCPCID": common.GetRandom(32),
  449. "operator_id": -1,
  450. })
  451. uodateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  452. "clue_id": clueId,
  453. "position_id": positionId,
  454. "change_type": "加入任务车",
  455. "new_value": "线索自动分配" + "-" + topname + "-" + subname,
  456. "createtime": nowTime,
  457. "BCPCID": common.GetRandom(32),
  458. "operator_id": -1,
  459. })
  460. return clueId > -1 && seaId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1
  461. }) {
  462. log.Println("线索分配成功")
  463. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  464. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  465. "status": 1,
  466. "is_delete": 1,
  467. "createtime": nowTime,
  468. "updatetime": nowTime,
  469. "phone": phone,
  470. "baseinfo_id": uId,
  471. "SOURCE": source,
  472. })
  473. }
  474. return true
  475. } else {
  476. log.Println("线索分配失败!!!", clueId, seaId, uodateId1, uodateId2, uodateId3, uodateId4, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  477. return false
  478. }
  479. }
  480. func UpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64, source int) bool {
  481. nowTime := time.Now().Format("2006-01-02 15:04:05")
  482. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  483. //trailstatus = 无意向 为变更线索状态
  484. //没有变更所属人查任务车,有车不变
  485. trailstatus := common.ObjToString(data["trailstatus"])
  486. clueId := common.Int64All(data["id"])
  487. is_assign := common.IntAll(data["is_assign"])
  488. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  489. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  490. //已在任务车的判断任务时间
  491. privateData := TiDb.FindOne("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, "", "")
  492. if privateData != nil && len(*privateData) > 0 {
  493. oldTaskTime = common.ObjToString((*privateData)["tasktime"])
  494. is_task = common.IntAll(data["is_task"])
  495. taskstatus = common.IntAll(data["taskstatus"])
  496. }
  497. if taskstatus == 1 || is_task == 0 {
  498. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes))
  499. } else {
  500. if is_task == 1 {
  501. if oldTaskTime != "" {
  502. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  503. if err == nil && time.Now().Before(t1) {
  504. taskTime = nowTime
  505. } else {
  506. taskTime = oldTaskTime
  507. }
  508. }
  509. }
  510. }
  511. //加入任务车判断节假日
  512. count, counts, t := 0, 0, time.Now()
  513. for {
  514. count++
  515. currentTime := t.AddDate(0, 0, -count)
  516. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  517. isok := false
  518. for k, v := range DateMap {
  519. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  520. isok = true
  521. }
  522. }
  523. if isok {
  524. counts++
  525. }
  526. } else {
  527. isok := true
  528. for k, v := range DateMap {
  529. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  530. isok = false
  531. }
  532. }
  533. if isok {
  534. counts++
  535. }
  536. }
  537. if counts >= 2 {
  538. break
  539. }
  540. }
  541. recordCount := TiDb.CountBySql(`select count(1) from dwd_f_crm_trail_content where clue_id = ? and createtime > ?`, clueId, t.AddDate(0, 0, -count).Format(date.Date_Full_Layout))
  542. //
  543. clueUpdateData := map[string]interface{}{
  544. "is_assign": 1,
  545. "updatetime": nowTime,
  546. "top_cluetype": top_cluetype,
  547. "sub_cluetype": sub_cluetype,
  548. "userid": userId,
  549. }
  550. seaUpdateData := map[string]interface{}{
  551. "comeinsource": 2,
  552. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  553. }
  554. insertSeaData := map[string]interface{}{
  555. "clue_id": clueId,
  556. "comeintime": nowTime,
  557. "comeinsource": 2,
  558. "is_task": 1,
  559. "task_time": nowTime,
  560. "tasktime": taskTime,
  561. "taskstatus": 0,
  562. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  563. }
  564. if cluename != "" {
  565. clueUpdateData["cluename"] = cluename
  566. }
  567. if item != "orders" && item != "users" {
  568. clueUpdateData["name"] = name
  569. clueUpdateData["position"] = position
  570. clueUpdateData["department"] = department
  571. clueUpdateData["industry"] = industry
  572. clueUpdateData["follow_project_area"] = follow_project_area
  573. clueUpdateData["role"] = role
  574. }
  575. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 {
  576. clueUpdateData["seatNumber"] = seatNumber
  577. clueUpdateData["position_id"] = positionId
  578. clueUpdateData["comeintime"] = nowTime
  579. seaUpdateData["seatNumber"] = seatNumber
  580. seaUpdateData["position_id"] = positionId
  581. seaUpdateData["comeintime"] = nowTime
  582. insertSeaData["seatNumber"] = seatNumber
  583. insertSeaData["position_id"] = positionId
  584. for _, v := range saleData {
  585. if common.ObjToString(data["seatNumber"]) == common.ObjToString(v["seatNumber"]) {
  586. oldsaleName = common.ObjToString(v["name"])
  587. }
  588. }
  589. } else {
  590. insertSeaData["seatNumber"] = old_seatNumber
  591. insertSeaData["position_id"] = old_position_id
  592. }
  593. if trailstatus != "08" && is_assign == 0 {
  594. clueUpdateData["trailstatus"] = "01"
  595. }
  596. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  597. seaUpdateData["is_task"] = 1
  598. seaUpdateData["task_time"] = nowTime
  599. seaUpdateData["tasktime"] = taskTime
  600. seaUpdateData["taskstatus"] = 0
  601. }
  602. ok, ok1, ok2, seaId := true, true, true, int64(0)
  603. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  604. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  605. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  606. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  607. //线索
  608. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  609. if is_assign == 1 {
  610. ok1 = TiDb.UpdateByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, seaUpdateData)
  611. } else {
  612. ok2 = TiDb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  613. seaId = TiDb.InsertByTx(tx, "dwd_f_crm_private_sea", insertSeaData)
  614. }
  615. //变更记录
  616. if (trailstatus == "00" || old_position_id == 0) && is_assign == 1 {
  617. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  618. "clue_id": clueId,
  619. "position_id": positionId,
  620. "change_field": "position_id",
  621. "change_type": "所属人变更",
  622. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  623. "new_value": common.If(saleName != "", saleName, "/"),
  624. "createtime": nowTime,
  625. "BCPCID": common.GetRandom(32),
  626. "operator_id": -1,
  627. })
  628. if trailstatus == "00" {
  629. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  630. "clue_id": clueId,
  631. "position_id": common.Int64All(data["position_id"]),
  632. "change_field": "trailstatus",
  633. "change_type": "基本信息变更",
  634. "old_value": "无意向客户",
  635. "new_value": "流失",
  636. "createtime": nowTime,
  637. "BCPCID": common.GetRandom(32),
  638. "operator_id": -1,
  639. })
  640. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  641. "clue_id": clueId,
  642. "position_id": positionId,
  643. "change_field": "trailstatus",
  644. "change_type": "基本信息变更",
  645. "old_value": "商机线索",
  646. "new_value": "新增",
  647. "createtime": nowTime,
  648. "BCPCID": common.GetRandom(32),
  649. "operator_id": -1,
  650. })
  651. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  652. "clue_id": clueId,
  653. "position_id": positionId,
  654. "change_field": "trailstatus",
  655. "change_type": "基本信息变更",
  656. "old_value": "无意向客户",
  657. "new_value": "商机线索",
  658. "createtime": nowTime,
  659. "BCPCID": common.GetRandom(32),
  660. "operator_id": -1,
  661. })
  662. } else if old_position_id == 0 {
  663. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  664. "clue_id": clueId,
  665. "position_id": positionId,
  666. "change_field": "trailstatus",
  667. "change_type": "基本信息变更",
  668. "old_value": CodeTrail[trailstatus],
  669. "new_value": "新增",
  670. "createtime": nowTime,
  671. "BCPCID": common.GetRandom(32),
  672. "operator_id": -1,
  673. })
  674. } else {
  675. if common.Int64All(data["position_id"]) > 0 {
  676. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  677. "clue_id": clueId,
  678. "position_id": common.Int64All(data["position_id"]),
  679. "change_field": "trailstatus",
  680. "change_type": "基本信息变更",
  681. "old_value": "商机线索",
  682. "new_value": "流失",
  683. "createtime": nowTime,
  684. "BCPCID": common.GetRandom(32),
  685. "operator_id": -1,
  686. })
  687. }
  688. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  689. "clue_id": clueId,
  690. "position_id": positionId,
  691. "change_field": "trailstatus",
  692. "change_type": "基本信息变更",
  693. "old_value": "商机线索",
  694. "new_value": "新增",
  695. "createtime": nowTime,
  696. "BCPCID": common.GetRandom(32),
  697. "operator_id": -1,
  698. })
  699. }
  700. } else if trailstatus != "08" && is_assign == 0 {
  701. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  702. "clue_id": clueId,
  703. "position_id": positionId,
  704. "change_field": "position_id",
  705. "change_type": "所属人变更",
  706. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  707. "new_value": common.If(saleName != "", saleName, "/"),
  708. "createtime": nowTime,
  709. "BCPCID": common.GetRandom(32),
  710. "operator_id": -1,
  711. })
  712. if trailstatus != "01" {
  713. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  714. "clue_id": clueId,
  715. "position_id": positionId,
  716. "change_field": "trailstatus",
  717. "change_type": "基本信息变更",
  718. "old_value": "商机线索",
  719. "new_value": "新增",
  720. "createtime": nowTime,
  721. "BCPCID": common.GetRandom(32),
  722. "operator_id": -1,
  723. })
  724. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  725. "clue_id": clueId,
  726. "position_id": positionId,
  727. "change_field": "trailstatus",
  728. "change_type": "基本信息变更",
  729. "old_value": CodeTrail[trailstatus],
  730. "new_value": "商机线索",
  731. "createtime": nowTime,
  732. "BCPCID": common.GetRandom(32),
  733. "operator_id": -1,
  734. })
  735. }
  736. }
  737. if trailstatus != "08" && recordCount <= 0 {
  738. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  739. "clue_id": clueId,
  740. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  741. "change_type": "加入任务车",
  742. "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
  743. "createtime": nowTime,
  744. "BCPCID": common.GetRandom(32),
  745. "operator_id": -1,
  746. })
  747. }
  748. BCPCID := common.GetRandom(32)
  749. old_name := common.ObjToString(data["name"])
  750. old_position := common.ObjToString(data["position"])
  751. old_department := common.ObjToString(data["department"])
  752. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  753. old_role := common.ObjToString(data["role"])
  754. old_cluename := common.ObjToString(data["cluename"])
  755. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  756. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  757. old_topname, old_subname := "", ""
  758. if old_top_cluetype != "" {
  759. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  760. if pcodeData != nil && len(*pcodeData) > 0 {
  761. old_topname = common.ObjToString((*pcodeData)["name"])
  762. }
  763. }
  764. if old_sub_cluetype != "" {
  765. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  766. if pcodeData != nil && len(*pcodeData) > 0 {
  767. old_subname = common.ObjToString((*pcodeData)["name"])
  768. }
  769. }
  770. if item != "orders" {
  771. if old_name != name && name != "" {
  772. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  773. "clue_id": clueId,
  774. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  775. "change_field": "name",
  776. "change_type": "基本信息变更",
  777. "old_value": common.If(old_name != "", old_name, "/"),
  778. "new_value": common.If(name != "", name, "/"),
  779. "createtime": nowTime,
  780. "BCPCID": BCPCID,
  781. "operator_id": -1,
  782. })
  783. }
  784. if old_position != position && position != "" {
  785. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  786. "clue_id": clueId,
  787. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  788. "change_field": "position",
  789. "change_type": "基本信息变更",
  790. "old_value": common.If(old_position != "", old_position, "/"),
  791. "new_value": common.If(position != "", position, "/"),
  792. "createtime": nowTime,
  793. "BCPCID": BCPCID,
  794. "operator_id": -1,
  795. })
  796. }
  797. if old_department != department && department != "" {
  798. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  799. "clue_id": clueId,
  800. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  801. "change_field": "department",
  802. "change_type": "基本信息变更",
  803. "old_value": common.If(old_department != "", old_department, "/"),
  804. "new_value": common.If(department != "", department, "/"),
  805. "createtime": nowTime,
  806. "BCPCID": BCPCID,
  807. "operator_id": -1,
  808. })
  809. }
  810. if old_role != role && role != "" {
  811. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  812. "clue_id": clueId,
  813. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  814. "change_field": "role",
  815. "change_type": "基本信息变更",
  816. "old_value": common.If(old_role != "", old_role, "/"),
  817. "new_value": common.If(role != "", role, "/"),
  818. "createtime": nowTime,
  819. "BCPCID": BCPCID,
  820. "operator_id": -1,
  821. })
  822. }
  823. if old_follow_project_area != follow_project_area {
  824. old_area, old_area_arr := "", []string{}
  825. new_area, new_area_arr := "", []string{}
  826. for _, v := range strings.Split(old_follow_project_area, ",") {
  827. old_area_arr = append(old_area_arr, CodeArea[v])
  828. }
  829. for _, v := range strings.Split(follow_project_area, ",") {
  830. new_area_arr = append(new_area_arr, CodeArea[v])
  831. }
  832. old_area = strings.Join(old_area_arr, ",")
  833. new_area = strings.Join(new_area_arr, ",")
  834. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  835. "clue_id": clueId,
  836. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  837. "change_field": "follow_project_area",
  838. "change_type": "基本信息变更",
  839. "old_value": common.If(old_area != "", old_area, "/"),
  840. "new_value": common.If(new_area != "", new_area, "/"),
  841. "createtime": nowTime,
  842. "BCPCID": BCPCID,
  843. "operator_id": -1,
  844. })
  845. }
  846. if old_cluename != cluename && cluename != "" {
  847. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  848. "clue_id": clueId,
  849. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  850. "change_field": "cluename",
  851. "change_type": "基本信息变更",
  852. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  853. "new_value": common.If(cluename != "", cluename, "/"),
  854. "createtime": nowTime,
  855. "BCPCID": BCPCID,
  856. "operator_id": -1,
  857. })
  858. }
  859. }
  860. if old_top_cluetype != top_cluetype {
  861. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  862. "clue_id": clueId,
  863. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  864. "change_field": "top_cluetype",
  865. "change_type": "基本信息变更",
  866. "old_value": common.If(old_topname != "", old_topname, "/"),
  867. "new_value": common.If(topname != "", topname, "/"),
  868. "createtime": nowTime,
  869. "BCPCID": BCPCID,
  870. "operator_id": -1,
  871. })
  872. }
  873. if old_sub_cluetype != sub_cluetype {
  874. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  875. "clue_id": clueId,
  876. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  877. "change_field": "sub_cluetype",
  878. "change_type": "基本信息变更",
  879. "old_value": common.If(old_subname != "", old_subname, "/"),
  880. "new_value": common.If(subname != "", subname, "/"),
  881. "createtime": nowTime,
  882. "BCPCID": BCPCID,
  883. "operator_id": -1,
  884. })
  885. }
  886. return ok && ok1 && seaId > -1 && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1 && updateId7 > -1 && updateId8 > -1 && updateId9 > -1 && updateId10 > -1 && updateId11 > -1 && updateId12 > -1 && updateId13 > -1
  887. }) {
  888. log.Println("线索更新成功")
  889. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  890. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  891. "status": 1,
  892. "is_delete": 1,
  893. "createtime": nowTime,
  894. "updatetime": nowTime,
  895. "phone": phone,
  896. "baseinfo_id": uId,
  897. "SOURCE": source,
  898. })
  899. }
  900. return true
  901. } else {
  902. log.Println("线索更新失败!!!", ok, ok1, ok2, seaId, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  903. return false
  904. }
  905. }
  906. func getAreaCode(userId string) (code string) {
  907. followData := Base.Find("follow_project_monitor", map[string]interface{}{"s_userid": userId}, "", "", -1, -1)
  908. sidArr := []string{}
  909. if followData != nil && len(*followData) > 0 {
  910. for _, v := range *followData {
  911. infoId := common.ObjToString(v["s_id"])
  912. sidArr = append(sidArr, infoId)
  913. }
  914. }
  915. if len(sidArr) > 0 {
  916. query := `{"query": {"bool": {"must": [{"terms": {"_id": ["%s"]}}],"must_not": [],"should": []}}}`
  917. query = fmt.Sprintf(query, strings.Join(sidArr, `","`))
  918. biddingData := Es.Get("bidding", "bidding", query)
  919. if biddingData != nil && len(*biddingData) > 0 {
  920. codeMap := map[string]string{}
  921. codeArr := []string{}
  922. for _, v := range *biddingData {
  923. area := common.ObjToString(v["area"])
  924. address := common.ObjToString(v["city"])
  925. if address == "" {
  926. address = area
  927. }
  928. codeMap[address] = AreaCode[address]
  929. }
  930. if len(codeMap) > 0 {
  931. for _, v := range codeMap {
  932. codeArr = append(codeArr, v)
  933. }
  934. }
  935. if len(codeArr) > 0 {
  936. code = strings.Join(codeArr, ",")
  937. }
  938. }
  939. }
  940. log.Println("code ", code)
  941. return
  942. }
  943. func getClueType(item string, data map[string]interface{}) (pcode, code, level, topname, subname string) {
  944. if item == "orders" {
  945. productType := common.ObjToString(data["product_type"])
  946. pcode = "1"
  947. level = "A"
  948. topname = "提交订单未支付"
  949. if productType == "VIP订阅" {
  950. code = "6"
  951. subname = "超级订阅"
  952. } else if productType == "大会员" {
  953. code = "7"
  954. subname = "大会员"
  955. } else if productType == "数据流量包" {
  956. code = "8"
  957. subname = "数据流量包"
  958. } else if productType == "历史数据" {
  959. code = "9"
  960. subname = "数据自助导出"
  961. }
  962. } else if item == "users" {
  963. pcode = "4"
  964. code = "154"
  965. level = "C"
  966. topname = "新增注册"
  967. subname = "新增注册用户"
  968. } else if item == "saleLeads" {
  969. sourceCode := common.ObjToString(data["source"])
  970. if sourceCode != "" {
  971. codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"source": sourceCode}, "", "")
  972. if codeData != nil && len(*codeData) > 0 {
  973. pcode = common.ObjToString((*codeData)["pcode"])
  974. code = common.ObjToString((*codeData)["code"])
  975. level = common.ObjToString((*codeData)["clue_level"])
  976. subname = common.ObjToString((*codeData)["name"])
  977. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  978. if pcodeData != nil && len(*pcodeData) > 0 {
  979. topname = common.ObjToString((*pcodeData)["name"])
  980. }
  981. }
  982. }
  983. }
  984. return
  985. }
  986. // 获取自动分配的人
  987. func autoDraw(mode string) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}) {
  988. query := `select * from jy_salesperson_info where`
  989. if mode == "A" || mode == "B" {
  990. query += ` is_complete = 1`
  991. } else {
  992. query += ` is_complete = 1 or is_complete = 0`
  993. }
  994. data := TiDb.SelectBySql(query)
  995. if data != nil && len(*data) > 0 {
  996. saleData = *data
  997. sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?"
  998. countData := TiDb.SelectBySql(sql, mode)
  999. if countData != nil && len(*countData) > 0 {
  1000. for _, v := range *data {
  1001. //判断是否有新员工
  1002. isOk := false
  1003. for _, vv := range *countData {
  1004. if common.ObjToString(v["seatNumber"]) == common.ObjToString(vv["seatNumber"]) {
  1005. if common.IntAll(v["status"]) == 0 {
  1006. vv["status"] = 1
  1007. } else {
  1008. vv["status"] = 2
  1009. }
  1010. isOk = true
  1011. }
  1012. }
  1013. //有新员工直接分给新员工
  1014. if !isOk {
  1015. seatNumber = common.ObjToString(v["seatNumber"])
  1016. saleName = common.ObjToString(v["name"])
  1017. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc")
  1018. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1019. "seatNumber": seatNumber,
  1020. "clue_level": mode,
  1021. "count": common.Int64All((*rData)["count"]),
  1022. })
  1023. break
  1024. }
  1025. }
  1026. if seatNumber == "" {
  1027. res := int64(0)
  1028. countres := 0
  1029. for _, v := range *countData {
  1030. if common.IntAll(v["status"]) == 1 {
  1031. if countres == 0 {
  1032. res = common.Int64All(v["count"])
  1033. seatNumber = common.ObjToString(v["seatNumber"])
  1034. } else {
  1035. if common.Int64All(v["count"]) <= res {
  1036. res = common.Int64All(v["count"])
  1037. seatNumber = common.ObjToString(v["seatNumber"])
  1038. }
  1039. }
  1040. countres++
  1041. }
  1042. // if k == 0 {
  1043. // res = common.Int64All(v["count"])
  1044. // seatNumber = common.ObjToString(v["seatNumber"])
  1045. // } else {
  1046. // if common.Int64All(v["count"]) <= res {
  1047. // res = common.Int64All(v["count"])
  1048. // seatNumber = common.ObjToString(v["seatNumber"])
  1049. // }
  1050. // }
  1051. }
  1052. }
  1053. } else {
  1054. seatNumber = common.ObjToString((*data)[0]["seatNumber"])
  1055. saleName = common.ObjToString((*data)[0]["name"])
  1056. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc")
  1057. if rData != nil && len(*rData) > 0 {
  1058. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1059. "seatNumber": seatNumber,
  1060. "clue_level": mode,
  1061. "count": common.Int64All((*rData)["count"]),
  1062. })
  1063. } else {
  1064. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1065. "seatNumber": seatNumber,
  1066. "clue_level": mode,
  1067. "count": 0,
  1068. })
  1069. }
  1070. }
  1071. for _, v := range *data {
  1072. if seatNumber == common.ObjToString(v["seatNumber"]) {
  1073. positionId = getPositionId(common.ObjToString(v["phone"]))
  1074. saleName = common.ObjToString(v["name"])
  1075. }
  1076. }
  1077. // if positionId > 0 {
  1078. // TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, mode)
  1079. // }
  1080. }
  1081. return
  1082. }
  1083. func getPositionId(phone string) (positionId int64) {
  1084. userData, ok := Mgo.FindOne("user", map[string]interface{}{"s_phone": phone})
  1085. if ok && userData != nil && len(*userData) > 0 {
  1086. userId := common.Int64All((*userData)["base_user_id"])
  1087. positionData := Base.FindOne("base_position", map[string]interface{}{"type": 1, "ent_id": 25917, "user_id": userId}, "", "") //TODO ent_id
  1088. if positionData != nil && len(*positionData) > 0 {
  1089. positionId = common.Int64All((*positionData)["id"])
  1090. }
  1091. }
  1092. return
  1093. }