You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
package main |
|
|
|
import ( |
|
"context" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"github.com/qiniu/qmgo" |
|
"go.mongodb.org/mongo-driver/bson" |
|
"go.mongodb.org/mongo-driver/bson/primitive" |
|
"log" |
|
) |
|
|
|
var ( |
|
MongoDatabaseList map[string]*qmgo.Database |
|
) |
|
|
|
func main() { |
|
ctx := context.Background() |
|
initMongo(ctx) |
|
for k, _ := range MongoDatabaseList { |
|
findUnit(ctx, k) |
|
} |
|
|
|
} |
|
|
|
func initMongo(ctx context.Context) { |
|
mongoCfg, err := g.Cfg().Get(ctx, "game.mongo") |
|
if err != nil { |
|
return |
|
} |
|
config := mongoCfg.Maps() |
|
|
|
MongoDatabaseList = map[string]*qmgo.Database{} |
|
for _, v := range config { |
|
mongoclient, err := qmgo.NewClient(ctx, &qmgo.Config{Uri: v["link"].(string)}) |
|
if err != nil { |
|
log.Println("数据库: ", v["id"], ", 创建错误:", err.Error()) |
|
continue |
|
} |
|
db := mongoclient.Database(v["name"].(string)) |
|
MongoDatabaseList[v["id"].(string)] = db |
|
} |
|
} |
|
|
|
func findUnit(ctx context.Context, serverId string) { |
|
query := bson.M{"$and": bson.A{bson.M{"Children.ConfigId": 845}, bson.M{"Children.Amount": bson.M{"$gt": 1000000}}}} |
|
selectStores(ctx, serverId, query) |
|
} |
|
|
|
func selectStores(ctx context.Context, serverId string, query bson.M) { |
|
stores := []map[string]interface{}{} |
|
MongoDatabaseList[serverId].Collection("StoreComponent").Find(ctx, query).All(&stores) |
|
if len(stores) == 0 { |
|
log.Printf("服务器编号[%s] 不存在数量!", serverId) |
|
return |
|
} |
|
log.Printf("服务器编号[%s] 搜素长度: %d", serverId, len(stores)) |
|
for _, v := range stores { |
|
store := v["Children"].(primitive.A) |
|
for _, v1 := range store { |
|
item := v1.(map[string]interface{}) |
|
if item["ConfigId"].(int32) == 845 && item["Amount"].(int32) > 1000000 { |
|
log.Printf("服务器编号: %s, id: %d, 物品: %d, 数量: %d", serverId, v["_id"], item["ConfigId"], item["Amount"]) |
|
break |
|
} |
|
} |
|
|
|
} |
|
log.Printf("服务器编号[%s] 结束!", serverId) |
|
}
|
|
|