Commit 6d7b4d4a by mushishixian

修复统计错误

parent 9eae0dd2
2021-02-02 11:31:02.644 ERROR (main.go:main.(*RecvPro).Consumer:208) not found
2021-02-02 11:33:00.958 ERROR (main.go:main.(*RecvPro).Consumer:208) not found
2021-02-02 11:38:52.737 ERROR (main.go:main.(*RecvPro).Consumer:208) not found
2021-02-02 11:39:28.825 ERROR (main.go:main.(*RecvPro).Consumer:211) not found
2021-02-02 11:39:30.882 ERROR (main.go:main.(*RecvPro).Consumer:211) not found
2021-02-02 11:39:32.436 ERROR (main.go:main.(*RecvPro).Consumer:211) not found
2021-02-02 11:39:53.796 ERROR (main.go:main.(*RecvPro).Consumer:213) not found
2021-02-02 11:41:37.89 ERROR (main.go:main.(*RecvPro).Consumer:208) not found
......@@ -78,40 +78,70 @@ type RecvPro struct {
}
func (t *RecvPro) Consumer(dataByte []byte) (err error) {
var viewData model.ViewData
//fmt.Println(string(dataByte))
if err = json.Unmarshal(dataByte, &viewData); err != nil {
//获取队列数据
var rbmqData model.ViewData
if err = json.Unmarshal(dataByte, &rbmqData); err != nil {
fmt.Println(err)
logger.Error(err.Error())
return
}
//先去遍历数据库,全部过一遍
if rbmqData.ActivityId == 0 {
return nil
}
var (
historyData model.HistoryData
whereMap map[string]interface{}
mongoHistory model.HistoryData
whereMap map[string]interface{}
)
//先去mongo查找
//if viewData.UserId != 0 {
// whereMap = bson.M{"activity_id": viewData.ActivityId, "user_id": viewData.UserId}
//} else {
whereMap = bson.M{"activity_id": viewData.ActivityId, "unique_id": viewData.UniqueId}
//}
//先去mongo查找有没有存在的记录,unique_id是由3.0写到cookie的,过期时间为1年
whereMap = bson.M{"activity_id": rbmqData.ActivityId, "unique_id": rbmqData.UniqueId}
err = mongoConn.DB("ichunt").C("activity_view_history").
Find(whereMap).One(&historyData)
if err != nil && err != mgo.ErrNotFound {
fmt.Println(err)
logger.Error(err.Error())
return
Find(whereMap).One(&mongoHistory)
//先去获取用户相关信息
var (
mobile string
isNewReg int
adTag string
)
if rbmqData.UserId != 0 {
//找出用户相关的信息
var user model.UserMain
err = db.Reset().Table(&user).Where("user_id", rbmqData.UserId).Select()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
mobile = user.Mobile
adTag = user.RegRemark
//再去判断是否是这个活动注册的用户
var userActivity model.UserActivity
count, err := db.Reset().Table(&userActivity).Where("activity_id", rbmqData.ActivityId).Where("user_id", rbmqData.UserId).Count()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
if count != 0 {
isNewReg = 1
}
}
//mysql数据操作
var result model.ActivityViewStatistics
err = specialDb.Reset().Table(&result).Where("activity_id", viewData.ActivityId).Select()
if result.ActivityId == 0 {
//先去数据库插入
//如果rbmq传过来的数据在mongo里面找不到,先去数据库查找活动是否存在
var statistics model.ActivityViewStatistics
err = specialDb.Reset().Table(&statistics).Where("activity_id", rbmqData.ActivityId).Select()
//活动不存在,则往数据库插入一条新的统计
if statistics.ActivityId == 0 {
var loginNum, regNum int
if rbmqData.UserId != 0 {
loginNum = 1
if isNewReg == 1 {
regNum = 1
}
}
activityViewStatistics := model.ActivityViewStatistics{
ActivityId: viewData.ActivityId,
RegNum: 0,
LoginNum: 0,
ActivityId: rbmqData.ActivityId,
RegNum: regNum,
LoginNum: loginNum,
CreateTime: int(time.Now().Unix()),
}
_, err = specialDb.Reset().Table("lie_activity_view_statistics").Data(activityViewStatistics).Insert()
......@@ -121,19 +151,24 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
return
}
} else {
loginNum := result.LoginNum
regNum := result.RegNum
if viewData.UserId != 0 {
loginNum := statistics.LoginNum
regNum := statistics.RegNum
//如果已经存在的mongo记录里面,已经存在了unique_id,但是user_id为空,同时新传过来的rbmq的数据的user_id不为空,就代表这个用户
//从未登录变成了登陆态,该活动的登陆人数+1,同时去判断该用户是否是这个活动注册的用户
if mongoHistory.UserId == 0 && rbmqData.UserId != 0 {
loginNum++
if isNewReg == 1 {
regNum++
}
}
//存在则去更新
activityViewStatistics := model.ActivityViewStatistics{
ActivityId: viewData.ActivityId,
ActivityId: rbmqData.ActivityId,
RegNum: regNum,
LoginNum: loginNum,
UpdateTime: int(time.Now().Unix()),
}
_, err = specialDb.Reset().Table("lie_activity_view_statistics").Where("activity_id", viewData.ActivityId).
_, err = specialDb.Reset().Table("lie_activity_view_statistics").Where("activity_id", rbmqData.ActivityId).
Data(activityViewStatistics).Update()
if err != nil {
fmt.Println(err)
......@@ -141,66 +176,39 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
return
}
}
//如果mongo存在,更新mongo数据
if historyData.UserId != 0 {
historyData.LastVisitTime = int(time.Now().Unix())
historyData.LastVisitTime = int(time.Now().Unix())
selector := bson.M{"unique_id": historyData.UniqueId}
err = mongoConn.DB("ichunt").C("activity_view_history").Update(selector, &historyData)
//如果已经存在的mongo记录里面,已经存在了unique_id,但是user_id为空,同时新传过来的rbmq的数据的user_id不为空
//此时只需要去更新mongo对应这条记录的user_id,mobile等信息
if mongoHistory.UserId != 0 {
mongoHistory.LastVisitTime = int(time.Now().Unix())
selector := bson.M{"unique_id": mongoHistory.UniqueId}
err = mongoConn.DB("ichunt").C("activity_view_history").Update(selector, &mongoHistory)
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
} else {
var (
mobile string
isNewReg int
adTag string
)
if viewData.UserId != 0 {
//找出用户相关的信息
var user model.UserMain
err = db.Reset().Table(&user).Where("user_id", viewData.UserId).Select()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
mobile = user.Mobile
adTag = user.RegRemark
//再去判断是否是这个活动注册的用户
var userActivity model.UserActivity
count, err := db.Reset().Table(&userActivity).Where("activity_id", viewData.ActivityId).Where("user_id", viewData.UserId).Count()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
if count != 0 {
isNewReg = 1
}
}
historyData := model.HistoryData{
UniqueId: viewData.UniqueId,
ActivityId: viewData.ActivityId,
newData := model.HistoryData{
UniqueId: rbmqData.UniqueId,
ActivityId: rbmqData.ActivityId,
Mobile: mobile,
UserId: viewData.UserId,
UserId: rbmqData.UserId,
IsNewReg: isNewReg,
AdTag: adTag,
Source: viewData.Source,
VisitTime: viewData.VisitTime,
LastVisitTime: 0,
Pf: viewData.Pf,
Source: rbmqData.Source,
VisitTime: rbmqData.VisitTime,
LastVisitTime: int(time.Now().Unix()),
Pf: rbmqData.Pf,
}
if historyData.UniqueId == "" {
err = mongoConn.DB("ichunt").C("activity_view_history").Insert(&historyData)
if mongoHistory.UniqueId == "" {
err = mongoConn.DB("ichunt").C("activity_view_history").Insert(&newData)
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
}
} else {
historyData.LastVisitTime = int(time.Now().Unix())
selector := bson.M{"unique_id": historyData.UniqueId}
err = mongoConn.DB("ichunt").C("activity_view_history").Update(selector, &historyData)
selector := bson.M{"unique_id": mongoHistory.UniqueId}
err = mongoConn.DB("ichunt").C("activity_view_history").Update(selector, &newData)
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment