package services import ( "encoding/json" "errors" "fmt" "hongze/hongze_cygx/models" "hongze/hongze_cygx/utils" "strconv" "strings" "time" ) func init() { //ArticleHistoryUserLabelLogAdd(8350, 53095) //UserLabelLogReduce() //var mobileArr []string //mobileArr = append(mobileArr, "15557270714") //ActivityUserLabelLogAdd(2239, mobileArr) //UserLabelLogReduce() } // 添加用户阅读标签到Redis func ArticleHistoryUserLabelLogAdd(articleId, uid int) (err error) { defer func() { if err != nil { fmt.Println(err) msg := fmt.Sprint("articleId:", articleId, "userId:", uid) go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2) } }() // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。 log := &models.CygxUserLabelLogRedis{UserId: uid, SourceId: articleId, SourceType: 1, CreateTime: time.Now()} if utils.Re == nil { err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log) if err != nil { fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error()) } } return } // 添加用户2产业关注标签到Redis func IndustryFllowUserLabelLogAdd(industrialManagementId, count, uid int) (err error) { var isFllow int if count == 0 { isFllow = 1 } else { isFllow = 0 } defer func() { if err != nil { fmt.Println(err) msg := fmt.Sprint("industrialManagementId:", industrialManagementId, "isFllow:", isFllow, "userId:", uid) go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2) } }() log := &models.CygxUserLabelLogRedis{UserId: uid, SourceId: industrialManagementId, SourceType: 2, IsFllow: isFllow, CreateTime: time.Now()} if utils.Re == nil { err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log) if err != nil { fmt.Println("RecordNewLogs LPush Err:" + err.Error()) } } return } // 添加用户活动到会标签到Redis func ActivityUserLabelLogAdd(activityId int, mobileArr []string) (err error) { defer func() { if err != nil { fmt.Println(err) msg := fmt.Sprint("activityId:", activityId, "mobile:", mobileArr) go utils.SendAlarmMsg("添加用户活动到会标签到Redis,写入Redis队列消息失败:"+err.Error()+msg, 2) } }() var condition string var pars []interface{} condition = ` AND activity_id = ? ` pars = append(pars, activityId) total, e := models.GetCygxIndustrialActivityGroupManagementCount(condition+" AND source = 1 ", pars) if e != nil { err = errors.New("GetCygxIndustrialActivityGroupManagementCount" + e.Error()) return } if total == 0 { //没有关联产业的活动不做标签处理 return } industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars) if e != nil { err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error()) return } //如果有行产业归类就按照产业报告处理 var topCond string var topPars []interface{} var industrialManagementIds []int for _, v := range industrialList { industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId) } idsLen := len(industrialManagementIds) if idsLen > 0 { topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)` topPars = append(topPars, industrialManagementIds) } else { return } industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars) if e != nil { err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error()) return } userList, e := models.GetWxUserByOutboundMobiles(mobileArr) if e != nil { err = errors.New("GetWxUserByOutboundMobiles" + e.Error()) return } listActivityHistory, e := models.GetCygxUserLabelActivity(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelActivity" + e.Error()) return } activityHistoryMap := make(map[int]bool) for _, v := range listActivityHistory { activityHistoryMap[v.UserId] = true } var items []*models.CygxUserLabelActivity for _, user := range userList { //已经提交到会的活动写入标签的不做二次添加处理 if activityHistoryMap[user.UserId] { continue } // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。 log := &models.CygxUserLabelLogRedis{UserId: user.UserId, SourceId: activityId, SourceType: 3, CreateTime: time.Now()} if utils.Re == nil { err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log) if err != nil { fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error()) } } for _, industr := range industrNamelist { item := new(models.CygxUserLabelActivity) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.ActivityId = activityId item.IndustrialManagementId = industr.IndustrialManagementId item.Label = industr.IndustryName item.CreateTime = time.Now() item.ModifyTime = time.Now() items = append(items, item) } } if len(items) > 0 { _, err = models.AddCygxUserLabelActivityList(items) } return } func UserLabelLogReduce() (err error) { for { //SourceType int `description:"1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。"` utils.Rc.Brpop(utils.CYGX_USER_KEY_LABEL, func(b []byte) { var log models.CygxUserLabelLogRedis if err := json.Unmarshal(b, &log); err != nil { fmt.Println("json unmarshal wrong!") go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2) } switch log.SourceType { case 1: go ArticleHistoryUserLabelLogReduce(log) fmt.Println("文章阅读") break case 2: go IndustryFllowUserLabelLogReduce(log) fmt.Println("2产业关注") break case 3: go ActivityUserLabelLogReduce(log) fmt.Println("活动到会") break case 4: go CategoryFllowUserLabelLogReduce(log) fmt.Println("4系列关注") break case 5: go ActivitySpecialUserLabelLogReduce(log) fmt.Println("5专项调研活动到会") break default: fmt.Println(string(b)) go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2) } }) } } // 1:文章阅读 func ArticleHistoryUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2) } }() articleId := log.SourceId userId := log.UserId if userId == 0 || articleId == 0 { return } var source int user, e := models.GetWxUserItemByUserId(userId) if e != nil { err = errors.New("GetWxUserItemByUserId" + e.Error()) return } var condition string var pars []interface{} condition = ` AND article_id = ? ` pars = append(pars, articleId) industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error()) return } var items []*models.CygxUserLabel if len(industrialList) == 0 { //如果没有行产业归类就按照行业报告处理 source = 2 detailArticle, e := models.GetArticleDetailById(articleId) if e != nil { err = errors.New("GetArticleDetailById, Err: " + e.Error()) return } labelDetail, e := models.GetdetailByCategoryIdLabel(detailArticle.CategoryId) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetdetailByCategoryIdLabel, Err: " + e.Error()) return } if labelDetail == nil { return } label := labelDetail.MatchTypeName industrialManagementId := labelDetail.Id var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ? ` pars = append(pars, industrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.Weight = 1 item.CreateTime = time.Now() item.ModifyTime = time.Now() items = append(items, item) if total == 0 { _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelWeight" + e.Error()) return } } } else { //如果有行产业归类就按照产业报告处理 var topCond string var topPars []interface{} var industrialManagementIds []int for _, v := range industrialList { industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId) } idsLen := len(industrialManagementIds) if idsLen > 0 { topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)` topPars = append(topPars, industrialManagementIds) } else { return } industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars) if e != nil { err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error()) return } source = 1 for _, v := range industrNamelist { label := v.IndustryName industrialManagementId := v.IndustrialManagementId var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ? ` pars = append(pars, v.IndustrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.Weight = 1 item.CreateTime = time.Now() item.ModifyTime = time.Now() items = append(items, item) if total == 0 { _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelWeight" + e.Error()) return } } } } if len(items) > 0 { go AddArticleHistoryUserLabelLog(items, articleId) } updateUserLabelByUserId(userId) return } // 添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑 func AddArticleHistoryUserLabelLog(items []*models.CygxUserLabel, articleId int) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑失败:"+err.Error(), 2) } }() if len(items) == 0 { return } for _, v := range items { item := new(models.CygxUserLabelArticle) item.UserId = v.UserId item.CompanyId = v.CompanyId item.RealName = v.RealName item.Mobile = v.Mobile item.Email = v.Email item.Label = v.Label item.SourceId = v.SourceId item.Source = v.Source item.ArticleId = articleId item.CreateTime = time.Now() item.ModifyTime = time.Now() _, e := models.AddCygxUserLabelArticle(item) if e != nil { err = errors.New("AddCygxUserLabelArticle" + e.Error()) return } } return } // 2:产业关注 func IndustryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("用户关注产业更新相关标签,处理Redis队列消息失败:"+err.Error(), 2) } }() isFllow := log.IsFllow industrialManagementId := log.SourceId userId := log.UserId source := 1 detailIndustrial, e := models.GetIndustrialManagementDetail(industrialManagementId) if e != nil { err = errors.New("GetIndustrialManagementDetail" + e.Error()) return } label := detailIndustrial.IndustryName if isFllow == 0 { e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error()) return } } else { var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ?` pars = append(pars, industrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } if total == 0 { user, e := models.GetWxUserItemByUserId(userId) if e != nil { err = errors.New("GetWxUserItemByUserId" + e.Error()) return } item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.IsFollow = isFllow item.CreateTime = time.Now() item.ModifyTime = time.Now() _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error()) return } } } updateUserLabelByUserId(userId) return } // 3:活动到会 func ActivityUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2) } }() activityId := log.SourceId userId := log.UserId var source int user, e := models.GetWxUserItemByUserId(userId) if e != nil { err = errors.New("GetWxUserItemByUserId" + e.Error()) return } var condition string var pars []interface{} condition = ` AND activity_id = ? ` pars = append(pars, activityId) industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars) if e != nil { err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error()) return } //如果有行产业归类就按照产业报告处理 var topCond string var topPars []interface{} var industrialManagementIds []int for _, v := range industrialList { industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId) } idsLen := len(industrialManagementIds) if idsLen > 0 { topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)` topPars = append(topPars, industrialManagementIds) } else { return } industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars) if e != nil { err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error()) return } source = 1 for _, v := range industrNamelist { label := v.IndustryName industrialManagementId := v.IndustrialManagementId var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ? ` pars = append(pars, v.IndustrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } if total == 0 { item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.Weight = 1 item.CreateTime = time.Now() item.ModifyTime = time.Now() _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelWeight" + e.Error()) return } } } updateUserLabelByUserId(userId) return } // 4:系列关注 func CategoryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("用户关注系列更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint(log), 2) } }() isFllow := log.IsFllow industrialManagementId := log.SourceId userId := log.UserId source := 2 detailIndustrial, e := models.GetCygxReportMappingById(industrialManagementId) if e != nil { err = errors.New("GetCygxReportMappingById" + e.Error()) return } label := detailIndustrial.MatchTypeName if isFllow == 0 { e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error()) return } } else { var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ?` pars = append(pars, industrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } if total == 0 { user, e := models.GetWxUserItemByUserId(userId) if e != nil { err = errors.New("GetWxUserItemByUserId" + e.Error()) return } item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.IsFollow = isFllow item.CreateTime = time.Now() item.ModifyTime = time.Now() _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error()) return } } } updateUserLabelByUserId(userId) return } // 5:专项调研活动到会 func ActivitySpecialUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2) } }() activityId := log.SourceId userId := log.UserId var source int user, e := models.GetWxUserItemByUserId(userId) if e != nil { err = errors.New("GetWxUserItemByUserId" + e.Error()) return } var condition string var pars []interface{} condition = ` AND activity_id = ? ` pars = append(pars, activityId) industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars) if e != nil { err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error()) return } //如果有行产业归类就按照产业报告处理 var topCond string var topPars []interface{} var industrialManagementIds []int for _, v := range industrialList { industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId) } idsLen := len(industrialManagementIds) if idsLen > 0 { topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)` topPars = append(topPars, industrialManagementIds) } else { return } industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars) if e != nil { err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error()) return } source = 1 for _, v := range industrNamelist { label := v.IndustryName industrialManagementId := v.IndustrialManagementId var condition string var pars []interface{} condition += ` AND source_id=? AND source = ? AND user_id = ? ` pars = append(pars, v.IndustrialManagementId, source, userId) total, e := models.GetCygxUserLabelCount(condition, pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } if total == 0 { item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = label item.SourceId = industrialManagementId item.Source = source item.Weight = 1 item.CreateTime = time.Now() item.ModifyTime = time.Now() _, e = models.AddCygxUserLabel(item) if e != nil { err = errors.New("AddCygxUserLabel" + e.Error()) return } } else { //source 来源1:产业、2:系列 e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label) if e != nil { err = errors.New("UpdateCygxUserLabelWeight" + e.Error()) return } } } updateUserLabelByUserId(userId) return } //func init() { // var condition string // var pars []interface{} // pars = make([]interface{}, 0) // condition = ` GROUP BY user_id` // listUser, _ := models.GetCygxUserLabelList(condition, pars) // for k, v := range listUser { // fmt.Println(k) // updateUserLabelByUserId(v.UserId) // } // //updateUserLabelByUserId(13269) //} // 更新用户标签 func updateUserLabelByUserId(userId int) (err error) { defer func() { if err != nil { fmt.Println(err) utils.FileLog.Info("更新用户标签,Err:%s", err.Error(), userId) go utils.SendAlarmMsg("更新用户标签失败:"+err.Error()+"userId:"+strconv.Itoa(userId), 2) } }() //加200毫秒的延迟 time.Sleep(200 * time.Millisecond) var condition string var pars []interface{} condition = ` AND is_follow=1 AND user_id = ? AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH) ` pars = append(pars, userId) totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars) if e != nil { err = errors.New("GetCygxProductInteriorCount" + e.Error()) return } condition = ` AND user_id = ? ` var list []*models.CygxUserLabel //当产业关注数量不超过10 ,系列关注数量不超过5 if totalSource1 <= 10 && totalSource2 <= 5 { condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC ` list, e = models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList当产业关注数量不超过10 ,系列关注数量不超过5" + e.Error()) return } } //当产业关注数量超过10 ,系列关注数量超过5 if totalSource1 > 10 && totalSource2 > 5 { condition += ` AND weight > 0 ORDER BY weight DESC ` list, e = models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量超过5" + e.Error()) return } } //当产业关注数量不超过10 ,系列关注数量超过5 if totalSource1 <= 10 && totalSource2 > 5 { condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC ` listfollow, e := models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList" + e.Error()) return } idMap := make(map[int]bool) for _, v := range listfollow { idMap[v.Id] = true list = append(list, v) } pars = make([]interface{}, 0) condition = ` AND user_id = ? ` pars = append(pars, userId) condition += ` AND weight > 0 ORDER BY weight DESC ` listother, e := models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList 当产业关注数量不超过10 ,系列关注数量超过5" + e.Error()) return } for _, v := range listother { if idMap[v.Id] { continue } list = append(list, v) } } //当产业关注数量超过10 ,系列关注数量不超过5 if totalSource1 > 10 && totalSource2 <= 5 { condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC ` listfollow, e := models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error()) return } idMap := make(map[int]bool) for _, v := range listfollow { idMap[v.Id] = true list = append(list, v) } pars = make([]interface{}, 0) condition = ` AND user_id = ? ` pars = append(pars, userId) condition += ` AND weight > 0 ORDER BY weight DESC ` listother, e := models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error()) return } for _, v := range listother { if idMap[v.Id] { continue } list = append(list, v) } } var labelUser string for _, v := range list { labelUser += v.Label + "," } labelUser = strings.TrimRight(labelUser, ",") e = models.UpdateUserLabel(labelUser, userId) if e != nil { err = errors.New("UpdateUserLabel" + e.Error()) return err } return } //func init() { // UpdateUserLabelWeight() //} // 更新用户标签权重 func UpdateUserLabelWeight() (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("更新用户标签权重失败:"+err.Error(), 2) } }() var condition string var pars []interface{} list, e := models.GetCygxUserLabelList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelList" + e.Error()) return } updateTime := time.Now().AddDate(0, -3, 0) condition = ` AND create_time > ?` pars = append(pars, updateTime) listArticle, e := models.GetCygxUserLabelArticleList(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelArticleList" + e.Error()) return } listActivity, e := models.GetCygxUserLabelActivity(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelActivity" + e.Error()) return } listActivitySpecial, e := models.GetCygxUserLabelActivitySpecial(condition, pars) if e != nil { err = errors.New("GetCygxUserLabelActivitySpecial" + e.Error()) return } userLabel := make(map[string]bool) userLabelWeight := make(map[string]int) for _, v := range list { userLabel[fmt.Sprint("user_id{|}", v.UserId, "{|}source_id{|}", v.SourceId, "{|}source{|}", v.Source)] = true } fmt.Println(userLabel) //处理文章标签 var items []*models.CygxUserLabel for _, user := range listArticle { if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] { item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = user.Label item.SourceId = user.SourceId item.Source = user.Source item.CreateTime = user.CreateTime item.ModifyTime = time.Now() items = append(items, item) userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] = true } userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)]++ } //处理活动标签 for _, user := range listActivity { if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] { item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = user.Label item.SourceId = user.IndustrialManagementId item.Source = 1 item.CreateTime = user.CreateTime item.ModifyTime = time.Now() items = append(items, item) userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true } userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++ } //处理专项调研活动标签 for _, user := range listActivitySpecial { if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] { item := new(models.CygxUserLabel) item.UserId = user.UserId item.CompanyId = user.CompanyId item.RealName = user.RealName item.Mobile = user.Mobile item.Email = user.Email item.Label = user.Label item.SourceId = user.IndustrialManagementId item.Source = 1 item.CreateTime = user.CreateTime item.ModifyTime = time.Now() items = append(items, item) userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true } userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++ } var itemLimt []*models.CygxUserLabel if len(items) > 0 { for _, v := range items { itemLimt = append(itemLimt, v) if len(itemLimt)%5000 == 0 { err = models.CygxUserLabelMulti(itemLimt) fmt.Println(err) itemLimt = make([]*models.CygxUserLabel, 0) } } } if len(itemLimt) > 0 { err = models.CygxUserLabelMulti(itemLimt) fmt.Println(err) } var itemsUpdate []*models.CygxUserLabel for k, v := range userLabelWeight { sliceUser := strings.Split(k, "{|}") userid, _ := strconv.Atoi(sliceUser[1]) label := sliceUser[len(sliceUser)-1] item := new(models.CygxUserLabel) item.UserId = userid item.Label = label item.Weight = v itemsUpdate = append(itemsUpdate, item) } e = models.UpdateCygxUserLabelWeightAll() if e != nil { err = errors.New("UpdateCygxUserLabelWeightAll" + e.Error()) return } e = models.UpdateCygxUserLabelMulti(itemsUpdate) if e != nil { err = errors.New("UpdateCygxUserLabelMulti" + e.Error()) return } pars = make([]interface{}, 0) condition = ` GROUP BY user_id` listUser, e := models.GetCygxUserLabelList(condition, pars) for _, v := range listUser { go updateUserLabelByUserId(v.UserId) } return }