service/vendor/codecreate/template/mongo.txt

68 lines
1.8 KiB
Plaintext

#{moduleName}proto "service/api/proto/#{moduleName}/proto"
DB#{EntityName} = "#{moduleName}"
COL#{EntityName} = "#{moduleName}"
// #{EntityCNName}表
func (m *Mongo) getCol#{EntityName}() *qmgo.Collection {
return m.clientMix.Database(DB#{EntityName}).Collection(COL#{EntityName})
}
// #{EntityCNName}相关
func (m *Mongo) Create#{EntityName}(ctx *gin.Context, #{moduleName} *dbstruct.#{EntityName}) error {
col := m.getCol#{EntityName}()
_, err := col.InsertOne(ctx, #{moduleName})
return err
}
func (m *Mongo) Update#{EntityName}(ctx *gin.Context, #{moduleName} *dbstruct.#{EntityName}) error {
col := m.getCol#{EntityName}()
set := util.EntityToM(#{moduleName})
set["ut"] = time.Now().Unix()
up := qmgo.M{
"$set": set,
}
err := col.UpdateId(ctx, #{moduleName}.Id, up)
return err
}
func (m *Mongo) Delete#{EntityName}(ctx *gin.Context, id int64) error {
col := m.getCol#{EntityName}()
update := qmgo.M{
"$set": qmgo.M{
"del_flag": 1,
},
}
err := col.UpdateId(ctx, id, update)
return err
}
func (m *Mongo) Get#{EntityName}List(ctx *gin.Context, req *#{moduleName}proto.OpListReq) ([]*dbstruct.#{EntityName}, error) {
list := make([]*dbstruct.#{EntityName}, 0)
col := m.getCol#{EntityName}()
query := qmgo.M{
"del_flag": 0,
}
err := col.Find(ctx, query).Sort("-ct").Skip(int64(req.Offset)).Limit(int64(req.Limit)).All(&list)
if err == qmgo.ErrNoSuchDocuments {
err = nil
return list, err
}
return list, err
}
func (m *Mongo) Get#{EntityName}ListByIds(ctx *gin.Context, ids []int64) ([]*dbstruct.#{EntityName}, error) {
list := make([]*dbstruct.#{EntityName}, 0)
col := m.getCol#{EntityName}()
query := qmgo.M{
"_id": bson.M{
"$in": ids,
},
"del_flag": 0,
}
err := col.Find(ctx, query).All(&list)
if err == qmgo.ErrNoSuchDocuments {
err = nil
return list, err
}
return list, err
}