xuzhiheng 1 жил өмнө
parent
commit
8c1d9ddc39
1 өөрчлөгдсөн 132 нэмэгдсэн , 113 устгасан
  1. 132 113
      service/clue.go

+ 132 - 113
service/clue.go

@@ -574,67 +574,87 @@ func DrawClues(positionId, count1, count2, count3 int64) int {
 	}
 	//
 	if data1 != nil && len(*data1) > 0 {
-		count := batchDraw(*data1, nowTime, seatNumber, name, positionId)
+		count := batchDraw(*data1, nowTime, seatNumber, name, positionId, "1")
 		drawCount += count
 	}
 	if data2 != nil && len(*data2) > 0 {
-		count := batchDraw(*data2, nowTime, seatNumber, name, positionId)
+		count := batchDraw(*data2, nowTime, seatNumber, name, positionId, "1")
 		drawCount += count
 	}
 	if data3 != nil && len(*data3) > 0 {
-		count := batchDraw(*data3, nowTime, seatNumber, name, positionId)
+		count := batchDraw(*data3, nowTime, seatNumber, name, positionId, "1")
 		drawCount += count
 	}
 	//
 	if count1 > 0 {
-		data1 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
+		counts := 0
+		for {
+			data := JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
 					LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
-					WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?`, 1, count1)
+					WHERE a.level_open = 1 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit 1`)
+			if data != nil && len(*data) > 0 {
+				count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
+				if count > 0 {
+					counts++
+				}
+			}
+			if counts == count1 {
+				drawCount += count1
+				break
+			}
+		}
 	}
 	if count2 > 0 {
-		data2 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
+		counts := 0
+		for {
+			data := JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
 					LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
-					WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?`, 2, count2)
+					WHERE a.level_open = 2 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit 1`)
+			if data != nil && len(*data) > 0 {
+				count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
+				if count > 0 {
+					counts++
+				}
+			}
+			if counts == count2 {
+				drawCount += count2
+				break
+			}
+		}
 	}
 	if count3 > 0 {
-		data3 = JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
+		counts := 0
+		for {
+			data := JyBiTidb.SelectBySql(`SELECT max(c.createTime) as ctime,a.id,a.trailstatus FROM dwd_f_crm_clue_info a 
 					LEFT JOIN Call_Accounting.voice_record c ON c.CalledNo = a.phone
-					WHERE a.level_open = ? AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit ?`, 3, count3)
-	}
-	if data1 != nil && len(*data1) > 0 {
-		count := batchDraw(*data1, nowTime, seatNumber, name, positionId)
-		drawCount += count
-	}
-	if data2 != nil && len(*data2) > 0 {
-		count := batchDraw(*data2, nowTime, seatNumber, name, positionId)
-		drawCount += count
-	}
-	if data3 != nil && len(*data3) > 0 {
-		count := batchDraw(*data3, nowTime, seatNumber, name, positionId)
-		drawCount += count
+					WHERE a.level_open = 3 AND a.is_assign = 0 AND a.uid !="" AND a.is_transfer != 1 GROUP BY a.id ORDER BY ctime asc limit 1`)
+			if data != nil && len(*data) > 0 {
+				count := batchDraw(*data, nowTime, seatNumber, name, positionId, "2")
+				if count > 0 {
+					counts++
+				}
+			}
+			if counts == count3 {
+				drawCount += count3
+				break
+			}
+		}
+
 	}
 	return drawCount
 }
 
-func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64) int {
-	wg := new(sync.WaitGroup)
-	ch := make(chan bool, 20)
-	chs := make(chan bool, 1000)
+func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string, positionId int64, dataType string) int {
+	count := 0
 	for _, v := range data {
-		wg.Add(1)
-		ch <- true
-		go func(v map[string]interface{}) {
-			defer func() {
-				wg.Done()
-				<-ch
-			}()
-			clueId := common.Int64All(v["id"])
-			cluename := common.ObjToString(v["cluename"])
-			cdata := JyBiMysql.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "", "")
+		clueId := common.Int64All(v["id"])
+		cluename := common.ObjToString(v["cluename"])
+		if dataType == "2" {
+			cdata := JyBiMysql.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1, "company_nature": 0, "company_verification": 1}, "", "")
 			if cdata != nil && len(*cdata) > 0 {
 				salePositionId := common.Int64All((*cdata)["position_id"])
 				if pcount := JyBiMysql.Count("dwd_f_crm_personnel_management", map[string]interface{}{"resign": 0, "position_id": salePositionId}); pcount > 0 {
-					return
+					return count
 				} else {
 					saledata := JyBiMysql.SelectBySql(`SELECT position_id from dwd_d_crm_department_level_succbi WHERE bi_pcode in (SELECT bi_pcode from dwd_d_crm_department_level_succbi WHERE position_id = ?) AND resign = 0`, salePositionId)
 					if saledata != nil && len(*saledata) > 0 {
@@ -646,96 +666,95 @@ func batchDraw(data []map[string]interface{}, nowTime, seatNumber, name string,
 							}
 						}
 						if !isOk {
-							return
+							return count
 						}
 					}
 				}
 			}
-			trailstatus := common.ObjToString(v["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,
-					"comeinsource_private": 3,
-					"is_task":              1,
-					"task_time":            nowTime,
-					"tasktime":             nowTime,
-					"taskstatus":           0,
-					"tasksource":           "领取公海线索",
-					"level_open":           nil,
-					"clue_level":           nil,
-				}
-				if trailstatus != "08" {
-					updateClue["trailstatus"] = "01"
-				}
-				ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
-				recordId := JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+		}
+		trailstatus := common.ObjToString(v["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,
+				"comeinsource_private": 3,
+				"is_task":              1,
+				"task_time":            nowTime,
+				"tasktime":             nowTime,
+				"taskstatus":           0,
+				"tasksource":           "领取公海线索",
+				"level_open":           nil,
+				"clue_level":           nil,
+			}
+			if trailstatus != "08" {
+				updateClue["trailstatus"] = "01"
+			}
+			ok1 := JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateClue)
+			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": "position_id",
-					"change_type":  "所属人变更",
-					"old_value":    "/",
-					"new_value":    name,
+					"change_field": "trailstatus",
+					"change_type":  "基本信息变更",
+					"old_value":    "商机线索",
+					"new_value":    "新增",
 					"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,
+				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,
 				})
-				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 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
-			}) {
-				log.Println("领取线索成功")
-				chs <- true
-			} else {
-				log.Println("领取线索失败")
 			}
-		}(v)
+			return ok1 && recordId > 0 && recordId1 > 0 && recordId2 > 0 && recordId3 > -1 && recordId4 > -1
+		}) {
+			log.Println("领取线索成功")
+			count++
+		} else {
+			log.Println("领取线索失败")
+		}
 	}
-	wg.Wait()
-	return len(chs)
+	return count
 }
 
 func getSeatNumber(positionId int64) (seatNumber, name string) {