Browse Source

Merge branch 'dev_v1.5.18_wh' of jianyu/datatools into feature/v1.5.18

王浩 5 tháng trước cách đây
mục cha
commit
e7d3cfbefb
6 tập tin đã thay đổi với 239 bổ sung1145 xóa
  1. 18 160
      clueSync/autoTask.go
  2. 4 18
      clueSync/everything.go
  3. 97 698
      clueSync/job.go
  4. 86 23
      clueSync/jobutil.go
  5. 34 245
      clueSync/kc.go
  6. 0 1
      clueSync/main.go

+ 18 - 160
clueSync/autoTask.go

@@ -103,15 +103,7 @@ func autoTask() {
 					"taskstatus": 0,
 					"tasksource": "超时未跟进自动加车",
 				}) {
-					TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":     clueId,
-						"position_id": position_id,
-						"change_type": "加入任务车",
-						"new_value":   "超时未跟进自动加车",
-						"createtime":  nowTime,
-						"BCPCID":      common.GetRandom(32),
-						"operator_id": -1,
-					})
+					AddChangeRecord(nil, clueId, position_id, "", "加入任务车", "超时未跟进自动加车", "", nowTime, "", -1)
 				}
 			}
 			return true
@@ -138,15 +130,7 @@ func autoTasks() {
 						"taskstatus": 0,
 						"tasksource": "即将到达下次跟进时间",
 					}) {
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":     clueId,
-							"position_id": position_id,
-							"change_type": "加入任务车",
-							"new_value":   "即将到达下次跟进时间",
-							"createtime":  nowTime2,
-							"BCPCID":      common.GetRandom(32),
-							"operator_id": -1,
-						})
+						AddChangeRecord(nil, clueId, position_id, "", "加入任务车", "即将到达下次跟进时间", "", nowTime2, "", -1)
 					}
 				}
 			}
@@ -199,26 +183,8 @@ GROUP BY
 			if TiDb.ExecTx("退出线索", func(tx *sql.Tx) bool {
 				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 := TiDb.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": -1,
-				})
-				recordId1 := TiDb.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":    GetPositionName(seatNumber),
-					"new_value":    "/",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
+				recordId2 := AddChangeRecord(tx, clueId, positionId, "", "退回公海", "所有人员私海已达上限", "", nowTime, "", -1)
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", GetPositionName(seatNumber), nowTime, "", -1)
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1
 			}) {
 				//发送消息,找wjh
@@ -255,46 +221,10 @@ func autoExitSea() {
 				// } else if is_assign == 0 {
 				// 	ok1 = TiDb.DeleteByTx(tx, "dwd_f_crm_open_sea", map[string]interface{}{"clue_id": clueId})
 				// }
-				recordId1 := TiDb.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":    GetPositionName(seatNumber),
-					"new_value":    "/",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				recordId2 := TiDb.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":  -1,
-				})
-				recordId3 := TiDb.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": -1,
-				})
-				recordId4 := TiDb.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": -1,
-				})
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", GetPositionName(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)
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
 			}) {
 				log.Println("自动退出线索成功")
@@ -314,46 +244,10 @@ func autoExitSea() {
 			// 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 := TiDb.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":    GetPositionName(seatNumber),
-					"new_value":    "/",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				recordId2 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  positionId,
-					"change_field": "trailstatus",
-					"change_type":  "基本信息变更",
-					"old_value":    common.If(trailstatus == "00", "无意向客户", "拒绝客户"),
-					"new_value":    "流失",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				recordId3 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "退出任务车",
-					"new_value":   common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海"),
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
-				recordId4 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": positionId,
-					"change_type": "退回公海",
-					"new_value":   common.If(trailstatus == "00", "无意向客户自动退回公海", "拒绝客户自动退回公海"),
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
+				recordId1 := AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", "/", GetPositionName(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)
 				return recordId > -1 && recordId1 > -1 && recordId2 > -1 && recordId3 > -1 && recordId4 > -1
 			}) {
 				log.Println("自动退出私海成功")
@@ -409,6 +303,7 @@ func Thaw() {
 				freezeInt64 = t.Unix()
 			}
 			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{}{
@@ -416,55 +311,18 @@ func Thaw() {
 					"position_id": 0,
 					"seatNumber":  nil,
 				}) {
-					TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  0,
-						"change_field": "position_id",
-						"change_type":  "所属人变更",
-						"old_value":    nameMap[positionId],
-						"new_value":    "/",
-						"createtime":   time.Now().Format(date.Date_Full_Layout),
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  -1,
-					})
+					AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", "/", nameMap[positionId], nowTime, "", -1)
 				}
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  0,
-					"change_field": "position_id",
-					"change_type":  "线索解冻",
-					"new_value":    "自动退回公海",
-					"createtime":   time.Now().Format(date.Date_Full_Layout),
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
+				AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "自动退回公海", "", nowTime, "", -1)
 			} 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{}{
 						"is_assign": 1,
 					}) {
-						TiDb.Insert("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":    nameMap[positionId],
-							"createtime":   time.Now().Format(date.Date_Full_Layout),
-							"BCPCID":       common.GetRandom(32),
-							"operator_id":  -1,
-						})
+						AddChangeRecord(nil, clueId, positionId, "position_id", "所属人变更", nameMap[positionId], "/", nowTime, "", -1)
 					}
-					TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":      clueId,
-						"position_id":  positionId,
-						"change_field": "position_id",
-						"change_type":  "线索解冻",
-						"new_value":    "销售人员私海线索已释放",
-						"createtime":   time.Now().Format(date.Date_Full_Layout),
-						"BCPCID":       common.GetRandom(32),
-						"operator_id":  -1,
-					})
+					AddChangeRecord(nil, clueId, positionId, "position_id", "线索解冻", "销售人员私海线索已释放", "", nowTime, "", -1)
 				}
 			}
 		}

+ 4 - 18
clueSync/everything.go

@@ -171,15 +171,7 @@ func everythingSync() {
 			if clueId > 0 && is_assign != 1 && is_transfer != 1 {
 				TiDb.Update("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": user_id}, map[string]interface{}{"belong_to": "0301"})
 				TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, map[string]interface{}{"is_assign": -1})
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": common.If(position_id > 0, position_id, -1),
-					"change_type": "退出公海",
-					"new_value":   "通过合作渠道一切就好" + fmt.Sprint(common.If(user_mold == 1 || user_mold == 2, "登录", "注册")),
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
+				AddChangeRecord(nil, clueId, gconv.Int64(common.If(position_id > 0, position_id, -1)), "", "退出公海", "通过合作渠道一切就好"+fmt.Sprint(common.If(user_mold == 1 || user_mold == 2, "登录", "注册")), "", nowTime, "", -1)
 			}
 		}
 		cfg.LastEverythingTime = common.ObjToString((*data)[len(*data)-1]["create_time"])
@@ -320,15 +312,9 @@ func saveEverything(user_id, phone, item, sourceName, sourceCode string) bool {
 	}
 	if clueId > 0 && is_assign != 1 && is_transfer != 1 {
 		TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, map[string]interface{}{"is_assign": -1})
-		TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-			"clue_id":     clueId,
-			"position_id": common.If(position_id > 0, position_id, -1),
-			"change_type": "退出公海",
-			"new_value":   "通过合作渠道一切就好登录",
-			"createtime":  nowTime,
-			"BCPCID":      common.GetRandom(32),
-			"operator_id": -1,
-		})
+		AddChangeRecord(nil, clueId, gconv.Int64(common.If(position_id > 0, position_id, -1)), "", "退出公海",
+			"通过合作渠道一切就好登录", "", nowTime, "", -1)
+
 	} else if is_assign == 1 || is_transfer == 1 {
 		isOk = true
 	}

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 97 - 698
clueSync/job.go


+ 86 - 23
clueSync/jobutil.go

@@ -3,6 +3,7 @@ package main
 import (
 	"app.yhyue.com/moapp/jybase/mail"
 	"app.yhyue.com/moapp/jybase/redis"
+	"database/sql"
 	"fmt"
 	"github.com/tealeg/xlsx"
 	"log"
@@ -51,6 +52,7 @@ func rderAcceptance() {
 						product = gconv.String(v1["field_value"])
 					}
 				}
+				//工单开始创建
 				if !WorkDataHandle(company, phone, demand, name, product, v) {
 					log.Println("工单创建失败")
 				}
@@ -58,6 +60,8 @@ func rderAcceptance() {
 		}
 	}
 }
+
+// 工单创建处理
 func WorkDataHandle(company, phone, demand, name, product string, acceptanceData map[string]interface{}) bool {
 	uId := ""
 	query := map[string]interface{}{}
@@ -80,14 +84,17 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 	isGroup, isCommerce := GetCompanyType(cluename, uId)
 	uCount, _ := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在
 	if uCount != nil && len(*uCount) > 0 {
+		//线索信息修改
 		isUpdate := gconv.Int64((*uCount)["is_artificially_modified"])
 		if isUpdate == 1 {
 			cluename = ""
 		}
 		clueId := gconv.Int64((*uCount)["id"])
 		positionId := gconv.Int64((*uCount)["position_id"])
+		lockPositionId := gconv.Int64((*uCount)["lock_position_id"])
 		trailstatus := gconv.String((*uCount)["trailstatus"])
 		IS_TRANSFER := gconv.Int64((*uCount)["IS_TRANSFER"])
+		//1客成0销售
 		if IS_TRANSFER == 1 {
 			//客成处理
 			//客成
@@ -96,7 +103,6 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 				"clue_id": clueId,
 			}, "position_id,name", "")
 			if customerMap != nil && len(*customerMap) > 0 {
-				//UpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", company, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
 				WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String((*uCount)["name"]), phone, "", "", "", "", "", "", gconv.String((*uCount)["seat_number"]), "", gconv.Int64((*uCount)["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, false, demand)
 				data = map[string]interface{}{
 					"type":        "kc",
@@ -107,17 +113,10 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 				log.Println("查询不到客成数据", clueId, uId)
 				return false
 			}
-			TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-				"clue_id":     clueId,
-				"position_id": (*customerMap)["position_id"],
-				"change_type": "加入任务车",
-				"new_value":   "咨询客服转客成",
-				"createtime":  time.Now().Format(date.Date_Full_Layout),
-				"BCPCID":      common.GetRandom(32),
-				"operator_id": -1,
-			})
+			AddChangeRecord(nil, clueId, gconv.Int64((*customerMap)["position_id"]), "", "加入任务车", "咨询客服转客成", "", time.Now().Format(date.Date_Full_Layout), "", -1)
 		} else {
-			if positionId == 0 {
+			//普通销售
+			if positionId == 0 && lockPositionId == 0 {
 				//线索变更
 				ok, data, saleData = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
 				if !ok {
@@ -126,7 +125,12 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 				}
 				WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
 			} else {
-				ok, data, saleData = FindPosition(positionId, trailstatus, gconv.String(acceptanceData["creator_time"]))
+				ok, data, saleData := true, map[string]interface{}{}, []map[string]interface{}{}
+				if positionId == 0 {
+					ok, data, saleData = FindPosition(lockPositionId, trailstatus, gconv.String(acceptanceData["creator_time"]))
+				} else {
+					ok, data, saleData = FindPosition(positionId, trailstatus, gconv.String(acceptanceData["creator_time"]))
+				}
 				if !ok {
 					log.Println(positionId, "用户查询失败")
 					return false
@@ -136,12 +140,12 @@ func WorkDataHandle(company, phone, demand, name, product string, acceptanceData
 				} else {
 					WorkUpdateClue(*uCount, saleData, "", "", uId, "5", "169", "新增线索", "主动咨询客服留资客户", cluename, name, gconv.String(data["name"]), phone, "", "", "", "", "", "", gconv.String(data["seat_number"]), "", gconv.Int64(data["position_id"]), "", "", "", []string{}, "", isGroup, isCommerce, true, demand)
 				}
-
 			}
 		}
 	} else {
 		//新增线索
-		ok, data, _ = FindPosition(0, "", gconv.String(acceptanceData["creator_time"]))
+		relatedId, _ := FindRelatedData(cluename)
+		ok, data, _ = FindPosition(relatedId, "", gconv.String(acceptanceData["creator_time"]))
 		if !ok {
 			return false
 		}
@@ -856,15 +860,7 @@ func userbase() {
 					})
 				}
 				if clueId > 0 {
-					TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-						"clue_id":     clueId,
-						"position_id": -1,
-						"change_type": "创建线索",
-						"new_value":   "系统自动创建",
-						"createtime":  nowTime,
-						"BCPCID":      common.GetRandom(32),
-						"operator_id": -1,
-					})
+					AddChangeRecord(nil, clueId, -1, "", "创建线索", "系统自动创建", "/", nowTime, "", -1)
 				}
 			}
 		}
@@ -1943,3 +1939,70 @@ 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 {
+		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)
+		}
+	}
+	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, "锁定人变更失败")
+		}
+		for _, v := range clueList {
+			AddChangeRecord(nil, v, positionId, "lock_position_id", "锁定人变更", personName, oldpersonName, nowTime, "", positionId)
+		}
+	}
+	return 1
+}
+func FindRelatedData(companyName string) (int64, []int64) {
+	sqlStr := fmt.Sprintf(`SELECT
+			position_id,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 {
+		return 0, []int64{}
+	}
+	positionId := int64(0)
+	clueList := []int64{}
+	for _, v := range *companyList {
+		if gconv.Int64(v["position_id"]) > 0 {
+			positionId = gconv.Int64(v["position_id"])
+		}
+		clueList = append(clueList, gconv.Int64(v["id"]))
+	}
+	return positionId, clueList
+}

+ 34 - 245
clueSync/kc.go

@@ -17,6 +17,13 @@ import (
 	"app.yhyue.com/moapp/jybase/mongodb"
 )
 
+var (
+	CodeLock = map[int64]interface{}{
+		1: "无需锁定",
+		0: "/",
+	}
+)
+
 func kcSync() {
 	log.Println("客户成功系统移交定时任务开始")
 	sql := `select * from dwd_f_userbase_order_info where (payable_money > 0 or  (payable_money=0 and  ( filter  like "%分期付款补充权益%"  or   filter  like "%原订单不支持开通多项权益%"  or    filter  like "%权益码兑换%")))
@@ -122,15 +129,7 @@ func kcJob(data map[string]interface{}) int {
 			"label":                common.If(position_id > 0, 1, nil),
 		})
 		if clueId > 0 {
-			TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-				"clue_id":     clueId,
-				"position_id": common.If(position_id > 0, position_id, -1),
-				"change_type": "创建线索",
-				"new_value":   "系统自动创建",
-				"createtime":  nowTime,
-				"BCPCID":      common.GetRandom(32),
-				"operator_id": -1,
-			})
+			AddChangeRecord(nil, clueId, gconv.Int64(common.If(position_id > 0, position_id, -1)), "", "创建线索", "系统自动创建", "", nowTime, "", -1)
 			clueData = TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
 			if clueData == nil || len(*clueData) == 0 {
 				return 0
@@ -241,36 +240,10 @@ func kcJob(data map[string]interface{}) int {
 				updateMap["is_task"] = 1
 			}
 			TiDb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, updateMap)
-			TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-				"clue_id":     clueId,
-				"position_id": customerPositionId,
-				"change_type": "成交客户移交",
-				"new_value":   "移交至客户成功组",
-				"createtime":  nowTime,
-				"BCPCID":      common.GetRandom(32),
-				"operator_id": -1,
-			})
+			AddChangeRecord(nil, clueId, customerPositionId, "", "成交客户移交", "移交至客户成功组", "", nowTime, "", -1)
 			if customerPositionId > 0 && (is_transfer == 1 || customerPositionId != gconv.Int64((*csmdata)["position_id"])) {
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  customerPositionId,
-					"change_field": "position_id",
-					"change_type":  "客户成功经理",
-					"old_value":    common.If(oldName != customerName, oldName, common.If(oldName == "", "/", oldName)),
-					"new_value":    customerName,
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": customerPositionId,
-					"change_type": "加入任务车",
-					"new_value":   "未建联",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
+				AddChangeRecord(nil, clueId, customerPositionId, "position_id", "客户成功经理", customerName, gconv.String(common.If(oldName != customerName, oldName, common.If(oldName == "", "/", oldName))), nowTime, "", -1)
+				AddChangeRecord(nil, clueId, customerPositionId, "", "加入任务车", "未建联", "", nowTime, "", -1)
 			}
 			if is_transfer == 1 || customerPositionId != gconv.Int64((*csmdata)["position_id"]) {
 				if customerName == db.KeCheng.Admin {
@@ -293,37 +266,11 @@ func kcJob(data map[string]interface{}) int {
 			cId = TiDb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap)
 			ok = TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName})
 			if customerPositionId > 0 {
-				updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": customerPositionId,
-					"change_type": "加入任务车",
-					"new_value":   "未建联",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
-				updateId3 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  customerPositionId,
-					"change_field": "position_id",
-					"change_type":  "客户成功经理",
-					"old_value":    "/",
-					"new_value":    customerName,
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
+				updateId1 = AddChangeRecord(tx, clueId, customerPositionId, "", "加入任务车", "未建联", "", nowTime, "", -1)
+				updateId3 = AddChangeRecord(tx, clueId, customerPositionId, "position_id", "客户成功经理", customerName, "/", nowTime, "", -1)
 			}
 			KcSend(orderCode, customerName)
-			updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-				"clue_id":     clueId,
-				"position_id": customerPositionId,
-				"change_type": "成交客户移交",
-				"new_value":   "移交至客户成功组",
-				"createtime":  nowTime,
-				"BCPCID":      common.GetRandom(32),
-				"operator_id": -1,
-			})
+			updateId2 := AddChangeRecord(tx, clueId, customerPositionId, "", "成交客户移交", "移交至客户成功组", "", nowTime, "", -1)
 			return cId > -1 && ok && updateId1 > -1 && updateId2 > -1 && updateId3 > -1
 		}) {
 			//TiDb.UpdateOrDeleteBySql(`update dwd_f_csm_customer_autodraw_record set count = count + 1 where name = ?`, name)
@@ -479,26 +426,11 @@ func kcAuto() {
 			taskstatus := common.IntAll(v["taskstatus"])
 			if relationship_building_way != 1 && inventory_way == 1 {
 				tasksources = "2"
-				TiDb.Insert("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": -1,
-				})
+				AddChangeRecord(nil, clueId, positionId, "", "加入任务车", "未盘点", "", nowTime, "", -1)
+
 			} else if inventory_way != 1 && training_way == 1 {
 				tasksources = "3"
-				TiDb.Insert("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": -1,
-				})
+				AddChangeRecord(nil, clueId, positionId, "", "加入任务车", "未培训", "", nowTime, "", -1)
 			}
 			if taskstatus == 1 {
 				tasksource = tasksources
@@ -655,26 +587,11 @@ func ordersClue() {
 						})
 					}
 					if clueId > 0 {
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":     clueId,
-							"position_id": common.If(positionId > 0, positionId, -1),
-							"change_type": "创建线索",
-							"new_value":   "系统自动创建",
-							"createtime":  nowTime,
-							"BCPCID":      common.GetRandom(32),
-							"operator_id": -1,
-						})
+						AddChangeRecord(nil, clueId, gconv.Int64(common.If(positionId > 0, positionId, -1)), "", "创建线索", "系统自动创建", "", nowTime, "", -1)
 						if is_assign == -3 {
 							//新增冻结记录
-							TiDb.Insert("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": -1,
-							})
+							AddChangeRecord(nil, clueId, positionId, "", "线索冻结", "销售人员私海已达上限", "", nowTime, "", -1)
+
 							//移交失败发送消息
 							if IsFreeze(source, pay_money, gconv.String(positionId), trailstatus) {
 								//发送邮件信息
@@ -763,46 +680,10 @@ func refundAuto() {
 			if isAllRefund {
 				//全退款了
 				TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_crm_clue_info SET is_unfollow=0,is_assign=0,trailstatus="01",position_id=null,seatNumber=null,updatetime=?,comeintime_open=?,comeinsource_open=1,level_open=3,next_trail_time=null,is_task=null,tasktime=null,taskstatus=null,comeinsource_private=null,tasksource=null,is_transfer=0 WHERE id = ?`, nowTime, nowTime, clueId)
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": common.If(kcposition_id > 0, kcposition_id, -1),
-					"change_type": "退回公海",
-					"new_value":   "成交客户申请退款",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  common.If(kcposition_id > 0, kcposition_id, -1),
-					"change_field": "trailstatus",
-					"change_type":  "基本信息变更",
-					"old_value":    "成交客户",
-					"new_value":    "商机线索",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":      clueId,
-					"position_id":  common.If(kcposition_id > 0, kcposition_id, -1),
-					"change_type":  "客户成功经理",
-					"change_field": "position_id",
-					"old_value":    name,
-					"new_value":    "/",
-					"createtime":   nowTime,
-					"BCPCID":       common.GetRandom(32),
-					"operator_id":  -1,
-				})
-				TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-					"clue_id":     clueId,
-					"position_id": common.If(kcposition_id > 0, kcposition_id, -1),
-					"change_type": "移交销售",
-					"new_value":   "成交客户申请退款",
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
+				AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "", "退回公海", "成交客户申请退款", "", nowTime, "", -1)
+				AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "trailstatus", "基本信息变更", "商机线索", "成交客户", nowTime, "", -1)
+				AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "position_id", "客户成功经理", "/", name, nowTime, "", -1)
+				AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "", "移交销售", "成交客户申请退款", "", nowTime, "", -1)
 				mailData[name] = append(mailData[name], map[string]interface{}{
 					"company_name": company_name,
 					"phone":        phone,
@@ -936,58 +817,14 @@ func refundAuto() {
 								"cluename":     cluename,
 							})
 						}
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":      clueId,
-							"position_id":  common.If(kcposition_id > 0, kcposition_id, -1),
-							"change_type":  "基本信息变更",
-							"old_value":    "成交客户",
-							"change_field": "trailstatus",
-							"new_value":    "商机线索",
-							"createtime":   nowTime,
-							"BCPCID":       common.GetRandom(32),
-							"operator_id":  -1,
-						})
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":     clueId,
-							"position_id": saleId,
-							"change_type": "加入任务车",
-							"new_value":   "线索自动分配-其他-成交客户续约失败",
-							"createtime":  nowTime,
-							"BCPCID":      common.GetRandom(32),
-							"operator_id": -1,
-						})
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":      clueId,
-							"position_id":  common.If(kcposition_id > 0, kcposition_id, -1),
-							"change_type":  "客户成功经理",
-							"change_field": "position_id",
-							"old_value":    name,
-							"new_value":    "/",
-							"createtime":   nowTime,
-							"BCPCID":       common.GetRandom(32),
-							"operator_id":  -1,
-						})
-						TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-							"clue_id":     clueId,
-							"position_id": saleId,
-							"change_type": "移交销售",
-							"new_value":   "成交客户续费失败,到期自动移交",
-							"createtime":  nowTime,
-							"BCPCID":      common.GetRandom(32),
-							"operator_id": -1,
-						})
+						AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "trailstatus", "基本信息变更", "商机线索", "", nowTime, "", -1)
+						AddChangeRecord(nil, clueId, saleId, "trailstatus", "加入任务车", "线索自动分配-其他-成交客户续约失败", "", nowTime, "", -1)
+						AddChangeRecord(nil, clueId, gconv.Int64(common.If(kcposition_id > 0, kcposition_id, -1)), "position_id", "客户成功经理", "/", name, nowTime, "", -1)
+						AddChangeRecord(nil, clueId, saleId, "", "移交销售", "成交客户续费失败,到期自动移交", "", nowTime, "", -1)
 						TiDb.UpdateOrDeleteBySql(`UPDATE dwd_f_csm_customer_info SET is_transfer=1 WHERE clue_id = ?`, clueId)
 						if isFull {
 							//新增冻结记录
-							TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
-								"clue_id":     clueId,
-								"position_id": saleId,
-								"change_type": "线索冻结",
-								"new_value":   "销售人员私海已达上限",
-								"createtime":  nowTime,
-								"BCPCID":      common.GetRandom(32),
-								"operator_id": -1,
-							})
+							AddChangeRecord(nil, clueId, saleId, "", "线索冻结", "销售人员私海已达上限", "", nowTime, "", -1)
 							//发送邮件
 							mailContent := ""
 							if cluename != "" {
@@ -1486,59 +1323,11 @@ func kcClue(userId, uId, seatNumber, cluename, top_cluetype, sub_cluetype,
 			"label":                1,
 			"labelChangeTime":      time.Now().Format("2006-01-02"),
 		})
-		uodateId1 = TiDb.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": -1,
-		})
-		uodateId2 = TiDb.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":    saleName,
-			"createtime":   nowTime,
-			"BCPCID":       common.GetRandom(32),
-			"operator_id":  -1,
-		})
-		uodateId3 = TiDb.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":  -1,
-		})
-		uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-			"clue_id":      clueId,
-			"position_id":  positionId,
-			"change_field": "top_cluetype",
-			"change_type":  "基本信息变更",
-			"old_value":    "/",
-			"new_value":    topname,
-			"createtime":   nowTime,
-			"BCPCID":       common.GetRandom(32),
-			"operator_id":  -1,
-		})
-		uodateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
-			"clue_id":      clueId,
-			"position_id":  positionId,
-			"change_field": "sub_cluetype", //222
-			"change_type":  "基本信息变更",
-			"old_value":    "/",
-			"new_value":    subname,
-			"createtime":   nowTime,
-			"BCPCID":       common.GetRandom(32),
-			"operator_id":  -1,
-		})
+		uodateId1 = AddChangeRecord(tx, clueId, positionId, "", "创建线索", "系统自动创建", "", nowTime, "", -1)
+		uodateId2 = AddChangeRecord(tx, clueId, positionId, "position_id", "所属人变更", saleName, "/", nowTime, "", -1)
+		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)
 		return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 && uodateId4 > -1 && uodateId5 > -1
 	}) {
 		log.Println("线索分配成功")

+ 0 - 1
clueSync/main.go

@@ -167,7 +167,6 @@ func main() {
 		})
 		//5分钟一次
 		go p.VarTimeTask.RunInTimeSection("5分钟定时任务1", db.CornExp2Start, db.CornExp2End, db.CornExp2, func(dayFirst bool) {
-
 			users()               //新注册用户进线索
 			saleLeads()           //留资进线索
 			eventReg()            //渠道

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác