1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
- 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
- }
|