@@ -0,0 +1,152 @@
|
||||
package immod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"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.Msg
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// ActInitIndex 索引设置
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "msgID", Value: -1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "toUserId", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "content", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "operate", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 保存消息
|
||||
func InsertOne(msg Msg) (err error) {
|
||||
if _, err = coll(nil).InsertOne(msg); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// MsgList 消息列表
|
||||
func MsgList(uid uint64, stdQuery commod.StdQuery) (msg []MsgAppRes, err error) {
|
||||
query := bson.M{"$or": bson.A{bson.M{"operate": bson.M{"$in": []Action{System}}}, bson.M{"toUserId": uid}}}
|
||||
opts := commod.ConvertToListQuery(stdQuery).SetSort(bson.D{{Key: "updatedAt", Value: -1}})
|
||||
if err = coll(nil).Find(&msg, query, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "MsgList", table, "Find", err), log.Any("uid", uid))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据uid获取个人消息
|
||||
func FindMsgAndCountByUID(uid uint64, stdQuery commod.StdQuery) (data []Msg, total int64, err error) {
|
||||
*stdQuery.Order = append(*stdQuery.Order, commod.OrderBy{Key: "createdAt", Desc: true})
|
||||
query := bson.M{"toUserId": uid, "operate": Private}
|
||||
if err = coll(nil).Find(&data, query, commod.ConvertToListQuery(stdQuery)); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMsgAndCountByUID", table, "Find", err), log.Any("uid", uid))
|
||||
return
|
||||
}
|
||||
total, err = coll(nil).Count(query)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMsgAndCountByUID", table, "Count", err), log.Any("uid", uid))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据消息行为获取消息
|
||||
func FindMsgAndCountByAction(action Action, stdQuery commod.StdQuery) (data []Msg, total int64, err error) {
|
||||
*stdQuery.Order = append(*stdQuery.Order, commod.OrderBy{Key: "createdAt", Desc: true})
|
||||
query := bson.M{"operate": action}
|
||||
if err = coll(nil).Find(&data, query, commod.ConvertToListQuery(stdQuery)); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMsgAndCountByAction", table, "Find", err), log.Any("action", action))
|
||||
return
|
||||
}
|
||||
total, err = coll(nil).Count(query)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMsgAndCountByAction", table, "Count", err), log.Any("action", action))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// MsgWebList 消息列表
|
||||
func MsgWebList(uid, msgID, operater *uint64, title *string, start, end *time.Time, stdQuery commod.StdQuery) (res WebMsgRes, err error) {
|
||||
query := bson.M{}
|
||||
if uid != nil {
|
||||
query["uid"] = uid
|
||||
}
|
||||
if msgID != nil {
|
||||
query["msgID"] = msgID
|
||||
}
|
||||
if operater != nil {
|
||||
query["operater"] = operater
|
||||
}
|
||||
if title != nil {
|
||||
query["title"] = title
|
||||
}
|
||||
if start != nil {
|
||||
query["created"] = bson.M{"$gte": start}
|
||||
}
|
||||
if end != nil {
|
||||
query["created"] = bson.M{"$lt": end}
|
||||
}
|
||||
opts := commod.ConvertToListQuery(stdQuery).SetSort(bson.D{{Key: "updatedAt", Value: -1}})
|
||||
total, err := coll(nil).Count(query)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "MsgWebList", table, "Count", err))
|
||||
return
|
||||
}
|
||||
m := []Msg{}
|
||||
if err = coll(nil).Find(&m, query, opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "MsgWebList", table, "find", err))
|
||||
return
|
||||
}
|
||||
res.List = m
|
||||
res.Total = total
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteOneByMsgID(msgID uint64) (err error) {
|
||||
if _, err = coll(nil).DeleteOne(bson.M{"msgID": msgID}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOneByMsgID", table, "DeleteOne", err), log.Any("msgID", msgID))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user