clue.go 32 KB

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