Commit 8176893b by mushishixian

统计

parent 46873e98
This diff could not be displayed because it is too large.
2021-01-15 13:53:13.783 ERROR (main.go:main.main:77) sdsd
2021-01-15 13:54:25.879 ERROR (main.go:main.main:75) sadisaiod
2021-01-21 15:52:41.309 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.426 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.544 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.654 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.778 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.886 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:41.995 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:42.103 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:42.22 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:42.329 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:42.437 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
2021-01-21 15:52:42.545 ERROR (main.go:main.main:208) Error 1054: Unknown column 'lie_order_activity_count.type' in 'where clause'
......@@ -81,19 +81,20 @@ func main() {
mongoConn.Close()
}()
nowTime := time.Now().Unix()
//首先去找最后的更新时间,一般来说找统计表最后一条记录的时间就对了
updateTime, _ := specialDb.Reset().Table("lie_activity_statistics").OrderBy("id desc").Value("update_time")
//先去取出所有的正在进行的活动列表
var giftActivityList []model.GiftActivity
err = specialDb.Table(&giftActivityList).Fields("id,start_time,end_time").Where("end_time", ">", nowTime).Select()
err = specialDb.Reset().Table(&giftActivityList).Fields("id,start_time,end_time").Where("status", "!=", -1).
Where("end_time", ">", updateTime).Select()
if err != nil {
logger.Error(err.Error())
}
//统计满赠活动******************
for _, activity := range giftActivityList {
orderNum, _ := db.Table("lie_order_gift").Where("activity_id", activity.Id).Count()
orderNum, _ := db.Reset().Table("lie_order_gift").Where("activity_id", activity.Id).Where("status", "!=", -1).Count()
newUserNum, _ := db.Reset().Table("lie_order_gift").LeftJoin("lie_user_main on lie_order_gift.user_id = lie_user_main.user_id").
Where("lie_order_gift.activity_id", activity.Id).Where("lie_user_main.is_new", 1).Count()
Where("lie_order_gift.activity_id", activity.Id).Where("lie_user_main.is_new", 1).Where("lie_order_gift.status", "!=", -1).Count()
//先查询统计是否存在
count, _ := specialDb.Reset().Table("lie_activity_statistics").Where("activity_id", activity.Id).
Where("type", 2).Count()
......@@ -115,16 +116,66 @@ func main() {
logger.Error(err.Error())
}
}
//将明细写进mongoDB
//先去查找明细是否存在
var orderGifts []model.OrderGift
err = db.Reset().Table(&orderGifts).Where("activity_id", activity.Id).Where("status", "!=", -1).Select()
if err != nil {
logger.Error(err.Error())
}
for _, orderGif := range orderGifts {
var giftActivityDetail model.GiftActivityDetail
err = mongoConn.DB("ichunt").C("gift_activity_detail").
Find(bson.M{"order_id": orderGif.OrderId, "activity_id": orderGif.ActivityId}).
One(&giftActivityDetail)
if err != nil && err != mgo.ErrNotFound {
logger.Error(err.Error())
}
//不存在,则插入
if giftActivityDetail.ActivityId == 0 {
//去找订单信息
var order model.Order
err = db.Reset().Table(&order).Fields("order_id,status,create_time").Where("order_id", orderGif.OrderId).Select()
if err != nil {
logger.Error(err.Error())
}
//去找用户信息
var userMain model.UserMain
err = db.Reset().Table(&userMain).Fields("user_id,mobile,is_new").Where("user_id", orderGif.UserId).Select()
if err != nil {
logger.Error(err.Error())
}
//组装最后的统计结果到mongo
data := model.GiftActivityDetail{
UserId: userMain.UserId,
Mobile: userMain.Mobile,
IsNew: userMain.IsNew,
OrderSn: orderGif.OrderSn,
OrderId: orderGif.OrderId,
OrderTime: order.CreateTime,
OrderStatus: order.Status,
ActivityId: orderGif.ActivityId,
ItemName: gjson.Get(orderGif.GiftInfo, "item_name").String(),
ItemNum: int(gjson.Get(orderGif.GiftInfo, "num").Int()),
ItemStatus: orderGif.Status,
}
err = mongoConn.DB("ichunt").C("gift_activity_detail").Insert(&data)
if err != nil {
logger.Error(err.Error())
}
}
}
}
//价格促销活动*****************
var priceActivityList []model.PriceActivity
err = specialDb.Reset().Table(&priceActivityList).Fields("id,start_time,end_time").Where("end_time", ">", nowTime).Select()
err = specialDb.Reset().Table(&priceActivityList).Fields("id,start_time,end_time").Where("status", "!=", -1).
Where("end_time", ">", updateTime).Select()
if err != nil {
logger.Error(err.Error())
}
for _, activity := range priceActivityList {
//一个个去统计(type=4代表是活动)
orderNum, _ := db.Reset().Table("lie_order_activity_count").Where("activity_id", activity.Id).Count("order_id")
newUserNum, _ := db.Reset().Table("lie_order_activity_count").LeftJoin("lie_user_main on lie_order_activity_count.user_id = lie_user_main.user_id").
Where("lie_order_activity_count.activity_id", activity.Id).Where("lie_user_main.is_new", 1).
......@@ -139,20 +190,20 @@ func main() {
data["activity_id"] = activity.Id
data["type"] = 1
if count > 0 {
_,err = specialDb.Reset().Table("lie_activity_statistics").Where("activity_id", activity.Id).Data(data).Update()
_, err = specialDb.Reset().Table("lie_activity_statistics").Where("activity_id", activity.Id).Data(data).Update()
if err != nil {
logger.Error(err.Error())
}
} else {
data["create_time"] = time.Now().Unix()
_,err = specialDb.Reset().Table("lie_activity_statistics").Data(data).Insert()
_, err = specialDb.Reset().Table("lie_activity_statistics").Data(data).Insert()
if err != nil {
logger.Error(err.Error())
}
}
items, err := db.Reset().Fields("*,count(amount) as total_amount").Table("lie_order_items_price").
LeftJoin("lie_user_main on lie_order_items_price.user_id = lie_user_main.user_id").
Where("lie_order_items_price.activity_id", activity.Id).Where("lie_order_items_price.type", 4).
items, err := db.Reset().Fields("*,count(amount) as total_amount").Table("lie_order_activity_count").
LeftJoin("lie_user_main on lie_order_activity_count.user_id = lie_user_main.user_id").
Where("lie_order_activity_count.activity_id", activity.Id).
GroupBy("order_id").Get()
if err != nil {
logger.Error(err.Error())
......@@ -164,7 +215,8 @@ func main() {
//先去检查这个订单是否在里面
var priceActivityMongo model.PriceActivityMongo
err = mongoConn.DB("ichunt").C("price_activity_statistics").
Find(bson.M{"order_id": gjson.Get(result, "order_id").Int()}).One(&priceActivityMongo)
Find(bson.M{"order_id": gjson.Get(result, "order_id").Int(), "activity_id": gjson.Get(result, "activity_id").Int()}).
One(&priceActivityMongo)
if err != nil && err != mgo.ErrNotFound {
logger.Error(err.Error())
}
......
......@@ -9,3 +9,20 @@ type GiftActivity struct {
func (ga *GiftActivity) TableName() string {
return "lie_gift_activity"
}
//活动明细
type GiftActivityDetail struct {
UserId int `bson:"user_id"`
Mobile string `bson:"mobile"`
IsNew int `bson:"is_new"`
OrderId int `bson:"order_id"`
OrderSn string `bson:"order_sn"`
OrderTime int `bson:"order_time"`
OrderStatus int `bson:"order_status"`
ActivityId int `bson:"activity_id"`
ItemName string `bson:"item_name"`
ItemNum int `bson:"item_num"`
//赠品发放状态
ItemStatus int `bson:"status"`
}
\ No newline at end of file
package model
type Order struct {
OrderId int `gorose:"order_id"`
Status int `gorose:"status"`
CreateTime int `gorose:"create_time"`
}
func (o *Order) TableName() string {
return "lie_order"
}
package model
type OrderGift struct {
Id int `gorose:"id"`
OrderId int `gorose:"order_id"`
OrderSn string `gorose:"order_sn"`
UserId int `gorose:"user_id"`
ActivityId int `gorose:"activity_id"`
GiftInfo string `gorose:"gift_info"`
Status int `gorose:"status"`
CreateTime int `gorose:"create_time"`
UpdateTime int `gorose:"update_time"`
}
func (og *OrderGift) TableName() string {
return "lie_order_gift"
}
package model
type UserMain struct {
UserId int `gorose:"user_id"`
Mobile string `gorose:"mobile"`
IsNew int `gorose:"is_new"`
RegRemark string `gorose:"reg_remark"`
}
func (um *UserMain) TableName() string {
return "lie_user_main"
}
......@@ -7,6 +7,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/gohouse/gorose/v2"
"github.com/ichunt2019/go-rabbitmq/utils/rabbitmq"
"github.com/ichunt2019/logger"
"go-queue-server/special/activity_view_statistics/model"
"go-queue-server/util"
"gopkg.in/mgo.v2"
......@@ -28,9 +29,21 @@ var (
func init() {
initArgs()
util.Init(ConfigDir)
initLog()
initDB()
}
func initLog() {
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir + "special/activity_view_statistics/logs"
logConfig["log_chan_size"] = "100"
err := logger.InitLogger("file", logConfig)
if err != nil {
panic(err)
}
logger.Init()
}
// 解析命令行参数
func initArgs() {
//go run main.go -configDir xxx
......@@ -66,26 +79,26 @@ 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 {
fmt.Println(err)
logger.Error(err.Error())
return
}
//先去遍历数据库,全部过一遍
var (
historyData 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}
}
//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}
//}
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
}
//mysql数据操作
......@@ -101,7 +114,7 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
}
_, err = specialDb.Reset().Table("lie_activity_view_statistics").Data(activityViewStatistics).Insert()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
return
}
} else {
......@@ -120,14 +133,20 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
_, err = specialDb.Reset().Table("lie_activity_view_statistics").Where("activity_id", viewData.ActivityId).
Data(activityViewStatistics).Update()
if err != nil {
fmt.Println(err)
logger.Error(err.Error())
return
}
}
//如果mongo存在,更新mongo数据
if historyData.ActivityId != 0 {
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)
if err != nil {
logger.Error(err.Error())
}
} else {
var (
mobile string
......@@ -139,7 +158,7 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
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
......@@ -147,7 +166,7 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
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
......@@ -165,10 +184,20 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
LastVisitTime: 0,
Pf: viewData.Pf,
}
err = mongoConn.DB("ichunt").C("activity_view_history").Insert(&historyData)
if err != nil {
fmt.Println(err)
if historyData.UniqueId == "" {
err = mongoConn.DB("ichunt").C("activity_view_history").Insert(&historyData)
if err != nil {
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)
if err != nil {
logger.Error(err.Error())
}
}
}
return nil
}
......
......@@ -12,3 +12,27 @@ func (avs *ActivityViewStatistics) TableName() string {
return "lie_activity_view_statistics"
}
type HistoryData struct {
UniqueId string `bson:"unique_id"`
ActivityId int `bson:"activity_id"`
Mobile string `bson:"mobile"`
UserId int `bson:"user_id"`
IsNewReg int `bson:"is_new_reg"`
AdTag string `bson:"ad_tag"`
Source string `bson:"source"`
VisitTime int `bson:"visit_time"`
LastVisitTime int `bson:"last_visit_time"`
Pf int `bson:"pf"`
}
type ViewData struct {
ActivityId int `json:"activity_id"`
UniqueId string `json:"unique_id"`
UserId int `json:"user_id"`
Source string `json:"source"`
Referer string `json:"referer"`
VisitTime int `json:"visit_time"`
Pf int `json:"pf"`
}
package model
type HistoryData struct {
UniqueId string `bson:"unique_id"`
ActivityId int `bson:"activity_id"`
Mobile string `bson:"mobile"`
UserId int `bson:"user_id"`
IsNewReg int `bson:"is_new_reg"`
AdTag string `bson:"ad_tag"`
Source string `bson:"source"`
VisitTime int `bson:"visit_time"`
LastVisitTime int `bson:"last_visit_time"`
Pf int `bson:"pf"`
}
package model
type ViewData struct {
ActivityId int `json:"activity_id"`
UniqueId string `json:"unique_id"`
UserId int `json:"user_id"`
Source string `json:"source"`
Referer string `json:"referer"`
VisitTime int `json:"visit_time"`
Pf int `json:"pf"`
}
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