12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- 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
- }
- countFllow, e := models.GetCountCygxIndustryFllowByType(userId, industrialManagementId) // 是否重点关注
- if e != nil {
- err = errors.New("GetCountCygxIndustryFllowByType" + e.Error())
- return
- }
- isFllow = countFllow
- 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
- countFllow, e := models.GetCountCygxXzsChooseCategoryFllowByType(userId, industrialManagementId) // 是否重点关注
- if e != nil {
- err = errors.New("GetCountCygxIndustryFllowByType" + e.Error())
- return
- }
- isFllow = countFllow
- 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
- }
|