clue.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. count, status := DrawClueSync(this)
  13. log.Println("领取数量 ", count)
  14. return &biservice.AddProjectResp{
  15. ErrorCode: 0,
  16. Data: &biservice.AddProject{
  17. Status: int64(status),
  18. Count: int64(count),
  19. },
  20. }
  21. }
  22. func DrawClueSync(this *biservice.DrawClueReq) (int, int) {
  23. if DataLock.TryLock() {
  24. defer DataLock.Unlock()
  25. count1 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 1})
  26. count2 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 2})
  27. // count3 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 3})
  28. counts1, counts2, counts3 := int64(0), int64(0), int64(0)
  29. counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 2))
  30. if this.Count-counts1 == 0 {
  31. counts2 = 0
  32. counts3 = 0
  33. } else {
  34. counts2 = int64(math.Ceil(float64(this.Count) / float64(10) * 4))
  35. if this.Count-counts1-counts2 == 0 {
  36. counts3 = 0
  37. } else {
  38. counts3 = this.Count - counts1 - counts2
  39. }
  40. }
  41. if counts1 > count1 {
  42. counts2 += counts1 - count1
  43. counts1 = count1
  44. }
  45. if counts2 > count2 {
  46. counts3 += counts2 - count2
  47. counts2 = count2
  48. }
  49. log.Println(count1, count2)
  50. log.Println(counts1, counts2, counts3)
  51. return DrawClues(this.PositionId, counts1, counts2, counts3), 1
  52. } else {
  53. return 0, 2
  54. }
  55. }
  56. func DrawClues(positionId, count1, count2, count3 int64) int {
  57. data1, data2, data3, drawCount := &[]map[string]interface{}{}, &[]map[string]interface{}{}, &[]map[string]interface{}{}, 0
  58. if count1 > 0 {
  59. data1 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 1}, "", "", 0, int(count1))
  60. }
  61. if count2 > 0 {
  62. data2 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 2}, "", "", 0, int(count2))
  63. }
  64. if count3 > 0 {
  65. data3 = JyBiTidb.Find("dwd_f_crm_open_sea", map[string]interface{}{"level": 3}, "", "", 0, int(count3))
  66. }
  67. nowTime := time.Now().Format("2006-01-02 15:04:05")
  68. seatNumber, name := getSeatNumber(positionId)
  69. if data1 != nil && len(*data1) > 0 {
  70. for _, v := range *data1 {
  71. //update postionid and update record
  72. clueId := common.Int64All(v["clue_id"])
  73. trailstatus := ""
  74. cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
  75. if cluedata != nil && len(*cluedata) > 0 {
  76. trailstatus = common.ObjToString((*cluedata)["trailstatus"])
  77. }
  78. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  79. updateClue := map[string]interface{}{
  80. "position_id": positionId,
  81. "seatNumber": seatNumber,
  82. "is_assign": 1,
  83. "updatetime": nowTime,
  84. "comeintime": nowTime,
  85. }
  86. if trailstatus != "08" {
  87. updateClue["trailstatus"] = "01"
  88. }
  89. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  90. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  91. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  92. "clue_id": clueId,
  93. "seatNumber": seatNumber,
  94. "position_id": positionId,
  95. "comeintime": nowTime,
  96. "comeinsource": 3,
  97. "is_task": 1,
  98. "task_time": nowTime,
  99. "tasktime": nowTime,
  100. "taskstatus": 0,
  101. "tasksource": "领取公海线索",
  102. })
  103. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  104. "clue_id": clueId,
  105. "position_id": positionId,
  106. "change_field": "position_id",
  107. "change_type": "所属人变更",
  108. "old_value": "/",
  109. "new_value": name,
  110. "createtime": nowTime,
  111. "BCPCID": common.GetRandom(32),
  112. "operator_id": positionId,
  113. })
  114. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  115. "clue_id": clueId,
  116. "position_id": positionId,
  117. "change_type": "领取公海线索",
  118. "createtime": nowTime,
  119. "BCPCID": common.GetRandom(32),
  120. "operator_id": positionId,
  121. })
  122. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  123. "clue_id": clueId,
  124. "position_id": positionId,
  125. "change_type": "加入任务车",
  126. "new_value": "领取公海线索",
  127. "createtime": nowTime,
  128. "BCPCID": common.GetRandom(32),
  129. "operator_id": positionId,
  130. })
  131. recordId3, recordId4 := int64(0), int64(0)
  132. if trailstatus != "08" {
  133. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  134. "clue_id": clueId,
  135. "position_id": positionId,
  136. "change_field": "trailstatus",
  137. "change_type": "基本信息变更",
  138. "old_value": "商机线索",
  139. "new_value": "新增",
  140. "createtime": nowTime,
  141. "BCPCID": common.GetRandom(32),
  142. "operator_id": positionId,
  143. })
  144. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  145. "clue_id": clueId,
  146. "position_id": positionId,
  147. "change_field": "trailstatus",
  148. "change_type": "基本信息变更",
  149. "old_value": CodeTrail[trailstatus],
  150. "new_value": "商机线索",
  151. "createtime": nowTime,
  152. "BCPCID": common.GetRandom(32),
  153. "operator_id": positionId,
  154. })
  155. }
  156. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  157. }) {
  158. drawCount++
  159. log.Println("领取线索1成功")
  160. } else {
  161. log.Println("领取线索1失败")
  162. }
  163. }
  164. }
  165. if data2 != nil && len(*data2) > 0 {
  166. for _, v := range *data2 {
  167. clueId := common.Int64All(v["clue_id"])
  168. trailstatus := ""
  169. cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
  170. if cluedata != nil && len(*cluedata) > 0 {
  171. trailstatus = common.ObjToString((*cluedata)["trailstatus"])
  172. }
  173. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  174. updateClue := map[string]interface{}{
  175. "position_id": positionId,
  176. "seatNumber": seatNumber,
  177. "is_assign": 1,
  178. "updatetime": nowTime,
  179. "comeintime": nowTime,
  180. }
  181. if trailstatus != "08" {
  182. updateClue["trailstatus"] = "01"
  183. }
  184. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  185. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  186. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  187. "clue_id": clueId,
  188. "seatNumber": seatNumber,
  189. "position_id": positionId,
  190. "comeintime": nowTime,
  191. "comeinsource": 3,
  192. "is_task": 1,
  193. "task_time": nowTime,
  194. "tasktime": nowTime,
  195. "taskstatus": 0,
  196. "tasksource": "领取公海线索",
  197. })
  198. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  199. "clue_id": clueId,
  200. "position_id": positionId,
  201. "change_field": "position_id",
  202. "change_type": "所属人变更",
  203. "old_value": "/",
  204. "new_value": name,
  205. "createtime": nowTime,
  206. "BCPCID": common.GetRandom(32),
  207. "operator_id": positionId,
  208. })
  209. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  210. "clue_id": clueId,
  211. "position_id": positionId,
  212. "change_type": "领取公海线索",
  213. "createtime": nowTime,
  214. "BCPCID": common.GetRandom(32),
  215. "operator_id": positionId,
  216. })
  217. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  218. "clue_id": clueId,
  219. "position_id": positionId,
  220. "change_type": "加入任务车",
  221. "new_value": "领取公海线索",
  222. "createtime": nowTime,
  223. "BCPCID": common.GetRandom(32),
  224. "operator_id": positionId,
  225. })
  226. recordId3, recordId4 := int64(0), int64(0)
  227. if trailstatus != "08" {
  228. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  229. "clue_id": clueId,
  230. "position_id": positionId,
  231. "change_field": "trailstatus",
  232. "change_type": "基本信息变更",
  233. "old_value": "商机线索",
  234. "new_value": "新增",
  235. "createtime": nowTime,
  236. "BCPCID": common.GetRandom(32),
  237. "operator_id": positionId,
  238. })
  239. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  240. "clue_id": clueId,
  241. "position_id": positionId,
  242. "change_field": "trailstatus",
  243. "change_type": "基本信息变更",
  244. "old_value": CodeTrail[trailstatus],
  245. "new_value": "商机线索",
  246. "createtime": nowTime,
  247. "BCPCID": common.GetRandom(32),
  248. "operator_id": positionId,
  249. })
  250. }
  251. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  252. }) {
  253. drawCount++
  254. log.Println("领取线索2成功")
  255. } else {
  256. log.Println("领取线索2失败")
  257. }
  258. }
  259. }
  260. if data3 != nil && len(*data3) > 0 {
  261. for _, v := range *data3 {
  262. clueId := common.Int64All(v["clue_id"])
  263. trailstatus := ""
  264. cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
  265. if cluedata != nil && len(*cluedata) > 0 {
  266. trailstatus = common.ObjToString((*cluedata)["trailstatus"])
  267. }
  268. if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
  269. updateClue := map[string]interface{}{
  270. "position_id": positionId,
  271. "seatNumber": seatNumber,
  272. "is_assign": 1,
  273. "updatetime": nowTime,
  274. "comeintime": nowTime,
  275. }
  276. if trailstatus != "08" {
  277. updateClue["trailstatus"] = "01"
  278. }
  279. ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
  280. ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
  281. seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
  282. "clue_id": clueId,
  283. "seatNumber": seatNumber,
  284. "position_id": positionId,
  285. "comeintime": nowTime,
  286. "comeinsource": 3,
  287. "is_task": 1,
  288. "task_time": nowTime,
  289. "tasktime": nowTime,
  290. "taskstatus": 0,
  291. "tasksource": "领取公海线索",
  292. })
  293. recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  294. "clue_id": clueId,
  295. "position_id": positionId,
  296. "change_field": "position_id",
  297. "change_type": "所属人变更",
  298. "old_value": "/",
  299. "new_value": name,
  300. "createtime": nowTime,
  301. "BCPCID": common.GetRandom(32),
  302. "operator_id": positionId,
  303. })
  304. recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  305. "clue_id": clueId,
  306. "position_id": positionId,
  307. "change_type": "领取公海线索",
  308. "createtime": nowTime,
  309. "BCPCID": common.GetRandom(32),
  310. "operator_id": positionId,
  311. })
  312. recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  313. "clue_id": clueId,
  314. "position_id": positionId,
  315. "change_type": "加入任务车",
  316. "new_value": "领取公海线索",
  317. "createtime": nowTime,
  318. "BCPCID": common.GetRandom(32),
  319. "operator_id": positionId,
  320. })
  321. recordId3, recordId4 := int64(0), int64(0)
  322. if trailstatus != "08" {
  323. recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  324. "clue_id": clueId,
  325. "position_id": positionId,
  326. "change_field": "trailstatus",
  327. "change_type": "基本信息变更",
  328. "old_value": "商机线索",
  329. "new_value": "新增",
  330. "createtime": nowTime,
  331. "BCPCID": common.GetRandom(32),
  332. "operator_id": positionId,
  333. })
  334. recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
  335. "clue_id": clueId,
  336. "position_id": positionId,
  337. "change_field": "trailstatus",
  338. "change_type": "基本信息变更",
  339. "old_value": CodeTrail[trailstatus],
  340. "new_value": "商机线索",
  341. "createtime": nowTime,
  342. "BCPCID": common.GetRandom(32),
  343. "operator_id": positionId,
  344. })
  345. }
  346. return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
  347. }) {
  348. drawCount++
  349. log.Println("领取线索3成功")
  350. } else {
  351. log.Println("领取线索3失败")
  352. }
  353. }
  354. }
  355. return drawCount
  356. }
  357. func getSeatNumber(positionId int64) (seatNumber, name string) {
  358. positionData := JyTidb.FindOne("base_position", map[string]interface{}{"id": positionId}, "", "")
  359. if positionData != nil && len(*positionData) > 0 {
  360. userId := common.Int64All((*positionData)["user_id"])
  361. if userId > 0 {
  362. userData, ok := Mgo.FindOne("user", map[string]interface{}{"base_user_id": userId})
  363. if ok && userData != nil && len(*userData) > 0 {
  364. s_phone := common.ObjToString((*userData)["s_phone"])
  365. if s_phone == "" {
  366. s_phone = common.ObjToString((*userData)["s_m_phone"])
  367. }
  368. saleData := JyBiTidb.FindOne("jy_salesperson_info", map[string]interface{}{"phone": s_phone}, "", "")
  369. if saleData != nil && len(*saleData) > 0 {
  370. seatNumber = common.ObjToString((*saleData)["seatNumber"])
  371. name = common.ObjToString((*saleData)["name"])
  372. }
  373. }
  374. }
  375. }
  376. return
  377. }