瀏覽代碼

Merge branch 'dev_v1.5.36.2_wh' of jianyu/datatools into hotfix/v1.5.36.2

王浩 1 月之前
父節點
當前提交
68ef2f9ac8
共有 1 個文件被更改,包括 152 次插入127 次删除
  1. 152 127
      clueSync/everything.go

+ 152 - 127
clueSync/everything.go

@@ -22,6 +22,7 @@ import (
 	"app.yhyue.com/moapp/jybase/common"
 )
 
+// 渠道定时任务
 func everythingSync() {
 	lastEverythingTime := cfg.LastEverythingTime
 	nowTime := time.Now().Format(dates.Date_Full_Layout)
@@ -35,146 +36,170 @@ func everythingSync() {
 	log.Println("三方渠道数据定时任务开始", sql)
 	data := ThirdParty.SelectBySql(sql)
 	if data != nil && *data != nil && len(*data) > 0 {
-		for _, v := range *data {
-			channel_code := gconv.String(v["channel_code"])
-			changeMap := UserChannel[channel_code]
-			channeName := changeMap["name"]
-			channelCode := changeMap["clueCode"]
-			user_id := common.ObjToString(v["user_id"])
-			position_id := common.Int64All(v["position_id"])
-			phone := common.ObjToString(v["phone"])
-			state := common.IntAll(v["state"])
-			user_mold, is_assign, is_transfer, last_login_time, registe_time, mailbox, source, clueId, order_type := 4, 0, 0, "", "", "", "", int64(0), ""
-			owner, sales_lead_phone, sales_ent_name, sales_position, sales_dep, data_request, sales_leads_source := "", "", "", "", "", "", ""
-			cData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
-			if cData != nil {
-				is_assign = common.IntAll((*cData)["is_assign"])
-				is_transfer = common.IntAll((*cData)["is_transfer"])
-				clueId = common.Int64All((*cData)["id"])
-			}
-			udata := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": user_id}, "", "")
-			if udata != nil {
-				userSource := common.ObjToString((*udata)["source"])
-				if userSource == "0102" {
-					continue
-				}
-			}
-			//
-			/*cuData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "")
-			if cuData != nil {
-				owner = common.ObjToString((*cuData)["owner"])
-				unique_id = common.ObjToString((*cuData)["unique_id"])
-				belongTo = common.ObjToString((*cuData)["belongTo"])
-			}*/
-			vData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": user_id}, "", "")
-			if vData != nil {
-				last_login_time = common.ObjToString((*vData)["date"])
+		everythingSyncHandle(data, true)
+	}
+	sql = fmt.Sprintf(`select * from user_source where channel_code in (%s) and ishandle=1  order by create_time asc`, strings.Join(codeArr, ","), lastEverythingTime)
+	log.Println("三方渠道数据定时任务开始1", sql)
+	data = ThirdParty.SelectBySql(sql)
+	if data != nil && *data != nil && len(*data) > 0 {
+		everythingSyncHandle(data, false)
+	}
+	log.Println("三方渠道数据定时任务结束")
+}
+func everythingSyncHandle(data *[]map[string]interface{}, isUpdate bool) {
+	for _, v := range *data {
+		id := gconv.Int64(v["id"])
+		channel_code := gconv.String(v["channel_code"])
+		changeMap := UserChannel[channel_code]
+		channeName := changeMap["name"]
+		channelCode := changeMap["clueCode"]
+		user_id := common.ObjToString(v["user_id"])
+		position_id := common.Int64All(v["position_id"])
+		phone := common.ObjToString(v["phone"])
+		state := common.IntAll(v["state"])
+		user_mold, is_assign, is_transfer, last_login_time, registe_time, mailbox, source, clueId, order_type := 4, 0, 0, "", "", "", "", int64(0), ""
+		owner, sales_lead_phone, sales_ent_name, sales_position, sales_dep, data_request, sales_leads_source := "", "", "", "", "", "", ""
+		cData := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"phone": phone}, "", "")
+		if cData != nil {
+			is_assign = common.IntAll((*cData)["is_assign"])
+			is_transfer = common.IntAll((*cData)["is_transfer"])
+			clueId = common.Int64All((*cData)["id"])
+		}
+		udata := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{"userid": user_id}, "", "")
+		if udata != nil {
+			userSource := common.ObjToString((*udata)["source"])
+			if userSource == "0102" {
+				continue
 			}
-			dData := Mysql.Find("dataexport_order", map[string]interface{}{"user_id": user_id}, "", "", -1, -1)
-			if dData != nil && len(*dData) > 0 {
-				pMap := map[string]string{}
-				pArr := []string{}
-				for _, v := range *dData {
-					product_type := common.ObjToString(v["product_type"])
-					pMap[product_type] = "1"
-				}
-				for k := range pMap {
-					pArr = append(pArr, k)
-				}
-				if len(pArr) > 0 {
-					order_type = strings.Join(pArr, ",")
-				}
+		}
+		//
+		/*cuData := TiDbData.FindOne("customer", map[string]interface{}{"phone": phone}, "", "")
+		if cuData != nil {
+			owner = common.ObjToString((*cuData)["owner"])
+			unique_id = common.ObjToString((*cuData)["unique_id"])
+			belongTo = common.ObjToString((*cuData)["belongTo"])
+		}*/
+		vData := TiDb.FindOne("dwd_f_userbase_visit_info", map[string]interface{}{"userid": user_id}, "", "")
+		if vData != nil {
+			last_login_time = common.ObjToString((*vData)["date"])
+		}
+		dData := Mysql.Find("dataexport_order", map[string]interface{}{"user_id": user_id}, "", "", -1, -1)
+		if dData != nil && len(*dData) > 0 {
+			pMap := map[string]string{}
+			pArr := []string{}
+			for _, v := range *dData {
+				product_type := common.ObjToString(v["product_type"])
+				pMap[product_type] = "1"
 			}
-			uData, ok := Mgo.FindOne("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(user_id)})
-			if ok && uData != nil {
-				l_registedate := common.Int64All((*uData)["l_registedate"])
-				registe_time = time.Unix(l_registedate, 0).Format(dates.Date_Full_Layout)
+			for k := range pMap {
+				pArr = append(pArr, k)
 			}
-			sData, oks := Mgo.Find("saleLeads", map[string]interface{}{"userid": user_id}, `{"_id":-1}`, nil, false, 0, 1)
-			if oks && sData != nil {
-				if len(*sData) > 0 {
-					sDatas := (*sData)[0]
-					sales_lead_phone = common.ObjToString(sDatas["phone"])
-					sales_position = common.ObjToString(sDatas["position"])
-					sales_ent_name = common.ObjToString(sDatas["company"])
-					sales_dep = common.ObjToString(sDatas["branch"])
-					if sales_dep == "" {
-						sales_dep = common.ObjToString(sDatas["department"])
-					}
-					data_request = common.ObjToString(sDatas["data_requirement"])
-					mailbox = common.ObjToString(sDatas["mail"])
-					source = common.ObjToString(sDatas["interest"])
-				}
+			if len(pArr) > 0 {
+				order_type = strings.Join(pArr, ",")
 			}
-			if state == 1 {
-				sales_leads_source = fmt.Sprintf("%s登录", channeName)
-				if is_assign == 1 || is_transfer == 1 || owner != "" {
-					user_mold = 1
-				} else {
-					user_mold = 2
-				}
-			} else if state == 2 {
-				sales_leads_source = fmt.Sprintf("%s注册", channeName)
-				if is_assign == 1 || is_transfer == 1 || owner != "" {
-					user_mold = 3
-				} else {
-					user_mold = 4
+		}
+		uData, ok := Mgo.FindOne("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(user_id)})
+		if ok && uData != nil {
+			l_registedate := common.Int64All((*uData)["l_registedate"])
+			registe_time = time.Unix(l_registedate, 0).Format(dates.Date_Full_Layout)
+		}
+		if uData == nil || len(*uData) == 0 {
+			ThirdParty.Update("", map[string]interface{}{
+				"id": id,
+			}, map[string]interface{}{
+				"ishandle": 1,
+			})
+			continue
+		}
+		sData, oks := Mgo.Find("saleLeads", map[string]interface{}{"userid": user_id}, `{"_id":-1}`, nil, false, 0, 1)
+		if oks && sData != nil {
+			if len(*sData) > 0 {
+				sDatas := (*sData)[0]
+				sales_lead_phone = common.ObjToString(sDatas["phone"])
+				sales_position = common.ObjToString(sDatas["position"])
+				sales_ent_name = common.ObjToString(sDatas["company"])
+				sales_dep = common.ObjToString(sDatas["branch"])
+				if sales_dep == "" {
+					sales_dep = common.ObjToString(sDatas["department"])
 				}
+				data_request = common.ObjToString(sDatas["data_requirement"])
+				mailbox = common.ObjToString(sDatas["mail"])
+				source = common.ObjToString(sDatas["interest"])
 			}
-			if ThirdParty.Count("user_channel_info", map[string]interface{}{"user_id": user_id}) > 0 {
-				ThirdParty.Update("user_channel_info", map[string]interface{}{"user_id": user_id}, map[string]interface{}{
-					"user_mold":          user_mold,
-					"last_login_time":    common.If(last_login_time != "", last_login_time, nil),
-					"sales_lead_phone":   sales_lead_phone,
-					"sales_ent_name":     sales_ent_name,
-					"sales_position":     sales_position,
-					"sales_dep":          sales_dep,
-					"sales_leads_source": sales_leads_source,
-					"mailbox":            mailbox,
-					"order_type":         order_type,
-					"data_request":       data_request,
-					"source":             source,
-					"update_time":        nowTime,
-				})
+		}
+		if state == 1 {
+			sales_leads_source = fmt.Sprintf("%s登录", channeName)
+			if is_assign == 1 || is_transfer == 1 || owner != "" {
+				user_mold = 1
 			} else {
-				ThirdParty.Insert("user_channel_info", map[string]interface{}{
-					"user_id":            user_id,
-					"registe_time":       registe_time,
-					"phone":              phone,
-					"user_mold":          user_mold,
-					"last_login_time":    common.If(last_login_time != "", last_login_time, nil),
-					"sales_lead_phone":   sales_lead_phone,
-					"sales_ent_name":     sales_ent_name,
-					"sales_position":     sales_position,
-					"sales_dep":          sales_dep,
-					"sales_leads_source": sales_leads_source,
-					"order_type":         order_type,
-					"mailbox":            mailbox,
-					"data_request":       data_request,
-					"source":             source,
-					"update_time":        nowTime,
-				})
+				user_mold = 2
 			}
-			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": channelCode})
-				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.Sprintf("通过合作渠道%s", channeName) + fmt.Sprint(common.If(user_mold == 1 || user_mold == 2, "登录", "注册")),
-					"createtime":  nowTime,
-					"BCPCID":      common.GetRandom(32),
-					"operator_id": -1,
-				})
+		} else if state == 2 {
+			sales_leads_source = fmt.Sprintf("%s注册", channeName)
+			if is_assign == 1 || is_transfer == 1 || owner != "" {
+				user_mold = 3
+			} else {
+				user_mold = 4
 			}
 		}
+		if ThirdParty.Count("user_channel_info", map[string]interface{}{"user_id": user_id}) > 0 {
+			ThirdParty.Update("user_channel_info", map[string]interface{}{"user_id": user_id}, map[string]interface{}{
+				"user_mold":          user_mold,
+				"last_login_time":    common.If(last_login_time != "", last_login_time, nil),
+				"sales_lead_phone":   sales_lead_phone,
+				"sales_ent_name":     sales_ent_name,
+				"sales_position":     sales_position,
+				"sales_dep":          sales_dep,
+				"sales_leads_source": sales_leads_source,
+				"mailbox":            mailbox,
+				"order_type":         order_type,
+				"data_request":       data_request,
+				"source":             source,
+				"update_time":        nowTime,
+			})
+		} else {
+			ThirdParty.Insert("user_channel_info", map[string]interface{}{
+				"user_id":            user_id,
+				"registe_time":       registe_time,
+				"phone":              phone,
+				"user_mold":          user_mold,
+				"last_login_time":    common.If(last_login_time != "", last_login_time, nil),
+				"sales_lead_phone":   sales_lead_phone,
+				"sales_ent_name":     sales_ent_name,
+				"sales_position":     sales_position,
+				"sales_dep":          sales_dep,
+				"sales_leads_source": sales_leads_source,
+				"order_type":         order_type,
+				"mailbox":            mailbox,
+				"data_request":       data_request,
+				"source":             source,
+				"update_time":        nowTime,
+			})
+		}
+		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": channelCode})
+			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.Sprintf("通过合作渠道%s", channeName) + fmt.Sprint(common.If(user_mold == 1 || user_mold == 2, "登录", "注册")),
+				"createtime":  nowTime,
+				"BCPCID":      common.GetRandom(32),
+				"operator_id": -1,
+			})
+		}
+		ThirdParty.Update("", map[string]interface{}{
+			"id": id,
+		}, map[string]interface{}{
+			"ishandle": 0,
+		})
+	}
+	if isUpdate {
 		cfg.LastEverythingTime = common.ObjToString((*data)[len(*data)-1]["create_time"])
+		common.WriteSysConfig(&cfg)
 	}
-	common.WriteSysConfig(&cfg)
-	log.Println("三方渠道数据定时任务结束")
 }
-
 func saveEverything(user_id, phone, item, sourceName, sourceCode, belongTo string) bool {
 	changeMap := UserChannel[belongTo]
 	changeName := gconv.String(changeMap["name"])