Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package feedbackmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/mongos"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var coll *mongos.Collection
|
|
|
|
// Init 初始化 feedback 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.Feedback)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.Feedback.UserId, Value: 1},
|
|
{Key: FN.Feedback.CreatedAt, Value: -1},
|
|
{Key: FN.Feedback.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.Feedback.Status, Value: 1},
|
|
{Key: FN.Feedback.CreatedAt, Value: -1},
|
|
{Key: FN.Feedback.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.Feedback.CreatedAt, Value: -1},
|
|
{Key: FN.Feedback.Id, Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(feedbackmod:initIndex) Create Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOneFeedback 插入一条用户意见反馈数据
|
|
//
|
|
// 当 doc.Id 为空时,会使用 mongos.Lid.Feedback 自动生成自增ID
|
|
func InsertOneFeedback(ctx context.Context, doc *Feedback) *srvdef.SrvErr {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.Feedback.Get()
|
|
}
|
|
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
|
log.Warn("用户意见反馈写入失败", log.Any("doc", doc), log.E(err))
|
|
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindManyFeedback 按条件查询多条用户意见反馈数据
|
|
func FindManyFeedback(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (FeedbackList, *srvdef.SrvErr) {
|
|
var data FeedbackList
|
|
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("用户意见反馈列表查询失败", log.E(err), log.Any("filter", filter))
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// CountFeedback 统计满足条件的用户意见反馈数量
|
|
func CountFeedback(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.Count(ctx, filter, opts...)
|
|
if err != nil {
|
|
log.Warn("用户意见反馈数量统计失败", log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// UpdateManyFeedbackWithResult 批量更新用户意见反馈数据,并返回命中数量和修改数量
|
|
func UpdateManyFeedbackWithResult(ctx context.Context, filter bson.M, update bson.M) (matchedCount int64, modifiedCount int64, serr *srvdef.SrvErr) {
|
|
result, err := coll.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("用户意见反馈批量更新失败", log.E(err), log.Any("filter", filter))
|
|
return 0, 0, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return result.MatchedCount, result.ModifiedCount, nil
|
|
}
|