@@ -0,0 +1,80 @@
|
||||
package contentlibmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.ContentLibrary
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func Insert(doc ContentLibrary) (err error) {
|
||||
if _, err = coll(nil).InsertOne(doc); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteByIDS(ids []ObjectID) (err error) {
|
||||
if _, err = coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByIDS", table, "DeleteMany", err), log.Any("ids", ids))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetContentList(robotType *string, skip int64, limit int64) (total int64, data []ContentLibrary, err error) {
|
||||
var query = bson.M{}
|
||||
if robotType != nil {
|
||||
query["robotType"] = *robotType
|
||||
}
|
||||
var opts = options.Find()
|
||||
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(skip).SetLimit(limit)
|
||||
if err = coll(nil).Find(&data, query, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentList", table, "Find", err), log.Any("robotType", robotType))
|
||||
return
|
||||
}
|
||||
if total, err = coll(nil).Count(query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentList", table, "Count", err), log.Any("robotType", robotType))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetContentListByIDS(ids []ObjectID) (data []ContentLibrary, err error) {
|
||||
if err = coll(nil).Find(&data, bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentListByIDS", table, "Find", err), log.Any("ids", ids))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package contentlibmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// 机器人聊天的内容库
|
||||
type ContentLibrary struct {
|
||||
ID ObjectID `bson:"_id,omitempty"`
|
||||
RobotType string `bson:"robotType"` // 机器人类型
|
||||
Content string `bson:"content"` // 聊天内容
|
||||
CreatedAt time.Time `bson:"createdAt"` // 创建时间
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package contentlibmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/models/commod"
|
||||
)
|
||||
|
||||
type RespList struct {
|
||||
ID ObjectID `json:"id"`
|
||||
RobotType string `json:"robotType"` // 机器人类型
|
||||
Content string `json:"content"` // 聊天内容
|
||||
CreatedAt time.Time `json:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
// 内容库列表请求信息
|
||||
type ReqContentList struct {
|
||||
RobotType *string `form:"robotType" json:"robotType"`
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// 内容库增加内容请求参数
|
||||
type ReqContentAdd struct {
|
||||
RobotType string `json:"robotType" binding:"required"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
}
|
||||
|
||||
// 内容库删除请求参数
|
||||
type ReqContentDel struct {
|
||||
IDS []ObjectID `json:"ids" binding:"required"`
|
||||
}
|
||||
Reference in New Issue
Block a user