clue.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package service
  2. import (
  3. "database/sql"
  4. "log"
  5. "math"
  6. "time"
  7. common "app.yhyue.com/moapp/jybase/common"
  8. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  9. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  10. )
  11. func DrawClue(this *biservice.DrawClueReq) *biservice.AddProjectResp {
  12. count1 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 1})
  13. count2 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 2})
  14. // count3 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 3})
  15. counts1, counts2, counts3 := int64(0), int64(0), int64(0)
  16. counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 2))
  17. if this.Count-counts1 == 0 {
  18. counts2 = 0
  19. counts3 = 0
  20. } else {
  21. counts2 = int64(math.Ceil(float64(this.Count) / float64(10) * 4))
  22. if this.Count-counts1-counts2 == 0 {
  23. counts3 = 0
  24. } else {
  25. counts3 = this.Count - counts1 - counts2
  26. }
  27. }
  28. if counts1 > count1 {
  29. counts2 += counts1 - count1
  30. }
  31. if counts2 > count2 {
  32. counts3 += counts2 - count2
  33. }
  34. log.Println(count1, count2)
  35. log.Println(counts1, counts2, counts3)
  36. DrawClues(this.PositionId, counts1, counts2, counts3)
  37. return &biservice.AddProjectResp{
  38. ErrorCode: 0,
  39. Data: &biservice.AddProject{
  40. Status: 1,
  41. },
  42. }
  43. }
  44. func DrawClues(positionId, count1, count2, count3 int64) {
  45. data1, data2, data3 := &[]map[string]interface{}{}, &[]map[string]interface{}{}, &[]map[string]interface{}{}
  46. if count1 > 0 {
  47. data1 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 1}, "", "", 0, int(count1))
  48. }
  49. if count2 > 0 {
  50. data2 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 2}, "", "", 0, int(count2))
  51. }
  52. if count3 > 0 {
  53. data3 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 3}, "", "", 0, int(count3))
  54. }
  55. nowTime := time.Now().Format("2006-01-02 15:04:05")
  56. seatNumber, name := getSeatNumber(positionId)
  57. if data1 != nil && len(*data1) > 0 {
  58. for _, v := range *data1 {
  59. //update postionid and update record
  60. clueId := common.Int64All(v["clue_id"])
  61. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  62. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  63. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  64. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  65. "clue_id": clueId,
  66. "seatNumber": seatNumber,
  67. "position_id": positionId,
  68. "comeintime": nowTime,
  69. "comeinsource": 3,
  70. "is_task": 1,
  71. "task_time": nowTime,
  72. "tasktime": nowTime,
  73. "taskstatus": 0,
  74. "tasksource": "领取公海线索",
  75. })
  76. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  77. "clue_id": clueId,
  78. "position_id": positionId,
  79. "change_field": "position_id",
  80. "change_type": "所属人变更",
  81. "old_value": "/",
  82. "new_value": name,
  83. "createtime": nowTime,
  84. "BCPCID": common.GetRandom(32),
  85. "operator_id": positionId,
  86. })
  87. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  88. "clue_id": clueId,
  89. "position_id": positionId,
  90. "change_type": "领取公海线索",
  91. "createtime": nowTime,
  92. "BCPCID": common.GetRandom(32),
  93. "operator_id": positionId,
  94. })
  95. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  96. "clue_id": clueId,
  97. "position_id": positionId,
  98. "change_type": "加入任务车",
  99. "new_value": "领取公海线索",
  100. "createtime": nowTime,
  101. "BCPCID": common.GetRandom(32),
  102. "operator_id": positionId,
  103. })
  104. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0
  105. }) {
  106. log.Println("领取线索1成功")
  107. } else {
  108. log.Println("领取线索1失败")
  109. }
  110. }
  111. }
  112. if data2 != nil && len(*data2) > 0 {
  113. for _, v := range *data2 {
  114. clueId := common.Int64All(v["clue_id"])
  115. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  116. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  117. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  118. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  119. "clue_id": clueId,
  120. "seatNumber": seatNumber,
  121. "position_id": positionId,
  122. "comeintime": nowTime,
  123. "comeinsource": 3,
  124. "is_task": 0,
  125. })
  126. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  127. "clue_id": clueId,
  128. "position_id": positionId,
  129. "change_field": "position_id",
  130. "change_type": "所属人变更",
  131. "old_value": "/",
  132. "new_value": name,
  133. "createtime": nowTime,
  134. "BCPCID": common.GetRandom(32),
  135. "operator_id": positionId,
  136. })
  137. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  138. "clue_id": clueId,
  139. "position_id": positionId,
  140. "change_type": "领取公海线索",
  141. "createtime": nowTime,
  142. "BCPCID": common.GetRandom(32),
  143. "operator_id": positionId,
  144. })
  145. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  146. "clue_id": clueId,
  147. "position_id": positionId,
  148. "change_type": "加入任务车",
  149. "new_value": "领取公海线索",
  150. "createtime": nowTime,
  151. "BCPCID": common.GetRandom(32),
  152. "operator_id": positionId,
  153. })
  154. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0
  155. }) {
  156. log.Println("领取线索2成功")
  157. } else {
  158. log.Println("领取线索2失败")
  159. }
  160. }
  161. }
  162. if data3 != nil && len(*data3) > 0 {
  163. for _, v := range *data3 {
  164. clueId := common.Int64All(v["clue_id"])
  165. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  166. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"position_id": positionId, "seatNumber": seatNumber})
  167. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  168. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  169. "clue_id": clueId,
  170. "seatNumber": seatNumber,
  171. "position_id": positionId,
  172. "comeintime": nowTime,
  173. "comeinsource": 3,
  174. "is_task": 0,
  175. })
  176. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  177. "clue_id": clueId,
  178. "position_id": positionId,
  179. "change_field": "position_id",
  180. "change_type": "所属人变更",
  181. "old_value": "/",
  182. "new_value": name,
  183. "createtime": nowTime,
  184. "BCPCID": common.GetRandom(32),
  185. "operator_id": positionId,
  186. })
  187. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  188. "clue_id": clueId,
  189. "position_id": positionId,
  190. "change_type": "领取公海线索",
  191. "createtime": nowTime,
  192. "BCPCID": common.GetRandom(32),
  193. "operator_id": positionId,
  194. })
  195. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  196. "clue_id": clueId,
  197. "position_id": positionId,
  198. "change_type": "加入任务车",
  199. "new_value": "领取公海线索",
  200. "createtime": nowTime,
  201. "BCPCID": common.GetRandom(32),
  202. "operator_id": positionId,
  203. })
  204. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0
  205. }) {
  206. log.Println("领取线索3成功")
  207. } else {
  208. log.Println("领取线索3失败")
  209. }
  210. }
  211. }
  212. }
  213. func getSeatNumber(positionId int64) (seatNumber, name string) {
  214. positionData := JyTidb.FindOne("base_position", map[string]interface{}{"id": positionId}, "", "")
  215. if positionData != nil && len(*positionData) > 0 {
  216. userId := common.Int64All((*positionData)["user_id"])
  217. if userId > 0 {
  218. userData, ok := Mgo.FindOne("user", map[string]interface{}{"base_user_id": userId})
  219. if ok && userData != nil && len(*userData) > 0 {
  220. s_phone := common.ObjToString((*userData)["s_phone"])
  221. if s_phone == "" {
  222. s_phone = common.ObjToString((*userData)["s_m_phone"])
  223. }
  224. saleData := JyBiTidb.FindOne("jy_salesperson_info", map[string]interface{}{"phone": s_phone}, "", "")
  225. if saleData != nil && len(*saleData) > 0 {
  226. seatNumber = common.ObjToString((*saleData)["seatNumber"])
  227. name = common.ObjToString((*saleData)["name"])
  228. }
  229. }
  230. }
  231. }
  232. return
  233. }