99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package chatrobotmod
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"91porn-server/common"
|
|
"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.ChatRobot
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initCommentIndex 设置index
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "isActive", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func Insert(doc ChatRobot) 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 err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ConfExists(rType EnumRobotType) (isExists bool, err error) {
|
|
if isExists, err = coll(nil).Exists(bson.M{"type": rType}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ConfExists", table, "Exists", err), log.Any("rType", rType))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetRobotConfByType(rType EnumRobotType) (data ChatRobot, err error) {
|
|
if err = coll(nil).FindOne(&data, bson.M{"type": rType}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetRobotConfByType", table, "FindOne", err), log.Any("rType", rType))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetRobotConf(rType *EnumRobotType, skip int64, limit int64) (total int64, data []ChatRobot, err error) {
|
|
var query = bson.M{}
|
|
if rType != nil {
|
|
query["type"] = *rType
|
|
}
|
|
var opts = options.Find()
|
|
opts.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:", "GetRobotConf", table, "Find", err), log.Any("rType", rType))
|
|
return
|
|
}
|
|
if total, err = coll(nil).Count(query); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetRobotConf", table, "Count", err), log.Any("rType", rType))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func UpdateCmtRobotConf(rType EnumRobotType, doc CmtUpdateDoc) (err error) {
|
|
var update, _ = common.ToBsonM(doc)
|
|
if _, err = coll(nil).UpdateOne(bson.M{"type": rType}, bson.M{"$set": update}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateCmtRobotConf", table, "UpdateOne", err),
|
|
log.Any("rType", rType),
|
|
log.Any("update", update),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|