mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-15 12:16:34 +08:00
refactor: 新增base.Repo与base.App,重构repo与app层代码
This commit is contained in:
@@ -3,40 +3,22 @@ package persistence
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type accountRepoImpl struct{}
|
||||
type accountRepoImpl struct {
|
||||
base.RepoImpl[*entity.Account]
|
||||
}
|
||||
|
||||
func newAccountRepo() repository.Account {
|
||||
return new(accountRepoImpl)
|
||||
return &accountRepoImpl{base.RepoImpl[*entity.Account]{M: new(entity.Account)}}
|
||||
}
|
||||
|
||||
func (a *accountRepoImpl) GetAccount(condition *entity.Account, cols ...string) error {
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
func (a *accountRepoImpl) GetById(id uint64) *entity.Account {
|
||||
ac := new(entity.Account)
|
||||
if err := gormx.GetById(ac, id); err != nil {
|
||||
return nil
|
||||
}
|
||||
return ac
|
||||
}
|
||||
|
||||
func (m *accountRepoImpl) GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
func (m *accountRepoImpl) GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(new(entity.Account)).
|
||||
Like("name", condition.Name).
|
||||
Like("username", condition.Username)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *accountRepoImpl) Insert(account *entity.Account) {
|
||||
biz.ErrIsNil(gormx.Insert(account), "新增账号信息失败")
|
||||
}
|
||||
|
||||
func (m *accountRepoImpl) Update(account *entity.Account) {
|
||||
biz.ErrIsNil(gormx.UpdateById(account), "更新账号信息失败")
|
||||
}
|
||||
|
||||
@@ -3,37 +3,23 @@ package persistence
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type configRepoImpl struct{}
|
||||
|
||||
func newConfigRepo() repository.Config {
|
||||
return new(configRepoImpl)
|
||||
type configRepoImpl struct {
|
||||
base.RepoImpl[*entity.Config]
|
||||
}
|
||||
|
||||
func (m *configRepoImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
func newConfigRepo() repository.Config {
|
||||
return &configRepoImpl{base.RepoImpl[*entity.Config]{M: new(entity.Config)}}
|
||||
}
|
||||
|
||||
func (m *configRepoImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(condition).
|
||||
Eq("key", condition.Key).
|
||||
And("permission = 'all' OR permission LIKE ?", "%"+condition.Permission+",%").
|
||||
WithOrderBy(orderBy...)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *configRepoImpl) Insert(config *entity.Config) {
|
||||
biz.ErrIsNil(gormx.Insert(config), "新增系统配置失败")
|
||||
}
|
||||
|
||||
func (m *configRepoImpl) Update(config *entity.Config) {
|
||||
biz.ErrIsNil(gormx.UpdateById(config), "更新系统配置失败")
|
||||
}
|
||||
|
||||
func (m *configRepoImpl) GetConfig(condition *entity.Config, cols ...string) error {
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
func (r *configRepoImpl) GetByCondition(condition *entity.Config, cols ...string) error {
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
@@ -3,35 +3,18 @@ package persistence
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
)
|
||||
|
||||
type resourceRepoImpl struct{}
|
||||
type resourceRepoImpl struct {
|
||||
base.RepoImpl[*entity.Resource]
|
||||
}
|
||||
|
||||
func newResourceRepo() repository.Resource {
|
||||
return new(resourceRepoImpl)
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string) {
|
||||
gormx.ListByOrder(condition, toEntity, orderBy...)
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) GetById(id uint64, cols ...string) *entity.Resource {
|
||||
res := new(entity.Resource)
|
||||
if err := gormx.GetById(res, id, cols...); err != nil {
|
||||
return nil
|
||||
|
||||
return &resourceRepoImpl{
|
||||
base.RepoImpl[*entity.Resource]{M: new(entity.Resource)},
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) Delete(id uint64) {
|
||||
biz.ErrIsNil(gormx.DeleteById(new(entity.Resource), id), "删除失败")
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) GetByCondition(condition *entity.Resource, cols ...string) error {
|
||||
return gormx.GetBy(condition, cols...)
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) GetChildren(uiPath string) []entity.Resource {
|
||||
@@ -41,12 +24,12 @@ func (r *resourceRepoImpl) GetChildren(uiPath string) []entity.Resource {
|
||||
return rs
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) UpdateByUiPathLike(resource *entity.Resource) {
|
||||
func (r *resourceRepoImpl) UpdateByUiPathLike(resource *entity.Resource) error {
|
||||
sql := "UPDATE t_sys_resource SET status=? WHERE (ui_path LIKE ?)"
|
||||
gormx.ExecSql(sql, resource.Status, resource.UiPath+"%")
|
||||
return gormx.ExecSql(sql, resource.Status, resource.UiPath+"%")
|
||||
}
|
||||
|
||||
func (r *resourceRepoImpl) GetAccountResources(accountId uint64, toEntity any) {
|
||||
func (r *resourceRepoImpl) GetAccountResources(accountId uint64, toEntity any) error {
|
||||
sql := `SELECT
|
||||
m.id,
|
||||
m.pid,
|
||||
@@ -80,5 +63,5 @@ func (r *resourceRepoImpl) GetAccountResources(accountId uint64, toEntity any) {
|
||||
ORDER BY
|
||||
m.pid ASC,
|
||||
m.weight ASC`
|
||||
biz.ErrIsNilAppendErr(gormx.GetListBySql2Model(sql, toEntity, accountId), "查询账号资源失败: %s")
|
||||
return gormx.GetListBySql2Model(sql, toEntity, accountId)
|
||||
}
|
||||
|
||||
@@ -3,26 +3,24 @@ package persistence
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type roleRepoImpl struct{}
|
||||
type roleRepoImpl struct {
|
||||
base.RepoImpl[*entity.Role]
|
||||
}
|
||||
|
||||
func newRoleRepo() repository.Role {
|
||||
return new(roleRepoImpl)
|
||||
return &roleRepoImpl{base.RepoImpl[*entity.Role]{M: new(entity.Role)}}
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
func (m *roleRepoImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) Delete(id uint64) {
|
||||
biz.ErrIsNil(gormx.DeleteById(new(entity.Role), id), "删除角色失败")
|
||||
}
|
||||
|
||||
// 获取角色拥有的资源id数组,从role_resource表获取
|
||||
func (m *roleRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
||||
var rrs []entity.RoleResource
|
||||
@@ -51,7 +49,7 @@ func (m *roleRepoImpl) SaveRoleResource(rr []*entity.RoleResource) {
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) DeleteRoleResource(roleId uint64, resourceId uint64) {
|
||||
gormx.DeleteByCondition(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
|
||||
gormx.DeleteBy(&entity.RoleResource{RoleId: roleId, ResourceId: resourceId})
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) GetAccountRoleIds(accountId uint64) []uint64 {
|
||||
@@ -72,7 +70,7 @@ func (m *roleRepoImpl) SaveAccountRole(ar *entity.AccountRole) {
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) DeleteAccountRole(accountId, roleId uint64) {
|
||||
gormx.DeleteByCondition(&entity.AccountRole{RoleId: roleId, AccountId: accountId})
|
||||
gormx.DeleteBy(&entity.AccountRole{RoleId: roleId, AccountId: accountId})
|
||||
}
|
||||
|
||||
// 获取账号角色信息列表
|
||||
|
||||
@@ -3,22 +3,21 @@ package persistence
|
||||
import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type syslogRepoImpl struct{}
|
||||
|
||||
func newSyslogRepo() repository.Syslog {
|
||||
return new(syslogRepoImpl)
|
||||
type syslogRepoImpl struct {
|
||||
base.RepoImpl[*entity.SysLog]
|
||||
}
|
||||
|
||||
func (m *syslogRepoImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
|
||||
func newSyslogRepo() repository.Syslog {
|
||||
return &syslogRepoImpl{base.RepoImpl[*entity.SysLog]{M: new(entity.SysLog)}}
|
||||
}
|
||||
|
||||
func (m *syslogRepoImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := gormx.NewQuery(new(entity.SysLog)).Like("description", condition.Description).
|
||||
Eq("creator_id", condition.CreatorId).Eq("type", condition.Type).WithOrderBy(orderBy...)
|
||||
return gormx.PageQuery(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *syslogRepoImpl) Insert(syslog *entity.SysLog) {
|
||||
gormx.Insert(syslog)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user