package dao import ( "context" "fmt" "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/frame/g" "gmanager/library/base" "gmanager/library/mongo/util" "log" "github.com/gogf/gf/container/garray" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) // 查询多个 func (m *Mgo) FindAllItemm(form *base.BaseForm) (*mongo.Cursor, int) { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() sid := 0 itemType := "" if form.Params["operTable"] != "" { sid = gconv.Int(form.Params["operTable"]) } filter := bson.D{{"serverid", sid}} SORT := bson.D{{"_id", 1}} //filter := bson.D{{key,value}} Limit := gconv.Int64(form.Rows) Skip := form.Page - 1 SkipNum := gconv.Int64(Skip) * Limit groupStage := bson.D{{"$group", bson.D{ {"_id", ""}, {"sum", bson.D{{"$sum", "$itemNum"}}}, {"count", bson.D{{"$sum", 1}}}, }}} matchStage := bson.D{{"$match", bson.D{{"serverid", sid}}}} if form.Params["logType"] != "" { itemType = form.Params["logType"] filter = bson.D{{"serverid", sid}, {"type", itemType}} matchStage = bson.D{{"$match", bson.D{{"serverid", sid}, {"type", itemType}}}} } showInfoCursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStage, groupStage}) var showsWithInfo []bson.M if err = showInfoCursor.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } if showsWithInfo == nil { return nil, 0 } datarow := gconv.Int(showsWithInfo[0]["count"]) datasum := gconv.Int(showsWithInfo[0]["sum"]) form.TotalSize = datarow form.TotalPage = datarow / form.Rows fmt.Println(len(showsWithInfo)) findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(SkipNum) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult, datasum } func (m *Mgo) FindAllTime(form *base.BaseForm) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //collection. starttime := form.Params["starttime"] if starttime == "" { starttime = gtime.Now().Format("Y-m-d") } else { starttime = gtime.New(gconv.Time(starttime)).Format("Y-m-d") } fmt.Println("--=-" + starttime) filter := bson.D{{"checkTimeday", starttime}} singleResult, _ := collection.Find(context.TODO(), filter) return singleResult } func (m *Mgo) FindDauTime(starttime string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() filter := bson.D{{"logTime", starttime}} singleResult, _ := collection.Find(context.TODO(), filter) return singleResult } func (m *Mgo) FindDauTimeUser(starttime string, roles interface{}, time string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection + "_" + starttime).Clone() filter := bson.D{{"userid", bson.M{"$in": roles}}} singleResult, _ := collection.Find(context.TODO(), filter) return singleResult } func (m *Mgo) FindDauTimeChannel(starttime string, channel string) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection + "_" + starttime).Clone() filter := bson.D{{"channelId", channel}} values, err := collection.Distinct(context.TODO(), "userid", filter) if err != nil { //log.Fatal(err) log.Println(err) } return len(values) } func (m *Mgo) FindRegTime() []bson.M { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() dateBson := bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": bson.M{"$add": bson.A{gconv.Time(28800), "$register.registerTime"}}}} pipe := []bson.M{ {"$project": bson.M{"date": dateBson, "user_id": "$register.openId"}}, {"$group": bson.M{"_id": bson.M{"date": "$date", "user": "$user_id"}}}, {"$group": bson.M{"_id": "$_id.date", "count": bson.M{"$sum": 1}}}, {"$project": bson.M{"date": "$_id", "count": 1, "_id": 0}}, } ginfo, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = ginfo.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } //listMap := gmap.NewListMap(true) //for _, b := range showsWithInfo { // listMap.Set(b["count"],b["date"]) // //} fmt.Println(showsWithInfo) return showsWithInfo } func (m *Mgo) FindDau() []bson.M { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() dateBson := bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": bson.M{"$add": bson.A{gconv.Time(28800), "$loginTime.loginTime"}}}} pipe := []bson.M{ {"$project": bson.M{"date": dateBson, "user_id": "$loginTime.openId"}}, {"$group": bson.M{"_id": bson.M{"date": "$date", "user": "$user_id"}}}, {"$group": bson.M{"_id": "$_id.date", "count": bson.M{"$sum": 1}}}, {"$project": bson.M{"date": "$_id", "count": 1, "_id": 0}}, } ginfo, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = ginfo.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } fmt.Println(showsWithInfo) return showsWithInfo } func (m *Mgo) FindNew(startTime int64, endTime int64) *garray.Array { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{{"register.registerTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}} //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { s2 := b["register"].(primitive.M) list.Append(s2["uid"]) } return list } func (m *Mgo) FindNewByServerId(startTime int64, endTime int64, serverId string) *garray.Array { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{{"register.serverId", serverId}, {"register.registerTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}} //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { s2 := b["register"].(primitive.M) list.Append(s2["uid"]) } return list } func (m *Mgo) FindLogin(startTime int64, endTime int64, roles interface{}) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{ {"loginTime.loginTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"loginTime.uid", bson.M{"$in": roles}}} //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { s2 := b["loginTime"].(primitive.M) if list.Contains(s2["uid"]) { continue } list.Append(s2["uid"]) } return list.Len() } // 充值过的用户 func (m *Mgo) FindPay(startTime string, endTime string, roles interface{}, serverId string) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{} if serverId == "" { filter = bson.D{ {"creattime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, {"uid", bson.M{"$in": roles}}} } else { filter = bson.D{ {"creattime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, {"region", gconv.Int32(serverId)}, {"uid", bson.M{"$in": roles}}} } //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { if list.Contains(b["uid"]) { continue } list.Append(b["uid"]) } return list.Len() } func (m *Mgo) FindLogin1(startTime int64, endTime int64, roles interface{}, serverId string) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{ {"loginTime.loginTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"loginTime.serverId", serverId}, {"loginTime.uid", bson.M{"$in": roles}}} //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { s2 := b["loginTime"].(primitive.M) if list.Contains(s2["uid"]) { continue } list.Append(s2["uid"]) } return list.Len() } func (m *Mgo) FindLogin2(startTime int64, endTime int64) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //filter := bson.D{bson.M{"register.registerTime":bson.D{{"$gte",startTime},{"$lte",endTime}}}, "_id": 1} filter := bson.D{ {"loginTime.loginTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}} //oppo:=bson.M{"register.openId": 1} var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) for _, b := range showsWithInfo { s2 := b["loginTime"].(primitive.M) if list.Contains(s2["uid"]) { continue } list.Append(s2["uid"]) } return list.Len() } //查询各服务器的注册人数 func (m *Mgo) FindRegisterByServer(startTime int64, endTime int64) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() groupStage := bson.D{{"$group", bson.D{ {"_id", "$register.serverId"}, {"count", bson.D{{"$sum", 1}}}, }}} matchStage := bson.D{{"$match", bson.D{{"register.registerTime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}}}} showInfoCursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStage, groupStage}) if err != nil { fmt.Println(err) } return showInfoCursor } //查询各服务器的登录人数 func (m *Mgo) FindLoginByServer(startTime int64, endTime int64) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() pipe := []bson.M{ {"$match": bson.M{"loginTime.loginTime": bson.M{"$gte": startTime, "$lte": endTime}}}, {"$group": bson.M{"_id": bson.M{"serverId": "$loginTime.serverId", "uid": "$loginTime.uid"}, "count": bson.M{"$sum": 1}}}, {"$project": bson.M{"serverId": "$_id.serverId", "uid": "$_id.uid", "count": 1}}, } showInfoCursor, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } return showInfoCursor } func (m *Mgo) FindLiuCun(filter bson.D, serverId string) *mongo.Cursor { client := DB.Mongo if serverId == "" { collection, _ := client.Database(m.database).Collection(m.collection).Clone() //oppo:=bson.M{"register.openId": 1} singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } else { collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone() //oppo:=bson.M{"register.openId": 1} singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } } func (m *Mgo) FindHour(filter bson.D, page util.Page) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() SORT := bson.D{{"_id", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) GetDataByTimeDesc(filter bson.D, page util.Page) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() SORT := bson.D{{"creatday", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) GetDataByServerTimeDesc(filter bson.D, page util.Page, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone() SORT := bson.D{{"creatday", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) GetDataByServerTimeDesc2(filter bson.D, page util.Page, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() SORT := bson.D{{"time", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) FindHourByServer(filter bson.D, page util.Page, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone() SORT := bson.D{{"_id", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) FindListByServer(filter bson.D, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) FindUser(filter bson.D, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() //oppo:=bson.M{"register.openId": 1} singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } func (m *Mgo) FindLV(serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() pipe := []bson.M{ {"$group": bson.M{"_id": "$playerManager.level", "value": bson.M{"$sum": 1}}}, {"$sort": bson.M{"_id": 1}}, } showInfoCursor, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } return showInfoCursor } func (m *Mgo) FindVIP(serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() pipe := []bson.M{ {"$group": bson.M{"_id": "$playerManager.vipLevel", "value": bson.M{"$sum": 1}}}, {"$sort": bson.M{"_id": 1}}, } showInfoCursor, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } return showInfoCursor } func (m *Mgo) FindServerInfoList(filter interface{}) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } // 新增服务器信息 func (m *Mgo) AddServer(value interface{}) *mongo.InsertOneResult { client := DB.Mongo collection := client.Database(m.database).Collection(m.collection) insertResult, err := collection.InsertOne(context.TODO(), value) if err != nil { fmt.Println(err) } return insertResult } // 更新服务器信息 func (m *Mgo) UpdateServer(id int, value interface{}) *mongo.UpdateResult { serverInfo := gconv.Map(value) client := DB.Mongo collection := client.Database(m.database).Collection(m.collection) filter := bson.D{{"_id", id}} update := bson.D{{"$set", bson.D{ {"name", gconv.String(serverInfo["Name"])}, {"ip", gconv.String(serverInfo["Ip"])}, {"port", gconv.String(serverInfo["Port"])}, {"plat", gconv.String(serverInfo["Plat"])}, {"state", gconv.String(serverInfo["State"])}, {"open_time", gconv.String(serverInfo["OpenTime"])}, {"open_type", gconv.String(serverInfo["OpenType"])}, {"is_banreg", gconv.String(serverInfo["IsBanreg"])}, {"is_new", gconv.String(serverInfo["IsNew"])}, {"server_version", gconv.String(serverInfo["ServerVersion"])}, {"gm_ip", gconv.String(serverInfo["GMIp"])}, {"gm_port", gconv.String(serverInfo["GMPort"])}, {"isWhite", gconv.String(serverInfo["IsWhite"])}, {"currency", gconv.String(serverInfo["Currency"])}, {"time_zone", gconv.String(serverInfo["Timezone"])}, {"exportdata", gconv.String(serverInfo["Exportdata"])}, }}} updateResult, err := collection.UpdateOne(context.TODO(), filter, update) fmt.Println(updateResult) if err != nil { fmt.Println(err) } return updateResult } func (m *Mgo) DelServer(filter bson.D) *mongo.DeleteResult { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() deleteResult, err := collection.DeleteOne(context.TODO(), filter) if err != nil { fmt.Println(err) } return deleteResult } //白名单列表 func (m *Mgo) FindWhiteList(filter bson.D, page util.Page) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() SORT := bson.D{{"_id", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } //黑名单列表 func (m *Mgo) FindBlackList(filter bson.D, page util.Page) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() SORT := bson.D{{"_id", -1}} //1:升序; -1:降序 findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1))) singleResult, err := collection.Find(context.TODO(), filter, findOptions) if err != nil { fmt.Println(err) } return singleResult } //新增、更新单个 func (m *Mgo) AddWhiteList(id int, value interface{}) *mongo.UpdateResult { client := DB.Mongo collection := client.Database(m.database).Collection(m.collection) filter := bson.D{{"_id", id}} update := bson.D{{"$set", value}} fmt.Println(value) options1 := options.Update().SetUpsert(true) updateResult, err := collection.UpdateOne(context.TODO(), filter, update, options1) fmt.Println(updateResult) if err != nil { fmt.Println(err) } return updateResult } //新增、更新单个 func (m *Mgo) AddBlackList(id string, openid string, value interface{}) int { res := 1 client := DB.Mongo collection := client.Database(m.database).Collection(m.collection) //如果id为空 添加 if id == "" { insertResult, err := collection.InsertOne(context.TODO(), value) fmt.Println(insertResult) if err != nil { res = 0 fmt.Println(err) } } else { obj_id, _ := primitive.ObjectIDFromHex(id) filter := bson.D{{"_id", obj_id}} update := bson.D{{"$set", value}} //options1 := options.Update().SetUpsert(true) updateResult, err := collection.UpdateOne(context.TODO(), filter, update) fmt.Println(updateResult) if err != nil { res = 0 fmt.Println(err) } } return res } func (m *Mgo) DelWhiteList(filter bson.D) *mongo.DeleteResult { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() deleteResult, err := collection.DeleteOne(context.TODO(), filter) if err != nil { fmt.Println(err) } return deleteResult } func (m *Mgo) DelBlackList(filter bson.D) *mongo.DeleteResult { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() deleteResult, err := collection.DeleteOne(context.TODO(), filter) if err != nil { fmt.Println(err) } return deleteResult } //公告 func (m *Mgo) FindNoticeInfo(filter bson.D) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } //新增、更新单个 func (m *Mgo) AddNoticeInfo(id string, value interface{}) *mongo.UpdateResult { client := DB.Mongo collection := client.Database(m.database).Collection(m.collection) filter := bson.D{{"_id", id}} update := bson.D{{"$set", value}} fmt.Println(value) options1 := options.Update().SetUpsert(true) updateResult, err := collection.UpdateOne(context.TODO(), filter, update, options1) fmt.Println(updateResult) if err != nil { fmt.Println(err) } return updateResult } func (m *Mgo) DelNoticeInfo(filter bson.D) *mongo.DeleteResult { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() deleteResult, err := collection.DeleteOne(context.TODO(), filter) if err != nil { fmt.Println(err) } return deleteResult } func (m *Mgo) FindRechargeInfo(filter bson.D, serverId string) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println("err==================", err) } return singleResult } //查询一天的注册/登录人数 例:creatDay = "2021-06-08" func (m *Mgo) GetOneDayRegister(creatDay string) int { client := DB.Mongo sum := 0 collection, _ := client.Database(m.database).Collection(m.collection).Clone() matchStage := bson.D{{"$match", bson.D{{"creatday", creatDay}}}} groupStage := bson.D{{"$group", bson.D{ {"_id", "$creatday"}, {"sum", bson.D{{"$sum", "$newuser"}}}, }}} singleResult, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStage, groupStage}) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } for _, b := range showsWithInfo { sum = sum + gconv.Int(b["sum"]) } return sum } func (m *Mgo) GetOneDayRegisterByServer(creatDay string, serverId string) int { client := DB.Mongo sum := 0 collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone() matchStage := bson.D{{"$match", bson.D{{"creatday", creatDay}}}} groupStage := bson.D{{"$group", bson.D{ {"_id", "$creatday"}, {"sum", bson.D{{"$sum", "$newuser"}}}, }}} singleResult, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStage, groupStage}) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } for _, b := range showsWithInfo { sum = sum + gconv.Int(b["sum"]) } return sum } //查询注册账号数 func (m *Mgo) GetOneDayRegisterAccount(creatDay string, serverId string) int { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() startTime := gtime.NewFromStr(creatDay) //时间范围 daystartTime := startTime.Local().TimestampMilli() dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1 //注册时间条件 pipe := []bson.M{} if serverId == "" { pipe = []bson.M{ {"$match": bson.M{"register.registerTime": bson.M{"$gte": daystartTime, "$lte": dayendTime}}}, {"$group": bson.M{"_id": bson.M{"openId": "$register.openId"}, "count": bson.M{"$sum": 1}}}, {"$project": bson.M{"openId": "$_id.openId", "count": 1}}, } } else { pipe = []bson.M{ {"$match": bson.M{"register.serverId": serverId, "register.registerTime": bson.M{"$gte": daystartTime, "$lte": dayendTime}}}, {"$group": bson.M{"_id": bson.M{"openId": "$register.openId"}, "count": bson.M{"$sum": 1}}}, {"$project": bson.M{"openId": "$_id.openId", "count": 1}}, } } showInfoCursor, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = showInfoCursor.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } return len(showsWithInfo) } func (m *Mgo) GetLoginNum(filter bson.D) int { sum := 0 client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } for _, b := range showsWithInfo { sum = sum + gconv.Int(b["newuser"]) } return sum } //获得当天老用户登录数(当天登录人数-当天注册人数) func (m *Mgo) GetOldUserLoginNum(creatDay string) int { client := DB.Mongo startTime := gtime.NewFromStr(creatDay) //时间范围 daystartTime := startTime.Local().TimestampMilli() dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1 //注册时间条件 filter := bson.D{{"register.registerTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}}} var findoptions *options.FindOptions collection, _ := client.Database(g.Config().GetString("db.core")).Collection("logRegister").Clone() singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) //遍历加入注册用户集合 for _, b := range showsWithInfo { s2 := b["register"].(primitive.M) list.Append(s2["uid"]) } //fmt.Println("注册=======================", list) usersList := gconv.SliceAny(list) collection1, _ := client.Database(g.Config().GetString("db.core")).Collection("logLogin").Clone() //查询登录用户条件 filter1 := bson.D{ {"loginTime.loginTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}}, {"loginTime.uid", bson.M{"$nin": gconv.Ints(usersList)}}} var findoptions1 *options.FindOptions singleResult1, err := collection1.Find(context.TODO(), filter1, findoptions1) if err != nil { fmt.Println(err) } var showsWithInfo1 []bson.M if err = singleResult1.All(context.TODO(), &showsWithInfo1); err != nil { fmt.Println(err) } list1 := garray.New(false) for _, b := range showsWithInfo1 { s2 := b["loginTime"].(primitive.M) if list1.Contains(s2["uid"]) { continue } list1.Append(s2["uid"]) } //fmt.Println("登录=======================",list1) return list1.Len() } func (m *Mgo) GetOldUserLoginNumByServer(creatDay string, serverId string) int { client := DB.Mongo startTime := gtime.NewFromStr(creatDay) //时间范围 daystartTime := startTime.Local().TimestampMilli() dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1 //注册时间条件 filter := bson.D{ {"register.serverId", serverId}, {"register.registerTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}}, } var findoptions *options.FindOptions collection, _ := client.Database(g.Config().GetString("db.core")).Collection("logRegister").Clone() singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } list := garray.New(false) //遍历加入注册用户集合 for _, b := range showsWithInfo { s2 := b["register"].(primitive.M) list.Append(s2["uid"]) } //fmt.Println("注册=======================", list) usersList := gconv.SliceAny(list) collection1, _ := client.Database(g.Config().GetString("db.core")).Collection("logLogin").Clone() //查询登录用户条件 filter1 := bson.D{ {"loginTime.serverId", serverId}, {"loginTime.loginTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}}, {"loginTime.uid", bson.M{"$nin": gconv.Ints(usersList)}}} var findoptions1 *options.FindOptions singleResult1, err := collection1.Find(context.TODO(), filter1, findoptions1) if err != nil { fmt.Println(err) } var showsWithInfo1 []bson.M if err = singleResult1.All(context.TODO(), &showsWithInfo1); err != nil { fmt.Println(err) } list1 := garray.New(false) for _, b := range showsWithInfo1 { s2 := b["loginTime"].(primitive.M) if list1.Contains(s2["uid"]) { continue } list1.Append(s2["uid"]) } //fmt.Println("登录=======================",list1) return list1.Len() } func (m *Mgo) GetPayInfoTask(filter bson.D) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), filter) if err != nil { fmt.Println(err) } return singleResult } //插入单个 func (m *Mgo) InsertOneByServer(value interface{}, serverId string) *mongo.InsertOneResult { client := DB.Mongo collection := client.Database(m.database).Collection(m.collection + "_" + serverId) insertResult, err := collection.InsertOne(context.TODO(), value) if err != nil { fmt.Println(err) } return insertResult } // 添加修改 统计充值汇总 func (m *Mgo) InsertAndUpdateStatisticalRecharge(id primitive.ObjectID, value interface{}, serverId string) *mongo.UpdateResult { statisticalRecharge := gconv.Map(value) client := DB.Mongo filter := bson.D{{"_id", id}} update := bson.D{{"$set", bson.D{ {"creatday", gconv.String(statisticalRecharge["creatday"])}, {"new_recharge_num", gconv.Int32(statisticalRecharge["newrechargenum"])}, {"new_recharge_amount", gconv.Float64(statisticalRecharge["newrechargeamount"])}, {"new_recharge_count", gconv.Int32(statisticalRecharge["newrechargecount"])}, {"recharge_num", gconv.Int32(statisticalRecharge["rechargenum"])}, {"recharge_amount", gconv.Float64(statisticalRecharge["rechargeamount"])}, {"recharge_count", gconv.Int32(statisticalRecharge["rechargecount"])}, }}} options := options.Update().SetUpsert(true) if serverId == "" { collection := client.Database(m.database).Collection(m.collection) updateResult, err := collection.UpdateOne(context.TODO(), filter, update, options) if err != nil { fmt.Println(err) } return updateResult } else { collection := client.Database(m.database).Collection(m.collection + "_" + serverId) updateResult, err := collection.UpdateOne(context.TODO(), filter, update, options) if err != nil { fmt.Println(err) } return updateResult } } // pay表 取符合条件的服务器id func (m *Mgo) GetPayServerId(filter bson.D) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() pipe := []bson.M{ {"$match": filter}, {"$group": bson.M{"_id": bson.M{"region": "$region"}}}, {"$project": bson.M{"region": "$_id.region"}}, } showInfoCursor, err := collection.Aggregate(context.TODO(), pipe) if err != nil { fmt.Println(err) } return showInfoCursor } //查询当日新增玩家累计付费信息 func (m *Mgo) GetTodayNewCumulativeRecharge(todayUsers []interface{}, creatDay string, serverid string) interface{} { amount, count, num := 0, 0, 0 resMap := gmap.New() userList := garray.New(false) todayBs := bson.D{} if serverid == "" { todayBs = bson.D{ {"uid", bson.M{"$in": gconv.Ints(todayUsers)}}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, {"creattime", bson.D{{"$lte", creatDay + " 23:59:59"}}}} } else { todayBs = bson.D{ {"uid", bson.M{"$in": gconv.Ints(todayUsers)}}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, {"region", gconv.Int32(serverid)}, {"creattime", bson.D{{"$lte", creatDay + " 23:59:59"}}}} } client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() singleResult, err := collection.Find(context.TODO(), todayBs) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } for _, v := range showsWithInfo { if !userList.Contains(v["uid"]) { num = num + 1 } userList.Append(v["uid"]) count = count + 1 amount = amount + gconv.Int(v["money"]) } resMap.Set("amount", amount) resMap.Set("count", count) resMap.Set("num", num) return resMap } func (m *Mgo) GetLtvStatistical(startTime string, endTime string, roles interface{}, serverId string) int64 { var res int64 res = 0 client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() filter := bson.D{} if serverId == "" { filter = bson.D{ {"creattime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"uid", bson.M{"$in": roles}}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, } } else { filter = bson.D{ {"creattime", bson.D{{"$gte", startTime}, {"$lte", endTime}}}, {"uid", bson.M{"$in": roles}}, {"region", gconv.Int32(serverId)}, {"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, } } var findoptions *options.FindOptions singleResult, err := collection.Find(context.TODO(), filter, findoptions) if err != nil { fmt.Println(err) } var showsWithInfo []bson.M if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil { fmt.Println(err) } for _, b := range showsWithInfo { res = res + (gconv.Int64(b["money"])) } return res } func (m *Mgo) FindUserList(filter bson.M, serverId string, projection bson.M) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone() //oppo:=bson.M{"register.openId": 1} options := options.Find().SetProjection(projection) singleResult, err := collection.Find(context.TODO(), filter, options) if err != nil { fmt.Println(err) } return singleResult }