init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package translationmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 表名
|
||||
var db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.Shop.Coll(TN.TranslationInfo)
|
||||
// 获取数据库表
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.TranslationInfo.Status, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.TranslationInfo.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(channelmod:initTranslationInfoIndex) Create TranslationInfo Index failed! err:%+v:", err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// InsertOneTranslationInfo 插入一个渠道统计
|
||||
func InsertOneTranslationInfo(ctx context.Context, qa *TranslationInfo) *srvdef.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, qa); err != nil {
|
||||
// log.Warn("TranslationInfomod:InsertOneTranslationInfo db.InsertOne failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneTranslationInfo 删除一个渠道统计
|
||||
func DelOneTranslationInfo(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("TranslationInfomod:DelOneTranslationInfo db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneTranslationInfo 查找一个渠道统计
|
||||
func FindOneTranslationInfo(ctx context.Context, filter bson.M) (
|
||||
*TranslationInfo, *srvdef.SrvErr) {
|
||||
var TranslationInfo TranslationInfo
|
||||
if err := db.FindOneEx(ctx, &TranslationInfo, filter); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("TranslationInfomod:FindOneTranslationInfo db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return &TranslationInfo, nil
|
||||
}
|
||||
|
||||
func FindManyTranslationInfos(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (TranslationInfoList, *srvdef.SrvErr) {
|
||||
data := make(TranslationInfoList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); nil != err {
|
||||
log.Warn("faq:FindManyTranslationInfos", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQuery.Code, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountTranslationInfos 统计渠道统计数量
|
||||
func CountTranslationInfos(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("faqmod:CountTranslationInfos db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneTranslationInfo 更新一个渠道统计
|
||||
func UpdateOneTranslationInfo(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("post:UpdateOneTranslationInfo db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("post:UpdateOneTranslationInfo db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user