Browse Source

增加标签需求

xuzhiheng 2 years ago
parent
commit
f469394d88
6 changed files with 466 additions and 100 deletions
  1. 46 41
      clueSync/config.go
  2. 52 0
      clueSync/config.json
  3. 1 0
      clueSync/config.yaml
  4. 119 37
      clueSync/job.go
  5. 36 12
      clueSync/main.go
  6. 212 10
      clueSync/tag.go

+ 46 - 41
clueSync/config.go

@@ -2,50 +2,55 @@ package main
 
 type (
 	Config struct {
-		CornExp1    string `yaml:"cornexp1"`
-		CornExp2    string `yaml:"cornexp2"`
-		CornExp3    string `yaml:"cornexp3"`
-		LastOrderId int    `yaml:"lastOrderId"`
-		LastUserId  string `yaml:"lastUserId"`
-		LastId      string `yaml:"lastId"`
-		TiDb        struct {
-			Host        string `yaml:"host"`
-			Port        int    `yaml:"port"`
-			Database    string `yaml:"database"`
-			User        string `yaml:"user"`
-			Password    string `yaml:"password"`
-			PollSize    int    `yaml:"poolsize"`
-			MaxIdle     int    `yaml:"maxidle"`
-			MaxLeftTime int    `yaml:"maxleft"`
-		} `yaml:"tiDb"`
+		CornExp1      string `json:"cornexp1"`
+		CornExp2      string `json:"cornexp2"`
+		CornExp3      string `json:"cornexp3"`
+		CornExp4      string `json:"cornexp4"`
+		CornExp5      string `json:"cornexp5"`
+		LastOrderId   int    `json:"lastOrderId"`
+		LastUserId    string `json:"lastUserId"`
+		LastId        string `json:"lastId"`
+		LastOrderTime string `json:"lastOrderTime"`
+		LastUserTime  string `json:"lastUserTime"`
+		CountLimit    int64  `json:"countLimit"`
+		TiDb          struct {
+			Host        string `json:"host"`
+			Port        int    `json:"port"`
+			Database    string `json:"database"`
+			User        string `json:"user"`
+			Password    string `json:"password"`
+			PollSize    int    `json:"poolsize"`
+			MaxIdle     int    `json:"maxidle"`
+			MaxLeftTime int    `json:"maxleft"`
+		} `json:"tiDb"`
 		BaseService struct {
-			Host        string `yaml:"host"`
-			Port        int    `yaml:"port"`
-			Database    string `yaml:"database"`
-			User        string `yaml:"user"`
-			Password    string `yaml:"password"`
-			PollSize    int    `yaml:"poolsize"`
-			MaxIdle     int    `yaml:"maxidle"`
-			MaxLeftTime int    `yaml:"maxleft"`
-		} `yaml:"baseService"`
+			Host        string `json:"host"`
+			Port        int    `json:"port"`
+			Database    string `json:"database"`
+			User        string `json:"user"`
+			Password    string `json:"password"`
+			PollSize    int    `json:"poolsize"`
+			MaxIdle     int    `json:"maxidle"`
+			MaxLeftTime int    `json:"maxleft"`
+		} `json:"baseService"`
 		Mysql struct {
-			Host        string `yaml:"host"`
-			Port        int    `yaml:"port"`
-			Database    string `yaml:"database"`
-			User        string `yaml:"user"`
-			Password    string `yaml:"password"`
-			PollSize    int    `yaml:"poolsize"`
-			MaxIdle     int    `yaml:"maxidle"`
-			MaxLeftTime int    `yaml:"maxleft"`
-		} `yaml:"mysql"`
+			Host        string `json:"host"`
+			Port        int    `json:"port"`
+			Database    string `json:"database"`
+			User        string `json:"user"`
+			Password    string `json:"password"`
+			PollSize    int    `json:"poolsize"`
+			MaxIdle     int    `json:"maxidle"`
+			MaxLeftTime int    `json:"maxleft"`
+		} `json:"mysql"`
 		Mgo struct {
-			Address string `yaml:"address"`
-			DbName  string `yaml:"dbName"`
-			DbSize  int    `yaml:"dbSize"`
-		} `yaml:"mgo"`
+			Address string `json:"address"`
+			DbName  string `json:"dbName"`
+			DbSize  int    `json:"dbSize"`
+		} `json:"mgo"`
 		Es struct {
-			Address string `yaml:"address"`
-			DbSize  int    `yaml:"dbSize"`
-		} `yaml:"es"`
+			Address string `json:"address"`
+			DbSize  int    `json:"dbSize"`
+		} `json:"es"`
 	}
 )

+ 52 - 0
clueSync/config.json

@@ -0,0 +1,52 @@
+{
+	"cornexp1": "0 */30 * * * ?",
+	"cornexp2": "0 */10 * * * ?",
+	"cornexp3": "0 */5 * * * ?",
+	"cornexp4": "0 0 0 /1 * ? ",
+	"cornexp5": "0 */5 * * * ?",
+	"lastOrderId": 172463,
+	"lastUserId": "6440f0e37242fb7cbc685b7b",
+	"lastId": "6440faf7bd4ea1862af84c56",
+	"lastOrderTime": "2023-04-23 00:00:00",
+	"lastUserTime": "2023-04-23 00:00:00",
+	"countLimit": 1000,
+	"tiDb": {
+		"host": "127.0.0.1",
+		"port": 4000,
+		"database": "jianyu_subjectdb_test",
+		"user": "root",
+		"password": "Tibi#20211222",
+		"poolsize": 20,
+		"maxidle": 40,
+		"maxleft": 40
+	},
+	"baseService": {
+		"host": "192.168.3.217",
+		"port": 4000,
+		"database": "base_service",
+		"user": "root",
+		"password": "=PDT49#80Z!RVv52_z",
+		"poolsize": 20,
+		"maxidle": 40,
+		"maxleft": 40
+	},
+	"mysql": {
+		"host": "192.168.3.149",
+		"port": 3306,
+		"database": "jianyu",
+		"user": "root",
+		"password": "Topnet123",
+		"poolsize": 20,
+		"maxidle": 40,
+		"maxleft": 40
+	},
+	"mgo": {
+		"address": "192.168.3.206:27080",
+		"dbName": "qfw",
+		"dbSize": 20
+	},
+	"es": {
+		"address": "http://192.168.3.206:9800",
+		"dbSize": 20
+	}
+}

+ 1 - 0
clueSync/config.yaml

@@ -4,6 +4,7 @@ cornexp3: "0 */5 * * * ?"
 lastOrderId: 1660438800
 lastUserId: 5751076de3ec62b1697b737e
 lastId: 5751076de3ec62b1697b737e
+countLimit: 1000
 tiDb:
   host: 127.0.0.1
   port: 4000

+ 119 - 37
clueSync/job.go

@@ -28,6 +28,7 @@ func orders() {
 		}
 	}
 	cfg.LastOrderId = lastOrderId
+	common.WriteSysConfig(&cfg)
 	log.Println("未支付订单定时任务结束")
 }
 
@@ -51,12 +52,16 @@ func users() {
 
 	for {
 		if !iter.Next(&thisData) {
-			lastUserId = mongodb.BsonIdToSId(thisData["_id"])
 			break
 		}
-		FormatData(thisData, "users")
+		ok1, _ := FormatData(thisData, "users")
+		if !ok1 {
+			common.WriteSysConfig(&cfg)
+			break
+		}
+		cfg.LastUserId = mongodb.BsonIdToSId(thisData["_id"])
 	}
-	cfg.LastUserId = lastUserId
+	common.WriteSysConfig(&cfg)
 	log.Println("新注册用户定时任务结束")
 }
 
@@ -82,16 +87,16 @@ func saleLeads() {
 
 	for {
 		if !iter.Next(&thisData) {
-			lastId = mongodb.BsonIdToSId(thisData["_id"])
 			break
 		}
+		cfg.LastId = mongodb.BsonIdToSId(thisData["_id"])
 		FormatData(thisData, "saleLeads")
 	}
-	cfg.LastId = lastId
+	common.WriteSysConfig(&cfg)
 	log.Println("用户留资定时任务结束")
 }
 
-func FormatData(data map[string]interface{}, item string) {
+func FormatData(data map[string]interface{}, item string) (bool, bool) {
 	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, topname, subname := map[string]interface{}{}, "", ""
@@ -154,9 +159,12 @@ func FormatData(data map[string]interface{}, item string) {
 	if cluename == "" {
 		cluename = phone
 	}
-	//域外用户和内部用户,不存线索
-	if source == 5 || source == 6 {
-		return
+	//域外用户和内部用户和没有手机号,不存线索
+	if source == 5 || source == 6 || phone == "" {
+		return true, false
+	}
+	if uId == "" {
+		return false, false
 	}
 	//不是留资的要查一遍留资
 	if item == "orders" {
@@ -185,6 +193,9 @@ func FormatData(data map[string]interface{}, item string) {
 	position_id, seatNumber, saleName, saleData := autoDraw(level)
 	log.Println("data -------", position_id, seatNumber, saleName)
 	if position_id > 0 && seatNumber != "" {
+		if TiDb.Count("dwd_f_crm_private_sea", map[string]interface{}{"position_id": position_id}) >= cfg.CountLimit {
+			return true, false
+		}
 		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)
@@ -192,6 +203,7 @@ func FormatData(data map[string]interface{}, item string) {
 			SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, cluename, name, saleName, phone, position, department, industry, follow_project_area, role, seatNumber, position_id)
 		}
 	}
+	return true, true
 }
 
 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) {
@@ -259,7 +271,7 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, c
 			"clue_id":      clueId,
 			"position_id":  positionId,
 			"change_field": "trailstatus",
-			"change_type":  "线索状态变更",
+			"change_type":  "基本信息变更",
 			"old_value":    "商机线索",
 			"new_value":    "新增",
 			"createtime":   nowTime,
@@ -281,20 +293,33 @@ func SaveClue(item, userId, uId, top_cluetype, sub_cluetype, topname, subname, c
 
 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")
+	// 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 := ""
+	oldsaleName, oldTaskTime, taskTime := "", "", ""
+	//已在任务车的判断任务时间
+	privateData := TiDb.FindOne("dwd_f_crm_private_sea", map[string]interface{}{"clue_id": clueId}, "", "")
+	if privateData != nil && len(*privateData) > 0 {
+		oldTaskTime = common.ObjToString((*privateData)["tasktime"])
+	}
+	if oldTaskTime != "" {
+		t1, err := time.Parse("2006-01-02 15:04:05", oldTaskTime)
+		if err == nil && time.Now().Before(t1) {
+			taskTime = oldTaskTime
+		} else {
+			taskTime = nowTime
+		}
+	}
 	clueUpdateData := map[string]interface{}{
 		"is_assign":           1,
 		"updatetime":          nowTime,
 		"cluename":            cluename,
 		"top_cluetype":        top_cluetype,
 		"sub_cluetype":        sub_cluetype,
-		"trailstatus":         "01",
+		"name":                name,
 		"position":            position,
 		"department":          department,
 		"industry":            industry,
@@ -305,7 +330,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		"comeinsource": 2,
 		"is_task":      1,
 		"task_time":    nowTime,
-		"tasktime":     common.If(item == "users", nowTimes, nowTime),
+		"tasktime":     taskTime,
 		"taskstatus":   0,
 		"tasksource":   "线索来源自动更新" + "-" + topname + "-" + subname,
 	}
@@ -315,13 +340,14 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		"comeinsource": 2,
 		"is_task":      1,
 		"task_time":    nowTime,
-		"tasktime":     common.If(item == "users", nowTimes, nowTime),
+		"tasktime":     taskTime,
 		"taskstatus":   0,
 		"tasksource":   "线索来源自动更新" + "-" + topname + "-" + subname,
 	}
 	if trailstatus == "00" || trailstatus == "01" {
 		clueUpdateData["seatNumber"] = seatNumber
 		clueUpdateData["position_id"] = positionId
+		clueUpdateData["trailstatus"] = "01"
 		seaUpdateData["seatNumber"] = seatNumber
 		seaUpdateData["position_id"] = positionId
 		insertSeaData["seatNumber"] = seatNumber
@@ -346,6 +372,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		}
 		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)
+		updateId11, updateId12, updateId13 := int64(0), int64(0), int64(0)
 		//变更记录
 		if trailstatus == "00" || trailstatus == "01" {
 			updateId1 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
@@ -364,7 +391,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					"clue_id":      clueId,
 					"position_id":  common.Int64All(data["position_id"]),
 					"change_field": "trailstatus",
-					"change_type":  "线索状态变更",
+					"change_type":  "基本信息变更",
 					"old_value":    "无意向客户",
 					"new_value":    "流失",
 					"createtime":   nowTime,
@@ -375,7 +402,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					"clue_id":      clueId,
 					"position_id":  positionId,
 					"change_field": "trailstatus",
-					"change_type":  "线索状态变更",
+					"change_type":  "基本信息变更",
 					"old_value":    "商机线索",
 					"new_value":    "新增",
 					"createtime":   nowTime,
@@ -386,7 +413,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					"clue_id":      clueId,
 					"position_id":  positionId,
 					"change_field": "trailstatus",
-					"change_type":  "线索状态变更",
+					"change_type":  "基本信息变更",
 					"old_value":    "无意向客户",
 					"new_value":    "商机线索",
 					"createtime":   nowTime,
@@ -398,7 +425,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					"clue_id":      clueId,
 					"position_id":  common.Int64All(data["position_id"]),
 					"change_field": "trailstatus",
-					"change_type":  "线索状态变更",
+					"change_type":  "基本信息变更",
 					"old_value":    "商机线索",
 					"new_value":    "流失",
 					"createtime":   nowTime,
@@ -409,7 +436,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 					"clue_id":      clueId,
 					"position_id":  positionId,
 					"change_field": "trailstatus",
-					"change_type":  "线索状态变更",
+					"change_type":  "基本信息变更",
 					"old_value":    "商机线索",
 					"new_value":    "新增",
 					"createtime":   nowTime,
@@ -420,7 +447,7 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		}
 		updateId5 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
 			"clue_id":     clueId,
-			"position_id": positionId,
+			"position_id": common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 			"change_type": "加入任务车",
 			"new_value":   "线索来源自动更新" + "-" + topname + "-" + subname,
 			"createtime":  nowTime,
@@ -433,14 +460,30 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		old_department := common.ObjToString(data["department"])
 		old_follow_project_area := common.ObjToString(data["follow_project_area"])
 		old_role := common.ObjToString(data["role"])
+		old_cluename := common.ObjToString(data["cluename"])
+		old_top_cluetype := common.ObjToString(data["top_cluetype"])
+		old_sub_cluetype := common.ObjToString(data["sub_cluetype"])
+		old_topname, old_subname := "", ""
+		if old_top_cluetype != "" {
+			pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "")
+			if pcodeData != nil && len(*pcodeData) > 0 {
+				old_topname = common.ObjToString((*pcodeData)["name"])
+			}
+		}
+		if old_sub_cluetype != "" {
+			pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "")
+			if pcodeData != nil && len(*pcodeData) > 0 {
+				old_subname = common.ObjToString((*pcodeData)["name"])
+			}
+		}
 		if old_name != name {
 			updateId6 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
 				"clue_id":      clueId,
-				"position_id":  positionId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 				"change_field": "name",
 				"change_type":  "基本信息变更",
-				"old_value":    old_name,
-				"new_value":    name,
+				"old_value":    common.If(old_name != "", old_name, "/"),
+				"new_value":    common.If(name != "", name, "/"),
 				"createtime":   nowTime,
 				"BCPCID":       BCPCID,
 				"operator_id":  -1,
@@ -449,11 +492,11 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		if old_position != position {
 			updateId7 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
 				"clue_id":      clueId,
-				"position_id":  positionId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 				"change_field": "position",
 				"change_type":  "基本信息变更",
-				"old_value":    old_position,
-				"new_value":    position,
+				"old_value":    common.If(old_position != "", old_position, "/"),
+				"new_value":    common.If(position != "", position, "/"),
 				"createtime":   nowTime,
 				"BCPCID":       BCPCID,
 				"operator_id":  -1,
@@ -462,11 +505,11 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		if old_department != department {
 			updateId8 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
 				"clue_id":      clueId,
-				"position_id":  positionId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 				"change_field": "department",
 				"change_type":  "基本信息变更",
-				"old_value":    old_department,
-				"new_value":    department,
+				"old_value":    common.If(old_department != "", old_department, "/"),
+				"new_value":    common.If(department != "", department, "/"),
 				"createtime":   nowTime,
 				"BCPCID":       BCPCID,
 				"operator_id":  -1,
@@ -475,11 +518,11 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		if old_role != role {
 			updateId9 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
 				"clue_id":      clueId,
-				"position_id":  positionId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 				"change_field": "role",
 				"change_type":  "基本信息变更",
-				"old_value":    old_role,
-				"new_value":    role,
+				"old_value":    common.If(old_role != "", old_role, "/"),
+				"new_value":    common.If(role != "", role, "/"),
 				"createtime":   nowTime,
 				"BCPCID":       BCPCID,
 				"operator_id":  -1,
@@ -488,17 +531,56 @@ func UpdateClue(data map[string]interface{}, saleData []map[string]interface{},
 		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,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
 				"change_field": "follow_project_area",
 				"change_type":  "基本信息变更",
-				"old_value":    old_follow_project_area,
-				"new_value":    follow_project_area,
+				"old_value":    common.If(old_follow_project_area != "", old_follow_project_area, "/"),
+				"new_value":    common.If(follow_project_area != "", follow_project_area, "/"),
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_cluename != cluename {
+			updateId11 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
+				"change_field": "cluename",
+				"change_type":  "基本信息变更",
+				"old_value":    common.If(old_cluename != "", old_cluename, "/"),
+				"new_value":    common.If(cluename != "", cluename, "/"),
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_top_cluetype != top_cluetype {
+			updateId12 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
+				"change_field": "top_cluetype",
+				"change_type":  "基本信息变更",
+				"old_value":    common.If(old_topname != "", old_topname, "/"),
+				"new_value":    common.If(topname != "", topname, "/"),
+				"createtime":   nowTime,
+				"BCPCID":       BCPCID,
+				"operator_id":  -1,
+			})
+		}
+		if old_sub_cluetype != sub_cluetype {
+			updateId13 = TiDb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{
+				"clue_id":      clueId,
+				"position_id":  common.If(trailstatus == "00" || trailstatus == "01", positionId, common.Int64All(data["position_id"])),
+				"change_field": "sub_cluetype",
+				"change_type":  "基本信息变更",
+				"old_value":    common.If(old_subname != "", old_subname, "/"),
+				"new_value":    common.If(subname != "", subname, "/"),
 				"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
+		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 && updateId11 > -1 && updateId12 > -1 && updateId13 > -1
 	}) {
 		log.Println("线索更新成功")
 		if TiDb.Count("dwd_f_userbase_contacts", map[string]interface{}{"phone": phone}) == 0 {

+ 36 - 12
clueSync/main.go

@@ -5,28 +5,28 @@ import (
 	"fmt"
 	"log"
 
-	"app.yhyue.com/moapp/jybase/mongodb"
-
+	"app.yhyue.com/moapp/jybase/common"
 	elastic "app.yhyue.com/moapp/jybase/esv1"
+	"app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/mysql"
-	"app.yhyue.com/zhp/util/config"
 	"github.com/robfig/cron"
 )
 
 var (
-	cfg        = new(Config)
-	Mysql      *mysql.Mysql
-	TiDb       *mysql.Mysql
-	Base       *mysql.Mysql
-	Es         *elastic.Elastic
-	Mgo        *mongodb.MongodbSim
-	configFile = flag.String("c", "./config.yaml", "配置文件")
-	mode       = flag.Int("m", 1, "")
+	cfg   = new(Config)
+	Mysql *mysql.Mysql
+	TiDb  *mysql.Mysql
+	Base  *mysql.Mysql
+	Es    *elastic.Elastic
+	Mgo   *mongodb.MongodbSim
+	// configFile = flag.String("c", "./config.yaml", "配置文件")
+	mode = flag.Int("m", 1, "")
 )
 
 func main() {
 	flag.Parse()
-	_ = config.LoadConfigWithYaml(*configFile, cfg)
+	common.ReadConfig(&cfg)
+	// _ = config.LoadConfigWithYaml(*configFile, cfg)
 	log.Println("cfg ", cfg)
 	TiDb = &mysql.Mysql{
 		Address:      cfg.TiDb.Host + ":" + fmt.Sprint(cfg.TiDb.Port),
@@ -83,6 +83,20 @@ func main() {
 			saleLeads()
 		})
 		c.Start()
+		// 用户标签一天一次
+		// tagAllSync()
+		d := cron.New()
+		d.AddFunc(cfg.CornExp4, func() {
+			tagAllSync()
+		})
+		d.Start()
+		// 用户标签增量 5分钟一次
+		tagAddSync()
+		e := cron.New()
+		e.AddFunc(cfg.CornExp5, func() {
+			tagAddSync()
+		})
+		e.Start()
 		select {}
 	} else if *mode == 2 {
 		users()
@@ -91,5 +105,15 @@ func main() {
 	} else if *mode == 4 {
 		saleLeads()
 		users()
+	} else if *mode == 5 {
+		tagAllSync()
+	} else if *mode == 6 {
+		tagAddSync()
+		e := cron.New()
+		e.AddFunc(cfg.CornExp5, func() {
+			tagAddSync()
+		})
+		e.Start()
+		select {}
 	}
 }

+ 212 - 10
clueSync/tag.go

@@ -2,9 +2,13 @@ package main
 
 import (
 	"log"
+	"strings"
+	"time"
+
+	"app.yhyue.com/moapp/jybase/common"
 )
 
-func tagSync() {
+func tagAllSync() {
 	//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
@@ -12,25 +16,223 @@ func tagSync() {
 	//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_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 {
+	TiDb.SelectByBath(100, func(l *[]map[string]interface{}) bool {
 		for _, v := range *l {
 			FormatTag(v)
 		}
 		return true
-	}, `select l_registedate,userid from dwd_f_userbase_baseinfo`)
+	}, `select l_registedate,userid,uid 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
-}
+	registedate := common.ObjToString(data["l_registedate"])
+	userId := common.ObjToString(data["userid"])
+	uId := common.ObjToString(data["uid"])
+	log.Println(uId, registedate)
+	if registedate == "" || uId == "" || userId == "" {
+		log.Println("缺少信息")
+		return
+	}
+	date, search_areass, search_wordss, product_types, subscribe_keywords, subscribe_areas := "", "", "", "", "", ""
+	//访问数据
+	visitData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": userId}, "", "date desc")
+	if visitData != nil && len(*visitData) > 0 {
+		date = common.ObjToString((*visitData)["date"])
+	}
 
-func SaveTag(data map[string]interface{}) {
+	//搜索数据
+	searchData := TiDb.Find("dwd_f_userbase_search_info", map[string]interface{}{"userid": userId}, "", "", -1, -1)
+	if searchData != nil && len(*searchData) > 0 {
+		search_areas, search_words := "", ""
+		for k, v := range *searchData {
+			search_area := common.ObjToString(v["search_area"])
+			search_word := common.ObjToString(v["search_word"])
+			if k == len(*searchData)-1 && search_area != "" {
+				search_areas += search_area
+			} else if search_area != "" {
+				search_areas += search_area + ","
+			}
+			if k == len(*searchData)-1 && search_word != "" {
+				search_words += search_word
+			} else if search_word != "" {
+				search_words += search_word + ","
+			}
+		}
+		search_areas_arr, search_words_arr := []string{}, []string{}
+		for _, v := range strings.Split(search_areas, ",") {
+			isOk := false
+			for _, vv := range search_areas_arr {
+				if vv == v {
+					isOk = true
+				}
+			}
+			if !isOk && v != "" {
+				search_areas_arr = append(search_areas_arr, v)
+			}
+		}
+		for _, v := range strings.Split(search_words, ",") {
+			isOk := false
+			for _, vv := range search_words_arr {
+				if vv == v {
+					isOk = true
+				}
+			}
+			if !isOk && v != "" {
+				search_words_arr = append(search_words_arr, v)
+			}
+		}
+		search_areass = strings.Join(search_areas_arr, ",")
+		search_wordss = strings.Join(search_words_arr, ",")
+	}
 
+	//订单购买的服务
+	orderData := TiDb.Find("dwd_f_userbase_order_info", map[string]interface{}{"userid": userId, "order_status": 1, "delete_status": 0}, "", "", -1, -1)
+	if orderData != nil && len(*orderData) > 0 {
+		product_type_arr, product_type_arrs := []string{}, []string{}
+		for _, v := range *orderData {
+			product_type := common.ObjToString(v["product_type"])
+			if product_type != "" {
+				product_type_arr = append(product_type_arr, product_type)
+			}
+		}
+		for _, v := range product_type_arr {
+			isOk := false
+			for _, vv := range product_type_arrs {
+				if vv == v {
+					isOk = true
+				}
+			}
+			if !isOk {
+				product_type_arrs = append(product_type_arrs, v)
+			}
+		}
+		product_types = strings.Join(product_type_arrs, ",")
+	}
+
+	//订阅数据
+	subscribeData := TiDb.FindOne("dwd_f_userbase_subscribe_info", map[string]interface{}{"userid": userId}, "", "updatetime desc")
+	if subscribeData != nil && len(*subscribeData) > 0 {
+		subscribe_keywords = common.ObjToString((*subscribeData)["subscribe_keywords"])
+		subscribe_areas = common.ObjToString((*subscribeData)["subscribe_areas"])
+	}
+
+	//会员介绍页面
+	start := time.Now().AddDate(0, 0, -1).Format("2006-01-02") + " 00:00:00"
+	end := time.Now().Format("2006-01-02") + " 00:00:00"
+	eventStr := "昨日未浏览"
+	eventCount := TiDb.SelectBySql(`select * from dwd_f_userbase_event_info where userid = "` + userId + `" and eventtype = "会员介绍页面" and createtime >= "` + start + `" and createtime <= "` + end + `"`)
+	if eventCount != nil && len(*eventCount) > 0 {
+		eventStr = "昨日浏览过"
+	}
+	nowTime := time.Now().Format("2006-01-02 15:04:05")
+	if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
+		TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
+			"last_login_time":    common.If(date != "", date, nil),
+			"search_areas":       search_areass,
+			"search_words":       search_wordss,
+			"subscribe_areas":    subscribe_areas,
+			"subscribe_keywords": subscribe_keywords,
+			"product_type":       product_types,
+			"updatetime":         nowTime,
+			"members_info":       eventStr,
+		})
+	} else {
+		TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
+			"uid":                uId,
+			"registedate":        registedate,
+			"last_login_time":    common.If(date != "", date, nil),
+			"search_areas":       search_areass,
+			"search_words":       search_wordss,
+			"subscribe_areas":    subscribe_areas,
+			"subscribe_keywords": subscribe_keywords,
+			"product_type":       product_types,
+			"updatetime":         nowTime,
+			"members_info":       eventStr,
+		})
+	}
+}
+
+func tagAddSync() {
+	log.Println("注册日期、订单增量定时任务开始")
+	userData := TiDb.SelectBySql(`select l_registedate,createtime,uid,userid from dwd_f_userbase_baseinfo where createtime >= "` + cfg.LastUserTime + `"`)
+	if userData != nil && len(*userData) > 0 {
+		for k, v := range *userData {
+			nowTime := time.Now().Format("2006-01-02 15:04:05")
+			registedate := common.ObjToString(v["l_registedate"])
+			createtime := common.ObjToString(v["createtime"])
+			userId := common.ObjToString(v["userid"])
+			uId := common.ObjToString(v["uid"])
+			if registedate == "" || uId == "" || userId == "" {
+				log.Println("缺少信息")
+				return
+			}
+			if k == len(*userData)-1 {
+				cfg.LastOrderTime = createtime
+			}
+			if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
+				TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
+					"registedate": registedate,
+					"updatetime":  nowTime,
+				})
+			} else {
+				TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
+					"uid":         uId,
+					"registedate": registedate,
+					"updatetime":  nowTime,
+				})
+			}
+		}
+	}
+	orderData := TiDb.SelectBySql(`select product_type,autoUpdate,uid from dwd_f_userbase_order_info where order_status = 1 and autoUpdate >= "` + cfg.LastOrderTime + `"`)
+	if orderData != nil && len(*orderData) > 0 {
+		for k, vv := range *orderData {
+			nowTime := time.Now().Format("2006-01-02 15:04:05")
+			autoUpdate := common.ObjToString(vv["autoUpdate"])
+			uId := common.ObjToString(vv["uid"])
+			product_types := ""
+			if k == len(*userData)-1 {
+				cfg.LastOrderTime = autoUpdate
+			}
+			orderDatas := TiDb.Find("dwd_f_userbase_order_info", map[string]interface{}{"uid": uId, "order_status": 1, "delete_status": 0}, "", "", -1, -1)
+			if orderDatas != nil && len(*orderDatas) > 0 {
+				product_type_arr, product_type_arrs := []string{}, []string{}
+				for _, v := range *orderData {
+					product_type := common.ObjToString(v["product_type"])
+					if product_type != "" {
+						product_type_arr = append(product_type_arr, product_type)
+					}
+				}
+				for _, v := range product_type_arr {
+					isOk := false
+					for _, vv := range product_type_arrs {
+						if vv == v {
+							isOk = true
+						}
+					}
+					if !isOk {
+						product_type_arrs = append(product_type_arrs, v)
+					}
+				}
+				product_types = strings.Join(product_type_arrs, ",")
+			}
+			if TiDb.Count("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}) > 0 {
+				TiDb.Update("dwd_f_crm_attribute_label", map[string]interface{}{"uid": uId}, map[string]interface{}{
+					"product_type": product_types,
+					"updatetime":   nowTime,
+				})
+			} else {
+				TiDb.Insert("dwd_f_crm_attribute_label", map[string]interface{}{
+					"uid":          uId,
+					"product_type": product_types,
+					"updatetime":   nowTime,
+				})
+			}
+		}
+	}
+	common.WriteSysConfig(&cfg)
+	log.Println("注册日期、订单增量定时任务结束")
 }