clue.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  99. "clue_id": v,
  100. "position_id": positionId,
  101. "change_type": "加入任务车",
  102. "new_value": "主动分配客户",
  103. "createtime": nowTime,
  104. "BCPCID": common.GetRandom(32),
  105. "operator_id": thispositionId,
  106. })
  107. }
  108. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  109. // ok := JyBiTidb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": v}, map[string]interface{}{
  110. // "position_id": positionId,
  111. // "seatNumber": seatNumber,
  112. // "comeinsource": 4,
  113. // "comeintime": nowTime,
  114. // })
  115. if ok {
  116. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  117. "clue_id": v,
  118. "position_id": positionId,
  119. "change_field": "position_id",
  120. "change_type": "所属人变更",
  121. "old_value": oldName,
  122. "new_value": name,
  123. "createtime": nowTime,
  124. "BCPCID": common.GetRandom(32),
  125. "operator_id": thispositionId,
  126. })
  127. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  128. "clue_id": v,
  129. "position_id": oldpositionId,
  130. "change_field": "trailstatus",
  131. "change_type": "基本信息变更",
  132. "old_value": CodeTrail[trailstatus],
  133. "new_value": "流失",
  134. "createtime": nowTime,
  135. "BCPCID": common.GetRandom(32),
  136. "operator_id": thispositionId,
  137. })
  138. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  139. "clue_id": v,
  140. "position_id": positionId,
  141. "change_field": "trailstatus",
  142. "change_type": "基本信息变更",
  143. "old_value": CodeTrail[trailstatus],
  144. "new_value": "新增",
  145. "createtime": nowTime,
  146. "BCPCID": common.GetRandom(32),
  147. "operator_id": thispositionId,
  148. })
  149. } else {
  150. log.Println("私海修改失败 ", v, positionId, seatNumber)
  151. }
  152. } else {
  153. updateClue := map[string]interface{}{
  154. "position_id": positionId,
  155. "seatNumber": seatNumber,
  156. "is_assign": 1,
  157. "updatetime": nowTime,
  158. "comeintime": nowTime,
  159. "comeinsource_private": 4,
  160. "is_task": 0,
  161. "taskstatus": 0,
  162. "level_open": nil,
  163. "clue_level": nil,
  164. "out_task_time": nil,
  165. "out_task_status": nil,
  166. "trail_time": nil,
  167. "next_trail_time": nil,
  168. // "comeinsource_open": nil,
  169. }
  170. if trailstatus != "08" {
  171. updateClue["trailstatus"] = "01"
  172. }
  173. if isTask == int64(1) {
  174. updateClue["is_task"] = 1
  175. updateClue["task_time"] = nowTime
  176. updateClue["tasktime"] = nowTime
  177. updateClue["taskstatus"] = 0
  178. updateClue["tasksource"] = "主动分配客户"
  179. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  180. "clue_id": v,
  181. "position_id": positionId,
  182. "change_type": "加入任务车",
  183. "new_value": "主动分配客户",
  184. "createtime": nowTime,
  185. "BCPCID": common.GetRandom(32),
  186. "operator_id": thispositionId,
  187. })
  188. }
  189. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  190. // seaId := JyBiTidb.Insert("dwd_f_crm_private_sea", map[string]interface{}{
  191. // "clue_id": v,
  192. // "position_id": positionId,
  193. // "seatNumber": seatNumber,
  194. // "comeinsource": 4,
  195. // "comeintime": nowTime,
  196. // "is_task": 0,
  197. // // "task_time": nowTime,
  198. // // "tasktime": nowTime,
  199. // "taskstatus": 0,
  200. // // "tasksource": "线索批量分配",
  201. // })
  202. if ok {
  203. // JyBiTidb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": v})
  204. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  205. "clue_id": v,
  206. "position_id": positionId,
  207. "change_field": "position_id",
  208. "change_type": "所属人变更",
  209. "old_value": "/",
  210. "new_value": name,
  211. "createtime": nowTime,
  212. "BCPCID": common.GetRandom(32),
  213. "operator_id": thispositionId,
  214. })
  215. if trailstatus != "08" {
  216. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  217. "clue_id": v,
  218. "position_id": positionId,
  219. "change_field": "trailstatus",
  220. "change_type": "基本信息变更",
  221. "old_value": "商机线索",
  222. "new_value": "新增",
  223. "createtime": nowTime,
  224. "BCPCID": common.GetRandom(32),
  225. "operator_id": thispositionId,
  226. })
  227. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  228. "clue_id": v,
  229. "position_id": positionId,
  230. "change_field": "trailstatus",
  231. "change_type": "基本信息变更",
  232. "old_value": CodeTrail[trailstatus],
  233. "new_value": "商机线索",
  234. "createtime": nowTime,
  235. "BCPCID": common.GetRandom(32),
  236. "operator_id": thispositionId,
  237. })
  238. }
  239. } else {
  240. log.Println("私海插入失败 ", v, positionId, seatNumber)
  241. }
  242. }
  243. }
  244. }(v)
  245. }
  246. wg.Wait()
  247. }
  248. func DrawClue(this *biservice.DrawClueReq) *biservice.AddProjectResp {
  249. count, status := DrawClueSync(this)
  250. log.Println("领取数量 ", count)
  251. return &biservice.AddProjectResp{
  252. ErrorCode: 0,
  253. Data: &biservice.AddProject{
  254. Status: int64(status),
  255. Count: int64(count),
  256. },
  257. }
  258. }
  259. func DrawClueSync(this *biservice.DrawClueReq) (int, int) {
  260. if DataLock.TryLock() {
  261. defer DataLock.Unlock()
  262. count1 := JyBiTidb.Count("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 1, "is_assign": 0})
  263. count2 := JyBiTidb.Count("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 2, "is_assign": 0})
  264. counts1, counts2, counts3 := int64(0), int64(0), int64(0)
  265. counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 5))
  266. if this.Count-counts1 == 0 {
  267. counts2 = 0
  268. counts3 = 0
  269. } else {
  270. counts2 = int64(math.Ceil(float64(this.Count) / float64(10) * 4))
  271. if this.Count-counts1-counts2 == 0 {
  272. counts3 = 0
  273. } else {
  274. counts3 = this.Count - counts1 - counts2
  275. }
  276. }
  277. if counts1 > count1 {
  278. counts2 += counts1 - count1
  279. counts1 = count1
  280. }
  281. if counts2 > count2 {
  282. counts3 += counts2 - count2
  283. counts2 = count2
  284. }
  285. log.Println(count1, count2)
  286. log.Println(counts1, counts2, counts3)
  287. return DrawClues(this.PositionId, counts1, counts2, counts3), 1
  288. } else {
  289. return 0, 2
  290. }
  291. }
  292. func DrawClues(positionId, count1, count2, count3 int64) int {
  293. data1, data2, data3, drawCount := &[]map[string]interface{}{}, &[]map[string]interface{}{}, &[]map[string]interface{}{}, 0
  294. if count1 > 0 {
  295. // data1 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 1, "is_assign": 0}, "", "", 0, int(count1))
  296. data1 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  297. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  298. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 1, count1)
  299. }
  300. if count2 > 0 {
  301. // data2 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 2, "is_assign": 0}, "", "", 0, int(count2))
  302. data2 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  303. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  304. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 2, count2)
  305. }
  306. if count3 > 0 {
  307. // data3 = JyBiTidb.Find("dwd_f_crm_clue_info", map[string]interface{}{"level_open": 3, "is_assign": 0}, "", "", 0, int(count3))
  308. data3 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a
  309. LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
  310. WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" GROUP BY a.id ORDER BY ctime asc limit ?`, 3, count3)
  311. }
  312. nowTime := time.Now().Format("2006-01-02 15:04:05")
  313. seatNumber, name := getSeatNumber(positionId)
  314. if data1 != nil && len(*data1) > 0 {
  315. batchDraw(*data1, nowTime, seatNumber, name, positionId)
  316. drawCount += len(*data1)
  317. }
  318. if data2 != nil && len(*data2) > 0 {
  319. batchDraw(*data2, nowTime, seatNumber, name, positionId)
  320. drawCount += len(*data2)
  321. }
  322. if data3 != nil && len(*data3) > 0 {
  323. batchDraw(*data3, nowTime, seatNumber, name, positionId)
  324. drawCount += len(*data3)
  325. }
  326. return drawCount
  327. }
  328. func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64) {
  329. wg := new(sync.WaitGroup)
  330. ch := make(chan bool, 20)
  331. for _, v := range data {
  332. wg.Add(1)
  333. ch <- true
  334. go func(v map[string]interface{}) {
  335. defer func() {
  336. wg.Done()
  337. <-ch
  338. }()
  339. clueId := common.Int64All(v["id"])
  340. trailstatus := common.ObjToString(v["trailstatus"])
  341. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  342. updateClue := map[string]interface{}{
  343. "position_id": positionId,
  344. "seatNumber": seatNumber,
  345. "is_assign": 1,
  346. "updatetime": nowTime,
  347. "comeintime": nowTime,
  348. "comeinsource_private": 3,
  349. "is_task": 1,
  350. "task_time": nowTime,
  351. "tasktime": nowTime,
  352. "taskstatus": 0,
  353. "tasksource": "领取公海线索",
  354. "level_open": nil,
  355. "clue_level": nil,
  356. // "comeinsource_open": nil,
  357. }
  358. if trailstatus != "08" {
  359. updateClue["trailstatus"] = "01"
  360. }
  361. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  362. // ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  363. // seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  364. // "clue_id": clueId,
  365. // "seatNumber": seatNumber,
  366. // "position_id": positionId,
  367. // "comeintime": nowTime,
  368. // "comeinsource": 3,
  369. // "is_task": 1,
  370. // "task_time": nowTime,
  371. // "tasktime": nowTime,
  372. // "taskstatus": 0,
  373. // "tasksource": "领取公海线索",
  374. // })
  375. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  376. "clue_id": clueId,
  377. "position_id": positionId,
  378. "change_field": "position_id",
  379. "change_type": "所属人变更",
  380. "old_value": "/",
  381. "new_value": name,
  382. "createtime": nowTime,
  383. "BCPCID": common.GetRandom(32),
  384. "operator_id": positionId,
  385. })
  386. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  387. "clue_id": clueId,
  388. "position_id": positionId,
  389. "change_type": "领取公海线索",
  390. "createtime": nowTime,
  391. "BCPCID": common.GetRandom(32),
  392. "operator_id": positionId,
  393. })
  394. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  395. "clue_id": clueId,
  396. "position_id": positionId,
  397. "change_type": "加入任务车",
  398. "new_value": "领取公海线索",
  399. "createtime": nowTime,
  400. "BCPCID": common.GetRandom(32),
  401. "operator_id": positionId,
  402. })
  403. recordId3, recordId4 := int64(0), int64(0)
  404. if trailstatus != "08" {
  405. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  406. "clue_id": clueId,
  407. "position_id": positionId,
  408. "change_field": "trailstatus",
  409. "change_type": "基本信息变更",
  410. "old_value": "商机线索",
  411. "new_value": "新增",
  412. "createtime": nowTime,
  413. "BCPCID": common.GetRandom(32),
  414. "operator_id": positionId,
  415. })
  416. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  417. "clue_id": clueId,
  418. "position_id": positionId,
  419. "change_field": "trailstatus",
  420. "change_type": "基本信息变更",
  421. "old_value": CodeTrail[trailstatus],
  422. "new_value": "商机线索",
  423. "createtime": nowTime,
  424. "BCPCID": common.GetRandom(32),
  425. "operator_id": positionId,
  426. })
  427. }
  428. return ok1 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  429. }) {
  430. log.Println("领取线索成功")
  431. } else {
  432. log.Println("领取线索失败")
  433. }
  434. }(v)
  435. }
  436. wg.Wait()
  437. }
  438. func getSeatNumber(positionId int64) (seatNumber, name string) {
  439. positionData := JyTidb.FindOne("base_position", map[string]interface{}{"id": positionId}, "", "")
  440. if positionData != nil && len(*positionData) > 0 {
  441. userId := common.Int64All((*positionData)["user_id"])
  442. if userId > 0 {
  443. userData, ok := Mgo.FindOne("user", map[string]interface{}{"base_user_id": userId})
  444. if ok && userData != nil && len(*userData) > 0 {
  445. s_phone := common.ObjToString((*userData)["s_phone"])
  446. if s_phone == "" {
  447. s_phone = common.ObjToString((*userData)["s_m_phone"])
  448. }
  449. saleData := JyBiTidb.FindOne("jy_salesperson_info", map[string]interface{}{"phone": s_phone}, "", "")
  450. if saleData != nil && len(*saleData) > 0 {
  451. seatNumber = common.ObjToString((*saleData)["seatNumber"])
  452. name = common.ObjToString((*saleData)["name"])
  453. }
  454. }
  455. }
  456. }
  457. return
  458. }
  459. func ClueImport(this *biservice.ClueImportReq) *biservice.ClueImportResp {
  460. result, status := ClueImportSync(this)
  461. return &biservice.ClueImportResp{
  462. ErrorCode: 0,
  463. Data: &biservice.ClueImport{
  464. Status: int64(status),
  465. Result: result,
  466. },
  467. }
  468. }
  469. func ClueImportSync(this *biservice.ClueImportReq) (string, int) {
  470. if ClueImportLock.TryLock() {
  471. defer ClueImportLock.Unlock()
  472. result, status, dataArr, countOpen, countAdd, countPrivate := "", 1, []map[string]interface{}{}, 0, 0, 0
  473. phoneRegexp := regexp.MustCompile(`^1[0-9]{10}$`)
  474. nowTime, counts := time.Now().Format(date.Date_Full_Layout), 0
  475. data := JyBiTidb.Find("dwd_f_crm_bulk_import", map[string]interface{}{"PCBH": this.Pcbh}, "", "", -1, -1)
  476. if data != nil && len(*data) > 0 {
  477. for k, v := range *data {
  478. companyName := common.ObjToString(v["GSMC"])
  479. name := common.ObjToString(v["XM"])
  480. position := common.ObjToString(v["ZW"])
  481. phone := common.ObjToString(v["LXFS"])
  482. clueSource := common.ObjToString(v["XSXSLY"])
  483. if companyName == "" {
  484. companyName = phone
  485. }
  486. isOK, isOks, dataMap := false, true, map[string]interface{}{
  487. "cluename": companyName,
  488. "name": name,
  489. "phone": phone,
  490. "position": position,
  491. "clueSource": clueSource,
  492. }
  493. if phone == "" {
  494. isOks = false
  495. counts++
  496. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“联系方式为空”"
  497. } else {
  498. if !phoneRegexp.MatchString(phone) {
  499. if !isOks {
  500. result += ",“联系方式格式有误”"
  501. } else {
  502. counts++
  503. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“联系方式格式有误”"
  504. }
  505. isOks = false
  506. } else {
  507. userData := JyBiTidb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"phone": phone}, "", "")
  508. if userData != nil && len(*userData) > 0 {
  509. source := common.IntAll((*userData)["source"])
  510. userId := common.ObjToString((*userData)["userid"])
  511. uId := common.ObjToString((*userData)["uid"])
  512. if source == 5 {
  513. if !isOks {
  514. result += ",“该线索已归属域外”"
  515. } else {
  516. counts++
  517. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“该线索已归属域外”"
  518. }
  519. isOks = false
  520. } else {
  521. isOK = true
  522. dataMap["userId"] = userId
  523. dataMap["uId"] = uId
  524. }
  525. } else {
  526. dataMap["uId"] = common.GetRandom(32)
  527. dataMap["isOk"] = "0"
  528. }
  529. }
  530. }
  531. if clueSource == "" {
  532. if !isOks {
  533. result += ",“销售线索来源为空”"
  534. } else {
  535. counts++
  536. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“销售线索来源为空”"
  537. }
  538. isOks = false
  539. } else {
  540. sourceData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": clueSource}, "", "")
  541. if sourceData == nil || len(*sourceData) == 0 {
  542. if !isOks {
  543. result += ",“销售线索来源不存在”"
  544. } else {
  545. counts++
  546. result += fmt.Sprint(counts) + ".第" + fmt.Sprint(k+2) + "行,“销售线索来源不存在”"
  547. }
  548. isOks = false
  549. } else {
  550. isOK = true
  551. code := common.ObjToString((*sourceData)["code"])
  552. pcode := common.ObjToString((*sourceData)["pcode"])
  553. dataMap["top_cluetype"] = pcode
  554. dataMap["sub_cluetype"] = code
  555. }
  556. }
  557. if isOK {
  558. dataArr = append(dataArr, dataMap)
  559. }
  560. if !isOks {
  561. result += "\n"
  562. }
  563. }
  564. if result != "" {
  565. status = -1
  566. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  567. "DRJSSJ": nowTime, //结束时间
  568. "SBYY": result, //失败原因
  569. "DRZT": "导入失败",
  570. })
  571. } else {
  572. for _, v := range dataArr {
  573. phone := common.ObjToString(v["phone"])
  574. cluename := common.ObjToString(v["cluename"])
  575. name := common.ObjToString(v["name"])
  576. position := common.ObjToString(v["position"])
  577. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
  578. if clueData == nil || len(*clueData) == 0 {
  579. countAdd++
  580. if v["isOk"] != nil {
  581. JyBiTidb.Insert("dwd_f_userbase_baseinfo", map[string]interface{}{
  582. "userid": v["userId"],
  583. "uid": v["uId"],
  584. "name": name,
  585. "phone": phone,
  586. "source": 4,
  587. "company_name": cluename,
  588. "status": 3,
  589. "createtime": nowTime,
  590. "updatetime": nowTime,
  591. })
  592. }
  593. clueId := JyBiTidb.Insert("dwd_f_crm_clue_info", map[string]interface{}{
  594. "userid": v["userId"],
  595. "uid": v["uId"],
  596. "is_assign": 0,
  597. "comeintime": nowTime,
  598. "createtime": nowTime,
  599. "updatetime": nowTime,
  600. "cluename": cluename,
  601. "top_cluetype": v["top_cluetype"],
  602. "sub_cluetype": v["sub_cluetype"],
  603. "trailstatus": "01",
  604. "name": name,
  605. "phone": phone,
  606. "position": position,
  607. "batch_import": this.Pcbh,
  608. "comeintime_open": nowTime,
  609. "comeinsource_open": 1,
  610. })
  611. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  612. "clue_id": clueId,
  613. "position_id": -1,
  614. "change_type": "创建线索",
  615. "new_value": "线索导入创建",
  616. "createtime": nowTime,
  617. "BCPCID": common.GetRandom(32),
  618. "operator_id": this.PositionId,
  619. })
  620. if JyBiTidb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
  621. JyBiTidb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
  622. "status": 1,
  623. "is_delete": 1,
  624. "createtime": nowTime,
  625. "updatetime": nowTime,
  626. "phone": phone,
  627. "baseinfo_id": v["uId"],
  628. })
  629. }
  630. } else {
  631. is_assign := common.IntAll((*clueData)["is_assign"])
  632. clueId := common.Int64All((*clueData)["id"])
  633. batch_import := common.ObjToString((*clueData)["batch_import"])
  634. old_cluename := common.ObjToString((*clueData)["cluename"])
  635. old_name := common.ObjToString((*clueData)["name"])
  636. old_position := common.ObjToString((*clueData)["position"])
  637. old_top_cluetype := common.ObjToString((*clueData)["top_cluetype"])
  638. old_sub_cluetype := common.ObjToString((*clueData)["sub_cluetype"])
  639. top_cluetype := common.ObjToString(v["top_cluetype"])
  640. sub_cluetype := common.ObjToString(v["sub_cluetype"])
  641. old_topname, old_subname := "", ""
  642. if old_top_cluetype != "" {
  643. pcodeData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
  644. if pcodeData != nil && len(*pcodeData) > 0 {
  645. old_topname = common.ObjToString((*pcodeData)["name"])
  646. }
  647. }
  648. if old_sub_cluetype != "" {
  649. pcodeData := JyBiTidb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
  650. if pcodeData != nil && len(*pcodeData) > 0 {
  651. old_subname = common.ObjToString((*pcodeData)["name"])
  652. }
  653. }
  654. positionId := common.Int64All((*clueData)["position_id"])
  655. batch_imports := ""
  656. if is_assign == 0 {
  657. countOpen++
  658. if batch_import != "" {
  659. batch_imports = batch_import + "," + this.Pcbh
  660. } else {
  661. batch_imports = this.Pcbh
  662. }
  663. updateData := map[string]interface{}{
  664. "batch_import": batch_imports,
  665. "updatetime": nowTime,
  666. "top_cluetype": top_cluetype,
  667. "sub_cluetype": sub_cluetype,
  668. }
  669. if cluename != "" && cluename != old_cluename {
  670. updateData["cluename"] = cluename
  671. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  672. "clue_id": clueId,
  673. "position_id": -1,
  674. "change_field": "cluename",
  675. "change_type": "基本信息变更",
  676. "old_value": common.If(old_cluename == "", "/", old_cluename),
  677. "new_value": cluename,
  678. "createtime": nowTime,
  679. "BCPCID": common.GetRandom(32),
  680. "operator_id": this.PositionId,
  681. })
  682. }
  683. if name != "" && name != old_name {
  684. updateData["name"] = name
  685. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  686. "clue_id": clueId,
  687. "position_id": -1,
  688. "change_field": "name",
  689. "change_type": "基本信息变更",
  690. "old_value": common.If(old_name == "", "/", old_name),
  691. "new_value": name,
  692. "createtime": nowTime,
  693. "BCPCID": common.GetRandom(32),
  694. "operator_id": this.PositionId,
  695. })
  696. }
  697. if position != "" && position != old_position {
  698. updateData["position"] = position
  699. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  700. "clue_id": clueId,
  701. "position_id": -1,
  702. "change_field": "position",
  703. "change_type": "基本信息变更",
  704. "old_value": common.If(old_position == "", "/", old_position),
  705. "new_value": position,
  706. "createtime": nowTime,
  707. "BCPCID": common.GetRandom(32),
  708. "operator_id": this.PositionId,
  709. })
  710. }
  711. if top_cluetype != "" && top_cluetype != old_top_cluetype {
  712. updateData["position"] = position
  713. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  714. "clue_id": clueId,
  715. "position_id": -1,
  716. "change_field": "top_cluetype",
  717. "change_type": "基本信息变更",
  718. "old_value": common.If(old_topname == "", "/", old_topname),
  719. "new_value": "批量导入线索",
  720. "createtime": nowTime,
  721. "BCPCID": common.GetRandom(32),
  722. "operator_id": this.PositionId,
  723. })
  724. }
  725. if sub_cluetype != "" && sub_cluetype != old_sub_cluetype {
  726. updateData["position"] = position
  727. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  728. "clue_id": clueId,
  729. "position_id": -1,
  730. "change_field": "sub_cluetype",
  731. "change_type": "基本信息变更",
  732. "old_value": common.If(old_subname == "", "/", old_subname),
  733. "new_value": v["clueSource"],
  734. "createtime": nowTime,
  735. "BCPCID": common.GetRandom(32),
  736. "operator_id": this.PositionId,
  737. })
  738. }
  739. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  740. } else if is_assign == 1 {
  741. countPrivate++
  742. if batch_import != "" {
  743. batch_imports = batch_import + "," + this.Pcbh
  744. } else {
  745. batch_imports = this.Pcbh
  746. }
  747. updateData := map[string]interface{}{
  748. "batch_import": batch_imports,
  749. "updatetime": nowTime,
  750. "top_cluetype": v["top_cluetype"],
  751. "sub_cluetype": v["sub_cluetype"],
  752. }
  753. updateData["is_task"] = 1
  754. updateData["task_time"] = nowTime
  755. updateData["tasktime"] = nowTime
  756. updateData["taskstatus"] = 0
  757. updateData["tasksource"] = "线索来源更新客户"
  758. if cluename != "" && cluename != old_cluename {
  759. updateData["cluename"] = cluename
  760. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  761. "clue_id": clueId,
  762. "position_id": positionId,
  763. "change_field": "cluename",
  764. "change_type": "基本信息变更",
  765. "old_value": common.If(old_cluename == "", "/", old_cluename),
  766. "new_value": cluename,
  767. "createtime": nowTime,
  768. "BCPCID": common.GetRandom(32),
  769. "operator_id": this.PositionId,
  770. })
  771. }
  772. if name != "" && name != old_name {
  773. updateData["name"] = name
  774. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  775. "clue_id": clueId,
  776. "position_id": positionId,
  777. "change_field": "name",
  778. "change_type": "基本信息变更",
  779. "old_value": common.If(old_name == "", "/", old_name),
  780. "new_value": name,
  781. "createtime": nowTime,
  782. "BCPCID": common.GetRandom(32),
  783. "operator_id": this.PositionId,
  784. })
  785. }
  786. if position != "" && position != old_position {
  787. updateData["position"] = position
  788. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  789. "clue_id": clueId,
  790. "position_id": positionId,
  791. "change_field": "position",
  792. "change_type": "基本信息变更",
  793. "old_value": common.If(old_position == "", "/", old_position),
  794. "new_value": position,
  795. "createtime": nowTime,
  796. "BCPCID": common.GetRandom(32),
  797. "operator_id": this.PositionId,
  798. })
  799. }
  800. if top_cluetype != "" && top_cluetype != old_top_cluetype {
  801. updateData["position"] = position
  802. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  803. "clue_id": clueId,
  804. "position_id": positionId,
  805. "change_field": "top_cluetype",
  806. "change_type": "基本信息变更",
  807. "old_value": common.If(old_topname == "", "/", old_topname),
  808. "new_value": "批量导入线索",
  809. "createtime": nowTime,
  810. "BCPCID": common.GetRandom(32),
  811. "operator_id": this.PositionId,
  812. })
  813. }
  814. if sub_cluetype != "" && sub_cluetype != old_sub_cluetype {
  815. updateData["position"] = position
  816. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  817. "clue_id": clueId,
  818. "position_id": positionId,
  819. "change_field": "sub_cluetype",
  820. "change_type": "基本信息变更",
  821. "old_value": common.If(old_subname == "", "/", old_subname),
  822. "new_value": v["clueSource"],
  823. "createtime": nowTime,
  824. "BCPCID": common.GetRandom(32),
  825. "operator_id": this.PositionId,
  826. })
  827. }
  828. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  829. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  830. "clue_id": clueId,
  831. "position_id": positionId,
  832. "change_type": "加入任务车",
  833. "new_value": "线索来源更新客户",
  834. "createtime": nowTime,
  835. "BCPCID": common.GetRandom(32),
  836. "operator_id": this.PositionId,
  837. })
  838. }
  839. }
  840. }
  841. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  842. "DRJSSJ": nowTime, //结束时间
  843. "ZJXST": len(dataArr), //总计线索条数
  844. "ZFXSSHXST": countPrivate, //私海重复条数
  845. "ZFXSGHXST": countOpen, //公海重复条数
  846. "XZXST": countAdd, //新增条数
  847. "DRZT": "导入成功",
  848. })
  849. }
  850. }
  851. return result, status
  852. } else {
  853. JyBiTidb.Update("dwd_f_crm_bulk_import_record", map[string]interface{}{"PCBH": this.Pcbh}, map[string]interface{}{
  854. "DRJSSJ": time.Now().Format(date.Date_Full_Layout), //结束时间
  855. "SBYY": "有正在进行的导入任务", //失败原因
  856. "DRZT": "导入失败",
  857. })
  858. return "有正在进行的导入任务", 2
  859. }
  860. }