clue.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "math"
  7. "regexp"
  8. "sync"
  9. "time"
  10. "app.yhyue.com/moapp/jybase/date"
  11. common "app.yhyue.com/moapp/jybase/common"
  12. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  13. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  14. )
  15. func DistributeClue(this *biservice.DistributeClueReq) *biservice.AddProjectResp {
  16. count, status := DistributeClueSync(this)
  17. log.Println("分配数量 ", count)
  18. return &biservice.AddProjectResp{
  19. ErrorCode: 0,
  20. Data: &biservice.AddProject{
  21. Status: int64(status),
  22. Count: int64(count),
  23. },
  24. }
  25. }
  26. func DistributeClueSync(this *biservice.DistributeClueReq) (int, int) {
  27. if DistributeLock.TryLock() {
  28. defer DistributeLock.Unlock()
  29. saleMap, count := map[string]map[string]interface{}{}, 0
  30. saleData := JyBiTidb.SelectBySql("select * from jy_salesperson_info where is_complete = 1 or is_complete = 0")
  31. if saleData != nil && len(*saleData) > 0 {
  32. for _, v := range *saleData {
  33. name := common.ObjToString(v["name"])
  34. saleMap[name] = v
  35. }
  36. }
  37. for _, data := range this.Datas {
  38. seatNumber := common.ObjToString(saleMap[data.Name]["seatNumber"])
  39. distributedCount := int(data.DistributedCount)
  40. if distributedCount > 0 {
  41. distributedArr := this.ClueIdList[count : count+distributedCount]
  42. count += distributedCount
  43. DistributeClueMore(saleMap, distributedArr, seatNumber, data.Name, data.PositionId, this.PositionId, this.IsTask)
  44. }
  45. }
  46. return count, 1
  47. } else {
  48. return 0, 2
  49. }
  50. }
  51. func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedArr []int64, seatNumber, name string, positionId, thispositionId, isTask int64) {
  52. wg := new(sync.WaitGroup)
  53. ch := make(chan bool, 20)
  54. for _, v := range distributedArr {
  55. wg.Add(1)
  56. ch <- true
  57. go func(v int64) {
  58. defer func() {
  59. wg.Done()
  60. <-ch
  61. }()
  62. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, "", "")
  63. nowTime := time.Now().Format(date.Date_Full_Layout)
  64. if clueData != nil && len(*clueData) > 0 {
  65. isAssign := common.IntAll((*clueData)["is_assign"])
  66. clueSeatNumber := common.ObjToString((*clueData)["seatNumber"])
  67. oldName := ""
  68. trailstatus := common.ObjToString((*clueData)["trailstatus"])
  69. if clueSeatNumber != "" {
  70. for _, s := range saleMap {
  71. if common.ObjToString(s["seatNumber"]) == clueSeatNumber {
  72. oldName = common.ObjToString(s["name"])
  73. }
  74. }
  75. }
  76. if isAssign == 1 {
  77. oldpositionId := common.Int64All((*clueData)["position_id"])
  78. updateClue := map[string]interface{}{
  79. "position_id": positionId,
  80. "seatNumber": seatNumber,
  81. "is_assign": 1,
  82. "updatetime": nowTime,
  83. "comeintime": nowTime,
  84. "comeinsource_private": 4,
  85. "level_open": nil,
  86. "clue_level": nil,
  87. "out_task_time": nil,
  88. "out_task_status": nil,
  89. "trail_time": nil,
  90. // "comeinsource_open": nil,
  91. }
  92. if isTask == int64(1) {
  93. updateClue["is_task"] = 1
  94. updateClue["task_time"] = nowTime
  95. updateClue["tasktime"] = nowTime
  96. updateClue["taskstatus"] = 0
  97. updateClue["tasksource"] = "主动分配客户"
  98. }
  99. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  100. // ok := JyBiTidb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": v}, map[string]interface{}{
  101. // "position_id": positionId,
  102. // "seatNumber": seatNumber,
  103. // "comeinsource": 4,
  104. // "comeintime": nowTime,
  105. // })
  106. if ok {
  107. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  108. "clue_id": v,
  109. "position_id": positionId,
  110. "change_field": "position_id",
  111. "change_type": "所属人变更",
  112. "old_value": oldName,
  113. "new_value": name,
  114. "createtime": nowTime,
  115. "BCPCID": common.GetRandom(32),
  116. "operator_id": thispositionId,
  117. })
  118. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  119. "clue_id": v,
  120. "position_id": oldpositionId,
  121. "change_field": "trailstatus",
  122. "change_type": "基本信息变更",
  123. "old_value": CodeTrail[trailstatus],
  124. "new_value": "流失",
  125. "createtime": nowTime,
  126. "BCPCID": common.GetRandom(32),
  127. "operator_id": thispositionId,
  128. })
  129. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  130. "clue_id": v,
  131. "position_id": positionId,
  132. "change_field": "trailstatus",
  133. "change_type": "基本信息变更",
  134. "old_value": CodeTrail[trailstatus],
  135. "new_value": "新增",
  136. "createtime": nowTime,
  137. "BCPCID": common.GetRandom(32),
  138. "operator_id": thispositionId,
  139. })
  140. } else {
  141. log.Println("私海修改失败 ", v, positionId, seatNumber)
  142. }
  143. } else {
  144. updateClue := map[string]interface{}{
  145. "position_id": positionId,
  146. "seatNumber": seatNumber,
  147. "is_assign": 1,
  148. "updatetime": nowTime,
  149. "comeintime": nowTime,
  150. "comeinsource_private": 4,
  151. "is_task": 0,
  152. "taskstatus": 0,
  153. "level_open": nil,
  154. "clue_level": nil,
  155. "out_task_time": nil,
  156. "out_task_status": nil,
  157. "trail_time": nil,
  158. "next_trail_time": nil,
  159. // "comeinsource_open": nil,
  160. }
  161. if trailstatus != "08" {
  162. updateClue["trailstatus"] = "01"
  163. }
  164. if isTask == int64(1) {
  165. updateClue["is_task"] = 1
  166. updateClue["task_time"] = nowTime
  167. updateClue["tasktime"] = nowTime
  168. updateClue["taskstatus"] = 0
  169. updateClue["tasksource"] = "主动分配客户"
  170. }
  171. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  172. // seaId := JyBiTidb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  173. // "clue_id": v,
  174. // "position_id": positionId,
  175. // "seatNumber": seatNumber,
  176. // "comeinsource": 4,
  177. // "comeintime": nowTime,
  178. // "is_task": 0,
  179. // // "task_time": nowTime,
  180. // // "tasktime": nowTime,
  181. // "taskstatus": 0,
  182. // // "tasksource": "线索批量分配",
  183. // })
  184. if ok {
  185. // JyBiTidb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": v})
  186. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  187. "clue_id": v,
  188. "position_id": positionId,
  189. "change_field": "position_id",
  190. "change_type": "所属人变更",
  191. "old_value": "/",
  192. "new_value": name,
  193. "createtime": nowTime,
  194. "BCPCID": common.GetRandom(32),
  195. "operator_id": thispositionId,
  196. })
  197. if trailstatus != "08" {
  198. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  199. "clue_id": v,
  200. "position_id": positionId,
  201. "change_field": "trailstatus",
  202. "change_type": "基本信息变更",
  203. "old_value": "商机线索",
  204. "new_value": "新增",
  205. "createtime": nowTime,
  206. "BCPCID": common.GetRandom(32),
  207. "operator_id": thispositionId,
  208. })
  209. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  210. "clue_id": v,
  211. "position_id": positionId,
  212. "change_field": "trailstatus",
  213. "change_type": "基本信息变更",
  214. "old_value": CodeTrail[trailstatus],
  215. "new_value": "商机线索",
  216. "createtime": nowTime,
  217. "BCPCID": common.GetRandom(32),
  218. "operator_id": thispositionId,
  219. })
  220. }
  221. } else {
  222. log.Println("私海插入失败 ", v, positionId, seatNumber)
  223. }
  224. }
  225. }
  226. }(v)
  227. }
  228. wg.Wait()
  229. }
  230. func DrawClue(this *biservice.DrawClueReq) *biservice.AddProjectResp {
  231. count, status := DrawClueSync(this)
  232. log.Println("领取数量 ", count)
  233. return &biservice.AddProjectResp{
  234. ErrorCode: 0,
  235. Data: &biservice.AddProject{
  236. Status: int64(status),
  237. Count: int64(count),
  238. },
  239. }
  240. }
  241. func DrawClueSync(this *biservice.DrawClueReq) (int, int) {
  242. if DataLock.TryLock() {
  243. defer DataLock.Unlock()
  244. count1 := JyBiTidb.Count("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 1, "is_assign": 0})
  245. count2 := JyBiTidb.Count("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 2, "is_assign": 0})
  246. counts1, counts2, counts3 := int64(0), int64(0), int64(0)
  247. counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 5))
  248. if this.Count-counts1 == 0 {
  249. counts2 = 0
  250. counts3 = 0
  251. } else {
  252. counts2 = int64(math.Ceil(float64(this.Count) / float64(10) * 4))
  253. if this.Count-counts1-counts2 == 0 {
  254. counts3 = 0
  255. } else {
  256. counts3 = this.Count - counts1 - counts2
  257. }
  258. }
  259. if counts1 > count1 {
  260. counts2 += counts1 - count1
  261. counts1 = count1
  262. }
  263. if counts2 > count2 {
  264. counts3 += counts2 - count2
  265. counts2 = count2
  266. }
  267. log.Println(count1, count2)
  268. log.Println(counts1, counts2, counts3)
  269. return DrawClues(this.PositionId, counts1, counts2, counts3), 1
  270. } else {
  271. return 0, 2
  272. }
  273. }
  274. func DrawClues(positionId, count1, count2, count3 int64) int {
  275. data1, data2, data3, drawCount := &[]map[string]interface{}{}, &[]map[string]interface{}{}, &[]map[string]interface{}{}, 0
  276. if count1 > 0 {
  277. // data1 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 1, "is_assign": 0}, "", "", 0, int(count1))
  278. data1 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  279. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  280. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 1, count1)
  281. }
  282. if count2 > 0 {
  283. // data2 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 2, "is_assign": 0}, "", "", 0, int(count2))
  284. data2 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  285. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  286. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 2, count2)
  287. }
  288. if count3 > 0 {
  289. // data3 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 3, "is_assign": 0}, "", "", 0, int(count3))
  290. data3 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  291. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  292. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 3, count3)
  293. }
  294. nowTime := time.Now().Format("2006-01-02 15:04:05")
  295. seatNumber, name := getSeatNumber(positionId)
  296. if data1 != nil && len(*data1) > 0 {
  297. batchDraw(*data1, nowTime, seatNumber, name, positionId)
  298. drawCount += len(*data1)
  299. }
  300. if data2 != nil && len(*data2) > 0 {
  301. batchDraw(*data2, nowTime, seatNumber, name, positionId)
  302. drawCount += len(*data2)
  303. }
  304. if data3 != nil && len(*data3) > 0 {
  305. batchDraw(*data3, nowTime, seatNumber, name, positionId)
  306. drawCount += len(*data3)
  307. }
  308. return drawCount
  309. }
  310. func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64) {
  311. wg := new(sync.WaitGroup)
  312. ch := make(chan bool, 20)
  313. for _, v := range data {
  314. wg.Add(1)
  315. ch <- true
  316. go func(v map[string]interface{}) {
  317. defer func() {
  318. wg.Done()
  319. <-ch
  320. }()
  321. clueId := common.Int64All(v["id"])
  322. trailstatus := common.ObjToString(v["trailstatus"])
  323. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  324. updateClue := map[string]interface{}{
  325. "position_id": positionId,
  326. "seatNumber": seatNumber,
  327. "is_assign": 1,
  328. "updatetime": nowTime,
  329. "comeintime": nowTime,
  330. "comeinsource_private": 3,
  331. "is_task": 1,
  332. "task_time": nowTime,
  333. "tasktime": nowTime,
  334. "taskstatus": 0,
  335. "tasksource": "领取公海线索",
  336. "level_open": nil,
  337. "clue_level": nil,
  338. // "comeinsource_open": nil,
  339. }
  340. if trailstatus != "08" {
  341. updateClue["trailstatus"] = "01"
  342. }
  343. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  344. // ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  345. // seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  346. // "clue_id": clueId,
  347. // "seatNumber": seatNumber,
  348. // "position_id": positionId,
  349. // "comeintime": nowTime,
  350. // "comeinsource": 3,
  351. // "is_task": 1,
  352. // "task_time": nowTime,
  353. // "tasktime": nowTime,
  354. // "taskstatus": 0,
  355. // "tasksource": "领取公海线索",
  356. // })
  357. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  358. "clue_id": clueId,
  359. "position_id": positionId,
  360. "change_field": "position_id",
  361. "change_type": "所属人变更",
  362. "old_value": "/",
  363. "new_value": name,
  364. "createtime": nowTime,
  365. "BCPCID": common.GetRandom(32),
  366. "operator_id": positionId,
  367. })
  368. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  369. "clue_id": clueId,
  370. "position_id": positionId,
  371. "change_type": "领取公海线索",
  372. "createtime": nowTime,
  373. "BCPCID": common.GetRandom(32),
  374. "operator_id": positionId,
  375. })
  376. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  377. "clue_id": clueId,
  378. "position_id": positionId,
  379. "change_type": "加入任务车",
  380. "new_value": "领取公海线索",
  381. "createtime": nowTime,
  382. "BCPCID": common.GetRandom(32),
  383. "operator_id": positionId,
  384. })
  385. recordId3, recordId4 := int64(0), int64(0)
  386. if trailstatus != "08" {
  387. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  388. "clue_id": clueId,
  389. "position_id": positionId,
  390. "change_field": "trailstatus",
  391. "change_type": "基本信息变更",
  392. "old_value": "商机线索",
  393. "new_value": "新增",
  394. "createtime": nowTime,
  395. "BCPCID": common.GetRandom(32),
  396. "operator_id": positionId,
  397. })
  398. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  399. "clue_id": clueId,
  400. "position_id": positionId,
  401. "change_field": "trailstatus",
  402. "change_type": "基本信息变更",
  403. "old_value": CodeTrail[trailstatus],
  404. "new_value": "商机线索",
  405. "createtime": nowTime,
  406. "BCPCID": common.GetRandom(32),
  407. "operator_id": positionId,
  408. })
  409. }
  410. return ok1 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  411. }) {
  412. log.Println("领取线索成功")
  413. } else {
  414. log.Println("领取线索失败")
  415. }
  416. }(v)
  417. }
  418. wg.Wait()
  419. }
  420. func getSeatNumber(positionId int64) (seatNumber, name string) {
  421. positionData := JyTidb.FindOne("base_position", map[string]interface{}{"id": positionId}, "", "")
  422. if positionData != nil && len(*positionData) > 0 {
  423. userId := common.Int64All((*positionData)["user_id"])
  424. if userId > 0 {
  425. userData, ok := Mgo.FindOne("user", map[string]interface{}{"base_user_id": userId})
  426. if ok && userData != nil && len(*userData) > 0 {
  427. s_phone := common.ObjToString((*userData)["s_phone"])
  428. if s_phone == "" {
  429. s_phone = common.ObjToString((*userData)["s_m_phone"])
  430. }
  431. saleData := JyBiTidb.FindOne("jy_salesperson_info", map[string]interface{}{"phone": s_phone}, "", "")
  432. if saleData != nil && len(*saleData) > 0 {
  433. seatNumber = common.ObjToString((*saleData)["seatNumber"])
  434. name = common.ObjToString((*saleData)["name"])
  435. }
  436. }
  437. }
  438. }
  439. return
  440. }
  441. func ClueImport(this *biservice.ClueImportReq) *biservice.ClueImportResp {
  442. result, status := ClueImportSync(this)
  443. return &biservice.ClueImportResp{
  444. ErrorCode: 0,
  445. Data: &biservice.ClueImport{
  446. Status: int64(status),
  447. Result: result,
  448. },
  449. }
  450. }
  451. func ClueImportSync(this *biservice.ClueImportReq) (string, int) {
  452. if ClueImportLock.TryLock() {
  453. defer ClueImportLock.Unlock()
  454. result, status, dataArr, countOpen, countAdd, countPrivate := "", 1, []map[string]interface{}{}, 0, 0, 0
  455. phoneRegexp := regexp.MustCompile(`^1[0-9]{10}$`)
  456. data := JyBiTidb.Find("dwd_f_crm_bulk_import", map[string]interface{}{"PCBH": this.Pcbh}, "", "", -1, -1)
  457. if data != nil && len(*data) > 0 {
  458. for k, v := range *data {
  459. companyName := common.ObjToString(v["GSMC"])
  460. name := common.ObjToString(v["XM"])
  461. position := common.ObjToString(v["ZW"])
  462. phone := common.ObjToString(v["LXFS"])
  463. clueSource := common.ObjToString(v["XSXSLY"])
  464. if companyName == "" {
  465. companyName = phone
  466. }
  467. isOK, dataMap := false, map[string]interface{}{
  468. "cluename": companyName,
  469. "name": name,
  470. "phone": phone,
  471. "position": position,
  472. }
  473. if phone == "" {
  474. result += "第" + fmt.Sprint(k+2) + "行,“联系方式为空”\n"
  475. } else {
  476. if !phoneRegexp.MatchString(phone) {
  477. result += "第" + fmt.Sprint(k+2) + "行,“联系方式格式有误”\n"
  478. } else {
  479. userData := JyBiTidb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"phone": phone}, "", "")
  480. if userData != nil && len(*userData) > 0 {
  481. source := common.IntAll((*userData)["source"])
  482. userId := common.ObjToString((*userData)["userid"])
  483. uId := common.ObjToString((*userData)["uid"])
  484. if source == 5 {
  485. result += "第" + fmt.Sprint(k+2) + "行,“该线索已归属域外”\n"
  486. } else {
  487. isOK = true
  488. dataMap["userId"] = userId
  489. dataMap["uId"] = uId
  490. }
  491. }
  492. }
  493. }
  494. if clueSource == "" {
  495. result += "第" + fmt.Sprint(k+2) + "行,“销售线索来源为空”\n"
  496. } else {
  497. sourceData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": clueSource}, "", "")
  498. if sourceData == nil || len(*sourceData) == 0 {
  499. result += "第" + fmt.Sprint(k+2) + "行,“销售线索来源不存在”\n"
  500. } else {
  501. isOK = true
  502. code := common.ObjToString((*sourceData)["code"])
  503. pcode := common.ObjToString((*sourceData)["pcode"])
  504. dataMap["top_cluetype"] = pcode
  505. dataMap["sub_cluetype"] = code
  506. }
  507. }
  508. if isOK {
  509. dataArr = append(dataArr, dataMap)
  510. }
  511. }
  512. nowTime := time.Now().Format(date.Date_Full_Layout)
  513. if result != "" {
  514. status = -1
  515. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  516. "DRJSSJ": nowTime, //结束时间
  517. "SBYY": result, //失败原因
  518. "DRZT": "导入失败",
  519. })
  520. } else {
  521. for _, v := range dataArr {
  522. phone := common.ObjToString(v["phone"])
  523. cluename := common.ObjToString(v["cluename"])
  524. name := common.ObjToString(v["name"])
  525. position := common.ObjToString(v["position"])
  526. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  527. if clueData == nil || len(*clueData) == 0 {
  528. countAdd++
  529. clueId := JyBiTidb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  530. "userid": v["userId"],
  531. "uid": v["uId"],
  532. "is_assign": 0,
  533. "comeintime": nowTime,
  534. "createtime": nowTime,
  535. "updatetime": nowTime,
  536. "cluename": cluename,
  537. "top_cluetype": v["top_cluetype"],
  538. "sub_cluetype": v["sub_cluetype"],
  539. "trailstatus": "01",
  540. "name": name,
  541. "phone": phone,
  542. "position": position,
  543. "batch_import": this.Pcbh,
  544. })
  545. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  546. "clue_id": clueId,
  547. "position_id": -1,
  548. "change_type": "创建线索",
  549. "new_value": "线索导入创建",
  550. "createtime": nowTime,
  551. "BCPCID": common.GetRandom(32),
  552. "operator_id": this.PositionId,
  553. })
  554. if JyBiTidb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  555. JyBiTidb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  556. "status": 1,
  557. "is_delete": 1,
  558. "createtime": nowTime,
  559. "updatetime": nowTime,
  560. "phone": phone,
  561. "baseinfo_id": v["uId"],
  562. })
  563. }
  564. } else {
  565. is_assign := common.IntAll((*clueData)["is_assign"])
  566. clueId := common.Int64All((*clueData)["id"])
  567. batch_import := common.ObjToString((*clueData)["batch_import"])
  568. old_cluename := common.ObjToString((*clueData)["cluename"])
  569. old_name := common.ObjToString((*clueData)["name"])
  570. old_position := common.ObjToString((*clueData)["position"])
  571. positionId := common.ObjToString((*clueData)["position_id"])
  572. batch_imports := ""
  573. if is_assign == 0 {
  574. countOpen++
  575. if batch_import != "" {
  576. batch_imports = batch_import + "," + this.Pcbh
  577. } else {
  578. batch_imports = this.Pcbh
  579. }
  580. updateData := map[string]interface{}{
  581. "batch_import": batch_imports,
  582. "updatetime": nowTime,
  583. "top_cluetype": v["top_cluetype"],
  584. "sub_cluetype": v["sub_cluetype"],
  585. }
  586. if cluename != "" && cluename != old_cluename {
  587. updateData["cluename"] = cluename
  588. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  589. "clue_id": clueId,
  590. "position_id": -1,
  591. "change_field": "cluename",
  592. "change_type": "基本信息变更",
  593. "old_value": old_cluename,
  594. "new_value": cluename,
  595. "createtime": nowTime,
  596. "BCPCID": common.GetRandom(32),
  597. "operator_id": this.PositionId,
  598. })
  599. }
  600. if name != "" && name != old_name {
  601. updateData["name"] = name
  602. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  603. "clue_id": clueId,
  604. "position_id": -1,
  605. "change_field": "name",
  606. "change_type": "基本信息变更",
  607. "old_value": old_name,
  608. "new_value": name,
  609. "createtime": nowTime,
  610. "BCPCID": common.GetRandom(32),
  611. "operator_id": this.PositionId,
  612. })
  613. }
  614. if position != "" && position != old_position {
  615. updateData["position"] = position
  616. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  617. "clue_id": clueId,
  618. "position_id": -1,
  619. "change_field": "position",
  620. "change_type": "基本信息变更",
  621. "old_value": old_position,
  622. "new_value": position,
  623. "createtime": nowTime,
  624. "BCPCID": common.GetRandom(32),
  625. "operator_id": this.PositionId,
  626. })
  627. }
  628. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  629. } else if is_assign == 1 {
  630. countPrivate++
  631. updateData := map[string]interface{}{
  632. "batch_import": batch_imports,
  633. "updatetime": nowTime,
  634. "top_cluetype": v["top_cluetype"],
  635. "sub_cluetype": v["sub_cluetype"],
  636. }
  637. updateData["is_task"] = 1
  638. updateData["task_time"] = nowTime
  639. updateData["tasktime"] = nowTime
  640. updateData["taskstatus"] = 0
  641. updateData["tasksource"] = "线索来源更新客户"
  642. if cluename != "" && cluename != old_cluename {
  643. updateData["cluename"] = cluename
  644. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  645. "clue_id": clueId,
  646. "position_id": positionId,
  647. "change_field": "cluename",
  648. "change_type": "基本信息变更",
  649. "old_value": old_cluename,
  650. "new_value": cluename,
  651. "createtime": nowTime,
  652. "BCPCID": common.GetRandom(32),
  653. "operator_id": this.PositionId,
  654. })
  655. }
  656. if name != "" && name != old_name {
  657. updateData["name"] = name
  658. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  659. "clue_id": clueId,
  660. "position_id": positionId,
  661. "change_field": "name",
  662. "change_type": "基本信息变更",
  663. "old_value": old_name,
  664. "new_value": name,
  665. "createtime": nowTime,
  666. "BCPCID": common.GetRandom(32),
  667. "operator_id": this.PositionId,
  668. })
  669. }
  670. if position != "" && position != old_position {
  671. updateData["position"] = position
  672. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  673. "clue_id": clueId,
  674. "position_id": positionId,
  675. "change_field": "position",
  676. "change_type": "基本信息变更",
  677. "old_value": old_position,
  678. "new_value": position,
  679. "createtime": nowTime,
  680. "BCPCID": common.GetRandom(32),
  681. "operator_id": this.PositionId,
  682. })
  683. }
  684. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  685. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  686. "clue_id": clueId,
  687. "position_id": positionId,
  688. "change_type": "加入任务车",
  689. "new_value": "线索来源更新客户",
  690. "createtime": nowTime,
  691. "BCPCID": common.GetRandom(32),
  692. "operator_id": this.PositionId,
  693. })
  694. }
  695. }
  696. }
  697. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  698. "DRJSSJ": nowTime, //结束时间
  699. "ZJXST": len(dataArr), //总计线索条数
  700. "ZFXSSHXST": countPrivate, //私海重复条数
  701. "ZFXSGHXST": countOpen, //公海重复条数
  702. "XZXST": countAdd, //新增条数
  703. "DRZT": "导入成功",
  704. })
  705. }
  706. }
  707. return result, status
  708. } else {
  709. return "有正在进行的导入任务", 2
  710. }
  711. }