101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package contactusmod
|
|
|
|
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 初始化 contactus 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.ContactUs)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ContactUs.UserId, Value: 1},
|
|
{Key: FN.ContactUs.CreatedAt, Value: -1},
|
|
{Key: FN.ContactUs.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ContactUs.Status, Value: 1},
|
|
{Key: FN.ContactUs.CreatedAt, Value: -1},
|
|
{Key: FN.ContactUs.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ContactUs.QuestionType, Value: 1},
|
|
{Key: FN.ContactUs.CreatedAt, Value: -1},
|
|
{Key: FN.ContactUs.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ContactUs.CreatedAt, Value: -1},
|
|
{Key: FN.ContactUs.Id, Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(contactusmod:initIndex) Create Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOneContactUs 插入一条联系我们数据
|
|
//
|
|
// 当 doc.Id 为空时,会使用 mongos.Lid.ContactUs 自动生成自增ID
|
|
func InsertOneContactUs(ctx context.Context, doc *ContactUs) *srvdef.SrvErr {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.ContactUs.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
|
|
}
|
|
|
|
// FindManyContactUs 按条件查询多条联系我们数据
|
|
func FindManyContactUs(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (ContactUsList, *srvdef.SrvErr) {
|
|
var data ContactUsList
|
|
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
|
|
}
|
|
|
|
// CountContactUs 统计满足条件的联系我们数量
|
|
func CountContactUs(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
|
|
}
|
|
|
|
// UpdateManyContactUsWithResult 批量更新联系我们数据,并返回命中数量和修改数量
|
|
func UpdateManyContactUsWithResult(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
|
|
}
|