package userResourcemod import ( "91porn-server/common" "91porn-server/common/db" "91porn-server/common/localcache" "91porn-server/common/log" "91porn-server/models" "fmt" "time" "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" ) var mdb *db.MongoDB const table = models.UserResource func coll(t *db.MongoTool) *db.MongoTool { if t == nil { return mdb.Coll(table) } return t.Coll(table) } func initIndex() { many := []mongo.IndexModel{ { Keys: bson.D{{"type", 1}}, }, { Keys: bson.D{{"updatedAt", -1}}, }, } _, err := coll(nil).CreateIndex(many) if err != nil { panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err)) } } // getListCnt 获取总数 func getListCnt(cond bson.M) (int64, error) { total, err := coll(nil).Count(cond) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getListCnt", table, "Count", err), log.Any("cond", cond), ) return 0, err } return total, nil } // GetSkipSize 计算跳转 func getSkipSize(page int, size int, cond bson.M) (int, int64, error) { total, err := getListCnt(cond) if err != nil { return 0, 0, err } return (page - 1) * size, total, nil } // todo app // GetUserResourceByType 获取用户资源配置 func GetUserResourceByType(rType string) ([]string, error) { cacheKey := fmt.Sprintf("GetUserResourceByType:%s", rType) cfg, ok := localcache.C.Get(cacheKey) if ok { return cfg.([]string), nil } var datas []UserResource cond := bson.M{} if rType != "" { cond = bson.M{"type": rType} } err := coll(nil).Find(&datas, cond) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetUserResource", table, "Find", err), log.Any("rType", rType)) } res := make([]string, len(datas)) if len(datas) > 0 { for i := range datas { res[i] = datas[i].Resource } } common.Go(func() { localcache.C.Set(cacheKey, res, 30*time.Minute) }) return res, err } // GetUserResource 获取用户资源配置 func GetUserResource(page int, size int, rType string) ([]UserResource, int64, error) { var datas []UserResource cond := bson.M{} if rType != "" { cond = bson.M{"type": rType} } sort := bson.D{{"updatedAt", -1}} skip, total, err := getSkipSize(page, size, cond) if err != nil { return datas, total, err } opts := options.FindOptions{} opts.SetSort(sort).SetSkip(int64(skip)).SetLimit(int64(size)) err = coll(nil).Find(&datas, cond, &opts) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetUserResource", table, "Find", err), log.Any("page", page), log.Any("size", size), ) } return datas, total, err } // Add 添加 func Add(ur UserResource) error { _, err := coll(nil).InsertOne(&ur) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Add", table, "InsertOne", err)) } return err } func getRecoUpdate(rType, resource *string) bson.M { update := bson.M{"updatedAt": time.Now()} if rType != nil { update["type"] = *rType } if resource != nil { update["resource"] = *resource } return update } // ModifyUserResource 修改推荐用户信息 func ModifyUserResource(id primitive.ObjectID, rType, resource *string) error { cond := bson.M{"_id": id} update := getRecoUpdate(rType, resource) _, err := coll(nil).UpdateOne(cond, bson.M{"$set": update}) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ModifyUserResource", table, "UpdateOne", err), log.Any("id", id), log.Any("rType", rType), log.Any("resource", resource), ) } return err } // RemoveUserResource 删除推荐用户信息 func RemoveUserResource(id primitive.ObjectID) error { cond := bson.M{"_id": id} _, err := coll(nil).DeleteOne(cond) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveUserResource", table, "DeleteOne", err), log.Any("id", id), ) } return err } // GetResourceIsExist 资源是否存在 func GetResourceIsExist(resource string) bool { b, err := coll(nil).Exists(bson.M{"resource": resource}) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetResourceIsExist", table, "Exists", err)) return false } return b }