2022-05-17 20:23:08 +08:00
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
"context"
|
2022-09-09 18:26:08 +08:00
|
|
|
"mayfly-go/internal/mongo/domain/entity"
|
|
|
|
|
"mayfly-go/internal/mongo/domain/repository"
|
2023-10-27 17:41:45 +08:00
|
|
|
"mayfly-go/internal/mongo/mgm"
|
2023-10-26 17:15:49 +08:00
|
|
|
"mayfly-go/pkg/base"
|
2023-10-27 17:41:45 +08:00
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/model"
|
2022-05-17 20:23:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Mongo interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
base.App[*entity.Mongo]
|
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
// 分页获取机器脚本信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
Count(condition *entity.MongoQuery) int64
|
2022-05-17 20:23:08 +08:00
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
Save(ctx context.Context, entity *entity.Mongo) error
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
|
|
|
// 删除数据库信息
|
2023-11-07 21:05:21 +08:00
|
|
|
Delete(ctx context.Context, id uint64) error
|
2022-05-17 20:23:08 +08:00
|
|
|
|
2023-08-25 19:41:52 +08:00
|
|
|
// 获取mongo连接实例
|
2022-05-17 20:23:08 +08:00
|
|
|
// @param id mongo id
|
2023-10-27 17:41:45 +08:00
|
|
|
GetMongoConn(id uint64) (*mgm.MongoConn, error)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func newMongoAppImpl(mongoRepo repository.Mongo) Mongo {
|
|
|
|
|
return &mongoAppImpl{
|
2023-10-26 17:15:49 +08:00
|
|
|
base.AppImpl[*entity.Mongo, repository.Mongo]{Repo: mongoRepo},
|
2022-09-09 18:26:08 +08:00
|
|
|
}
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
type mongoAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
base.AppImpl[*entity.Mongo, repository.Mongo]
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 分页获取数据库信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
func (d *mongoAppImpl) GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
|
|
|
|
return d.GetRepo().GetList(condition, pageParam, toEntity, orderBy...)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
func (d *mongoAppImpl) Count(condition *entity.MongoQuery) int64 {
|
2023-10-26 17:15:49 +08:00
|
|
|
return d.GetRepo().Count(condition)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
func (d *mongoAppImpl) Delete(ctx context.Context, id uint64) error {
|
2023-10-27 17:41:45 +08:00
|
|
|
mgm.CloseConn(id)
|
2023-11-07 21:05:21 +08:00
|
|
|
return d.GetRepo().DeleteById(ctx, id)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
func (d *mongoAppImpl) Save(ctx context.Context, m *entity.Mongo) error {
|
2022-05-17 20:23:08 +08:00
|
|
|
if m.Id == 0 {
|
2023-11-07 21:05:21 +08:00
|
|
|
return d.GetRepo().Insert(ctx, m)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
// 先关闭连接
|
2023-10-27 17:41:45 +08:00
|
|
|
mgm.CloseConn(m.Id)
|
2023-11-07 21:05:21 +08:00
|
|
|
return d.GetRepo().UpdateById(ctx, m)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
func (d *mongoAppImpl) GetMongoConn(id uint64) (*mgm.MongoConn, error) {
|
|
|
|
|
return mgm.GetMongoConn(id, func() (*mgm.MongoInfo, error) {
|
|
|
|
|
mongo, err := d.GetById(new(entity.Mongo), id)
|
2022-05-17 20:23:08 +08:00
|
|
|
if err != nil {
|
2023-10-27 17:41:45 +08:00
|
|
|
return nil, errorx.NewBiz("mongo信息不存在")
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
2023-10-27 17:41:45 +08:00
|
|
|
return mongo.ToMongoInfo(), nil
|
2022-05-17 20:23:08 +08:00
|
|
|
})
|
|
|
|
|
}
|