mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	refactor: 代码重构、分页数据组件支持多选
This commit is contained in:
		@@ -11,6 +11,7 @@ import (
 | 
			
		||||
	tagapp "mayfly-go/internal/tag/application"
 | 
			
		||||
	"mayfly-go/pkg/biz"
 | 
			
		||||
	"mayfly-go/pkg/ginx"
 | 
			
		||||
	"mayfly-go/pkg/gormx"
 | 
			
		||||
	"mayfly-go/pkg/model"
 | 
			
		||||
	"mayfly-go/pkg/req"
 | 
			
		||||
	"mayfly-go/pkg/utils"
 | 
			
		||||
@@ -40,7 +41,7 @@ func (d *Db) Dbs(rc *req.Ctx) {
 | 
			
		||||
	// 不存在可访问标签id,即没有可操作数据
 | 
			
		||||
	tagIds := d.TagApp.ListTagIdByAccountId(rc.LoginAccount.Id)
 | 
			
		||||
	if len(tagIds) == 0 {
 | 
			
		||||
		rc.ResData = model.EmptyPageResult()
 | 
			
		||||
		rc.ResData = model.EmptyPageResult[any]()
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	condition.TagIds = tagIds
 | 
			
		||||
@@ -96,10 +97,18 @@ func (d *Db) GetDatabaseNames(rc *req.Ctx) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *Db) DeleteDb(rc *req.Ctx) {
 | 
			
		||||
	dbId := GetDbId(rc.GinCtx)
 | 
			
		||||
	d.DbApp.Delete(dbId)
 | 
			
		||||
	// 删除该库的sql执行记录
 | 
			
		||||
	d.DbSqlExecApp.DeleteBy(&entity.DbSqlExec{DbId: dbId})
 | 
			
		||||
	idsStr := ginx.PathParam(rc.GinCtx, "dbId")
 | 
			
		||||
	rc.ReqParam = idsStr
 | 
			
		||||
	ids := strings.Split(idsStr, ",")
 | 
			
		||||
 | 
			
		||||
	for _, v := range ids {
 | 
			
		||||
		value, err := strconv.Atoi(v)
 | 
			
		||||
		biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
 | 
			
		||||
		dbId := uint64(value)
 | 
			
		||||
		d.DbApp.Delete(dbId)
 | 
			
		||||
		// 删除该库的sql执行记录
 | 
			
		||||
		d.DbSqlExecApp.DeleteBy(&entity.DbSqlExec{DbId: dbId})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *Db) TableInfos(rc *req.Ctx) {
 | 
			
		||||
@@ -381,21 +390,21 @@ func (d *Db) SaveSql(rc *req.Ctx) {
 | 
			
		||||
 | 
			
		||||
	dbId := GetDbId(g)
 | 
			
		||||
	// 判断dbId是否存在
 | 
			
		||||
	err := model.GetById(new(entity.Db), dbId)
 | 
			
		||||
	err := gormx.GetById(new(entity.Db), dbId)
 | 
			
		||||
	biz.ErrIsNil(err, "该数据库信息不存在")
 | 
			
		||||
 | 
			
		||||
	// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
 | 
			
		||||
	dbSql := &entity.DbSql{Type: dbSqlForm.Type, DbId: dbId, Name: dbSqlForm.Name, Db: dbSqlForm.Db}
 | 
			
		||||
	dbSql.CreatorId = account.Id
 | 
			
		||||
	e := model.GetBy(dbSql)
 | 
			
		||||
	e := gormx.GetBy(dbSql)
 | 
			
		||||
 | 
			
		||||
	dbSql.SetBaseInfo(account)
 | 
			
		||||
	// 更新sql信息
 | 
			
		||||
	dbSql.Sql = dbSqlForm.Sql
 | 
			
		||||
	if e == nil {
 | 
			
		||||
		model.UpdateById(dbSql)
 | 
			
		||||
		gormx.UpdateById(dbSql)
 | 
			
		||||
	} else {
 | 
			
		||||
		model.Insert(dbSql)
 | 
			
		||||
		gormx.Insert(dbSql)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -406,7 +415,7 @@ func (d *Db) GetSqlNames(rc *req.Ctx) {
 | 
			
		||||
	dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
 | 
			
		||||
	dbSql.CreatorId = rc.LoginAccount.Id
 | 
			
		||||
	var sqls []entity.DbSql
 | 
			
		||||
	model.ListBy(dbSql, &sqls, "id", "name")
 | 
			
		||||
	gormx.ListBy(dbSql, &sqls, "id", "name")
 | 
			
		||||
 | 
			
		||||
	rc.ResData = sqls
 | 
			
		||||
}
 | 
			
		||||
@@ -418,7 +427,7 @@ func (d *Db) DeleteSql(rc *req.Ctx) {
 | 
			
		||||
	dbSql.Name = rc.GinCtx.Query("name")
 | 
			
		||||
	dbSql.Db = rc.GinCtx.Query("db")
 | 
			
		||||
 | 
			
		||||
	model.DeleteByCondition(dbSql)
 | 
			
		||||
	gormx.DeleteByCondition(dbSql)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -430,7 +439,7 @@ func (d *Db) GetSql(rc *req.Ctx) {
 | 
			
		||||
	dbSql.CreatorId = rc.LoginAccount.Id
 | 
			
		||||
	dbSql.Name = rc.GinCtx.Query("name")
 | 
			
		||||
 | 
			
		||||
	e := model.GetBy(dbSql)
 | 
			
		||||
	e := gormx.GetBy(dbSql)
 | 
			
		||||
	if e != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,7 @@ import (
 | 
			
		||||
 | 
			
		||||
type Db interface {
 | 
			
		||||
	// 分页获取
 | 
			
		||||
	GetPageList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult
 | 
			
		||||
	GetPageList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
 | 
			
		||||
	Count(condition *entity.DbQuery) int64
 | 
			
		||||
 | 
			
		||||
@@ -58,7 +58,7 @@ type dbAppImpl struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 分页获取数据库信息列表
 | 
			
		||||
func (d *dbAppImpl) GetPageList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult {
 | 
			
		||||
func (d *dbAppImpl) GetPageList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
 | 
			
		||||
	return d.dbRepo.GetDbList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -52,7 +52,7 @@ type DbSqlExec interface {
 | 
			
		||||
	DeleteBy(condition *entity.DbSqlExec)
 | 
			
		||||
 | 
			
		||||
	// 分页获取
 | 
			
		||||
	GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult
 | 
			
		||||
	GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newDbSqlExecApp(dbExecSqlRepo repository.DbSqlExec) DbSqlExec {
 | 
			
		||||
@@ -151,7 +151,7 @@ func (d *dbSqlExecAppImpl) DeleteBy(condition *entity.DbSqlExec) {
 | 
			
		||||
	d.dbSqlExecRepo.DeleteBy(condition)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult {
 | 
			
		||||
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
 | 
			
		||||
	return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ import (
 | 
			
		||||
 | 
			
		||||
type Db interface {
 | 
			
		||||
	// 分页获取机器信息列表
 | 
			
		||||
	GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult
 | 
			
		||||
	GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
 | 
			
		||||
	Count(condition *entity.DbQuery) int64
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -11,5 +11,5 @@ type DbSqlExec interface {
 | 
			
		||||
	DeleteBy(condition *entity.DbSqlExec)
 | 
			
		||||
 | 
			
		||||
	// 分页获取
 | 
			
		||||
	GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult
 | 
			
		||||
	GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ import (
 | 
			
		||||
	"mayfly-go/internal/db/domain/entity"
 | 
			
		||||
	"mayfly-go/internal/db/domain/repository"
 | 
			
		||||
	"mayfly-go/pkg/biz"
 | 
			
		||||
	"mayfly-go/pkg/model"
 | 
			
		||||
	"mayfly-go/pkg/gormx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type dbSqlRepoImpl struct{}
 | 
			
		||||
@@ -15,5 +15,5 @@ func newDbSqlRepo() repository.DbSql {
 | 
			
		||||
 | 
			
		||||
// 分页获取数据库信息列表
 | 
			
		||||
func (d *dbSqlRepoImpl) DeleteBy(condition *entity.DbSql) {
 | 
			
		||||
	biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql失败")
 | 
			
		||||
	biz.ErrIsNil(gormx.DeleteByCondition(condition), "删除sql失败")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"mayfly-go/internal/db/domain/entity"
 | 
			
		||||
	"mayfly-go/internal/db/domain/repository"
 | 
			
		||||
	"mayfly-go/pkg/biz"
 | 
			
		||||
	"mayfly-go/pkg/gormx"
 | 
			
		||||
	"mayfly-go/pkg/model"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -14,14 +15,15 @@ func newDbSqlExecRepo() repository.DbSqlExec {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *dbSqlExecRepoImpl) Insert(dse *entity.DbSqlExec) {
 | 
			
		||||
	model.Insert(dse)
 | 
			
		||||
	gormx.Insert(dse)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *dbSqlExecRepoImpl) DeleteBy(condition *entity.DbSqlExec) {
 | 
			
		||||
	biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql执行记录失败")
 | 
			
		||||
	biz.ErrIsNil(gormx.DeleteByCondition(condition), "删除sql执行记录失败")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 分页获取
 | 
			
		||||
func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult {
 | 
			
		||||
	return model.GetPage(pageParam, condition, condition, toEntity, orderBy...)
 | 
			
		||||
func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
 | 
			
		||||
	qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
 | 
			
		||||
	return gormx.PageQuery(qd, pageParam, toEntity)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user