trailContent.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package main
  2. import (
  3. "log"
  4. "regexp"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "strings"
  7. "time"
  8. "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/date"
  10. "github.com/tealeg/xlsx"
  11. )
  12. func trail() {
  13. log.Println("合力亿捷跟进历史任务开始")
  14. nameReg := regexp.MustCompile("【.*?】")
  15. filePath := "./xlsx/跟进历史" + cfg.XlsxNum + ".xlsx"
  16. log.Println("文件名 ", filePath)
  17. xlFile, _ := xlsx.OpenFile(filePath)
  18. //获取行数
  19. length := len(xlFile.Sheets[0].Rows)
  20. log.Println("获取行数 ", length)
  21. //遍历sheet
  22. for _, sheet := range xlFile.Sheets {
  23. //遍历每一行
  24. for rowIndex, row := range sheet.Rows {
  25. //跳过第一行表头信息
  26. if rowIndex == 0 {
  27. continue
  28. }
  29. createtime := row.Cells[0].Value
  30. name := row.Cells[1].Value
  31. nexttype := row.Cells[2].Value
  32. content := row.Cells[3].Value
  33. phone := row.Cells[6].Value
  34. positionId, clueId := getSale(name, phone)
  35. log.Println("positionId,clueId ", positionId, clueId)
  36. if clueId > 0 {
  37. if nexttype == "无类型" {
  38. TiDb.Insert("dwd_f_crm_trail_content", map[string]interface{}{
  39. "clue_id": clueId,
  40. "position_id": positionId,
  41. "createtime": createtime,
  42. "content": content,
  43. "operator_id": positionId,
  44. })
  45. } else {
  46. old_value := strings.ReplaceAll(strings.ReplaceAll(nameReg.FindAllString(content, -1)[0], "【", ""), "】", "")
  47. new_value := strings.ReplaceAll(strings.ReplaceAll(nameReg.FindAllString(content, -1)[1], "【", ""), "】", "")
  48. TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  49. "clue_id": clueId,
  50. "position_id": positionId,
  51. "createtime": createtime,
  52. "change_field": common.If(nexttype == "所属人变更", "position_id", "trailstatus"),
  53. "change_type": common.If(nexttype == "所属人变更", nexttype, "基本信息变更"),
  54. "old_value": common.If(old_value == "销售线索", "商机线索", old_value),
  55. "new_value": common.If(new_value == "销售线索", "商机线索", new_value),
  56. "operator_id": positionId,
  57. })
  58. }
  59. }
  60. }
  61. }
  62. }
  63. func getSale(name, phone string) (positionId, clueId int64) {
  64. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"name": name, "resign": 0}, "", "")
  65. if saleData != nil && len(*saleData) > 0 {
  66. positionId = common.Int64All((*saleData)["position_id"])
  67. } else {
  68. positionId = -1
  69. }
  70. if positionId == 0 {
  71. positionId = -1
  72. }
  73. clueInfo := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  74. if clueInfo != nil && len(*clueInfo) > 0 {
  75. clueId = common.Int64All((*clueInfo)["id"])
  76. }
  77. return
  78. }
  79. func getSeatNumberPositionId(seatNumber string) (positionId int64) {
  80. saleData := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{"seat_number": seatNumber}, "", "")
  81. if saleData != nil && len(*saleData) > 0 {
  82. positionId = common.Int64All((*saleData)["position_id"])
  83. }
  84. return
  85. }
  86. func getXlsx() {
  87. filePath := "./code1.xlsx"
  88. nowTime := time.Now().Format(date.Date_Full_Layout)
  89. xlFile, _ := xlsx.OpenFile(filePath)
  90. //获取行数
  91. // length := len(xlFile.Sheets[0].Rows)
  92. //开辟除表头外的行数的数组内存
  93. // resourceArr := make([]map[string]interface{}, length-1)
  94. //遍历sheet
  95. for _, sheet := range xlFile.Sheets {
  96. //遍历每一行
  97. for rowIndex, row := range sheet.Rows {
  98. //跳过第一行表头信息
  99. if rowIndex == 0 {
  100. continue
  101. }
  102. company := row.Cells[1].Value
  103. name := row.Cells[2].Value
  104. position := row.Cells[3].Value
  105. phone := row.Cells[4].Value
  106. if phone != "" {
  107. data := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  108. if data != nil && len(*data) > 0 {
  109. isAssign := common.IntAll((*data)["is_assign"])
  110. clueId := common.Int64All((*data)["id"])
  111. if isAssign == 1 {
  112. //在私海
  113. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  114. "is_assign": 1,
  115. "comeintime": nowTime,
  116. "updatetime": nowTime,
  117. "top_cluetype": "377",
  118. "sub_cluetype": "381",
  119. })
  120. TiDb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, map[string]interface{}{
  121. "comeintime": nowTime,
  122. "comeinsource": 2,
  123. "is_task": 1,
  124. "task_time": nowTime,
  125. "tasktime": nowTime,
  126. "taskstatus": 0,
  127. "tasksource": "线索自动分配-批量导入-6月份获取拟建项目清单留资",
  128. })
  129. } else {
  130. //在公海
  131. positionId, seatNumber, _, _, _ := autoDraw("C", "", 0, 0)
  132. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  133. "seatNumber": seatNumber,
  134. "position_id": positionId,
  135. "is_assign": 1,
  136. "comeintime": nowTime,
  137. "updatetime": nowTime,
  138. "trailstatus": "01",
  139. "top_cluetype": "377",
  140. "sub_cluetype": "381",
  141. })
  142. TiDb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  143. TiDb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  144. "clue_id": clueId,
  145. "seatNumber": seatNumber,
  146. "position_id": positionId,
  147. "comeintime": nowTime,
  148. "comeinsource": 2,
  149. "is_task": 1,
  150. "task_time": nowTime,
  151. "tasktime": nowTime,
  152. "taskstatus": 0,
  153. "tasksource": "线索自动分配-批量导入-6月份获取拟建项目清单留资",
  154. })
  155. }
  156. } else {
  157. //无线索
  158. base := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"phone": phone}, "", "")
  159. if base != nil && len(*base) > 0 {
  160. uId := common.ObjToString((*base)["uid"])
  161. userId := common.ObjToString((*base)["userid"])
  162. positionId, seatNumber, _, _, _ := autoDraw("C", "", 0, 0)
  163. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  164. "userid": userId,
  165. "uid": uId,
  166. "seatNumber": seatNumber,
  167. "position_id": positionId,
  168. "is_assign": 1,
  169. "comeintime": nowTime,
  170. "createtime": nowTime,
  171. "updatetime": nowTime,
  172. "cluename": common.If(company != "", company, phone),
  173. "top_cluetype": "377",
  174. "sub_cluetype": "381",
  175. "trailstatus": "01",
  176. "name": name,
  177. "phone": phone,
  178. "position": position,
  179. })
  180. if clueId > -1 {
  181. TiDb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  182. "clue_id": clueId,
  183. "seatNumber": seatNumber,
  184. "position_id": positionId,
  185. "comeintime": nowTime,
  186. "comeinsource": 2,
  187. "is_task": 1,
  188. "task_time": nowTime,
  189. "tasktime": "2023-06-12 10:00:00",
  190. "taskstatus": 0,
  191. "tasksource": "线索自动分配-批量导入-6月份获取拟建项目清单留资",
  192. })
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. func getXlsx2() {
  201. filePath := "./code2.xlsx"
  202. nowTime := time.Now().Format(date.Date_Full_Layout)
  203. xlFile, _ := xlsx.OpenFile(filePath)
  204. //获取行数
  205. // length := len(xlFile.Sheets[0].Rows)
  206. //开辟除表头外的行数的数组内存
  207. // resourceArr := make([]map[string]interface{}, length-1)
  208. //遍历sheet
  209. for _, sheet := range xlFile.Sheets {
  210. //遍历每一行
  211. for rowIndex, row := range sheet.Rows {
  212. //跳过第一行表头信息
  213. if rowIndex == 0 {
  214. continue
  215. }
  216. userId := row.Cells[0].Value
  217. company := row.Cells[5].Value
  218. name := row.Cells[1].Value
  219. position := row.Cells[6].Value
  220. phone := row.Cells[7].Value
  221. positionId, seatNumber := int64(2047781), "8040"
  222. if phone != "" {
  223. data := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"userid": userId}, "", "")
  224. if data != nil && len(*data) > 0 {
  225. isAssign := common.IntAll((*data)["is_assign"])
  226. clueId := common.Int64All((*data)["id"])
  227. if isAssign != 1 {
  228. //不在私海
  229. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
  230. "seatNumber": seatNumber,
  231. "position_id": positionId,
  232. "is_assign": 1,
  233. "comeintime": nowTime,
  234. "updatetime": nowTime,
  235. "trailstatus": "01",
  236. "top_cluetype": "377",
  237. "sub_cluetype": "382",
  238. })
  239. TiDb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  240. TiDb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  241. "clue_id": clueId,
  242. "seatNumber": seatNumber,
  243. "position_id": positionId,
  244. "comeintime": nowTime,
  245. "comeinsource": 2,
  246. "is_task": 1,
  247. "task_time": nowTime,
  248. "tasktime": nowTime,
  249. "taskstatus": 0,
  250. "tasksource": "线索自动分配-批量导入-医械厂商线索",
  251. })
  252. }
  253. } else {
  254. //无线索
  255. base := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": userId}, "", "")
  256. if base != nil && len(*base) > 0 {
  257. uId := common.ObjToString((*base)["uid"])
  258. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  259. "userid": userId,
  260. "uid": uId,
  261. "seatNumber": seatNumber,
  262. "position_id": positionId,
  263. "is_assign": 1,
  264. "comeintime": nowTime,
  265. "createtime": nowTime,
  266. "updatetime": nowTime,
  267. "cluename": common.If(company != "", company, phone),
  268. "top_cluetype": "377",
  269. "sub_cluetype": "382",
  270. "trailstatus": "01",
  271. "name": name,
  272. "phone": phone,
  273. "position": position,
  274. })
  275. if clueId > -1 {
  276. TiDb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  277. "clue_id": clueId,
  278. "seatNumber": seatNumber,
  279. "position_id": positionId,
  280. "comeintime": nowTime,
  281. "comeinsource": 2,
  282. "is_task": 1,
  283. "task_time": nowTime,
  284. "tasktime": "2023-06-12 10:00:00",
  285. "taskstatus": 0,
  286. "tasksource": "线索自动分配-批量导入-医械厂商线索",
  287. })
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. func clueInfoSync() {
  296. TiDb.SelectByBath(500, func(l *[]map[string]interface{}) bool {
  297. for _, v := range *l {
  298. id := common.Int64All(v["id"])
  299. is_assign := common.IntAll(v["is_assign"])
  300. comeintime := common.ObjToString(v["comeintime"])
  301. comeintime_open := common.ObjToString(v["comeintime_open"])
  302. trail_time, trail_times, next_trail_time := common.ObjToString(v["createtime"]), "", ""
  303. tasktime := common.ObjToString(v["tasktime"])
  304. task_time := common.ObjToString(v["task_time"])
  305. is_task := common.IntAll(v["is_task"])
  306. taskstatus := common.IntAll(v["taskstatus"])
  307. comeinsource_private := common.IntAll(v["comeinsource_private"])
  308. tasksource := common.ObjToString(v["tasksource"])
  309. out_task_time := common.ObjToString(v["out_task_time"])
  310. out_task_status := common.IntAll(v["out_task_status"])
  311. comeinsource_open := common.IntAll(v["comeinsource_open"])
  312. level_open := common.IntAll(v["level_open"])
  313. clue_level := common.ObjToString(v["clue_level"])
  314. var (
  315. trail_time_s time.Time
  316. comeintime_s time.Time
  317. )
  318. if trail_time != "" && comeintime != "" {
  319. trail_time_s, _ = time.ParseInLocation(date.Date_Full_Layout, trail_time, time.Local)
  320. comeintime_s, _ = time.ParseInLocation(date.Date_Full_Layout, comeintime, time.Local)
  321. if trail_time_s.Unix() > comeintime_s.Unix() {
  322. trail_times = trail_time
  323. next_trail_time = common.ObjToString(v["next_time"])
  324. }
  325. }
  326. updateData := map[string]interface{}{
  327. "comeintime": common.If(comeintime != "", comeintime, nil),
  328. "comeintime_open": common.If(comeintime_open != "", comeintime_open, nil),
  329. }
  330. if is_assign == 1 {
  331. updateData["trail_time"] = common.If(trail_times != "", trail_times, nil)
  332. updateData["next_trail_time"] = common.If(next_trail_time != "", next_trail_time, nil)
  333. updateData["is_task"] = is_task
  334. updateData["tasktime"] = common.If(tasktime != "", tasktime, nil)
  335. updateData["task_time"] = common.If(task_time != "", task_time, nil)
  336. updateData["taskstatus"] = taskstatus
  337. updateData["comeinsource_private"] = comeinsource_private
  338. updateData["tasksource"] = tasksource
  339. updateData["out_task_time"] = common.If(out_task_time != "", out_task_time, nil)
  340. updateData["out_task_status"] = out_task_status
  341. } else if is_assign == 0 {
  342. updateData["comeinsource_open"] = comeinsource_open
  343. updateData["level_open"] = level_open
  344. updateData["clue_level"] = clue_level
  345. }
  346. ok := TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": id}, updateData)
  347. if ok {
  348. log.Println("线索表同步成功 ", id)
  349. } else {
  350. log.Println("线索表同步失败!!! ", id, updateData)
  351. }
  352. }
  353. return true
  354. }, `SELECT a.id,a.is_assign,c.comeintime,b.comeintime as comeintime_open,d.createtime,d.next_time,c.tasktime,c.task_time,c.is_task,c.taskstatus,c.comeinsource as comeinsource_private,c.tasksource,c.out_task_time,c.out_task_status,b.comeinsource as comeinsource_open,b.level as level_open,b.clue_level
  355. FROM dwd_f_crm_clue_info a
  356. LEFT JOIN dwd_f_crm_open_sea b ON b.clue_id=a.id
  357. LEFT JOIN dwd_f_crm_private_sea c ON c.clue_id=a.id
  358. LEFT JOIN dwd_f_crm_trail_content d ON d.clue_id=a.id and d.position_id=a.position_id`)
  359. }
  360. func app_xzcyh_sync() {
  361. query := map[string]interface{}{"source": "app_xzcyh"}
  362. session := Mgo.GetMgoConn()
  363. defer func() {
  364. Mgo.DestoryMongoConn(session)
  365. }()
  366. iter := session.DB(cfg.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
  367. thisData := map[string]interface{}{}
  368. for {
  369. if !iter.Next(&thisData) {
  370. break
  371. }
  372. userId := common.ObjToString(thisData["userid"])
  373. keyword := common.ObjArrToStringArr(thisData["keyword"].([]interface{}))
  374. if !mongodb.IsObjectIdHex(userId) {
  375. userMapping := TiDb.FindOne("dwd_f_userbase_id_mapping", map[string]interface{}{"position_id": userId}, "", "")
  376. if userMapping != nil && len(*userMapping) > 0 {
  377. userId = common.ObjToString((*userMapping)["userid"])
  378. }
  379. }
  380. data := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"userid": userId}, "", "")
  381. if data != nil && len(*data) > 0 {
  382. business_scope := common.ObjToString((*data)["business_scope"])
  383. clueUpdateData := map[string]interface{}{}
  384. keywords := ""
  385. if len(keyword) > 0 && keyword[0] != "" {
  386. keywords = strings.Join(keyword, ",")
  387. if business_scope != "" {
  388. clueUpdateData["business_scope"] = business_scope + "," + keywords
  389. } else {
  390. clueUpdateData["business_scope"] = keywords
  391. }
  392. ok := TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"userid": userId}, clueUpdateData)
  393. if ok {
  394. log.Println("主营业务修改成功", userId, keywords)
  395. } else {
  396. log.Println("主营业务修改失败!!!", userId, keywords)
  397. }
  398. }
  399. }
  400. }
  401. }
  402. func lzhSync() {
  403. nowTime, count := time.Now().Format("2006-01-02 15:04:05"), 0
  404. data := TiDbData.SelectBySql(`SELECT * FROM customer where owner in (2646,8050) and status999 in ("status0","status1","status2","status3")`)
  405. if data != nil {
  406. for _, v := range *data {
  407. count++
  408. owner := common.ObjToString(v["owner"])
  409. positionId, seatNumber, trailstatus, uId, userId := int64(0), "", "", "", ""
  410. if owner == "2646" {
  411. positionId = 1205321180
  412. seatNumber = "2646"
  413. } else {
  414. positionId = 1205321184
  415. seatNumber = "8050"
  416. }
  417. phone := common.ObjToString(v["phone"])
  418. status := common.ObjToString(v["status999"])
  419. cluename := common.ObjToString(v["company"])
  420. name := common.ObjToString(v["username"])
  421. position := common.ObjToString(v["job"])
  422. if status == "status0" {
  423. trailstatus = "08"
  424. } else if status == "status1" {
  425. trailstatus = "07"
  426. } else if status == "status2" {
  427. trailstatus = "06"
  428. } else {
  429. trailstatus = "05"
  430. }
  431. cdata := TiDb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}, "", "")
  432. if cdata != nil {
  433. uId = common.ObjToString((*cdata)["baseinfo_id"])
  434. cluedata := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
  435. if cluedata != nil {
  436. log.Println("第", count, "条已有线索", phone, uId)
  437. seatNumbersss := common.ObjToString((*cluedata)["seatNumber"])
  438. if seatNumbersss == "" {
  439. TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, map[string]interface{}{
  440. "seatNumber": seatNumber,
  441. "position_id": positionId,
  442. "is_assign": 1,
  443. "comeintime": nowTime,
  444. "updatetime": nowTime,
  445. "trailstatus": trailstatus,
  446. "is_task": 1,
  447. "task_time": nowTime,
  448. "tasktime": nowTime,
  449. "taskstatus": 0,
  450. "comeinsource_private": 2,
  451. })
  452. }
  453. } else {
  454. udata := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"uid": uId}, "", "")
  455. if udata != nil {
  456. userId = common.ObjToString((*udata)["userid"])
  457. clueId := TiDb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  458. "userid": userId,
  459. "uid": uId,
  460. "seatNumber": seatNumber,
  461. "position_id": positionId,
  462. "is_assign": 1,
  463. "comeintime": nowTime,
  464. "createtime": nowTime,
  465. "updatetime": nowTime,
  466. "cluename": cluename,
  467. "top_cluetype": "4",
  468. "sub_cluetype": "154",
  469. "trailstatus": trailstatus,
  470. "name": name,
  471. "phone": phone,
  472. "position": position,
  473. "comeinsource_private": 2,
  474. "is_task": 1,
  475. "task_time": nowTime,
  476. "tasktime": nowTime,
  477. "taskstatus": 0,
  478. })
  479. if clueId > 0 {
  480. log.Println("第", count, "条保存成功", phone, uId)
  481. }
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }