mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
refactor: 代码重构、分页数据组件支持多选
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type dbRepoImpl struct{}
|
||||
@@ -17,27 +15,14 @@ func newDbRepo() repository.Db {
|
||||
}
|
||||
|
||||
// 分页获取数据库信息列表
|
||||
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult {
|
||||
sql := "SELECT d.* FROM t_db d WHERE 1 = 1 "
|
||||
|
||||
values := make([]any, 0)
|
||||
if condition.Host != "" {
|
||||
sql = sql + " AND d.host LIKE ?"
|
||||
values = append(values, "%"+condition.Host+"%")
|
||||
}
|
||||
if condition.Database != "" {
|
||||
sql = sql + " AND d.database LIKE ?"
|
||||
values = append(values, "%"+condition.Database+"%")
|
||||
}
|
||||
if len(condition.TagIds) > 0 {
|
||||
sql = sql + " AND d.tag_id IN " + fmt.Sprintf("(%s)", strings.Join(utils.NumberArr2StrArr(condition.TagIds), ","))
|
||||
}
|
||||
if condition.TagPathLike != "" {
|
||||
sql = sql + " AND d.tag_path LIKE ?"
|
||||
values = append(values, "%"+condition.TagPathLike+"%")
|
||||
}
|
||||
sql = sql + " ORDER BY d.tag_path"
|
||||
return model.GetPageBySql(sql, pageParam, toEntity, values...)
|
||||
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
qd := gormx.NewQuery(new(entity.Db)).
|
||||
Like("host", condition.Host).
|
||||
Like("database", condition.Database).
|
||||
In("tag_id", condition.TagIds).
|
||||
RLike("tag_path", condition.TagPathLike).
|
||||
OrderByAsc("tag_path")
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
|
||||
@@ -48,18 +33,18 @@ func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
|
||||
if condition.TagId != 0 {
|
||||
where["tag_id"] = condition.TagId
|
||||
}
|
||||
return model.CountByMap(new(entity.Db), where)
|
||||
return gormx.CountByCond(new(entity.Db), where)
|
||||
}
|
||||
|
||||
// 根据条件获取账号信息
|
||||
func (d *dbRepoImpl) GetDb(condition *entity.Db, cols ...string) error {
|
||||
return model.GetBy(condition, cols...)
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
// 根据id获取
|
||||
func (d *dbRepoImpl) GetById(id uint64, cols ...string) *entity.Db {
|
||||
db := new(entity.Db)
|
||||
if err := model.GetById(db, id, cols...); err != nil {
|
||||
if err := gormx.GetById(db, id, cols...); err != nil {
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -67,13 +52,13 @@ func (d *dbRepoImpl) GetById(id uint64, cols ...string) *entity.Db {
|
||||
}
|
||||
|
||||
func (d *dbRepoImpl) Insert(db *entity.Db) {
|
||||
biz.ErrIsNil(model.Insert(db), "新增数据库信息失败")
|
||||
biz.ErrIsNil(gormx.Insert(db), "新增数据库信息失败")
|
||||
}
|
||||
|
||||
func (d *dbRepoImpl) Update(db *entity.Db) {
|
||||
biz.ErrIsNil(model.UpdateById(db), "更新数据库信息失败")
|
||||
biz.ErrIsNil(gormx.UpdateById(db), "更新数据库信息失败")
|
||||
}
|
||||
|
||||
func (d *dbRepoImpl) Delete(id uint64) {
|
||||
model.DeleteById(new(entity.Db), id)
|
||||
gormx.DeleteById(new(entity.Db), id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user