job.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "app.yhyue.com/moapp/jybase/date"
  11. "app.yhyue.com/moapp/jybase/redis"
  12. "app.yhyue.com/moapp/jybase/common"
  13. "app.yhyue.com/moapp/jybase/mongodb"
  14. )
  15. // 未支付订单 30分钟一次
  16. func orders() {
  17. //一个小时未支付进入线索 A
  18. log.Println("未支付订单定时任务开始")
  19. lastOrderId := cfg.LastOrderId
  20. selectTimeStart := time.Unix(time.Now().Unix()-7200, 0).Format(date.Date_Full_Layout)
  21. selectTimeEnd := time.Unix(time.Now().Unix()-3600, 0).Format(date.Date_Full_Layout)
  22. sql := fmt.Sprintf(`select * from dataexport_order where create_time <= "%s" and create_time >= "%s" and id > %s`, selectTimeEnd, selectTimeStart, fmt.Sprint(lastOrderId))
  23. data := Mysql.SelectBySql(sql)
  24. if data != nil && *data != nil && len(*data) > 0 {
  25. for _, v := range *data {
  26. order_status := common.IntAll(v["order_status"])
  27. is_backstage_order := common.IntAll(v["is_backstage_order"])
  28. product_type_str1 := `"大会员","VIP订阅","数据流量包","历史数据"`
  29. product_type := common.ObjToString(v["product_type"])
  30. if order_status == 0 && is_backstage_order == 0 && strings.Contains(product_type_str1, product_type) {
  31. ok1, _ := FormatData(v, "orders")
  32. if !ok1 {
  33. common.WriteSysConfig(&cfg)
  34. break
  35. }
  36. }
  37. cfg.LastOrderId = common.IntAll(v["id"])
  38. }
  39. }
  40. common.WriteSysConfig(&cfg)
  41. log.Println("未支付订单定时任务结束")
  42. }
  43. // 新注册用户 5分钟一次
  44. func users() {
  45. //判断节假日
  46. currentTime, runOk := time.Now(), false
  47. // if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  48. if currentTime.Weekday() == time.Sunday {
  49. isok := false
  50. for k, v := range DateMap {
  51. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  52. isok = true
  53. }
  54. }
  55. if isok {
  56. runOk = true
  57. }
  58. } else {
  59. isok := true
  60. for k, v := range DateMap {
  61. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  62. isok = false
  63. }
  64. }
  65. if isok {
  66. runOk = true
  67. }
  68. }
  69. if !runOk {
  70. log.Println("不是工作日,任务暂停")
  71. return
  72. } /*else {
  73. if currentTime.Hour() == 17 && currentTime.Minute() > 30 {
  74. log.Println("不是工作日,任务暂停")
  75. return
  76. }
  77. }*/
  78. //新用户注册后5分钟内进入线索 C
  79. log.Println("新注册用户定时任务开始")
  80. selectTimeEnd := cfg.LastUserId
  81. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where createtime > "%s" and source = 1`, selectTimeEnd)
  82. data := TiDb.SelectBySql(sql)
  83. if data != nil && *data != nil && len(*data) > 0 {
  84. for k, v := range *data {
  85. createtime := common.ObjToString(v["createtime"])
  86. FormatData(v, "users")
  87. if k == len(*data)-1 {
  88. cfg.LastUserId = createtime
  89. }
  90. }
  91. }
  92. common.WriteSysConfig(&cfg)
  93. log.Println("新注册用户定时任务结束")
  94. }
  95. func userbase() {
  96. log.Println("userbase定时任务开始")
  97. selectTimeEnd := time.Unix(time.Now().Unix()-1800, 0).Format("2006-01-02 15:04:05")
  98. sql := fmt.Sprintf(`select * from dwd_f_userbase_baseinfo where updatetime > "%s" and source > 0`, selectTimeEnd)
  99. data := TiDb.SelectBySql(sql)
  100. if data != nil && *data != nil && len(*data) > 0 {
  101. for _, v := range *data {
  102. phone := common.ObjToString(v["phone"])
  103. uId := common.ObjToString(v["uid"])
  104. userId := common.ObjToString(v["userid"])
  105. registedate := common.ObjToString(v["l_registedate"])
  106. name := common.ObjToString(v["name"])
  107. nowTime := time.Now().Format(date.Date_Full_Layout)
  108. source := common.IntAll(v["source"])
  109. if phone != "" {
  110. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  111. if contactsData == nil || len(*contactsData) == 0 {
  112. contactsData2 := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where baseinfo_id = ? and is_delete = 1", uId)
  113. if contactsData2 != nil && len(*contactsData2) > 0 {
  114. log.Println("userbase uid不为空 新增通讯录", uId)
  115. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  116. "status": 1,
  117. "is_delete": 1,
  118. "createtime": nowTime,
  119. "updatetime": nowTime,
  120. "phone": phone,
  121. "baseinfo_id": uId,
  122. "SOURCE": source,
  123. })
  124. } else {
  125. registedates, _ := time.Parse(date.Date_Full_Layout, registedate)
  126. count := TiDb.CountBySql("select count(1) as count from dwd_f_crm_clue_info where uid = ?", uId)
  127. log.Println("userbase uid 线索数量 ", count)
  128. log.Println("userbase uid 注册时间 ", registedates)
  129. if time.Now().Unix()-registedates.Unix() > int64(cfg.RegTimes)*86400 {
  130. if count == 0 {
  131. // TiDb.Insert("dwd_f_crm_open_sea", map[string]interface{}{
  132. // "clue_id": clueId,
  133. // "comeintime": nowTime,
  134. // "comeinsource": 2,
  135. // })
  136. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  137. "userid": userId,
  138. "uid": uId,
  139. "is_assign": 0,
  140. "comeintime": nowTime,
  141. "createtime": nowTime,
  142. "updatetime": nowTime,
  143. "cluename": phone,
  144. "top_cluetype": "474",
  145. "sub_cluetype": "475",
  146. "trailstatus": "01",
  147. "name": name,
  148. "phone": phone,
  149. "comeintime_open": nowTime,
  150. "comeinsource_open": 1,
  151. })
  152. if clueId > 0 {
  153. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  154. "status": 1,
  155. "is_delete": 1,
  156. "createtime": nowTime,
  157. "updatetime": nowTime,
  158. "phone": phone,
  159. "baseinfo_id": uId,
  160. "SOURCE": source,
  161. })
  162. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  163. "clue_id": clueId,
  164. "position_id": -1,
  165. "change_type": "创建线索",
  166. "new_value": "系统自动创建",
  167. "createtime": nowTime,
  168. "BCPCID": common.GetRandom(32),
  169. "operator_id": -1,
  170. })
  171. }
  172. } else {
  173. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  174. "status": 1,
  175. "is_delete": 1,
  176. "createtime": nowTime,
  177. "updatetime": nowTime,
  178. "phone": phone,
  179. "baseinfo_id": uId,
  180. "SOURCE": source,
  181. })
  182. }
  183. } else {
  184. if count == 0 {
  185. FormatData(v, "users")
  186. } else {
  187. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  188. "status": 1,
  189. "is_delete": 1,
  190. "createtime": nowTime,
  191. "updatetime": nowTime,
  192. "phone": phone,
  193. "baseinfo_id": uId,
  194. "SOURCE": source,
  195. })
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. log.Println("userbase定时任务结束")
  204. }
  205. // 留资 5分钟一次
  206. func saleLeads() {
  207. //判断节假日
  208. currentTime, runOk := time.Now(), false
  209. // if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  210. if currentTime.Weekday() == time.Sunday {
  211. isok := false
  212. for k, v := range DateMap {
  213. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  214. isok = true
  215. }
  216. }
  217. if isok {
  218. runOk = true
  219. }
  220. } else {
  221. isok := true
  222. for k, v := range DateMap {
  223. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  224. isok = false
  225. }
  226. }
  227. if isok {
  228. runOk = true
  229. }
  230. }
  231. if !runOk {
  232. log.Println("不是工作日,任务暂停")
  233. return
  234. } /*else {
  235. if currentTime.Hour() == 17 && currentTime.Minute() > 30 {
  236. log.Println("不是工作日,任务暂停")
  237. return
  238. }
  239. }*/
  240. //留资后5分钟内进入线索
  241. //分为免费留资和付费留资 付费B 免费C
  242. log.Println("用户留资定时任务开始")
  243. session := Mgo.GetMgoConn()
  244. lastId := cfg.LastId
  245. defer func() {
  246. Mgo.DestoryMongoConn(session)
  247. }()
  248. query := map[string]interface{}{}
  249. if lastId != "" {
  250. query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastId)}
  251. }
  252. // query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63f6049a4c172a1fd44ed3c8")}
  253. log.Println("query :", query)
  254. iter := session.DB(cfg.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
  255. thisData := map[string]interface{}{}
  256. for {
  257. if !iter.Next(&thisData) {
  258. break
  259. }
  260. ok1, _ := FormatData(thisData, "saleLeads")
  261. if !ok1 {
  262. common.WriteSysConfig(&cfg)
  263. break
  264. }
  265. cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
  266. // FormatData(thisData, "saleLeads")
  267. }
  268. common.WriteSysConfig(&cfg)
  269. log.Println("用户留资定时任务结束")
  270. }
  271. func FormatData(data map[string]interface{}, item string) (bool, bool) {
  272. userId, uId, positionId, source, cluename, phone, sourceCode, keyword := common.ObjToString(data["user_id"]), "", "", 0, "", "", "", []string{}
  273. role, industry, department, departments, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", "", ""
  274. query, topname, subname, belong_to, sourceName := map[string]interface{}{}, "", "", "01", ""
  275. nowTime := time.Now().Format("2006-01-02 15:04:05")
  276. if item == "orders" {
  277. if !mongodb.IsObjectIdHex(userId) {
  278. positionId = userId
  279. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  280. if userMapping != nil && len(*userMapping) > 0 {
  281. userId = common.ObjToString((*userMapping)["userid"])
  282. }
  283. }
  284. query["userid"] = userId
  285. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  286. if userInfo != nil && len(*userInfo) > 0 {
  287. uId = common.ObjToString((*userInfo)["uid"])
  288. source = common.IntAll((*userInfo)["source"])
  289. belong_to = common.ObjToString((*userInfo)["belong_to"])
  290. }
  291. //cluename --> company_name
  292. cluename = common.ObjToString(data["company_name"])
  293. phone = common.ObjToString(data["user_phone"])
  294. } else if item == "message" {
  295. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"base_user_id": common.Int64All(data["own_id"])}, "", "")
  296. if userMapping != nil && len(*userMapping) > 0 {
  297. userId = common.ObjToString((*userMapping)["userid"])
  298. positionId = fmt.Sprint((*userMapping)["position_id"])
  299. }
  300. query["userid"] = userId
  301. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  302. if userInfo != nil && len(*userInfo) > 0 {
  303. uId = common.ObjToString((*userInfo)["uid"])
  304. source = common.IntAll((*userInfo)["source"])
  305. belong_to = common.ObjToString((*userInfo)["belong_to"])
  306. // cluename = common.ObjToString((*userInfo)["company_name"])
  307. phone = common.ObjToString((*userInfo)["phone"])
  308. }
  309. } else if item == "users" {
  310. userId = mongodb.BsonIdToSId(data["userid"])
  311. //新用户没有uid、source要等5分钟
  312. cluename = common.ObjToString(data["company_name"])
  313. phone = common.ObjToString(data["phone"])
  314. if phone != "" {
  315. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  316. if contactsData != nil && len(*contactsData) > 0 {
  317. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  318. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  319. query["uid"] = uId
  320. } else {
  321. query["userid"] = userId
  322. }
  323. } else {
  324. query["userid"] = userId
  325. }
  326. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  327. if userInfo != nil && len(*userInfo) > 0 {
  328. uId = common.ObjToString((*userInfo)["uid"])
  329. source = common.IntAll((*userInfo)["source"])
  330. belong_to = common.ObjToString((*userInfo)["belong_to"])
  331. // userId = common.ObjToString((*userInfo)["userid"])
  332. }
  333. }
  334. } else if item == "saleLeads" {
  335. userId = common.ObjToString(data["userid"])
  336. if !mongodb.IsObjectIdHex(userId) {
  337. positionId = userId
  338. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  339. if userMapping != nil && len(*userMapping) > 0 {
  340. userId = common.ObjToString((*userMapping)["userid"])
  341. }
  342. }
  343. cluename = common.ObjToString(data["company"])
  344. phone = common.ObjToString(data["phone"])
  345. role = common.ObjToString(data["companyType"])
  346. industry = common.ObjToString(data["industry"])
  347. department = common.ObjToString(data["branch"])
  348. departments = common.ObjToString(data["department"])
  349. position = common.ObjToString(data["position"])
  350. name = common.ObjToString(data["name"])
  351. sourceCode = common.ObjToString(data["source"])
  352. keywordArr := data["keyword"]
  353. if keywordArr != nil {
  354. keyword = common.ObjArrToStringArr(data["keyword"].([]interface{}))
  355. }
  356. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  357. if contactsData != nil && len(*contactsData) > 0 {
  358. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  359. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  360. query["uid"] = uId
  361. } else {
  362. query["userid"] = userId
  363. }
  364. } else {
  365. query["userid"] = userId
  366. }
  367. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  368. if userInfo != nil && len(*userInfo) > 0 {
  369. uId = common.ObjToString((*userInfo)["uid"])
  370. source = common.IntAll((*userInfo)["source"])
  371. belong_to = common.ObjToString((*userInfo)["belong_to"])
  372. userId = common.ObjToString((*userInfo)["userid"])
  373. }
  374. } else if item == "eventReg" {
  375. userId = common.ObjToString(data["userid"])
  376. cluename = common.ObjToString(data["company"])
  377. phone = common.ObjToString(data["sign_phone"])
  378. role = common.ObjToString(data["company_type"])
  379. department = common.ObjToString(data["branch"])
  380. position = common.ObjToString(data["position"])
  381. name = common.ObjToString(data["sign_name"])
  382. sourceCode = common.ObjToString(data["source_code"])
  383. sourceName = common.ObjToString(data["source_name"])
  384. contactsData := TiDb.SelectBySql("select * from dwd_f_userbase_contacts where phone = ? and is_delete = 1", phone)
  385. if contactsData != nil && len(*contactsData) > 0 {
  386. if common.ObjToString((*contactsData)[0]["baseinfo_id"]) != "" {
  387. uId = common.ObjToString((*contactsData)[0]["baseinfo_id"])
  388. query["uid"] = uId
  389. } else {
  390. query["userid"] = userId
  391. }
  392. } else {
  393. query["userid"] = userId
  394. }
  395. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  396. if userInfo != nil && len(*userInfo) > 0 {
  397. uId = common.ObjToString((*userInfo)["uid"])
  398. source = common.IntAll((*userInfo)["source"])
  399. belong_to = common.ObjToString((*userInfo)["belong_to"])
  400. userId = common.ObjToString((*userInfo)["userid"])
  401. }
  402. if role == "集成商" || role == "设计院" {
  403. role = "其他-" + role
  404. }
  405. }
  406. //线索名称为空用手机号代替
  407. if cluename == "" && item != "message" {
  408. cluename = phone
  409. }
  410. //域外用户和内部用户和没有手机号,不存线索
  411. if source == 2 || strings.HasPrefix(belong_to, "02") || source == 6 || source == 11 || phone == "" {
  412. log.Println("线索分配失败,线索过滤!!", item, source, phone, userId)
  413. if strings.HasPrefix(belong_to, "02") && item == "eventReg" {
  414. saveMap := map[string]interface{}{
  415. "unique_id": phone,
  416. "phone": phone,
  417. "username": name,
  418. "source": sourceName,
  419. "status999": "status5",
  420. "company": cluename,
  421. "job": position,
  422. "belongTo": "市场部",
  423. "createTime": nowTime,
  424. "lastUpdateTime": nowTime,
  425. }
  426. token := getToken()
  427. updateData := map[string]interface{}{
  428. "dbType": "0001",
  429. "customerList": []map[string]interface{}{saveMap},
  430. }
  431. dataByte, _ := json.Marshal(&updateData)
  432. url := `https://a1.7x24cc.com/commonInte?flag=1007&account=N000000029739&accessToken=` + token + `&json=` + url.QueryEscape(string(dataByte))
  433. bs, err := doGet(url)
  434. if err != nil {
  435. log.Println("调用接口失败")
  436. } else {
  437. resMap := common.StringToMap(string(bs))
  438. if resMap["success"] != nil && resMap["success"].(bool) {
  439. TiDbData.Insert("customer", saveMap)
  440. } else {
  441. log.Println("新增线索失败")
  442. }
  443. }
  444. }
  445. return true, false
  446. }
  447. if uId == "" {
  448. if isExists, _ := redis.Exists("bidx", "bidx_userId_"+userId); isExists {
  449. redisInt := redis.GetInt("bidx", "bidx_userId_"+userId)
  450. if redisInt > 4 {
  451. log.Println("线索分配失败,线索缺少信息,任务已执行超过1次", item, userId, phone)
  452. return true, true
  453. } else {
  454. redis.Incr("bidx", "bidx_userId_"+userId)
  455. }
  456. } else {
  457. redis.Put("bidx", "bidx_userId_"+userId, 1, 3600)
  458. }
  459. log.Println("线索分配失败,线索缺少信息", item, phone, userId)
  460. return false, false
  461. }
  462. //不是留资的要查一遍留资
  463. if item == "orders" {
  464. qid := positionId
  465. if qid == "" {
  466. qid = userId
  467. }
  468. saleLeadsData, ok := Mgo.Find("saleLeads", map[string]interface{}{"userid": qid}, map[string]interface{}{"_id": -1}, nil, false, 0, 1)
  469. if ok && saleLeadsData != nil && len(*saleLeadsData) > 0 {
  470. sdata := (*saleLeadsData)[0]
  471. role = common.ObjToString(sdata["companyType"])
  472. industry = common.ObjToString(sdata["industry"])
  473. department = common.ObjToString(sdata["branch"])
  474. departments = common.ObjToString(sdata["department"])
  475. position = common.ObjToString(sdata["position"])
  476. name = common.ObjToString(sdata["name"])
  477. }
  478. }
  479. //top_cluetype
  480. top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data, sourceCode)
  481. //
  482. if topname == "市场活动" && item == "saleLeads" {
  483. log.Println("市场活动留资过滤 ", userId, phone)
  484. return true, false
  485. }
  486. if strings.HasPrefix(belong_to, "03") {
  487. isOk := saveEverything(userId, phone, item, subname, sourceCode)
  488. log.Println("渠道线索电销", userId, phone, item, subname, sourceCode)
  489. if !isOk {
  490. return true, false
  491. }
  492. }
  493. //
  494. //follow_project_area --> follow_project_monitor
  495. follow_project_area = getAreaCode(userId)
  496. //seatNumber position_id
  497. log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area)
  498. if top_cluetype == "" || sub_cluetype == "" || level == "" {
  499. log.Println("线索分配失败,线索过滤top_cluetype!!", item, uId, phone, userId)
  500. return true, true
  501. }
  502. position_id, seatNumber, saleName, saleData := autoDraw(level)
  503. log.Println("data -------", position_id, seatNumber, saleName)
  504. if position_id > 0 && seatNumber != "" {
  505. uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true
  506. if uCount != nil && len(*uCount) > 0 {
  507. batch_import := common.ObjToString((*uCount)["batch_import"])
  508. if batch_import != "" && item == "users" {
  509. return true, true
  510. }
  511. oks = UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, keyword, belong_to)
  512. } else {
  513. oks = SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber, position_id, source, sourceCode, keyword, belong_to)
  514. }
  515. if oks {
  516. TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, level)
  517. } else {
  518. log.Println("线索分配失败!!", item, position_id, seatNumber, uId, userId, phone)
  519. return false, false
  520. }
  521. }
  522. return true, true
  523. }
  524. func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source int, sourceCode string, keyword []string, belong_to string) bool {
  525. nowTime := time.Now().Format("2006-01-02 15:04:05")
  526. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  527. //
  528. if strings.HasPrefix(belong_to, "03") {
  529. return false
  530. }
  531. //
  532. clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6 := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  533. // BCPCID := common.GetRandom(32)
  534. if TiDb.ExecTx("保存线索", func(tx *sql.Tx) bool {
  535. keywords := ""
  536. if sourceCode == "app_xzcyh" {
  537. if len(keyword) > 0 && keyword[0] != "" {
  538. keywords = strings.Join(keyword, ",")
  539. }
  540. }
  541. if cluename == "" {
  542. cluename = phone
  543. }
  544. //线索
  545. clueId = TiDb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{
  546. "userid": userId,
  547. "uid": uId,
  548. "seatNumber": seatNumber,
  549. "position_id": positionId,
  550. "is_assign": 1,
  551. "comeintime": nowTime,
  552. "createtime": nowTime,
  553. "updatetime": nowTime,
  554. "cluename": cluename,
  555. "top_cluetype": top_cluetype,
  556. "sub_cluetype": sub_cluetype,
  557. "trailstatus": "01",
  558. "name": name,
  559. "phone": phone,
  560. "position": position,
  561. "department": common.If(sourceCode == "app_xzcyh", departments, department),
  562. "industry": industry,
  563. "follow_project_area": follow_project_area,
  564. "role": role,
  565. "comeinsource_private": 2,
  566. "is_task": 1,
  567. "task_time": nowTime,
  568. "tasktime": common.If(item == "users", nowTimes, nowTime),
  569. "taskstatus": 0,
  570. "tasksource": "线索自动分配" + "-" + topname + "-" + subname,
  571. "business_scope": common.If(sourceCode == "app_xzcyh", keywords, nil),
  572. })
  573. //变更记录
  574. uodateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  575. "clue_id": clueId,
  576. "position_id": positionId,
  577. "change_type": "创建线索",
  578. "new_value": "系统自动创建",
  579. "createtime": nowTime,
  580. "BCPCID": common.GetRandom(32),
  581. "operator_id": -1,
  582. })
  583. uodateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  584. "clue_id": clueId,
  585. "position_id": positionId,
  586. "change_field": "position_id",
  587. "change_type": "所属人变更",
  588. "old_value": "/",
  589. "new_value": saleName,
  590. "createtime": nowTime,
  591. "BCPCID": common.GetRandom(32),
  592. "operator_id": -1,
  593. })
  594. uodateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  595. "clue_id": clueId,
  596. "position_id": positionId,
  597. "change_field": "trailstatus",
  598. "change_type": "基本信息变更",
  599. "old_value": "商机线索",
  600. "new_value": "新增",
  601. "createtime": nowTime,
  602. "BCPCID": common.GetRandom(32),
  603. "operator_id": -1,
  604. })
  605. uodateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  606. "clue_id": clueId,
  607. "position_id": positionId,
  608. "change_type": "加入任务车",
  609. "new_value": "线索自动分配" + "-" + topname + "-" + subname,
  610. "createtime": nowTime,
  611. "BCPCID": common.GetRandom(32),
  612. "operator_id": -1,
  613. })
  614. uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  615. "clue_id": clueId,
  616. "position_id": positionId,
  617. "change_field": "top_cluetype",
  618. "change_type": "基本信息变更",
  619. "old_value": "/",
  620. "new_value": topname,
  621. "createtime": nowTime,
  622. "BCPCID": common.GetRandom(32),
  623. "operator_id": -1,
  624. })
  625. uodateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  626. "clue_id": clueId,
  627. "position_id": positionId,
  628. "change_field": "sub_cluetype",
  629. "change_type": "基本信息变更",
  630. "old_value": "/",
  631. "new_value": subname,
  632. "createtime": nowTime,
  633. "BCPCID": common.GetRandom(32),
  634. "operator_id": -1,
  635. })
  636. return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1 && uodateId6 > -1
  637. }) {
  638. log.Println("线索分配成功")
  639. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  640. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  641. "status": 1,
  642. "is_delete": 1,
  643. "createtime": nowTime,
  644. "updatetime": nowTime,
  645. "phone": phone,
  646. "baseinfo_id": uId,
  647. "SOURCE": source,
  648. })
  649. }
  650. return true
  651. } else {
  652. log.Println("线索分配失败!!!", clueId, uodateId1, uodateId2, uodateId3, uodateId4, uodateId5, uodateId6, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  653. return false
  654. }
  655. }
  656. 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, departments, industry, follow_project_area, role, seatNumber string, positionId int64, source int, sourceCode string, keyword []string, belong_to string) bool {
  657. nowTime := time.Now().Format("2006-01-02 15:04:05")
  658. nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
  659. //trailstatus = 无意向 为变更线索状态
  660. //没有变更所属人查任务车,有车不变
  661. trailstatus := common.ObjToString(data["trailstatus"])
  662. trailstatusTime := common.ObjToString(data["trailstatus_time"])
  663. var trailstatusTimes time.Time
  664. if trailstatusTime != "" {
  665. trailstatusTimes, _ = time.ParseInLocation(date.Date_Full_Layout, trailstatusTime, time.Local)
  666. }
  667. clueId := common.Int64All(data["id"])
  668. is_assign := common.IntAll(data["is_assign"])
  669. oldsaleName, oldTaskTime, taskTime, is_task, taskstatus := "", "", "", 0, 0
  670. old_position_id, old_seatNumber := common.Int64All(data["position_id"]), common.ObjToString(data["seatNumber"])
  671. oldTaskTime = common.ObjToString(data["tasktime"])
  672. is_task = common.IntAll(data["is_task"])
  673. taskstatus = common.IntAll(data["taskstatus"])
  674. BCPCID := common.GetRandom(32)
  675. old_name := common.ObjToString(data["name"])
  676. old_position := common.ObjToString(data["position"])
  677. old_department := common.ObjToString(data["department"])
  678. old_follow_project_area := common.ObjToString(data["follow_project_area"])
  679. old_role := common.ObjToString(data["role"])
  680. old_cluename := common.ObjToString(data["cluename"])
  681. old_top_cluetype := common.ObjToString(data["top_cluetype"])
  682. old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
  683. old_topname, old_subname := "", ""
  684. is_transfer := common.IntAll(data["is_transfer"])
  685. if taskstatus == 1 || is_task == 0 {
  686. taskTime = common.ObjToString(common.If(item != "users", nowTime, nowTimes))
  687. } else {
  688. if is_task == 1 {
  689. if oldTaskTime != "" {
  690. t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
  691. if err == nil && time.Now().Before(t1) {
  692. taskTime = nowTime
  693. } else {
  694. taskTime = oldTaskTime
  695. }
  696. }
  697. }
  698. }
  699. //加入任务车判断节假日
  700. count, counts, t := 0, 0, time.Now()
  701. for {
  702. count++
  703. currentTime := t.AddDate(0, 0, -count)
  704. if currentTime.Weekday() == time.Sunday || currentTime.Weekday() == time.Saturday {
  705. isok := false
  706. for k, v := range DateMap {
  707. if currentTime.Format(date.Date_Short_Layout) == k && v == 2 {
  708. isok = true
  709. }
  710. }
  711. if isok {
  712. counts++
  713. }
  714. } else {
  715. isok := true
  716. for k, v := range DateMap {
  717. if currentTime.Format(date.Date_Short_Layout) == k && v == 1 {
  718. isok = false
  719. }
  720. }
  721. if isok {
  722. counts++
  723. }
  724. }
  725. if counts >= 2 {
  726. break
  727. }
  728. }
  729. 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))
  730. //
  731. clueUpdateData := map[string]interface{}{
  732. "updatetime": nowTime,
  733. "top_cluetype": top_cluetype,
  734. "sub_cluetype": sub_cluetype,
  735. "userid": userId,
  736. "comeinsource_private": 2,
  737. "tasksource": "线索来源自动更新" + "-" + topname + "-" + subname,
  738. }
  739. if cluename != "" {
  740. clueUpdateData["cluename"] = cluename
  741. }
  742. if item != "orders" && item != "users" {
  743. if name != "" {
  744. clueUpdateData["name"] = name
  745. }
  746. if position != "" {
  747. clueUpdateData["position"] = position
  748. }
  749. if department != "" {
  750. clueUpdateData["department"] = department
  751. }
  752. if follow_project_area != "" {
  753. clueUpdateData["follow_project_area"] = follow_project_area
  754. }
  755. if role != "" {
  756. clueUpdateData["role"] = role
  757. }
  758. // clueUpdateData["industry"] = industry
  759. }
  760. if sourceCode == "app_xzcyh" {
  761. if departments != "" {
  762. clueUpdateData["department"] = departments
  763. department = departments
  764. }
  765. business_scope := common.ObjToString(data["business_scope"])
  766. keywords := ""
  767. if len(keyword) > 0 && keyword[0] != "" {
  768. keywords = strings.Join(keyword, ",")
  769. if business_scope != "" {
  770. clueUpdateData["business_scope"] = business_scope + "," + keywords
  771. } else {
  772. clueUpdateData["business_scope"] = keywords
  773. }
  774. }
  775. }
  776. if trailstatus == "00" || old_position_id == 0 || is_assign != 1 {
  777. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  778. } else {
  779. clueUpdateData["seatNumber"] = seatNumber
  780. clueUpdateData["position_id"] = positionId
  781. clueUpdateData["comeintime"] = nowTime
  782. clueUpdateData["is_assign"] = 1
  783. clueUpdateData["comeinsource_open"] = nil
  784. clueUpdateData["level_open"] = nil
  785. clueUpdateData["clue_level"] = nil
  786. for _, v := range saleData {
  787. if common.ObjToString(data["seatNumber"]) == common.ObjToString(v["seatNumber"]) {
  788. oldsaleName = common.ObjToString(v["name"])
  789. }
  790. }
  791. }
  792. } else {
  793. clueUpdateData["seatNumber"] = old_seatNumber
  794. clueUpdateData["position_id"] = old_position_id
  795. clueUpdateData["is_assign"] = 1
  796. clueUpdateData["comeinsource_open"] = nil
  797. clueUpdateData["level_open"] = nil
  798. clueUpdateData["clue_level"] = nil
  799. }
  800. if trailstatus != "08" && is_assign == 0 {
  801. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  802. } else {
  803. clueUpdateData["trailstatus"] = "01"
  804. clueUpdateData["trailstatus_time"] = nowTime
  805. }
  806. }
  807. is_ok := false
  808. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  809. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  810. } else {
  811. if item == "eventReg" {
  812. eData := TiDb.Find("dwd_f_crm_clue_change_record", map[string]interface{}{"clue_id": clueId, "change_type": "加入任务车"}, "", "", -1, -1)
  813. if eData != nil && len(*eData) > 0 {
  814. for _, e := range *eData {
  815. new_value := common.ObjToString(e["new_value"])
  816. if strings.Contains(new_value, subname) {
  817. is_ok = true
  818. }
  819. }
  820. }
  821. if !is_ok {
  822. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  823. if taskTime == "" {
  824. taskTime = nowTime
  825. }
  826. clueUpdateData["task_time"] = nowTime
  827. clueUpdateData["tasktime"] = taskTime
  828. clueUpdateData["taskstatus"] = 0
  829. }
  830. if is_transfer != 1 {
  831. clueUpdateData["is_task"] = 1
  832. }
  833. }
  834. } else {
  835. if old_sub_cluetype != sub_cluetype || is_assign == 0 {
  836. if taskTime == "" {
  837. taskTime = nowTime
  838. }
  839. clueUpdateData["task_time"] = nowTime
  840. clueUpdateData["tasktime"] = taskTime
  841. clueUpdateData["taskstatus"] = 0
  842. }
  843. if is_transfer != 1 {
  844. clueUpdateData["is_task"] = 1
  845. }
  846. }
  847. }
  848. }
  849. ok, updateId14 := true, true
  850. updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
  851. updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
  852. updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
  853. if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
  854. //线索
  855. ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
  856. //变更记录
  857. if (trailstatus == "00" || old_position_id == 0) && is_assign == 1 {
  858. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  859. } else if old_position_id == 0 {
  860. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  861. "clue_id": clueId,
  862. "position_id": positionId,
  863. "change_field": "position_id",
  864. "change_type": "所属人变更",
  865. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  866. "new_value": common.If(saleName != "", saleName, "/"),
  867. "createtime": nowTime,
  868. "BCPCID": common.GetRandom(32),
  869. "operator_id": -1,
  870. })
  871. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  872. "clue_id": clueId,
  873. "position_id": positionId,
  874. "change_field": "trailstatus",
  875. "change_type": "基本信息变更",
  876. "old_value": CodeTrail[trailstatus],
  877. "new_value": "新增",
  878. "createtime": nowTime,
  879. "BCPCID": common.GetRandom(32),
  880. "operator_id": -1,
  881. })
  882. } else {
  883. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  884. "clue_id": clueId,
  885. "position_id": positionId,
  886. "change_field": "position_id",
  887. "change_type": "所属人变更",
  888. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  889. "new_value": common.If(saleName != "", saleName, "/"),
  890. "createtime": nowTime,
  891. "BCPCID": common.GetRandom(32),
  892. "operator_id": -1,
  893. })
  894. updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  895. "clue_id": clueId,
  896. "position_id": common.Int64All(data["position_id"]),
  897. "change_field": "trailstatus",
  898. "change_type": "基本信息变更",
  899. "old_value": "无意向客户",
  900. "new_value": "流失",
  901. "createtime": nowTime,
  902. "BCPCID": common.GetRandom(32),
  903. "operator_id": -1,
  904. })
  905. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  906. "clue_id": clueId,
  907. "position_id": positionId,
  908. "change_field": "trailstatus",
  909. "change_type": "基本信息变更",
  910. "old_value": "商机线索",
  911. "new_value": "新增",
  912. "createtime": nowTime,
  913. "BCPCID": common.GetRandom(32),
  914. "operator_id": -1,
  915. })
  916. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  917. "clue_id": clueId,
  918. "position_id": positionId,
  919. "change_field": "trailstatus",
  920. "change_type": "基本信息变更",
  921. "old_value": "无意向客户",
  922. "new_value": "商机线索",
  923. "createtime": nowTime,
  924. "BCPCID": common.GetRandom(32),
  925. "operator_id": -1,
  926. })
  927. }
  928. } else if trailstatus != "08" && is_assign == 0 {
  929. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  930. } else {
  931. updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  932. "clue_id": clueId,
  933. "position_id": positionId,
  934. "change_field": "position_id",
  935. "change_type": "所属人变更",
  936. "old_value": common.If(oldsaleName != "", oldsaleName, "/"),
  937. "new_value": common.If(saleName != "", saleName, "/"),
  938. "createtime": nowTime,
  939. "BCPCID": common.GetRandom(32),
  940. "operator_id": -1,
  941. })
  942. if trailstatus != "01" {
  943. updateId4 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  944. "clue_id": clueId,
  945. "position_id": positionId,
  946. "change_field": "trailstatus",
  947. "change_type": "基本信息变更",
  948. "old_value": CodeTrail[trailstatus],
  949. "new_value": "商机线索",
  950. "createtime": nowTime,
  951. "BCPCID": common.GetRandom(32),
  952. "operator_id": -1,
  953. })
  954. }
  955. updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  956. "clue_id": clueId,
  957. "position_id": positionId,
  958. "change_field": "trailstatus",
  959. "change_type": "基本信息变更",
  960. "old_value": "商机线索",
  961. "new_value": "新增",
  962. "createtime": nowTime,
  963. "BCPCID": common.GetRandom(32),
  964. "operator_id": -1,
  965. })
  966. }
  967. }
  968. if (trailstatus != "08" && recordCount <= 0) || is_assign == 0 {
  969. if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
  970. } else {
  971. if old_sub_cluetype != sub_cluetype && !is_ok {
  972. if is_transfer != 1 {
  973. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  974. "clue_id": clueId,
  975. "position_id": common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])),
  976. "change_type": "加入任务车",
  977. "new_value": "线索来源自动更新" + "-" + topname + "-" + subname,
  978. "createtime": nowTime,
  979. "BCPCID": common.GetRandom(32),
  980. "operator_id": -1,
  981. })
  982. } else {
  983. cdata := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, "", "")
  984. if cdata != nil && len(*cdata) > 0 {
  985. kcpositionId := common.Int64All((*cdata)["position_id"])
  986. tasksource := common.ObjToString((*cdata)["tasksource"])
  987. taskstatus := common.IntAll((*cdata)["taskstatus"])
  988. updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  989. "clue_id": clueId,
  990. "position_id": kcpositionId,
  991. "change_type": "加入任务车",
  992. "new_value": "线上主动留资",
  993. "createtime": nowTime,
  994. "BCPCID": common.GetRandom(32),
  995. "operator_id": -1,
  996. })
  997. if taskstatus == 1 {
  998. tasksource = "10"
  999. } else {
  1000. if tasksource != "" && !strings.Contains(tasksource, "10") {
  1001. if !strings.Contains(tasksource, "10") {
  1002. tasksource += ",10"
  1003. }
  1004. } else {
  1005. tasksource = "10"
  1006. }
  1007. }
  1008. updateId14 = TiDb.UpdateByTx(tx, "dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  1009. "tasksource": tasksource,
  1010. "tasktime": nowTime,
  1011. "is_task": 1,
  1012. "taskstatus": 0,
  1013. })
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. if old_top_cluetype != "" {
  1020. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  1021. if pcodeData != nil && len(*pcodeData) > 0 {
  1022. old_topname = common.ObjToString((*pcodeData)["name"])
  1023. }
  1024. }
  1025. if old_sub_cluetype != "" {
  1026. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  1027. if pcodeData != nil && len(*pcodeData) > 0 {
  1028. old_subname = common.ObjToString((*pcodeData)["name"])
  1029. }
  1030. }
  1031. if item != "orders" {
  1032. if old_name != name && name != "" {
  1033. updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1034. "clue_id": clueId,
  1035. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1036. "change_field": "name",
  1037. "change_type": "基本信息变更",
  1038. "old_value": common.If(old_name != "", old_name, "/"),
  1039. "new_value": common.If(name != "", name, "/"),
  1040. "createtime": nowTime,
  1041. "BCPCID": BCPCID,
  1042. "operator_id": -1,
  1043. })
  1044. }
  1045. if old_position != position && position != "" {
  1046. updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1047. "clue_id": clueId,
  1048. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1049. "change_field": "position",
  1050. "change_type": "基本信息变更",
  1051. "old_value": common.If(old_position != "", old_position, "/"),
  1052. "new_value": common.If(position != "", position, "/"),
  1053. "createtime": nowTime,
  1054. "BCPCID": BCPCID,
  1055. "operator_id": -1,
  1056. })
  1057. }
  1058. if old_department != department && department != "" {
  1059. updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1060. "clue_id": clueId,
  1061. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1062. "change_field": "department",
  1063. "change_type": "基本信息变更",
  1064. "old_value": common.If(old_department != "", old_department, "/"),
  1065. "new_value": common.If(department != "", department, "/"),
  1066. "createtime": nowTime,
  1067. "BCPCID": BCPCID,
  1068. "operator_id": -1,
  1069. })
  1070. }
  1071. if old_role != role && role != "" {
  1072. updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1073. "clue_id": clueId,
  1074. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1075. "change_field": "role",
  1076. "change_type": "基本信息变更",
  1077. "old_value": common.If(old_role != "", old_role, "/"),
  1078. "new_value": common.If(role != "", role, "/"),
  1079. "createtime": nowTime,
  1080. "BCPCID": BCPCID,
  1081. "operator_id": -1,
  1082. })
  1083. }
  1084. if old_follow_project_area != follow_project_area && follow_project_area != "" {
  1085. old_area, old_area_arr := "", []string{}
  1086. new_area, new_area_arr := "", []string{}
  1087. for _, v := range strings.Split(old_follow_project_area, ",") {
  1088. old_area_arr = append(old_area_arr, CodeArea[v])
  1089. }
  1090. for _, v := range strings.Split(follow_project_area, ",") {
  1091. new_area_arr = append(new_area_arr, CodeArea[v])
  1092. }
  1093. old_area = strings.Join(old_area_arr, ",")
  1094. new_area = strings.Join(new_area_arr, ",")
  1095. updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1096. "clue_id": clueId,
  1097. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1098. "change_field": "follow_project_area",
  1099. "change_type": "基本信息变更",
  1100. "old_value": common.If(old_area != "", old_area, "/"),
  1101. "new_value": common.If(new_area != "", new_area, "/"),
  1102. "createtime": nowTime,
  1103. "BCPCID": BCPCID,
  1104. "operator_id": -1,
  1105. })
  1106. }
  1107. if old_cluename != cluename && cluename != "" {
  1108. updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1109. "clue_id": clueId,
  1110. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1111. "change_field": "cluename",
  1112. "change_type": "基本信息变更",
  1113. "old_value": common.If(old_cluename != "", old_cluename, "/"),
  1114. "new_value": common.If(cluename != "", cluename, "/"),
  1115. "createtime": nowTime,
  1116. "BCPCID": BCPCID,
  1117. "operator_id": -1,
  1118. })
  1119. }
  1120. }
  1121. if old_top_cluetype != top_cluetype {
  1122. updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1123. "clue_id": clueId,
  1124. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1125. "change_field": "top_cluetype",
  1126. "change_type": "基本信息变更",
  1127. "old_value": common.If(old_topname != "", old_topname, "/"),
  1128. "new_value": common.If(topname != "", topname, "/"),
  1129. "createtime": nowTime,
  1130. "BCPCID": BCPCID,
  1131. "operator_id": -1,
  1132. })
  1133. }
  1134. if old_sub_cluetype != sub_cluetype {
  1135. updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  1136. "clue_id": clueId,
  1137. "position_id": common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"]))),
  1138. "change_field": "sub_cluetype",
  1139. "change_type": "基本信息变更",
  1140. "old_value": common.If(old_subname != "", old_subname, "/"),
  1141. "new_value": common.If(subname != "", subname, "/"),
  1142. "createtime": nowTime,
  1143. "BCPCID": BCPCID,
  1144. "operator_id": -1,
  1145. })
  1146. }
  1147. return ok && 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 && updateId14
  1148. }) {
  1149. log.Println("线索更新成功")
  1150. if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  1151. TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  1152. "status": 1,
  1153. "is_delete": 1,
  1154. "createtime": nowTime,
  1155. "updatetime": nowTime,
  1156. "phone": phone,
  1157. "baseinfo_id": uId,
  1158. "SOURCE": source,
  1159. })
  1160. }
  1161. return true
  1162. } else {
  1163. log.Println("线索更新失败!!!", ok, updateId1, updateId2, updateId3, updateId4, updateId5, updateId6, updateId7, updateId8, updateId9, updateId10, updateId11, updateId12, updateId13, updateId14, " 用户信息 ", item, position, seatNumber, uId, userId, phone)
  1164. return false
  1165. }
  1166. }
  1167. func getAreaCode(userId string) (code string) {
  1168. followData := Base.Find("follow_project_monitor", map[string]interface{}{"s_userid": userId}, "", "", -1, -1)
  1169. sidArr := []string{}
  1170. if followData != nil && len(*followData) > 0 {
  1171. for _, v := range *followData {
  1172. infoId := common.ObjToString(v["s_id"])
  1173. sidArr = append(sidArr, infoId)
  1174. }
  1175. }
  1176. if len(sidArr) > 0 {
  1177. query := `{"query": {"bool": {"must": [{"terms": {"_id": ["%s"]}}],"must_not": [],"should": []}}}`
  1178. query = fmt.Sprintf(query, strings.Join(sidArr, `","`))
  1179. biddingData := Es.Get("bidding", "bidding", query)
  1180. if biddingData != nil && len(*biddingData) > 0 {
  1181. codeMap := map[string]string{}
  1182. codeArr := []string{}
  1183. for _, v := range *biddingData {
  1184. area := common.ObjToString(v["area"])
  1185. address := common.ObjToString(v["city"])
  1186. if address == "" {
  1187. address = area
  1188. }
  1189. codeMap[address] = AreaCode[address]
  1190. }
  1191. if len(codeMap) > 0 {
  1192. for _, v := range codeMap {
  1193. codeArr = append(codeArr, v)
  1194. }
  1195. }
  1196. if len(codeArr) > 0 {
  1197. code = strings.Join(codeArr, ",")
  1198. }
  1199. }
  1200. }
  1201. log.Println("code ", code)
  1202. return
  1203. }
  1204. func getClueType(item string, data map[string]interface{}, sourceCode string) (pcode, code, level, topname, subname string) {
  1205. if item == "orders" {
  1206. productType := common.ObjToString(data["product_type"])
  1207. pcode = "1"
  1208. level = "A"
  1209. topname = "提交订单未支付"
  1210. if productType == "VIP订阅" {
  1211. code = "6"
  1212. subname = "超级订阅"
  1213. } else if productType == "大会员" {
  1214. code = "7"
  1215. subname = "大会员"
  1216. } else if productType == "数据流量包" {
  1217. code = "8"
  1218. subname = "数据流量包"
  1219. } else if productType == "历史数据" {
  1220. code = "9"
  1221. subname = "数据自助导出"
  1222. }
  1223. } else if item == "users" {
  1224. pcode = "4"
  1225. code = "154"
  1226. level = "C"
  1227. topname = "新增注册"
  1228. subname = "新增注册用户"
  1229. } else if item == "message" {
  1230. pcode = "532"
  1231. code = "477"
  1232. level = "B"
  1233. topname = "其他"
  1234. subname = "机器人客服主动咨询"
  1235. } else {
  1236. if sourceCode != "" {
  1237. codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"source": sourceCode}, "", "")
  1238. if codeData != nil && len(*codeData) > 0 {
  1239. pcode = common.ObjToString((*codeData)["pcode"])
  1240. code = common.ObjToString((*codeData)["code"])
  1241. level = common.ObjToString((*codeData)["clue_level"])
  1242. subname = common.ObjToString((*codeData)["name"])
  1243. pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
  1244. if pcodeData != nil && len(*pcodeData) > 0 {
  1245. topname = common.ObjToString((*pcodeData)["name"])
  1246. }
  1247. }
  1248. }
  1249. }
  1250. return
  1251. }
  1252. // 获取自动分配的人
  1253. func autoDraw(mode string) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}) {
  1254. query := `select * from jy_salesperson_info where`
  1255. if mode == "A" || mode == "B" {
  1256. query += ` is_complete = 1`
  1257. } else {
  1258. query += ` is_complete = 1 or is_complete = 0`
  1259. }
  1260. data := TiDb.SelectBySql(query)
  1261. if data != nil && len(*data) > 0 {
  1262. saleData = *data
  1263. sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?"
  1264. countData := TiDb.SelectBySql(sql, mode)
  1265. if countData != nil && len(*countData) > 0 {
  1266. for _, v := range *data {
  1267. //判断是否有新员工
  1268. isOk := false
  1269. for _, vv := range *countData {
  1270. if common.ObjToString(v["seatNumber"]) == common.ObjToString(vv["seatNumber"]) {
  1271. if common.IntAll(v["status"]) == 0 {
  1272. vv["status"] = 1
  1273. } else {
  1274. vv["status"] = 2
  1275. }
  1276. isOk = true
  1277. }
  1278. }
  1279. //有新员工直接分给新员工
  1280. if !isOk {
  1281. seatNumber = common.ObjToString(v["seatNumber"])
  1282. saleName = common.ObjToString(v["name"])
  1283. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count desc")
  1284. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1285. "seatNumber": seatNumber,
  1286. "clue_level": mode,
  1287. "count": common.Int64All((*rData)["count"]),
  1288. })
  1289. break
  1290. }
  1291. }
  1292. if seatNumber == "" {
  1293. res := int64(0)
  1294. countres := 0
  1295. for _, v := range *countData {
  1296. if common.IntAll(v["status"]) == 1 {
  1297. if countres == 0 {
  1298. res = common.Int64All(v["count"])
  1299. seatNumber = common.ObjToString(v["seatNumber"])
  1300. } else {
  1301. if common.Int64All(v["count"]) <= res {
  1302. res = common.Int64All(v["count"])
  1303. seatNumber = common.ObjToString(v["seatNumber"])
  1304. }
  1305. }
  1306. countres++
  1307. }
  1308. // if k == 0 {
  1309. // res = common.Int64All(v["count"])
  1310. // seatNumber = common.ObjToString(v["seatNumber"])
  1311. // } else {
  1312. // if common.Int64All(v["count"]) <= res {
  1313. // res = common.Int64All(v["count"])
  1314. // seatNumber = common.ObjToString(v["seatNumber"])
  1315. // }
  1316. // }
  1317. }
  1318. }
  1319. } else {
  1320. seatNumber = common.ObjToString((*data)[0]["seatNumber"])
  1321. saleName = common.ObjToString((*data)[0]["name"])
  1322. rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc")
  1323. if rData != nil && len(*rData) > 0 {
  1324. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1325. "seatNumber": seatNumber,
  1326. "clue_level": mode,
  1327. "count": common.Int64All((*rData)["count"]),
  1328. })
  1329. } else {
  1330. TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
  1331. "seatNumber": seatNumber,
  1332. "clue_level": mode,
  1333. "count": 0,
  1334. })
  1335. }
  1336. }
  1337. for _, v := range *data {
  1338. if seatNumber == common.ObjToString(v["seatNumber"]) {
  1339. positionId = getPositionId(common.ObjToString(v["phone"]))
  1340. saleName = common.ObjToString(v["name"])
  1341. }
  1342. }
  1343. // if positionId > 0 {
  1344. // TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where seatNumber = ? and clue_level = ?`, seatNumber, mode)
  1345. // }
  1346. }
  1347. return
  1348. }
  1349. func getPositionId(phone string) (positionId int64) {
  1350. userData, ok := Mgo.FindOne("user", map[string]interface{}{"s_phone": phone})
  1351. if ok && userData != nil && len(*userData) > 0 {
  1352. userId := common.Int64All((*userData)["base_user_id"])
  1353. positionData := Base.FindOne("base_position", map[string]interface{}{"type": 1, "ent_id": 25917, "user_id": userId}, "", "") //TODO ent_id
  1354. if positionData != nil && len(*positionData) > 0 {
  1355. positionId = common.Int64All((*positionData)["id"])
  1356. }
  1357. }
  1358. return
  1359. }