package mediacontentmod import ( "91porn-server/models" "encoding/json" "errors" "fmt" "strings" "time" "91porn-server/common/db" "91porn-server/common/log" "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" ) const table = models.MediaContent func coll(t *db.MongoTool) *db.MongoTool { if t == nil { return mdb.Coll(table) } return t.Coll(table) } // Init 初始化索引 func Init() { mdb = db.Init(table) initIndex() } func initIndex() { many := []mongo.IndexModel{ { Keys: bson.D{{Key: "mediaId", Value: 1}}, }, { Keys: bson.D{{Key: "hashId", Value: 1}}, }, } if _, err := coll(nil).CreateIndex(many); err != nil { panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err)) } } // GetList 获取列表 func GetList(cond bson.M, skip, limit int64, sort bson.D) (res []MediaContent, count int64, hasNext bool, err error) { count, err = coll(nil).Count(cond) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetList", table, "Count", err), log.Any("skip", skip), log.Any("limit", limit), log.Any("cond", cond), log.Any("sort", sort), ) return } if len(sort) == 0 { sort = bson.D{{Key: "_id", Value: -1}} } opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit + 1) if err = coll(nil).Find(&res, cond, opts); err != nil { return } // 判断下一页 if len(res) > int(limit) { hasNext = true res = res[:limit] } return } func QueryContentsPrice(mediaId primitive.ObjectID) (price int64, err error) { type contentsPrice struct { Total int64 `bson:"total"` } res := []contentsPrice{} pipeline := []bson.M{ bson.M{"$match": bson.M{ "mediaId": mediaId, "isActive": true, "isDelete": false, }, }, bson.M{"$group": bson.M{"_id": "$mediaId", "total": bson.M{"$sum": "$price"}}}, } err = coll(nil).Aggregate(&res, pipeline) if err != nil { return } if len(res) > 0 { price = res[0].Total } return } // QueryAllList 分页查询文档 func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*MediaContent, err error) { if err = coll(nil).Find(&out, filter, opts...); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllList", table, "Find", err), log.Any("filter", filter), log.Any("opts", opts), ) return nil, err } return } // QueryAllCount 查询文档条目数 func QueryAllCount(filter primitive.M) (int64, error) { if count, err := coll(nil).Count(filter); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllCount", table, "Count", err), log.Any("filter", filter), ) return 0, err } else { return count, nil } } var MediaContentNotFound = errors.New("media not found") // GetInfoByHashId 通过hashId获取详细信息 func GetInfoByHashId(mediaId primitive.ObjectID, hashId string) (MediaContent, error) { v := MediaContent{} if err := coll(nil).FindOne(&v, bson.M{"mediaId": mediaId, "hashId": hashId, "isDelete": false}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfoByHashId", table, "FindOne", err), log.Any("hashId", hashId), ) return v, err } if v.ID.IsZero() { return v, MediaContentNotFound } return v, nil } // GetInfoByMediaIdAndName 通过mediaId与name获取详细信息 func GetInfoByMediaIdAndName(mediaId primitive.ObjectID, name string) (MediaContent, error) { v := MediaContent{} if err := coll(nil).FindOne(&v, bson.M{"mediaId": mediaId, "name": strings.TrimSpace(name), "isDelete": false}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfoByMediaIdAndName", table, "FindOne", err), log.Any("mediaId", mediaId), log.Any("title", name), ) return v, err } if v.ID.IsZero() { return v, MediaContentNotFound } return v, nil } // GetInfo 通过id获取详细信息 func GetInfo(id primitive.ObjectID, isActive ...bool) (MediaContent, error) { cond := bson.M{"_id": id} if len(isActive) > 0 { cond["isActive"] = isActive[0] } v := MediaContent{} if err := coll(nil).FindOne(&v, cond); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err), log.Any("id", id), ) return v, err } return v, nil } func GetInfoByCond(cond bson.M) (MediaContent, error) { v := MediaContent{} if err := coll(nil).FindOne(&v, cond); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err), log.Any("cond", cond), ) return v, err } return v, nil } // IncCountPurchases 购买次数增加 func IncCountPurchases(id primitive.ObjectID, inc int) error { return IncCountPurchasesWithTool(nil, id, inc) } func IncCountPurchasesWithTool(t *db.MongoTool, id primitive.ObjectID, inc int) error { query := bson.M{"_id": id} update := bson.M{"$set": bson.M{"updateTime": time.Now()}, "$inc": bson.M{"countPurchases": inc}} _, err := coll(t).UpdateOne(query, update) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncCountPurchases", table, "UpdateOne", err), log.Any("id", id), ) return err } return nil } // IncCollectCount 收藏次数增加 func IncCollectCount(id primitive.ObjectID, inc int) error { query := bson.M{"_id": id} update := bson.M{"$set": bson.M{"updateTime": time.Now()}, "$inc": bson.M{"countCollect": inc}} _, err := coll(nil).UpdateOne(query, update) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncLikeCount", table, "UpdateOne", err), log.Any("id", id), ) return err } return nil } // IncreaseCountLikeByID 更新媒体点赞数 func IncreaseCountLikeByID(id primitive.ObjectID, value int) (err error) { if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": bson.M{"countLike": value}}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncreaseLikeCountByID", table, "UpdateOne", err), log.Any("id", id), log.Any("value", value), ) return } return } // IncreaseCountDislikeByID 更新媒体点踩数 func IncreaseCountDislikeByID(id primitive.ObjectID, value int) (err error) { if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": bson.M{"countDisLike": value}}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncreaseLikeCountByID", table, "UpdateOne", err), log.Any("id", id), log.Any("value", value), ) return } return } // IncrCommentCountByID 更新媒体评论数 func IncrCommentCountByID(id primitive.ObjectID, value int) (err error) { if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": bson.M{"countComment": value}}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncrCommentCountByID", table, "UpdateOne", err), log.Any("id", id), log.Any("value", value), ) return } return } // IncBrowseCount 更新媒体评论数 func IncBrowseCount(id primitive.ObjectID, value int) (err error) { if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": bson.M{"countBrowse": value}}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncBrowseCount", table, "UpdateOne", err), log.Any("id", id), log.Any("value", value), ) return } return } func UpdateOneByID(id primitive.ObjectID, set bson.M) (*mongo.UpdateResult, error) { return coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": set}) } func InsetBatch(t *db.MongoTool, list []MediaContent) (err error) { if len(list) == 0 { return } if _, err := coll(t).InsertMany(list); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsetBatch", table, "InsertMany", err)) return err } return } // Insert 插入记录 func Insert(t *db.MongoTool, d MediaContent) (data primitive.ObjectID, err error) { result, err := coll(t).InsertOne(&d) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err)) return } byteID, err := json.Marshal(result.InsertedID) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Marshal", err)) return } if err = data.UnmarshalJSON(byteID); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "UnmarshalJSON", err)) return } return } // UpdateByID 根据id更新数据 func UpdateByID(t *db.MongoTool, id primitive.ObjectID, data map[string]interface{}) (int64, error) { cond := bson.M{"_id": id} return update(t, cond, data) } // UpdateByIDS 根据ids更新数据 func UpdateByIDS(t *db.MongoTool, ids []primitive.ObjectID, data map[string]interface{}) (int64, error) { cond := bson.M{"_id": bson.M{"$in": ids}} return update(t, cond, data) } // UpdateByCond 根据cond更新数据 func UpdateByCond(t *db.MongoTool, cond primitive.M, data map[string]interface{}) (int64, error) { return update(t, cond, data) } // update 更新数据 func update(t *db.MongoTool, cond primitive.M, data map[string]interface{}) (int64, error) { result, err := coll(t).UpdateMany(cond, bson.M{"$set": bson.M(data)}) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateMany", err), log.Any("cond", cond), log.Any("update", data), ) return 0, err } return result.ModifiedCount, nil } // DeleteByID 删除数据 func DeleteByID(t *db.MongoTool, id primitive.ObjectID) error { _, err := coll(t).UpdateOne(bson.M{"_id": id}, bson.M{"$set": bson.M{"isDelete": true}}) if err != nil { if err == mongo.ErrNoDocuments { return nil } else { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteMany", err), log.Any("id", id)) } } return err } // DeleteByMediaId 删除数据 func DeleteByMediaId(t *db.MongoTool, mediaId primitive.ObjectID) error { _, err := coll(t).UpdateMany(bson.M{"mediaId": mediaId}, bson.M{"$set": bson.M{"isDelete": true}}) if err != nil { if err == mongo.ErrNoDocuments { return nil } else { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByMediaId", table, "DeleteMany", err), log.Any("mediaId", mediaId)) } } return err } // UpdateForReview 内容审查通过后回写文本字段 // title 对应 MediaContent.Name,content 对应 MediaContent.Text func UpdateForReview(id primitive.ObjectID, title, content string) error { set := bson.M{} if title != "" { set["name"] = title } if content != "" { set["text"] = content } if len(set) == 0 { return nil } set["updateTime"] = time.Now() _, err := coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": set}) return err } // OffShelfManyForReview 内容审查命中后批量下架章节(isActive -> false) // 仅当前 isActive=true 的会被改动 func OffShelfManyForReview(ids []primitive.ObjectID) error { if len(ids) == 0 { return nil } _, err := coll(nil).UpdateMany( bson.M{"_id": bson.M{"$in": ids}, "isActive": true}, bson.M{"$set": bson.M{"isActive": false, "updateTime": time.Now()}}, ) return err }