Browse Source

627放撞单功能新增

WH01243 5 months ago
parent
commit
cd76aa537d
6 changed files with 425 additions and 255 deletions
  1. 22 13
      clueSync/autoTask.go
  2. 50 29
      clueSync/job.go
  3. 260 203
      clueSync/jobutil.go
  4. 6 2
      clueSync/kc.go
  5. 9 8
      clueSync/main.go
  6. 78 0
      clueSync/sqlOperate.go

+ 22 - 13
clueSync/autoTask.go

@@ -165,8 +165,11 @@ GROUP BY
 			//坐席信息查询
 			clueId := common.Int64All(v["id"])
 			createtime := gconv.String(v["comeintime"])
+			clueName := gconv.String(v["cluename"])
 			positionId := common.Int64All(v["position_id"])
+			lockStatus := common.Int64All(v["lock_status"])
 			seatNumber := common.ObjToString(v["seatNumber"])
+			isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
 			if trailstatus == "01" {
 				//该线索最近一次分配给该电销人员后存在通话记录
 				uid := gconv.String(v["uid"])
@@ -184,7 +187,8 @@ GROUP BY
 				positionMap[positionId] = true
 				recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=0,position_id=null,seatNumber=null,updatetime = ?,comeinsource_open=null,level_open=null,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null WHERE id = ?`, nowTime, clueId) //,start_trail_time=null,content=null
 				recordId2 := AddChangeRecord(tx, clueId, positionId, "", "退回公海", "所有人员私海已达上限", "", nowTime, "", -1)
-				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", GetPositionName(seatNumber), nowTime, "", -1)
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", FindSeatNumberToName(seatNumber), nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1
 			}) {
 				//发送消息,找wjh
@@ -211,6 +215,9 @@ func autoExitSea() {
 			clueId := common.Int64All(v["id"])
 			positionId := common.Int64All(v["position_id"])
 			seatNumber := common.ObjToString(v["seatNumber"])
+			clueName := gconv.String(v["cluename"])
+			lockStatus := common.Int64All(v["lock_status"])
+			isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
 			nowTime := time.Now().Format(date.Date_Full_Layout)
 			// is_assign := common.IntAll(v["is_assign"])
 			if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
@@ -221,10 +228,11 @@ func autoExitSea() {
 				// } else if is_assign == 0 {
 				// 	ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
 				// }
-				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", GetPositionName(seatNumber), "/", nowTime, "", -1)
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", FindSeatNumberToName(seatNumber), "/", nowTime, "", -1)
 				recordId2 := AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "流失", "空号停机", nowTime, "", -1)
 				recordId3 := AddChangeRecord(tx, clueId, positionId, "", "退出任务车", "空号停机自动从线索池删除", "", nowTime, "", -1)
 				recordId4 := AddChangeRecord(tx, clueId, positionId, "", "退出公海", "空号停机自动从线索池删除", "", nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
 			}) {
 				log.Println("自动退出线索成功")
@@ -241,13 +249,17 @@ func autoExitSea() {
 			positionId := common.Int64All(v["position_id"])
 			seatNumber := common.ObjToString(v["seatNumber"])
 			nowTime := time.Now().Format(date.Date_Full_Layout)
+			clueName := gconv.String(v["cluename"])
+			lockStatus := common.Int64All(v["lock_status"])
+			isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
 			// is_assign := common.IntAll(v["is_assign"])
 			if TiDb.ExecTx("自动退海", func(tx *sql.Tx) bool {
 				recordId := TiDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dwd_f_crm_clue_info SET is_assign=0,position_id=null,seatNumber=null,updatetime = ?,comeintime_open = ?,comeinsource_open=?,level_open=4,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null WHERE id = ?`, nowTime, nowTime, common.If(trailstatus == "00", 8, 11), clueId) //,content=null,start_trail_time=null
-				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", GetPositionName(seatNumber), nowTime, "", -1)
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", FindSeatNumberToName(seatNumber), nowTime, "", -1)
 				recordId2 := AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "流失", gconv.String(common.If(trailstatus == "00", "无意向客户", "拒绝客户")), nowTime, "", -1)
 				recordId3 := AddChangeRecord(tx, clueId, positionId, "", "退出任务车", gconv.String(common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海")), "", nowTime, "", -1)
 				recordId4 := AddChangeRecord(tx, clueId, positionId, "", "退回公海", gconv.String(common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海")), "", nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
 			}) {
 				log.Println("自动退出私海成功")
@@ -259,14 +271,6 @@ func autoExitSea() {
 	log.Println("自动退海任务结束")
 }
 
-func GetPositionName(seatNumber string) string {
-	data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
-	if data != nil && len(*data) > 0 {
-		return common.ObjToString((*data)[0]["name"])
-	}
-	return ""
-}
-
 func Thaw() {
 	//3个工作日查询
 	k := db.ThawDay
@@ -297,6 +301,9 @@ func Thaw() {
 			clueId := gconv.Int64(v["id"])
 			positionId := gconv.Int64(v["position_id"])
 			freezeInt64 := int64(0)
+			clueName := gconv.String(v["cluename"])
+			lockStatus := common.Int64All(v["lock_status"])
+			isGroup, isCommerce := gconv.Int(v["company_nature"]), gconv.Int(v["company_verification"])
 			freezeStr := gconv.String(v["FREEZE_TIME"])
 			if freezeStr != "" {
 				t, _ := time.ParseInLocation(layout, freezeStr, time.Local)
@@ -305,7 +312,6 @@ func Thaw() {
 			if freezeInt64 < nowTime.Unix() {
 				nowTime := time.Now().Format(date.Date_Full_Layout)
 				//超三天处理
-				//分配状态改改
 				if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
 					"is_assign":   0,
 					"position_id": 0,
@@ -314,7 +320,9 @@ func Thaw() {
 					AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", "/", nameMap[positionId], nowTime, "", -1)
 				}
 				AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "自动退回公海", "", nowTime, "", -1)
+				AddLockeStatusChange(nil, clueId, positionId, 0, lockStatus, isGroup, isCommerce, clueName, nowTime, "delete")
 			} else {
+				//分配状态改改
 				if !FindUpperLimit(gconv.String(positionId), "", false) {
 					nowTime := time.Now().Format(date.Date_Full_Layout)
 					if TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{
@@ -323,11 +331,12 @@ func Thaw() {
 						AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", nameMap[positionId], "/", nowTime, "", -1)
 					}
 					AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "销售人员私海线索已释放", "", nowTime, "", -1)
+					AddLockeStatusChange(nil, clueId, positionId, positionId, lockStatus, isGroup, isCommerce, clueName, nowTime, "add")
 				}
 			}
 		}
 		return true
-	}, `select id,position_id,FREEZE_TIME from dwd_f_crm_clue_info where is_assign = -3 `)
+	}, `select * from dwd_f_crm_clue_info where is_assign = -3 `)
 }
 func IsBreak(currentTime time.Time) bool {
 	if currentTime.Weekday() == time.Sunday {

+ 50 - 29
clueSync/job.go

@@ -361,6 +361,7 @@ func FormatData(data map[string]interface{}, item string) (bool, bool, bool) {
 					old_department := gconv.String((*uCount)["department"])
 					old_follow_project_area := gconv.String((*uCount)["follow_project_area"])
 					old_role := gconv.String((*uCount)["role"])
+					lock_status := common.Int64All((*uCount)["lock_status"])
 					old_cluename := gconv.String((*uCount)["cluename"])
 					old_top_cluetype := gconv.String((*uCount)["top_cluetype"])
 					old_sub_cluetype := gconv.String((*uCount)["sub_cluetype"])
@@ -389,6 +390,7 @@ func FormatData(data map[string]interface{}, item string) (bool, bool, bool) {
 					}
 					if old_cluename != cluename && cluename != "" {
 						clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
+						ClueNameChange(old_name, lock_status, clueId)
 						AddChangeRecord(nil, clueId, position_id, "cluename", "基本信息变更", gconv.String(common.If(cluename != "", cluename, "/")), gconv.String(common.If(old_cluename != "", old_cluename, "/")), nowTime, BCPCID, -1)
 					}
 					old_topname := ""
@@ -478,12 +480,13 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, c
 			"label":                1,
 			"labelChangeTime":      time.Now().Format("2006-01-02"),
 		})
-		updateId1 = AddChangeRecord(tx, clueId, clueId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
-		updateId2 = AddChangeRecord(tx, clueId, clueId, "position_id", "所属人变更", saleName, "/", nowTime, "", -1)
-		updateId3 = AddChangeRecord(tx, clueId, clueId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
-		updateId4 = AddChangeRecord(tx, clueId, clueId, "", "加入任务车", "线索自动分配"+"-"+topname+"-"+subname, "", nowTime, "", -1)
-		updateId5 = AddChangeRecord(tx, clueId, clueId, "top_cluetype", "基本信息变更", "/", topname, nowTime, "", -1)
-		updateId6 = AddChangeRecord(tx, clueId, clueId, "sub_cluetype", "基本信息变更", "/", subname, nowTime, "", -1)
+		updateId1 = AddChangeRecord(tx, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
+		updateId2 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", saleName, "/", nowTime, "", -1)
+		updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+		updateId4 = AddChangeRecord(tx, clueId, positionId, "", "加入任务车", "线索自动分配"+"-"+topname+"-"+subname, "", nowTime, "", -1)
+		updateId5 = AddChangeRecord(tx, clueId, positionId, "top_cluetype", "基本信息变更", "/", topname, nowTime, "", -1)
+		updateId6 = AddChangeRecord(tx, clueId, positionId, "sub_cluetype", "基本信息变更", "/", subname, nowTime, "", -1)
+		AddLockeStatusChange(tx, clueId, positionId, 0, 0, isGroup, isCommerce, cluename, nowTime, "delete")
 		//冻结处理
 		return clueId > -1 && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1
 	}) {
@@ -547,9 +550,10 @@ func SaveClue0(item, userId, uId, top_cluetype, sub_cluetype, topname, subname,
 			"label":                1,
 			"labelChangeTime":      time.Now().Format("2006-01-02"),
 		})
-		uodateId1 = AddChangeRecord(nil, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
-		uodateId2 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", "/", "", nowTime, "", -1)
-		uodateId3 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+		uodateId1 = AddChangeRecord(tx, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
+		uodateId2 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", "", nowTime, "", -1)
+		uodateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+		AddLockeStatusChange(tx, clueId, positionId, 0, 0, isGroup, isCommerce, cluename, nowTime, "delete")
 		return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1
 	}) {
 		log.Println("线索分配成功")
@@ -594,10 +598,12 @@ func WorkUpdateClue(data map[string]interface{}, saleData []map[string]interface
 	old_follow_project_area := gconv.String(data["follow_project_area"])
 	old_role := gconv.String(data["role"])
 	old_cluename := gconv.String(data["cluename"])
+
 	old_top_cluetype := gconv.String(data["top_cluetype"])
 	old_sub_cluetype := gconv.String(data["sub_cluetype"])
 	label := gconv.Int64(data["label"])
 	old_topname, old_subname := "", ""
+	lockStatus := common.Int64All(data["lock_status"])
 	is_transfer := common.IntAll(data["is_transfer"])
 	if taskstatus == 1 || is_task == 0 {
 		taskTime = gconv.String(common.If(item != "users", nowTime, nowTimes)) //任务时间,正常的是当前时间,新用户是当前时间+12个小时
@@ -801,33 +807,39 @@ func WorkUpdateClue(data map[string]interface{}, saleData []map[string]interface
 		if (trailstatus == "00" || old_position_id == 0 || (pIsOk && old_cluename != cluename && cluename != "")) && is_assign == 1 {
 			if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
 			} else if old_position_id == 0 {
-				updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
-				updateId2 = AddChangeRecord(nil, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
-				updateId3 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "新增", CodeTrail[trailstatus], nowTime, "", -1)
+				updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+				updateId2 = AddChangeRecord(tx, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
+				updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", CodeTrail[trailstatus], nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, "add")
 			} else {
 				if pIsOk && old_cluename != cluename && cluename != "" {
-					updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+					updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+					AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, "add")
 				} else {
-					updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
-					updateId2 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "流失", "无意向客户", nowTime, "", -1)
-					updateId3 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
-					updateId4 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", "无意向客户", nowTime, "", -1)
+					updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+					updateId2 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "流失", "无意向客户", nowTime, "", -1)
+					updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+					updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", "无意向客户", nowTime, "", -1)
+					AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, "add")
 				}
 			}
 		} else if is_assign == 0 {
 			if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
 			} else {
-				updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+				updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
 				if trailstatus != "01" {
-					updateId4 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
+					updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
 				}
-				updateId3 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+				updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 			}
 		} else if is_assign == -1 {
-			updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
-			updateId4 = AddChangeRecord(nil, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
+			updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+			updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
+			AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 		} else if (pIsOk && old_cluename == cluename && cluename != "") && is_assign == 1 && positionId != old_position_id {
-			updateId1 = AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+			updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
+			AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 		}
 		if recordCount <= 0 || is_assign == 0 {
 			if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
@@ -911,6 +923,7 @@ func WorkUpdateClue(data map[string]interface{}, saleData []map[string]interface
 			if old_cluename != cluename && cluename != "" {
 				updateId11 = AddChangeRecord(tx, clueId, gconv.Int64(common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.If(is_assign == 1, positionId, common.Int64All(data["position_id"]))))),
 					"cluename", "基本信息变更", gconv.String(common.If(cluename != "", cluename, "/")), gconv.String(common.If(old_cluename != "", old_cluename, "/")), nowTime, "", -1)
+				ClueNameChange(old_name, lockStatus, clueId)
 
 			}
 		}
@@ -985,6 +998,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 	old_top_cluetype := gconv.String(data["top_cluetype"])
 	old_sub_cluetype := gconv.String(data["sub_cluetype"])
 	old_topname, old_subname := "", ""
+	lockStatus := common.Int64All(data["lock_status"])
 	is_transfer := common.IntAll(data["is_transfer"])
 	oldLabelChangeTime := gconv.String(data["labelChangeTime"])
 	label := gconv.Int64(data["label"])
@@ -1052,7 +1066,6 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 	if cluename != "" {
 		clueUpdateData["cluename"] = cluename //新的线索名不为空才替换
 	}
-
 	if item != "orders" && item != "users" { //新用户和订单之外的替换一下新的留资信息
 		if name != "" {
 			clueUpdateData["name"] = name
@@ -1119,6 +1132,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 			Level:                level,
 			LabelChangeTime:      oldLabelChangeTime,
 			Label:                label,
+			LockStatus:           lockStatus,
 		}
 		return clueInfoUpdate(clueMapEntity)
 
@@ -1230,6 +1244,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 			} else if old_position_id == 0 {
 				updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
 				updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", CodeTrail[trailstatus], nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 			} else {
 				if pIsOk && old_cluename != cluename && cluename != "" {
 					updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(
@@ -1239,6 +1254,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					updateId2 = AddChangeRecord(tx, clueId, common.Int64All(data["position_id"]), "trailstatus", "基本信息变更", "流失", "无意向客户", nowTime, "", -1)
 					updateId3 = AddChangeRecord(tx, clueId, common.Int64All(data["position_id"]), "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
 					updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", "无意向客户", nowTime, "", -1)
+					AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 				}
 			}
 		} else if is_assign == 0 {
@@ -1252,10 +1268,12 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
 				}
 				updateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
+				AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 			}
 		} else if is_assign == -1 {
 			updateId1 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", gconv.String(common.If(saleName != "", saleName, "/")), gconv.String(common.If(oldsaleName != "", oldsaleName, "/")), nowTime, "", -1)
 			updateId4 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "商机线索", CodeTrail[trailstatus], nowTime, "", -1)
+			AddLockeStatusChange(tx, clueId, positionId, 0, lockStatus, isGroup, isCommerce, cluename, nowTime, gconv.String(common.If(saleName != "", "add", "delete")))
 		}
 		if recordCount <= 0 || is_assign == 0 {
 			if trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400 {
@@ -1343,6 +1361,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 			if old_cluename != cluename && cluename != "" {
 				updateId11 = AddChangeRecord(tx, clueId, gconv.Int64(common.If(trailstatus == "00" && trailstatusTime != "" && time.Now().Unix()-trailstatusTimes.Unix() < 86400, -1, common.If(trailstatus == "00" || is_assign == 0, positionId, common.Int64All(data["position_id"])))), "cluename", "基本信息变更",
 					gconv.String(common.If(cluename != "", cluename, "/")), gconv.String(common.If(old_cluename != "", old_cluename, "/")), nowTime, "", -1)
+				ClueNameChange(old_cluename, lockStatus, clueId)
 			}
 		}
 		if old_top_cluetype != top_cluetype {
@@ -1417,6 +1436,7 @@ type ClueMapEntity struct {
 	Label                int64
 	LabelChangeTime      string
 	Level                string
+	LockStatus           int64
 }
 
 // 基本信息变更
@@ -1459,7 +1479,7 @@ func clueInfoUpdate(data ClueMapEntity) bool {
 					gconv.String(common.If(data.NewDepartment != "", data.NewDepartment, "/")), gconv.String(common.If(data.OldDepartment != "", data.OldDepartment, "/")), nowTime, "", -1)
 			}
 			if data.OldRole != data.NewRole && data.NewRole != "" {
-				updateId9 = AddChangeRecord(nil, data.ClueId, data.PositionId, "role", "基本信息变更", gconv.String(common.If(data.NewRole != "", data.NewRole, "/")), gconv.String(common.If(data.OldRole != "", data.OldRole, "/")), nowTime, BCPCID, -1)
+				updateId9 = AddChangeRecord(tx, data.ClueId, data.PositionId, "role", "基本信息变更", gconv.String(common.If(data.NewRole != "", data.NewRole, "/")), gconv.String(common.If(data.OldRole != "", data.OldRole, "/")), nowTime, BCPCID, -1)
 			}
 			if data.OldFollowProjectArea != data.NewFollowProjectArea && data.NewFollowProjectArea != "" {
 				old_area, old_area_arr := "", []string{}
@@ -1472,17 +1492,18 @@ func clueInfoUpdate(data ClueMapEntity) bool {
 				}
 				old_area = strings.Join(old_area_arr, ",")
 				new_area = strings.Join(new_area_arr, ",")
-				updateId10 = AddChangeRecord(nil, data.ClueId, data.PositionId, "follow_project_area", "基本信息变更", gconv.String(common.If(new_area != "", new_area, "/")), gconv.String(common.If(old_area != "", old_area, "/")), nowTime, BCPCID, -1)
+				updateId10 = AddChangeRecord(tx, data.ClueId, data.PositionId, "follow_project_area", "基本信息变更", gconv.String(common.If(new_area != "", new_area, "/")), gconv.String(common.If(old_area != "", old_area, "/")), nowTime, BCPCID, -1)
 			}
 			if data.OldCluename != data.NewCluename && data.NewCluename != "" {
-				updateId11 = AddChangeRecord(nil, data.ClueId, data.PositionId, "cluename", "基本信息变更", gconv.String(common.If(data.NewCluename != "", data.NewCluename, "/")), gconv.String(common.If(data.OldCluename != "", data.OldCluename, "/")), nowTime, BCPCID, -1)
+				updateId11 = AddChangeRecord(tx, data.ClueId, data.PositionId, "cluename", "基本信息变更", gconv.String(common.If(data.NewCluename != "", data.NewCluename, "/")), gconv.String(common.If(data.OldCluename != "", data.OldCluename, "/")), nowTime, BCPCID, -1)
+				ClueNameChange(data.OldCluename, data.LockStatus, data.ClueId)
 			}
 		}
 		if data.OldTopCluetype != data.NewTopCluetype {
-			updateId12 = AddChangeRecord(nil, data.ClueId, data.PositionId, "top_cluetype", "基本信息变更", gconv.String(common.If(data.Topname != "", data.Topname, "/")), gconv.String(common.If(old_topname != "", old_topname, "/")), nowTime, BCPCID, -1)
+			updateId12 = AddChangeRecord(tx, data.ClueId, data.PositionId, "top_cluetype", "基本信息变更", gconv.String(common.If(data.Topname != "", data.Topname, "/")), gconv.String(common.If(old_topname != "", old_topname, "/")), nowTime, BCPCID, -1)
 		}
 		if data.OldSubCluetype != data.OldSubCluetype {
-			updateId13 = AddChangeRecord(nil, data.ClueId, data.PositionId, "sub_cluetype", "基本信息变更", gconv.String(common.If(data.Subname != "", data.Subname, "/")), gconv.String(common.If(old_subname != "", old_subname, "/")), nowTime, BCPCID, -1)
+			updateId13 = AddChangeRecord(tx, data.ClueId, data.PositionId, "sub_cluetype", "基本信息变更", gconv.String(common.If(data.Subname != "", data.Subname, "/")), gconv.String(common.If(old_subname != "", old_subname, "/")), nowTime, BCPCID, -1)
 			LabelHandel(tx, data.Label, 1, data.UId, data.Level, data.Trailstatus, data.Trailstatus, 0)
 		}
 		return ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1 && updateId7 > -1 && updateId8 > -1 && updateId9 > -1 && updateId10 > -1 && updateId11 > -1 && updateId12 > -1 && updateId13 > -1

+ 260 - 203
clueSync/jobutil.go

@@ -144,7 +144,7 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 		}
 	} else {
 		//新增线索
-		relatedId, _ := FindRelatedData(cluename)
+		relatedId, _, _ := FindRelatedClueData(cluename, isGroup, isCommerce)
 		ok, data, _ = FindPosition(relatedId, "", gconv.String(acceptanceData["creator_time"]))
 		if !ok {
 			return false
@@ -1040,173 +1040,66 @@ func getClueType(item string, data map[string]interface{}, sourceCode string, so
 func autoDraw(mode, cluename, phone string, isGroup, isCommerce int) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}, isOk, isFreeze bool, noticePositionId int64) {
 	isOk = false
 	isFreeze = false
+	pdata := TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
+	if pdata == nil {
+		positionId = 0
+		seatNumber = ""
+		saleName = ""
+		return
+	}
+	saleData = *pdata
 	if TiDb.Count("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone, "is_assign": 1}) == 0 { //线索没销售进入,有销售走分配次数最少的逻辑
+		//线索不存在
 		if isGroup == 0 && isCommerce == 1 && cluename != "" { //非集团在工商库线索名不为空
-			//cdata := TiDb.Find("dwd_f_crm_clue_info", map[string]interface{}{"cluename": cluename, "is_assign": 1}, "", "", -1, -1)
-			cdata := TiDb.SelectBySql(`select  position_id,MAX(trail_time) as trail_time,max(last_ring_time) as last_ring_time,seatNumber,count(id) as count from   dwd_f_crm_clue_info where  cluename=? and  is_assign =1 GROUP BY  position_id `, cluename)
-			if cdata != nil && len(*cdata) > 0 { //找到了公司有人在跟进
-				isOk = true
-				pdata := TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)
-				if pdata == nil {
-					positionId = 0
-					seatNumber = ""
-					saleName = ""
-					return
+			companyList := FindRelatedData(cluename, isGroup, isCommerce)
+			paramArr := []string{}
+			companyArr := []interface{}{}
+			if len(companyList) > 0 {
+				for _, v := range companyList {
+					paramArr = append(paramArr, "?")
+					companyArr = append(companyArr, v)
 				}
-				saleData = *pdata
-				cdataNew := []map[string]interface{}{}
-				if len(*cdata) > 1 {
-					//可能有多个人跟进
-					personMap := map[int64]bool{}
-					upperLimitPersonMap := map[int64]bool{}
-					upperLimitCdataNew := []map[string]interface{}{}
-					upperLimitCount := 0
-					isFull := false
-					for _, m := range *cdata {
-						positionid1 := gconv.Int64(m["position_id"])
-						for _, v := range *pdata {
-							resign := common.IntAll(v["resign"])
-							assign_type := common.IntAll(v["assign_type"])
-							role_id := common.IntAll(v["role_id"])
-							if positionid1 == common.Int64All(v["position_id"]) {
-								if resign == 0 && (assign_type == 1 || role_id == 8) {
-									if !FindUpperLimit(gconv.String(positionid1), mode, true) {
-										personMap[positionid1] = true
-										m["saleName"] = common.ObjToString(v["name"])
-										cdataNew = append(cdataNew, m)
-									} else {
-										upperLimitCount++
-										upperLimitPersonMap[positionid1] = true
-										m["saleName"] = common.ObjToString(v["name"])
-										upperLimitCdataNew = append(upperLimitCdataNew, m)
-									}
-								}
-							}
+			}
+			clueClueStr := fmt.Sprintf("select  position_id,is_assign from   dwd_f_crm_clue_info where  cluename in (%s) order by   id ", strings.Join(paramArr, ","))
+			cdata := TiDb.SelectBySql(clueClueStr, companyArr...)
+			if cdata != nil && len(*cdata) > 0 { //找到关联公司所有信息
+				isOk = true
+				lockPositionId := int64(0)
+				for _, v := range *cdata {
+					isAssign := gconv.Int64(v["is_assign"])
+					if isAssign == 1 {
+						//已经分配过
+						if gconv.Int64(v["position_id"]) > 0 {
+							positionId = gconv.Int64(v["position_id"])
+							seatNumber = gconv.String(v["seatNumber"])
 						}
-
-					}
-					if len(cdataNew) == 0 && len(upperLimitCdataNew) > 0 {
-						//没有达上限的人数为0    全部都是达上限的
-						personMap = upperLimitPersonMap
-						cdataNew = upperLimitCdataNew
-						isFull = true
+					} else {
+						lockPositionId = gconv.Int64(v["lock_position_id"])
 					}
-					//查询是否都有没有离职
-					if len(personMap) != 0 && len(cdataNew) > 0 {
-						layout := "2006-01-02 15:04:05"
-						//有人没有离职
-						data := map[string]interface{}{}
-						trailTime := int64(0)
-						for _, m := range cdataNew {
-							currentTime := int64(0)
-							if gconv.String(m["trail_time"]) == "" {
-								continue
-							}
-							t, _ := time.Parse(layout, gconv.String(m["trail_time"]))
-							currentTime = t.Unix()
-							if currentTime > trailTime {
-								trailTime = currentTime
-								data = m
-							}
-						}
-						if trailTime == 0 {
-							//需要查看通话记录
-							lastRingTime := int64(0)
-							for _, m := range cdataNew {
-								currentTime := int64(0)
-								if gconv.String(m["last_ring_time"]) == "" {
-									continue
-								}
-								t, _ := time.Parse(layout, gconv.String(m["last_ring_time"]))
-								currentTime = t.Unix()
-								if currentTime > lastRingTime {
-									lastRingTime = currentTime
-									data = m
-								}
-							}
-							if lastRingTime != 0 {
-								positionId = common.Int64All(data["position_id"])
-								noticePositionId = positionId
-								seatNumber = common.ObjToString(data["seatNumber"])
-								saleName = common.ObjToString(data["saleName"])
-								if isFull {
-									isFreeze = true
-									noticePositionId = positionId
-									positionId = 0
-									seatNumber = ""
-									saleName = ""
-								}
-								return
-							} else {
-								count := 0
-								//线索数量判断
-								for i, v := range cdataNew {
-									if i == 0 {
-										count = gconv.Int(v["count"])
-										data = v
-									} else {
-										if count < gconv.Int(v["count"]) {
-											count = gconv.Int(v["count"])
-											data = v
-										}
-									}
-								}
-								positionId = common.Int64All(data["position_id"])
-								noticePositionId = positionId
-								saleName = common.ObjToString(data["saleName"])
-								seatNumber = common.ObjToString(data["seatNumber"])
-								if isFull {
-									isFreeze = true
-									noticePositionId = positionId
-									positionId = 0
-									seatNumber = ""
-									saleName = ""
-								}
-								return
-							}
-						} else {
-							positionId = common.Int64All(data["position_id"])
-							noticePositionId = positionId
-							seatNumber = common.ObjToString(data["seatNumber"])
-							saleName = common.ObjToString(data["saleName"])
-							if isFull {
-								isFreeze = true
-								noticePositionId = positionId
-								positionId = 0
-								seatNumber = ""
-								saleName = ""
-							}
-							return
+				}
+				if positionId == 0 && lockPositionId > 0 {
+					positionId = lockPositionId
+					for _, v := range *pdata {
+						if lockPositionId == gconv.Int64(v["position_id"]) {
+							seatNumber = gconv.String(v["seat_number"])
 						}
 					}
+				}
+				if FindUpperLimit(gconv.String(positionId), mode, true) {
+					isFreeze = true
+					positionId = 0
+					seatNumber = ""
+					saleName = ""
 				} else {
-					//只有一人跟进
-					//(1)该销售人员未离职,则继续分配给该销售人员(保持现状);
-					//该销售人员已离职,则随机分配
-					positionId = common.Int64All((*cdata)[0]["position_id"])
-					noticePositionId = positionId
-					seatNumber = common.ObjToString((*cdata)[0]["seatNumber"])
 					for _, v := range *pdata {
-						resign := common.IntAll(v["resign"])
-						assign_type := common.IntAll(v["assign_type"])
-						role_id := common.IntAll(v["role_id"])
 						if positionId == common.Int64All(v["position_id"]) {
-							if resign == 0 && (assign_type == 1 || role_id == 8) {
-								if FindUpperLimit(gconv.String(positionId), mode, true) {
-									isFreeze = true
-									positionId = 0
-									seatNumber = ""
-									saleName = ""
-									break
-								} else {
-									saleName = common.ObjToString(v["name"])
-									return
-								}
-
-							}
+							seatNumber = common.ObjToString(v["seat_number"])
+							saleName = common.ObjToString(v["name"])
 						}
 					}
+					return
 				}
+
 			}
 		}
 	}
@@ -1940,69 +1833,233 @@ func nextYearActivity() {
 	log.Println("开年活动处理结束")
 }
 
-// 查询关联公司
-func relatedCompany(companyName string) int64 {
-	sqlStr := fmt.Sprintf(`SELECT
-			position_id 
-		FROM
-			dwd_f_crm_clue_info 
-		WHERE
-			cluename IN ( SELECT companyName FROM ( SELECT b_name AS companyName FROM ent_relation WHERE a_name ="%s" UNION SELECT a_name AS companyName FROM ent_relation WHERE b_name = "%s") ) 
-			AND position_id > 0 
-			LIMIT 1`, companyName, companyName)
-	companyList := TiDb.SelectBySql(sqlStr)
-	if companyList == nil || len(*companyList) == 0 {
+// AddLockeStatusChange
+// 锁定状态变更记录
+// '锁定状态,1:已锁定,2:未锁定,3:无需锁定',
+func AddLockeStatusChange(tx *sql.Tx, clueId, oldPositionId, newPositionId, oldLockStatus int64, isGroup, isCommerce int, nowTime, clueName, operateType string) int64 {
+	// operateType 操作类型  add 新增   delete 删除
+	//需要批量查处理   后期处理
+	if oldLockStatus == 3 {
 		return 0
 	}
-	return gconv.Int64((*companyList)[0]["position_id"])
-}
-
-// 锁定状态变更
-func AddLockeStatusChange(tx *sql.Tx, clueId, positionId, oldLockStatus, lock_status int64, nowTime, clueName string) int64 {
-	//需要批量查处理   后期处理
-	if clueName != "" {
-		_, clueList := FindRelatedData(clueName)
-		for _, v := range clueList {
-			AddChangeRecord(nil, v, positionId, "lock_status", "基本信息变更", gconv.String(CodeLock[lock_status]), gconv.String(common.If(lock_status == 0, CodeLock[lock_status], CodeLock[lock_status])), nowTime, "", -1)
-		}
+	newLockStatus := LockStatusHandle(isGroup, isCommerce, oldLockStatus)
+	if clueName == "" {
+		log.Println("m")
+		return 0
 	}
-	return 1
-}
-
-// 锁定认变更
-func AddLockePersonChange(tx *sql.Tx, clueId, positionId int64, oldpersonName, personName, nowTime, clueName string) int64 {
-	//需要批量查处理   后期处理
-	if clueName != "" {
-		lockPositionId, clueList := FindRelatedData(clueName)
-		if lockPositionId != positionId {
-			log.Println(clueName, positionId, lockPositionId, "锁定人变更失败")
+	nowPositionId, clueList, lockClueList := FindRelatedClueData(clueName, isGroup, isCommerce)
+	if operateType == "add" && newPositionId == 0 {
+		//锁定人看有没有人员变更  没有人员变更从其他数据上边去
+		newPositionId = nowPositionId
+	}
+	if operateType == "add" && newPositionId > 0 {
+		//新增变更
+		//变更
+		if newLockStatus != oldLockStatus {
+			//状态变更
+			for _, v := range clueList {
+				nowLockStatus := gconv.Int64(v["lock_status"])
+				clueId := gconv.Int64(v["id"])
+				TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{
+					"id": clueId,
+				}, map[string]interface{}{
+					"lock_status":      newLockStatus,
+					"lock_position_id": newPositionId,
+				})
+				//新增进来的
+				if nowLockStatus != newLockStatus {
+					UpdateLockStatus(clueId, newLockStatus)
+					AddChangeRecord(nil, clueId, newPositionId, "lock_status", "基本信息变更", CodeLock[newLockStatus], CodeLock[oldLockStatus], nowTime, "", -1)
+				}
+				if oldPositionId != newPositionId {
+					//锁定人变更
+					UpdateLockPositionId(clueId, newPositionId)
+					AddChangeRecord(nil, clueId, newPositionId, "lock_position_id", "锁定人变更", FindPositionToName(newPositionId), FindPositionToName(oldPositionId), nowTime, "", -1)
+				}
+			}
+			return 1
+		}
+		if oldPositionId != newPositionId && newLockStatus == oldLockStatus {
+			//只变人员  不变状态
+			_, clueList, _ := FindRelatedClueData(clueName, isGroup, isCommerce)
+			for _, v := range clueList {
+				clueId := gconv.Int64(v["id"])
+				//锁定人变更
+				UpdateLockPositionId(clueId, newPositionId)
+				AddChangeRecord(nil, clueId, newPositionId, "lock_position_id", "锁定人变更", FindPositionToName(newPositionId), FindPositionToName(oldPositionId), nowTime, "", -1)
+			}
+		}
+	} else {
+		//全部删除
+		isChecked := false
+		if len(lockClueList) == 1 {
+			//判断是不是只有一条选中
+			id := gconv.Int64(lockClueList[0]["id"])
+			if id == clueId {
+				isChecked = true
+			}
 		}
-		for _, v := range clueList {
-			AddChangeRecord(nil, v, positionId, "lock_position_id", "锁定人变更", personName, oldpersonName, nowTime, "", positionId)
+		if isChecked {
+			//所有数据都要取消状态
+			newLockStatus := int64(2)
+			if isGroup == 1 && isCommerce != 3 {
+				newLockStatus = 3
+			}
+			for _, v := range clueList {
+				clueId := gconv.Int64(v["id"])
+				TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{
+					"id": clueId,
+				}, map[string]interface{}{
+					"lock_status":      newLockStatus,
+					"lock_position_id": 0,
+				})
+				if isGroup == 0 && isCommerce == 1 {
+					UpdateLockPositionId(clueId, oldPositionId)
+				}
+				AddChangeRecord(nil, clueId, oldPositionId, "lock_position_id", "锁定人变更", "/", FindPositionToName(oldPositionId), nowTime, "", -1)
+			}
 		}
 	}
 	return 1
 }
-func FindRelatedData(companyName string) (int64, []int64) {
-	sqlStr := fmt.Sprintf(`SELECT
-			position_id,id
+
+// 关联公司线索查询
+func FindRelatedClueData(companyName string, isGroup, isCommerce int) (int64, []map[string]interface{}, []map[string]interface{}) {
+	sqlStr := ""
+	if isGroup == 0 && isCommerce == 1 {
+		sqlStr = fmt.Sprintf(`SELECT
+			*
 		FROM
 			dwd_f_crm_clue_info 
 		WHERE
-			cluename IN ( SELECT companyName FROM ( SELECT b_name AS companyName FROM ent_relation WHERE a_name ="%s" UNION SELECT a_name AS companyName FROM ent_relation WHERE b_name = "%s") ) 
-			AND position_id > 0 
-			LIMIT 1`, companyName, companyName)
-	companyList := TiDb.SelectBySql(sqlStr)
-	if companyList == nil || len(*companyList) == 0 {
-		return 0, []int64{}
+			cluename IN ( SELECT companyName FROM ( SELECT b_name AS companyName FROM ent_relation WHERE a_name ="%s" UNION SELECT a_name AS companyName FROM ent_relation WHERE b_name = "%s") ) or cluename="%s" `, companyName, companyName, companyName)
+	} else {
+		sqlStr = fmt.Sprintf(`SELECT
+			*
+		FROM
+			dwd_f_crm_clue_info 
+		WHERE
+			cluename="%s" `, companyName)
+	}
+	clueList := TiDb.SelectBySql(sqlStr)
+	if clueList == nil || len(*clueList) == 0 {
+		return 0, []map[string]interface{}{}, []map[string]interface{}{}
 	}
 	positionId := int64(0)
-	clueList := []int64{}
-	for _, v := range *companyList {
+	var clueArr []map[string]interface{}
+	var lockClueArr []map[string]interface{}
+	for _, v := range *clueList {
 		if gconv.Int64(v["position_id"]) > 0 {
 			positionId = gconv.Int64(v["position_id"])
+			lockClueArr = append(lockClueArr, v)
+		}
+		clueArr = append(clueArr, v)
+	}
+	return positionId, clueArr, lockClueArr
+}
+
+// LockStatusHandle 锁定状态逻辑
+/*
+1 已锁定:“线索名称”为非集团公司且在工商库,且有销售人员在跟进(注:线索名称为非集团公司但不在工商库,即此时线索名称不规范或不完整,即使在私海,也非锁定状态);
+2 未锁定:不符合“无需锁定”和“已锁定”条件的,则为“未锁定”;
+3 无需锁定:“线索名称”为集团公司。
+*/
+func LockStatusHandle(isGroup, isCommerce int, oldLockStatus int64) int64 {
+	newLockStatus := int64(2)
+	if isGroup == 1 && isCommerce != 3 {
+		//无需锁定处理
+		if oldLockStatus != 3 {
+			newLockStatus = 3
+		}
+	} else if isGroup == 0 && isCommerce == 1 {
+		//已锁定处理
+		newLockStatus = 1
+	}
+	return newLockStatus
+}
+func FindRelatedData(companyName string, isGroup, isCommerce int) []string {
+	sqlStr := ""
+	if isGroup == 0 && isCommerce == 1 {
+		sqlStr = fmt.Sprintf(`SELECT companyName FROM 
+                       ( SELECT b_name AS companyName FROM ent_relation WHERE a_name ="%s" 
+                          UNION 
+                         SELECT a_name AS companyName FROM ent_relation WHERE b_name = "%s")`, companyName, companyName)
+	}
+	companyList := TiDb.SelectBySql(sqlStr)
+	if companyList == nil || len(*companyList) == 0 {
+		return []string{companyName}
+	}
+	var companyArr []string
+	for _, v := range *companyList {
+		companyName := gconv.String(v["companyName"])
+		companyArr = append(companyArr, companyName)
+	}
+	companyArr = append(companyArr, companyName)
+	return companyArr
+}
+
+// 老公司数据处理
+func ClueNameChange(oldCompanyName string, oldLockStatus int64, clueId int64) {
+	//老的需要清除一下
+	nowTime := time.Now().Format("2006-01-02 15:04:05")
+	isGroup, isCommerce := int(0), int(0)
+	if oldCompanyName == "" {
+		log.Println(oldCompanyName, "这个公司是空的")
+		return
+	}
+	//是否是集团
+	if c := TiDb.CountBySql(`select count(1) from group_company_name where company_name=?`, oldCompanyName); c > 0 {
+		isGroup = 1
+	}
+	//是否在工商库
+	if oldCompanyName != "" {
+		/*if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": oldCompanyName, "company_type": map[string]interface{}{"$ne": "个体工商户"}}); c > 0 {
+			isCommerce = 1
+		}*/
+		isCommerce = 1
+	}
+	//老的线索名称看看要不要处理
+	if isGroup == 0 && isCommerce == 1 {
+		if oldLockStatus == 1 {
+			//已锁定可能变未锁定
+			_, clueList, _ := FindRelatedClueData(oldCompanyName, isGroup, isCommerce)
+			//状态变更
+			isChange := false
+			for _, v := range clueList {
+				clueid := gconv.Int64(v["id"])
+				positionId := gconv.Int64(v["position_id"])
+				if clueid == clueId {
+					continue
+				}
+				if positionId > 0 {
+					isChange = true
+					break
+				}
+			}
+			if !isChange {
+				//说明还有其他能决定绑定id的
+				newLockStatus := int64(2)
+				for _, v := range clueList {
+					clueid := gconv.Int64(v["id"])
+					if clueid == clueId {
+						continue
+					}
+					positionId := gconv.Int64(v["position_id"])
+					TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{
+						"id": clueId,
+					}, map[string]interface{}{
+						"lock_status":      newLockStatus,
+						"lock_position_id": nil,
+					})
+					//新增进来的
+					UpdateLockStatus(clueid, newLockStatus)
+					AddChangeRecord(nil, clueid, 0, "lock_status", "基本信息变更", CodeLock[newLockStatus], CodeLock[oldLockStatus], nowTime, "", -1)
+					//锁定人变更
+					UpdateLockPositionId(clueid, 0)
+					AddChangeRecord(nil, clueid, 0, "lock_position_id", "锁定人变更", FindPositionToName(0), FindPositionToName(positionId), nowTime, "", -1)
+				}
+			}
 		}
-		clueList = append(clueList, gconv.Int64(v["id"]))
 	}
-	return positionId, clueList
+	return
 }

+ 6 - 2
clueSync/kc.go

@@ -17,9 +17,11 @@ import (
 )
 
 var (
-	CodeLock = map[int64]interface{}{
-		1: "无需锁定",
+	CodeLock = map[int64]string{
+		1: "锁定",
 		0: "/",
+		2: "未锁定",
+		3: "无需锁定",
 	}
 )
 
@@ -202,6 +204,7 @@ func kcJob(data map[string]interface{}) int {
 			customerPositionId, customerName := cAutoDraw(0, orderPositionId, salesperson, saleDep, true, csmdata)
 			log.Println("移交客成positionId", customerPositionId, customerName, saleDep, orderPositionId, salesperson, true)
 			oldName := gconv.String((*csmdata)["name"])
+			clueId := common.Int64All((*csmdata)["id"])
 			updateMap := map[string]interface{}{
 				"is_transfer":           0,
 				"is_renewal_protection": 0,
@@ -1314,6 +1317,7 @@ func kcClue(userId, uId, seatNumber, cluename, top_cluetype, sub_cluetype,
 		uodateId3 = AddChangeRecord(tx, clueId, positionId, "trailstatus", "基本信息变更", "新增", "商机线索", nowTime, "", -1)
 		uodateId4 = AddChangeRecord(tx, clueId, positionId, "top_cluetype", "基本信息变更", topname, "/", nowTime, "", -1)
 		uodateId5 = AddChangeRecord(tx, clueId, positionId, "sub_cluetype", "基本信息变更", subname, "/", nowTime, "", -1)
+		AddLockeStatusChange(tx, clueId, positionId, 0, 0, isGroup, isCommerce, cluename, nowTime, "add")
 		return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1
 	}) {
 		log.Println("线索分配成功")

+ 9 - 8
clueSync/main.go

@@ -7,10 +7,8 @@ import (
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/mysql"
 	"app.yhyue.com/moapp/jybase/redis"
-	"bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
 	"flag"
 	"fmt"
-	"github.com/robfig/cron"
 	"log"
 )
 
@@ -145,15 +143,18 @@ func main() {
 		MaxOpenConns: db.DataAnalysis.PollSize,
 		MaxIdleConns: db.DataAnalysis.MaxIdle,
 	}
-	DataAnalysisService.Init()
-	Es = elastic.NewEs(db.Es.Version, db.Es.Address, db.Es.DbSize, db.Es.UserName, db.Es.Password)
+	//DataAnalysisService.Init()
+	//Es = elastic.NewEs(db.Es.Version, db.Es.Address, db.Es.DbSize, db.Es.UserName, db.Es.Password)
 	redis.InitRedis(db.RedisServer)
 	Mgo = mongodb.NewMgo(db.Mgo.Address, db.Mgo.DbName, db.Mgo.DbSize)
 	MgoLog = mongodb.NewMgoWithUser(db.MgoLog.Address, db.MgoLog.DbName, db.MgoLog.User, db.MgoLog.Password, db.MgoLog.DbSize)
 	MgoQyxy = mongodb.NewMgoWithUser(db.MgoQyxy.Address, db.MgoQyxy.DbName, db.MgoQyxy.User, db.MgoQyxy.Password, db.MgoQyxy.DbSize)
-	InitArea()
+	//InitArea()
 	InitProduct(db.ProductArr)
-	if *mode == 1 {
+
+	ClueNameChange("河南碧桂园建设工程有限公司", 1, 1176118)
+
+	/*if *mode == 1 {
 		go ordersClue() //后台订单进线索
 		//一秒钟一次
 		go p.VarTimeTask.RunInSecondTimeLoop("1秒钟定时任务", "", "", db.CornExp8, true, true, nil, func() {
@@ -205,7 +206,7 @@ func main() {
 			autoExitSea()      //自动退海
 			Thaw()             //自动解冻处理
 			subscribeAddSync() //订阅增量
-			CustomerChange()
+			CustomerChange()   //客成人员变更发邮件
 		})
 		//5分钟一次
 		go p.VarTimeTask.RunInTimeLoop("5分钟定时任务3", "", "", db.CornExp6, true, true, nil, func() {
@@ -243,5 +244,5 @@ func main() {
 	} else {
 		users()
 		saleLeads()
-	}
+	}*/
 }

+ 78 - 0
clueSync/sqlOperate.go

@@ -0,0 +1,78 @@
+package main
+
+import (
+	"app.yhyue.com/moapp/jybase/common"
+	"database/sql"
+	"log"
+)
+
+// 新增动态
+func AddChangeRecord(tx *sql.Tx, clueId, positionId int64, changeField, changeType, newValue, oldValue, nowTime, BCPCID string, operatorId int64) int64 {
+	if BCPCID == "" {
+		BCPCID = common.GetRandom(32)
+	}
+	if tx == nil {
+		return TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
+			"clue_id":      clueId,
+			"position_id":  positionId,
+			"change_field": changeField,
+			"change_type":  changeType,
+			"old_value":    oldValue,
+			"new_value":    newValue,
+			"createtime":   nowTime,
+			"BCPCID":       BCPCID,
+			"operator_id":  operatorId,
+		})
+	}
+	return TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+		"clue_id":      clueId,
+		"position_id":  positionId,
+		"change_field": changeField,
+		"change_type":  changeType,
+		"old_value":    oldValue,
+		"new_value":    newValue,
+		"createtime":   nowTime,
+		"BCPCID":       BCPCID,
+		"operator_id":  operatorId,
+	})
+}
+
+// 通过职位id换人名
+func FindPositionToName(positionId int64) string {
+	data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and position_id = ? limit 1`, positionId)
+	if data != nil && len(*data) > 0 {
+		return common.ObjToString((*data)[0]["name"])
+	}
+	return ""
+}
+
+// 通过坐席号换人名
+func FindSeatNumberToName(seatNumber string) string {
+	data := TiDb.SelectBySql(`select name from dwd_f_crm_personnel_management where resign = 0 and seat_number = ? limit 1`, seatNumber)
+	if data != nil && len(*data) > 0 {
+		return common.ObjToString((*data)[0]["name"])
+	}
+	return ""
+}
+
+// 锁定人变更
+func UpdateLockPositionId(clueId, lockPositionId int64) {
+	ok := TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{
+		"id": clueId,
+	}, map[string]interface{}{
+		"lock_position_id": lockPositionId,
+	})
+	log.Println("锁定人信息修改:", clueId, lockPositionId, ok)
+	return
+}
+
+// 锁定状态变更
+func UpdateLockStatus(clueId, lockStatus int64) {
+	ok := TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{
+		"id": clueId,
+	}, map[string]interface{}{
+		"lock_status": lockStatus,
+	})
+	log.Println("锁定状态信息修改:", clueId, lockStatus, ok)
+	return
+}