Browse Source

分配数量小于0

xuzhiheng 2 years ago
parent
commit
e84bd66c22
1 changed files with 86 additions and 207 deletions
  1. 86 207
      service/clue.go

+ 86 - 207
service/clue.go

@@ -41,9 +41,11 @@ func DistributeClueSync(this *biservice.DistributeClueReq) (int, int) {
 		for _, data := range this.Datas {
 			seatNumber := common.ObjToString(saleMap[data.Name]["seatNumber"])
 			distributedCount := int(data.DistributedCount)
-			distributedArr := this.ClueIdList[count : count+distributedCount]
-			count += distributedCount
-			DistributeClueMore(saleMap, distributedArr, seatNumber, data.Name, data.PositionId, this.PositionId)
+			if distributedCount > 0 {
+				distributedArr := this.ClueIdList[count : count+distributedCount]
+				count += distributedCount
+				DistributeClueMore(saleMap, distributedArr, seatNumber, data.Name, data.PositionId, this.PositionId)
+			}
 		}
 		return count, 1
 	} else {
@@ -77,16 +79,25 @@ func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedAr
 					}
 				}
 				if isAssign == 1 {
+					oldpositionId := common.Int64All((*clueData)["position_id"])
+					updateClue := map[string]interface{}{
+						"position_id": positionId,
+						"seatNumber":  seatNumber,
+						"is_assign":   1,
+						"updatetime":  nowTime,
+						"comeintime":  nowTime,
+					}
+					JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
 					ok := JyBiTidb.Update("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": v}, map[string]interface{}{
 						"position_id":  positionId,
 						"seatNumber":   seatNumber,
 						"comeinsource": 4,
 						"comeintime":   nowTime,
-						"is_task":      1,
-						"task_time":    nowTime,
-						"tasktime":     nowTime,
-						"taskstatus":   0,
-						"tasksource":   "线索批量分配",
+						// "is_task":      1,
+						// "task_time":    nowTime,
+						// "tasktime":     nowTime,
+						// "taskstatus":   0,
+						// "tasksource":   "线索批量分配",
 					})
 					if ok {
 						JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
@@ -100,6 +111,28 @@ func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedAr
 							"BCPCID":       common.GetRandom(32),
 							"operator_id":  thispositionId,
 						})
+						JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+							"clue_id":      v,
+							"position_id":  oldpositionId,
+							"change_field": "trailstatus",
+							"change_type":  "基本信息变更",
+							"old_value":    CodeTrail[trailstatus],
+							"new_value":    "流失",
+							"createtime":   nowTime,
+							"BCPCID":       common.GetRandom(32),
+							"operator_id":  thispositionId,
+						})
+						JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+							"clue_id":      v,
+							"position_id":  positionId,
+							"change_field": "trailstatus",
+							"change_type":  "基本信息变更",
+							"old_value":    CodeTrail[trailstatus],
+							"new_value":    "新增",
+							"createtime":   nowTime,
+							"BCPCID":       common.GetRandom(32),
+							"operator_id":  thispositionId,
+						})
 					} else {
 						log.Println("私海修改失败 ", v, positionId, seatNumber)
 					}
@@ -121,13 +154,14 @@ func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedAr
 						"seatNumber":   seatNumber,
 						"comeinsource": 4,
 						"comeintime":   nowTime,
-						"is_task":      1,
-						"task_time":    nowTime,
-						"tasktime":     nowTime,
-						"taskstatus":   0,
-						"tasksource":   "线索批量分配",
+						"is_task":      0,
+						// "task_time":    nowTime,
+						// "tasktime":     nowTime,
+						"taskstatus": 0,
+						// "tasksource":   "线索批量分配",
 					})
 					if seaId > 0 {
+						JyBiTidb.Delete("dwd_f_crm_open_sea", map[string]interface{}{"clue_id": v})
 						JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
 							"clue_id":      v,
 							"position_id":  positionId,
@@ -139,6 +173,19 @@ func DistributeClueMore(saleMap map[string]map[string]interface{}, distributedAr
 							"BCPCID":       common.GetRandom(32),
 							"operator_id":  thispositionId,
 						})
+						if trailstatus != "08" {
+							JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+								"clue_id":      v,
+								"position_id":  positionId,
+								"change_field": "trailstatus",
+								"change_type":  "基本信息变更",
+								"old_value":    "商机线索",
+								"new_value":    "新增",
+								"createtime":   nowTime,
+								"BCPCID":       common.GetRandom(32),
+								"operator_id":  thispositionId,
+							})
+						}
 					} else {
 						log.Println("私海插入失败 ", v, positionId, seatNumber)
 					}
@@ -168,7 +215,7 @@ func DrawClueSync(this *biservice.DrawClueReq) (int, int) {
 		count2 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 2})
 		// count3 := JyBiTidb.Count("dwd_f_crm_open_sea", map[string]interface{}{"level": 3})
 		counts1, counts2, counts3 := int64(0), int64(0), int64(0)
-		counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 2))
+		counts1 = int64(math.Ceil(float64(this.Count) / float64(10) * 5))
 		if this.Count-counts1 == 0 {
 			counts2 = 0
 			counts3 = 0
@@ -210,198 +257,31 @@ func DrawClues(positionId, count1, count2, count3 int64) int {
 	nowTime := time.Now().Format("2006-01-02 15:04:05")
 	seatNumber, name := getSeatNumber(positionId)
 	if data1 != nil && len(*data1) > 0 {
-		for _, v := range *data1 {
-			//update postionid and update record
-			clueId := common.Int64All(v["clue_id"])
-			trailstatus := ""
-			cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
-			if cluedata != nil && len(*cluedata) > 0 {
-				trailstatus = common.ObjToString((*cluedata)["trailstatus"])
-			}
-			if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
-				updateClue := map[string]interface{}{
-					"position_id": positionId,
-					"seatNumber":  seatNumber,
-					"is_assign":   1,
-					"updatetime":  nowTime,
-					"comeintime":  nowTime,
-				}
-				if trailstatus != "08" {
-					updateClue["trailstatus"] = "01"
-				}
-				ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
-				ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
-				seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
-					"clue_id":      clueId,
-					"seatNumber":   seatNumber,
-					"position_id":  positionId,
-					"comeintime":   nowTime,
-					"comeinsource": 3,
-					"is_task":      1,
-					"task_time":    nowTime,
-					"tasktime":     nowTime,
-					"taskstatus":   0,
-					"tasksource":   "领取公海线索",
-				})
-				recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  positionId,
-					"change_field": "position_id",
-					"change_type":  "所属人变更",
-					"old_value":    "/",
-					"new_value":    name,
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  positionId,
-				})
-				recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "领取公海线索",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": positionId,
-				})
-				recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "加入任务车",
-					"new_value":   "领取公海线索",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": positionId,
-				})
-				recordId3, recordId4 := int64(0), int64(0)
-				if trailstatus != "08" {
-					recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  positionId,
-						"change_field": "trailstatus",
-						"change_type":  "基本信息变更",
-						"old_value":    "商机线索",
-						"new_value":    "新增",
-						"createtime":   nowTime,
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  positionId,
-					})
-					recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  positionId,
-						"change_field": "trailstatus",
-						"change_type":  "基本信息变更",
-						"old_value":    CodeTrail[trailstatus],
-						"new_value":    "商机线索",
-						"createtime":   nowTime,
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  positionId,
-					})
-				}
-				return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
-			}) {
-				drawCount++
-				log.Println("领取线索1成功")
-			} else {
-				log.Println("领取线索1失败")
-			}
-		}
+		batchDraw(*data1, nowTime, seatNumber, name, positionId)
+		drawCount += len(*data1)
 	}
 	if data2 != nil && len(*data2) > 0 {
-		for _, v := range *data2 {
-			clueId := common.Int64All(v["clue_id"])
-			trailstatus := ""
-			cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
-			if cluedata != nil && len(*cluedata) > 0 {
-				trailstatus = common.ObjToString((*cluedata)["trailstatus"])
-			}
-			if JyBiMysql.ExecTx("领取线索等", func(tx *sql.Tx) bool {
-				updateClue := map[string]interface{}{
-					"position_id": positionId,
-					"seatNumber":  seatNumber,
-					"is_assign":   1,
-					"updatetime":  nowTime,
-					"comeintime":  nowTime,
-				}
-				if trailstatus != "08" {
-					updateClue["trailstatus"] = "01"
-				}
-				ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
-				ok2 := JyBiTidb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
-				seaId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{
-					"clue_id":      clueId,
-					"seatNumber":   seatNumber,
-					"position_id":  positionId,
-					"comeintime":   nowTime,
-					"comeinsource": 3,
-					"is_task":      1,
-					"task_time":    nowTime,
-					"tasktime":     nowTime,
-					"taskstatus":   0,
-					"tasksource":   "领取公海线索",
-				})
-				recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  positionId,
-					"change_field": "position_id",
-					"change_type":  "所属人变更",
-					"old_value":    "/",
-					"new_value":    name,
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  positionId,
-				})
-				recordId1 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "领取公海线索",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": positionId,
-				})
-				recordId2 := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "加入任务车",
-					"new_value":   "领取公海线索",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": positionId,
-				})
-				recordId3, recordId4 := int64(0), int64(0)
-				if trailstatus != "08" {
-					recordId3 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  positionId,
-						"change_field": "trailstatus",
-						"change_type":  "基本信息变更",
-						"old_value":    "商机线索",
-						"new_value":    "新增",
-						"createtime":   nowTime,
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  positionId,
-					})
-					recordId4 = JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  positionId,
-						"change_field": "trailstatus",
-						"change_type":  "基本信息变更",
-						"old_value":    CodeTrail[trailstatus],
-						"new_value":    "商机线索",
-						"createtime":   nowTime,
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  positionId,
-					})
-				}
-				return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
-			}) {
-				drawCount++
-				log.Println("领取线索2成功")
-			} else {
-				log.Println("领取线索2失败")
-			}
-		}
+		batchDraw(*data2, nowTime, seatNumber, name, positionId)
+		drawCount += len(*data2)
 	}
 	if data3 != nil && len(*data3) > 0 {
-		for _, v := range *data3 {
+		batchDraw(*data3, nowTime, seatNumber, name, positionId)
+		drawCount += len(*data3)
+	}
+	return drawCount
+}
+
+func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64) {
+	wg := new(sync.WaitGroup)
+	ch := make(chan bool, 20)
+	for _, v := range data {
+		wg.Add(1)
+		ch <- true
+		go func(v map[string]interface{}) {
+			defer func() {
+				wg.Done()
+				<-ch
+			}()
 			clueId := common.Int64All(v["clue_id"])
 			trailstatus := ""
 			cluedata := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, "", "")
@@ -488,14 +368,13 @@ func DrawClues(positionId, count1, count2, count3 int64) int {
 				}
 				return ok1 && ok2 && seaId > 0 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
 			}) {
-				drawCount++
-				log.Println("领取线索3成功")
+				log.Println("领取线索成功")
 			} else {
-				log.Println("领取线索3失败")
+				log.Println("领取线索失败")
 			}
-		}
+		}(v)
 	}
-	return drawCount
+	wg.Wait()
 }
 
 func getSeatNumber(positionId int64) (seatNumber, name string) {