Executable
+43
@@ -0,0 +1,43 @@
|
||||
package nakedchatmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// NakedChatSimple 移动端返回列表内容
|
||||
type NakedChatSimple struct {
|
||||
ID primitive.ObjectID `json:"id"` // 文档id
|
||||
Title string `json:"title"` // 标题
|
||||
Cover string `json:"cover"` // 封面
|
||||
Price uint64 `json:"price"` // 价格 n金币/分钟
|
||||
Age int `json:"age"` // 年龄 单位 岁
|
||||
Weight int `json:"weight"` // 体重 单位 kg
|
||||
Height int `json:"height"` // 身高 单位 cm
|
||||
Cup string `json:"cup"` // 罩杯
|
||||
SaleNum int64 `json:"saleNum"` // 销售数量(多少人看过)
|
||||
CreatedAt time.Time `json:"createdAt"` // 文档创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt"` // 文档更新时间
|
||||
}
|
||||
|
||||
// NakedChatInfo 移动端返回内容
|
||||
type NakedChatInfo struct {
|
||||
ID primitive.ObjectID `json:"id"` // 文档id
|
||||
Title string `json:"title"` // 标题
|
||||
Cover string `json:"cover"` // 封面
|
||||
Price uint64 `json:"price"` // 价格 n金币/分钟
|
||||
Options []uint64 `json:"options"` // 可供购买选项 ,多少分钟
|
||||
Images []string `json:"images"` // 图片列表
|
||||
Video string `json:"video"` // 展示的视频
|
||||
Contact string `json:"contact "` // 联系方式
|
||||
Age int `json:"age"` // 年龄 单位 岁
|
||||
Weight int `json:"weight"` // 体重 单位 kg
|
||||
Height int `json:"height"` // 身高 单位 cm
|
||||
Cup string `json:"cup"` // 罩杯
|
||||
SaleNum int64 `json:"saleNum"` // 销售数量(多少人看过)
|
||||
BusinessHours string `json:"businessHours"` // 连线时间
|
||||
Summary string `json:"summary"` // 简介
|
||||
CreatedAt time.Time `json:"createdAt"` // 文档创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt"` // 文档更新时间
|
||||
}
|
||||
Executable
+149
@@ -0,0 +1,149 @@
|
||||
package nakedchatmod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// GetList 获取列表
|
||||
func GetList(cond bson.M, skip, limit int64, sort bson.D) (res []NakedChat, 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
|
||||
}
|
||||
|
||||
// GetAll 查询全部文档
|
||||
func GetAll(filter primitive.M, sort bson.D) (out []*NakedChat, err error) {
|
||||
if len(sort) == 0 {
|
||||
sort = bson.D{{Key: "_id", Value: -1}}
|
||||
}
|
||||
opts := options.Find().SetSort(sort)
|
||||
if err = coll(nil).Find(&out, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAll", 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
|
||||
}
|
||||
}
|
||||
|
||||
// GetInfo 通过id获取详细信息
|
||||
func GetInfo(id primitive.ObjectID) (NakedChat, error) {
|
||||
v := NakedChat{}
|
||||
if err := coll(nil).FindOne(&v, bson.M{"_id": id}); 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
|
||||
}
|
||||
|
||||
if v.ID.IsZero() {
|
||||
return v, errors.New("record not found")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(t *db.MongoTool, d NakedChat) (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
|
||||
}
|
||||
|
||||
// IncSaleNum 增加购买数量(真实)
|
||||
func IncSaleNum(t *db.MongoTool, id primitive.ObjectID, num int) error {
|
||||
filter := bson.M{"_id": id}
|
||||
_, err := coll(nil).UpdateOne(filter, bson.M{"$inc": bson.M{"saleNum": num}})
|
||||
return err
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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).DeleteMany(bson.M{"_id": id})
|
||||
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
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
package nakedchatmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.NakedChat
|
||||
|
||||
type NakedChat struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 文档id
|
||||
Title string `json:"title" bson:"title"` // 标题
|
||||
Cover string `json:"cover" bson:"cover"` // 封面
|
||||
Price uint64 `json:"price" bson:"price"` // 价格 n金币/分钟
|
||||
Options []uint64 `json:"options" bson:"options"` // 可供购买选项 ,多少分钟
|
||||
Images []string `json:"images" bson:"images"` // 图片列表
|
||||
Video string `json:"video" bson:"video"` // 展示的视频
|
||||
Contact string `json:"contact" bson:"contact"` // 联系方式
|
||||
Age int `json:"age" bson:"age"` // 年龄 单位 岁
|
||||
Weight int `json:"weight" bson:"weight"` // 体重 单位 kg
|
||||
Height int `json:"height" bson:"height"` // 身高 单位 cm
|
||||
Cup string `json:"cup" bson:"cup"` // 罩杯
|
||||
SaleNum int64 `json:"saleNum" bson:"saleNum"` // 销售数量
|
||||
FakeSaleNum int64 `json:"fakeSaleNum" bson:"fakeSaleNum"` // 销售数量(假)
|
||||
BusinessHours string `json:"businessHours" bson:"businessHours"` // 连线时间
|
||||
Summary string `json:"summary" bson:"summary"` // 简介
|
||||
Status int `json:"status" bson:"status"` // 0-下架 1-上架
|
||||
Mid primitive.ObjectID `json:"mid" bson:"mid"` // 模块id
|
||||
SortCode int64 `json:"sortCode" bson:"sortCode"` // 排序号
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 文档创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 文档更新时间
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// Init 初始化索引 todo: 在models/Init/init.go中调用初始化
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "_id", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "title", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "mid", Value: 1}, {Key: "sortCode", Value: -1}, {Key: "createdAt", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user