xuzhiheng 2 жил өмнө
parent
commit
6cc097cbf8
3 өөрчлөгдсөн 308 нэмэгдсэн , 41 устгасан
  1. 267 41
      clueSync/job.go
  2. 5 0
      clueSync/main.go
  3. 36 0
      clueSync/tag.go

+ 267 - 41
clueSync/job.go

@@ -44,7 +44,7 @@ func users() {
 	if lastUserId != "" {
 		query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastUserId)}
 	}
-	query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63877e000000000000000000"), "$lte": mongodb.StringTOBsonId("63af0b000000000000000000")}
+	// query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("6145587145042f9c6444d02c")}
 	log.Println("query :", query)
 	iter := session.DB(cfg.Mgo.DbName).C("user").Find(&query).Sort("_id").Iter()
 	thisData := map[string]interface{}{}
@@ -74,7 +74,7 @@ func saleLeads() {
 	if lastId != "" {
 		query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId(lastId)}
 	}
-	query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63877e000000000000000000"), "$lte": mongodb.StringTOBsonId("63af0b000000000000000000")}
+	// query["_id"] = map[string]interface{}{"$gt": mongodb.StringTOBsonId("63f6049a4c172a1fd44ed3c8")}
 	log.Println("query :", query)
 
 	iter := session.DB(cfg.Mgo.DbName).C("saleLeads").Find(&query).Sort("_id").Iter()
@@ -94,7 +94,7 @@ func saleLeads() {
 func FormatData(data map[string]interface{}, item string) {
 	userId, uId, positionId, source, cluename, phone := common.ObjToString(data["user_id"]), "", "", 0, "", ""
 	role, industry, department, position, name, top_cluetype, sub_cluetype, follow_project_area, level := "", "", "", "", "", "", "", "", ""
-	query := map[string]interface{}{}
+	query, topname, subname := map[string]interface{}{}, "", ""
 	if item == "orders" {
 		if !mongodb.IsObjectIdHex(userId) {
 			positionId = userId
@@ -150,6 +150,10 @@ func FormatData(data map[string]interface{}, item string) {
 		name = common.ObjToString(data["name"])
 		// sourceCode = common.ObjToString(data["source"])
 	}
+	//线索名称为空用手机号代替
+	if cluename == "" {
+		cluename = phone
+	}
 	//域外用户和内部用户,不存线索
 	if source == 5 || source == 6 {
 		return
@@ -172,25 +176,25 @@ func FormatData(data map[string]interface{}, item string) {
 	}
 
 	//top_cluetype
-	top_cluetype, sub_cluetype, level = getClueType(item, data)
+	top_cluetype, sub_cluetype, level, topname, subname = getClueType(item, data)
 	//follow_project_area --> follow_project_monitor
 	follow_project_area = getAreaCode(userId)
-	//TODO seatNumber position_id
+	//seatNumber position_id
 	//自动分配规则暂不确定
 	log.Println("data +++", top_cluetype, sub_cluetype, level, follow_project_area)
-	position_id, seatNumber, saleName := autoDraw(level)
+	position_id, seatNumber, saleName, saleData := autoDraw(level)
 	log.Println("data -------", position_id, seatNumber, saleName)
 	if position_id > 0 && seatNumber != "" {
-		uCount := TiDb.Count("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId})
-		if uCount > 0 {
-			SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id)
+		uCount := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", "")
+		if uCount != nil && len(*uCount) > 0 {
+			UpdateClue(*uCount, saleData, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id)
 		} else {
-			SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id)
+			SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id)
 		}
 	}
 }
 
-func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64) {
+func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64) {
 	nowTime := time.Now().Format("2006-01-02 15:04:05")
 	nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
 	// BCPCID := common.GetRandom(32)
@@ -228,7 +232,7 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, sal
 			"task_time":    nowTime,
 			"tasktime":     common.If(item == "users", nowTimes, nowTime),
 			"taskstatus":   0,
-			"tasksource":   "线索自动分配" + "-" + top_cluetype + "-" + sub_cluetype,
+			"tasksource":   "线索自动分配" + "-" + topname + "-" + subname,
 		})
 		//变更记录
 		uodateId1 := TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
@@ -255,7 +259,8 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, sal
 			"clue_id":      clueId,
 			"position_id":  positionId,
 			"change_field": "trailstatus",
-			"change_type":  "基本信息变更",
+			"change_type":  "线索状态变更",
+			"old_value":    "商机线索",
 			"new_value":    "新增",
 			"createtime":   nowTime,
 			"BCPCID":       common.GetRandom(32),
@@ -265,7 +270,7 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, sal
 			"clue_id":     clueId,
 			"position_id": positionId,
 			"change_type": "加入任务车",
-			"new_value":   "线索自动分配" + "-" + top_cluetype + "-" + sub_cluetype,
+			"new_value":   "线索自动分配" + "-" + topname + "-" + subname,
 			"createtime":  nowTime,
 			"BCPCID":      common.GetRandom(32),
 			"operator_id": -1,
@@ -274,30 +279,239 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, sal
 	})
 }
 
-func UpdateClue(item, userId, uId, top_cluetype, sub_cluetype, cluename, name, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64) {
+func UpdateClue(data map[string]interface{}, saleData []map[string]interface{}, item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber string, positionId int64) {
 	nowTime := time.Now().Format("2006-01-02 15:04:05")
-	// nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
-	TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
+	nowTimes := time.Unix(time.Now().Unix()+3600*12, 0).Format("2006-01-02 15:04:05")
+	//trailstatus = 无意向 为变更线索状态
+	//没有变更所属人查任务车,有车不变
+	trailstatus := common.ObjToString(data["trailstatus"])
+	clueId := common.Int64All(data["id"])
+	is_assign := common.IntAll(data["is_assign"])
+	oldsaleName := ""
+	clueUpdateData := map[string]interface{}{
+		"is_assign":           1,
+		"updatetime":          nowTime,
+		"cluename":            cluename,
+		"top_cluetype":        top_cluetype,
+		"sub_cluetype":        sub_cluetype,
+		"trailstatus":         "01",
+		"position":            position,
+		"department":          department,
+		"industry":            industry,
+		"follow_project_area": follow_project_area,
+		"role":                role,
+	}
+	seaUpdateData := map[string]interface{}{
+		"comeinsource": 2,
+		"is_task":      1,
+		"task_time":    nowTime,
+		"tasktime":     common.If(item == "users", nowTimes, nowTime),
+		"taskstatus":   0,
+		"tasksource":   "线索来源自动更新" + "-" + topname + "-" + subname,
+	}
+	insertSeaData := map[string]interface{}{
+		"clue_id":      clueId,
+		"comeintime":   nowTime,
+		"comeinsource": 2,
+		"is_task":      1,
+		"task_time":    nowTime,
+		"tasktime":     common.If(item == "users", nowTimes, nowTime),
+		"taskstatus":   0,
+		"tasksource":   "线索来源自动更新" + "-" + topname + "-" + subname,
+	}
+	if trailstatus == "00" || trailstatus == "01" {
+		clueUpdateData["seatNumber"] = seatNumber
+		clueUpdateData["position_id"] = positionId
+		seaUpdateData["seatNumber"] = seatNumber
+		seaUpdateData["position_id"] = positionId
+		insertSeaData["seatNumber"] = seatNumber
+		insertSeaData["position_id"] = positionId
+		for _, v := range saleData {
+			if common.ObjToString(data["seatNumber"]) == common.ObjToString(v["seatNumber"]) {
+				oldsaleName = common.ObjToString(v["name"])
+			}
+		}
+	} else {
+		insertSeaData["seatNumber"] = common.ObjToString(data["seatNumber"])
+		insertSeaData["position_id"] = common.Int64All(data["position_id"])
+	}
+	if TiDb.ExecTx("更新线索", func(tx *sql.Tx) bool {
 		//线索
-		ok := TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, map[string]interface{}{
-			"seatNumber":          seatNumber,
-			"position_id":         positionId,
-			"is_assign":           1,
-			"updatetime":          nowTime,
-			"cluename":            cluename,
-			"top_cluetype":        top_cluetype,
-			"sub_cluetype":        sub_cluetype,
-			"trailstatus":         "01",
-			"name":                name,
-			"phone":               phone,
-			"position":            position,
-			"department":          department,
-			"industry":            industry,
-			"follow_project_area": follow_project_area,
-			"role":                role,
+		ok := TiDb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData)
+		ok1, seaId := false, int64(0)
+		if is_assign == 1 {
+			ok1 = TiDb.UpdateByTx(tx, "dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, seaUpdateData)
+		} else {
+			seaId = TiDb.InsertByTx(tx, "dwd_f_crm_private_sea", insertSeaData)
+		}
+		updateId1, updateId2, updateId3, updateId4, updateId5 := int64(0), int64(0), int64(0), int64(0), int64(0)
+		updateId6, updateId7, updateId8, updateId9, updateId10 := int64(0), int64(0), int64(0), int64(0), int64(0)
+		//变更记录
+		if trailstatus == "00" || trailstatus == "01" {
+			updateId1 = 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":    oldsaleName,
+				"new_value":    saleName,
+				"createtime":   nowTime,
+				"BCPCID":       common.GetRandom(32),
+				"operator_id":  -1,
+			})
+			if trailstatus == "00" {
+				updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+					"clue_id":      clueId,
+					"position_id":  common.Int64All(data["position_id"]),
+					"change_field": "trailstatus",
+					"change_type":  "线索状态变更",
+					"old_value":    "无意向客户",
+					"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":  positionId,
+					"change_field": "trailstatus",
+					"change_type":  "线索状态变更",
+					"old_value":    "商机线索",
+					"new_value":    "新增",
+					"createtime":   nowTime,
+					"BCPCID":       common.GetRandom(32),
+					"operator_id":  -1,
+				})
+				updateId4 = 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,
+				})
+			} else {
+				updateId2 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+					"clue_id":      clueId,
+					"position_id":  common.Int64All(data["position_id"]),
+					"change_field": "trailstatus",
+					"change_type":  "线索状态变更",
+					"old_value":    "商机线索",
+					"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":  positionId,
+					"change_field": "trailstatus",
+					"change_type":  "线索状态变更",
+					"old_value":    "商机线索",
+					"new_value":    "新增",
+					"createtime":   nowTime,
+					"BCPCID":       common.GetRandom(32),
+					"operator_id":  -1,
+				})
+			}
+		}
+		updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+			"clue_id":     clueId,
+			"position_id": positionId,
+			"change_type": "加入任务车",
+			"new_value":   "线索来源自动更新" + "-" + topname + "-" + subname,
+			"createtime":  nowTime,
+			"BCPCID":      common.GetRandom(32),
+			"operator_id": -1,
 		})
-		return ok
-	})
+		BCPCID := common.GetRandom(32)
+		old_name := common.ObjToString(data["name"])
+		old_position := common.ObjToString(data["position"])
+		old_department := common.ObjToString(data["department"])
+		old_follow_project_area := common.ObjToString(data["follow_project_area"])
+		old_role := common.ObjToString(data["role"])
+		if old_name != name {
+			updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  positionId,
+				"change_field": "name",
+				"change_type":  "基本信息变更",
+				"old_value":    old_name,
+				"new_value":    name,
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_position != position {
+			updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  positionId,
+				"change_field": "position",
+				"change_type":  "基本信息变更",
+				"old_value":    old_position,
+				"new_value":    position,
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_department != department {
+			updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  positionId,
+				"change_field": "department",
+				"change_type":  "基本信息变更",
+				"old_value":    old_department,
+				"new_value":    department,
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_role != role {
+			updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  positionId,
+				"change_field": "role",
+				"change_type":  "基本信息变更",
+				"old_value":    old_role,
+				"new_value":    role,
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_follow_project_area != follow_project_area {
+			updateId10 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  positionId,
+				"change_field": "follow_project_area",
+				"change_type":  "基本信息变更",
+				"old_value":    old_follow_project_area,
+				"new_value":    follow_project_area,
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		return ok && ok1 && seaId > -1 && updateId1 > -1 && updateId2 > -1 && updateId3 > -1 && updateId4 > -1 && updateId5 > -1 && updateId6 > -1 && updateId7 > -1 && updateId8 > -1 && updateId9 > -1 && updateId10 > -1
+	}) {
+		log.Println("线索更新成功")
+		if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {
+			TiDb.Insert("dwd_f_userbase_contacts", map[string]interface{}{
+				"status":      0,
+				"is_delete":   1,
+				"createtime":  nowTime,
+				"updatetime":  nowTime,
+				"phone":       phone,
+				"baseinfo_id": uId,
+			})
+		}
+	}
 }
 
 func getAreaCode(userId string) (code string) {
@@ -320,24 +534,31 @@ func getAreaCode(userId string) (code string) {
 	return
 }
 
-func getClueType(item string, data map[string]interface{}) (pcode, code, level string) {
+func getClueType(item string, data map[string]interface{}) (pcode, code, level, topname, subname string) {
 	if item == "orders" {
 		productType := common.ObjToString(data["product_type"])
 		pcode = "1"
 		level = "A"
+		topname = "提交订单未支付"
 		if productType == "VIP订阅" {
 			code = "6"
+			subname = "超级订阅"
 		} else if productType == "大会员" {
 			code = "7"
+			subname = "大会员"
 		} else if productType == "数据流量包" {
 			code = "8"
+			subname = "数据流量包"
 		} else if productType == "历史数据" {
 			code = "9"
+			subname = "数据自助导出"
 		}
 	} else if item == "users" {
 		pcode = "4"
 		code = "154"
 		level = "C"
+		topname = "新增注册"
+		subname = "新增注册用户"
 	} else if item == "saleLeads" {
 		sourceCode := common.ObjToString(data["source"])
 		if sourceCode != "" {
@@ -346,6 +567,11 @@ func getClueType(item string, data map[string]interface{}) (pcode, code, level s
 				pcode = common.ObjToString((*codeData)["pcode"])
 				code = common.ObjToString((*codeData)["code"])
 				level = common.ObjToString((*codeData)["clue_level"])
+				subname = common.ObjToString((*codeData)["name"])
+				pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "")
+				if pcodeData != nil && len(*pcodeData) > 0 {
+					topname = common.ObjToString((*pcodeData)["name"])
+				}
 			}
 		}
 	}
@@ -353,13 +579,14 @@ func getClueType(item string, data map[string]interface{}) (pcode, code, level s
 }
 
 // 获取自动分配的人
-func autoDraw(mode string) (positionId int64, seatNumber, saleName string) {
+func autoDraw(mode string) (positionId int64, seatNumber, saleName string, saleData []map[string]interface{}) {
 	query := `select * from jy_salesperson_info where status = 0 and position != 0`
 	if mode == "A" || mode == "B" {
 		query += ` and is_complete = 1`
 	}
 	data := TiDb.SelectBySql(query)
 	if data != nil && len(*data) > 0 {
+		saleData = *data
 		sql := "select * from dwd_f_crm_clue_autodraw_record where clue_level = ?"
 		countData := TiDb.SelectBySql(sql, mode)
 		if countData != nil && len(*countData) > 0 {
@@ -377,10 +604,9 @@ func autoDraw(mode string) (positionId int64, seatNumber, saleName string) {
 					saleName = common.ObjToString(v["name"])
 					rData := TiDb.FindOne("dwd_f_crm_clue_autodraw_record", map[string]interface{}{"clue_level": mode}, "", "count asc")
 					TiDb.Insert("dwd_f_crm_clue_autodraw_record", map[string]interface{}{
-						"position_id": positionId,
-						"seatNumber":  seatNumber,
-						"clue_level":  mode,
-						"count":       common.Int64All((*rData)["count"]),
+						"seatNumber": seatNumber,
+						"clue_level": mode,
+						"count":      common.Int64All((*rData)["count"]),
 					})
 					break
 				}

+ 5 - 0
clueSync/main.go

@@ -3,6 +3,7 @@ package main
 import (
 	"flag"
 	"fmt"
+	"log"
 
 	"app.yhyue.com/moapp/jybase/mongodb"
 
@@ -26,6 +27,7 @@ var (
 func main() {
 	flag.Parse()
 	_ = config.LoadConfigWithYaml(*configFile, cfg)
+	log.Println("cfg ", cfg)
 	TiDb = &mysql.Mysql{
 		Address:      cfg.TiDb.Host + ":" + fmt.Sprint(cfg.TiDb.Port),
 		UserName:     cfg.TiDb.User,
@@ -86,5 +88,8 @@ func main() {
 		users()
 	} else if *mode == 3 {
 		saleLeads()
+	} else if *mode == 4 {
+		saleLeads()
+		users()
 	}
 }

+ 36 - 0
clueSync/tag.go

@@ -0,0 +1,36 @@
+package main
+
+import (
+	"log"
+)
+
+func tagSync() {
+	//dwd_f_userbase_baseinfo -->l_registedate 注册日期 -->userid
+	//dwd_f_userbase_visit_info -->date 访问时间 -->userid
+	//dwd_f_userbase_search_info -->search_area去重 jianyu_subjectdb_test.d_area_code -->userid
+	//dwd_f_userbase_search_info -->search_word去重 -->userid
+	//dwd_f_userbase_subscribe_info -->subscribe_areas去重 jianyu_subjectdb_test.d_area_code -->userid
+	//dwd_f_userbase_subscribe_info -->subscribe_keywords去重 -->userid
+	//dwd_f_userbase_order_info -->product_type去重 -->userid
+	//dwd_f_userbase_order_info -->product_type=VIP订阅 vip_endtime 一周内到期、一月内到期 -->userid
+	//dwd_f_userbase_event_info -->createtime查昨天有没有数据, 有数据-昨日浏览过 没数据-昨日未浏览 -->userid
+	log.Println("用户标签定时任务开始")
+	TiDb.SelectByBath(500, func(l *[]map[string]interface{}) bool {
+		for _, v := range *l {
+			FormatTag(v)
+		}
+		return true
+	}, `select l_registedate,userid from dwd_f_userbase_baseinfo`)
+	log.Println("用户标签定时任务结束")
+}
+
+func FormatTag(data map[string]interface{}) {
+	// registedate := common.Int64All(v["l_registedate"])
+	// userId := common.ObjToString(v["userid"])
+	// uId := common.ObjToString(v["uid"])
+	// TiDb
+}
+
+func SaveTag(data map[string]interface{}) {
+
+}