@@ -0,0 +1,168 @@
|
||||
package integralconfigmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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.IntegralConfig
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndex 索引设置
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{"createdAt", -1}},
|
||||
},
|
||||
}
|
||||
_, err := coll(nil).CreateIndex(many)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("user model set index err ==>[%+v]", err))
|
||||
}
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
func InsertOne(mt *db.MongoTool, cfg IntegralConfig) error {
|
||||
if _, err := coll(mt).InsertOne(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindOneById(id primitive.ObjectID) (IntegralConfig, error) {
|
||||
cfg := IntegralConfig{}
|
||||
err := coll(nil).FindOne(&cfg, bson.M{"_id": id})
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
func FindOneByVId(vid primitive.ObjectID) (IntegralConfig, error) {
|
||||
cfg := IntegralConfig{}
|
||||
err := coll(nil).FindOne(&cfg, bson.M{"vidId": vid})
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
// GetByVidIds 根据vid获取列表
|
||||
func GetByVidIds(vidIds []primitive.ObjectID) ([]IntegralConfig, error) {
|
||||
vidTimeOnlineList := make([]IntegralConfig, 0)
|
||||
err := coll(nil).Find(&vidTimeOnlineList, bson.M{"vidId": bson.M{"$in": vidIds}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vidTimeOnlineList, nil
|
||||
}
|
||||
|
||||
// DeleteByIds 删除
|
||||
func DeleteByIds(ids []primitive.ObjectID) error {
|
||||
_, err := coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": ids}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByVids 根据帖子id删除数据
|
||||
func DeleteByVids(vids []primitive.ObjectID) error {
|
||||
_, err := coll(nil).DeleteMany(bson.M{"vidId": bson.M{"$in": vids}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAllList 分页查询文档
|
||||
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*IntegralConfig, 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
|
||||
}
|
||||
}
|
||||
|
||||
// Edit 修改文档
|
||||
func Edit(filter, update primitive.M) error {
|
||||
result, err := coll(nil).UpdateOne(filter, update)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "UpdateOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount == 0 {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "result.ModifiedCount", "ModifiedCount is zero"),
|
||||
log.Any("filter", filter),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return errors.New("result is null")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVideoList 获取作品列表
|
||||
func GetVideoList(skip, limit int64, cond bson.M, opts ...*options.FindOptions) (back []*APPIntegralConfig, total int64, hasNext bool, err error) {
|
||||
// 获取总条数
|
||||
total, err = coll(nil).Count(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getVideoList", table, "Find", err),
|
||||
log.Any("skip", skip),
|
||||
log.Any("limit", limit),
|
||||
log.Any("cond", cond))
|
||||
return
|
||||
}
|
||||
if total == 0 {
|
||||
return
|
||||
}
|
||||
err = coll(nil).Find(&back, cond, opts...)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getVideoList", table, "Find", err),
|
||||
log.Any("skip", skip),
|
||||
log.Any("limit", limit),
|
||||
log.Any("cond", cond),
|
||||
)
|
||||
return
|
||||
}
|
||||
if len(back) > int(limit) {
|
||||
hasNext = true
|
||||
back = back[:limit]
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user