clue.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "database/sql"
  4. "math"
  5. "time"
  6. common "app.yhyue.com/moapp/jybase/common"
  7. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  8. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  9. )
  10. func DrawClue(this *biservice.DrawClueReq) *biservice.AddProjectResp {
  11. count1 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 1})
  12. count2 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 2})
  13. // count3 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 3})
  14. counts1 := int64(math.Floor(float64(this.Count) / float64(10) * 2))
  15. counts2 := int64(math.Floor(float64(this.Count) / float64(10) * 4))
  16. counts3 := this.Count - counts1 - counts2
  17. if counts1 > count1 {
  18. counts2 += counts1 - count1
  19. }
  20. if counts2 > count2 {
  21. counts3 += counts2 - count2
  22. }
  23. DrawClues(this.PositionId, counts1, counts2, counts3)
  24. return &biservice.AddProjectResp{
  25. ErrorCode: 0,
  26. Data: &biservice.AddProject{
  27. Status: 1,
  28. },
  29. }
  30. }
  31. func DrawClues(positionId, count1, count2, count3 int64) {
  32. data1 := JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 1}, "", "", 0, int(count1))
  33. data2 := JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 2}, "", "", 0, int(count2))
  34. data3 := JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 3}, "", "", 0, int(count3))
  35. nowTime := time.Now().Format("2006-01-02 15:04:05")
  36. seatNumber, name := getSeatNumber(positionId)
  37. if data1 != nil && len(*data1) > 0 {
  38. for _, v := range *data1 {
  39. //update postionid and update record
  40. clueId := common.Int64All(v["clue_id"])
  41. JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  42. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  43. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"id": clueId})
  44. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  45. "clue_id": clueId,
  46. "seatNumber": seatNumber,
  47. "position_id": positionId,
  48. "comeintime": nowTime,
  49. "comeinsource": 3,
  50. "is_task": 0,
  51. })
  52. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  53. "clue_id": clueId,
  54. "position_id": positionId,
  55. "change_field": "position_id",
  56. "change_type": "所属人变更",
  57. "old_value": "/",
  58. "new_value": name,
  59. "createtime": nowTime,
  60. "BCPCID": common.GetRandom(32),
  61. "operator_id": positionId,
  62. })
  63. return ok1 && ok2 && seaId > 0 && recordId > 0
  64. })
  65. }
  66. }
  67. if data2 != nil && len(*data2) > 0 {
  68. for _, v := range *data2 {
  69. clueId := common.Int64All(v["clue_id"])
  70. JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  71. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  72. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"id": clueId})
  73. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  74. "clue_id": clueId,
  75. "seatNumber": seatNumber,
  76. "position_id": positionId,
  77. "comeintime": nowTime,
  78. "comeinsource": 3,
  79. "is_task": 0,
  80. })
  81. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  82. "clue_id": clueId,
  83. "position_id": positionId,
  84. "change_field": "position_id",
  85. "change_type": "所属人变更",
  86. "old_value": "/",
  87. "new_value": name,
  88. "createtime": nowTime,
  89. "BCPCID": common.GetRandom(32),
  90. "operator_id": positionId,
  91. })
  92. return ok1 && ok2 && seaId > 0 && recordId > 0
  93. })
  94. }
  95. }
  96. if data3 != nil && len(*data3) > 0 {
  97. for _, v := range *data3 {
  98. clueId := common.Int64All(v["clue_id"])
  99. JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  100. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  101. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"id": clueId})
  102. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  103. "clue_id": clueId,
  104. "seatNumber": seatNumber,
  105. "position_id": positionId,
  106. "comeintime": nowTime,
  107. "comeinsource": 3,
  108. "is_task": 0,
  109. })
  110. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  111. "clue_id": clueId,
  112. "position_id": positionId,
  113. "change_field": "position_id",
  114. "change_type": "所属人变更",
  115. "old_value": "/",
  116. "new_value": name,
  117. "createtime": nowTime,
  118. "BCPCID": common.GetRandom(32),
  119. "operator_id": positionId,
  120. })
  121. return ok1 && ok2 && seaId > 0 && recordId > 0
  122. })
  123. }
  124. }
  125. }
  126. func getSeatNumber(positionId int64) (seatNumber, name string) {
  127. positionData := JyTidb.FindOne("base_position", map[string]interface{}{"id": positionId}, "", "")
  128. if positionData != nil && len(*positionData) > 0 {
  129. userId := common.Int64All((*positionData)["user_id"])
  130. if userId > 0 {
  131. userData, ok := Mgo.FindOne("user", map[string]interface{}{"base_user_id": userId})
  132. if ok && userData != nil && len(*userData) > 0 {
  133. s_phone := common.ObjToString((*userData)["s_phone"])
  134. if s_phone == "" {
  135. s_phone = common.ObjToString((*userData)["s_m_phone"])
  136. }
  137. saleData := JyBiTidb.FindOne("jy_salesperson_info", map[string]interface{}{"phone": s_phone}, "", "")
  138. if saleData != nil && len(*saleData) > 0 {
  139. seatNumber = common.ObjToString((*saleData)["seatNumber"])
  140. name = common.ObjToString((*saleData)["name"])
  141. }
  142. }
  143. }
  144. }
  145. return
  146. }