| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- 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 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) 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
- }
|