refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -39,7 +39,7 @@ func (a *Account) GetPermissions(rc *req.Ctx) {
var resources vo.AccountResourceVOList
// 获取账号菜单资源
a.ResourceApp.GetAccountResources(account.Id, &resources)
biz.ErrIsNil(a.ResourceApp.GetAccountResources(account.Id, &resources))
// 菜单树与权限code数组
var menus vo.AccountResourceVOList
var permissions []string
@@ -66,7 +66,7 @@ func (a *Account) ChangePassword(rc *req.Ctx) {
biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
account := &entity.Account{Username: form.Username}
err = a.AccountApp.GetAccount(account, "Id", "Username", "Password", "Status")
err = a.AccountApp.GetBy(account, "Id", "Username", "Password", "Status")
biz.ErrIsNil(err, "旧密码错误")
biz.IsTrue(cryptox.CheckPwdHash(originOldPwd, account.Password), "旧密码错误")
biz.IsTrue(account.IsEnable(), "该账号不可用")
@@ -78,7 +78,7 @@ func (a *Account) ChangePassword(rc *req.Ctx) {
updateAccount := new(entity.Account)
updateAccount.Id = account.Id
updateAccount.Password = cryptox.PwdHash(originNewPwd)
a.AccountApp.Update(updateAccount)
biz.ErrIsNil(a.AccountApp.Update(updateAccount), "更新账号密码失败")
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
if rc.LoginAccount == nil {
@@ -111,13 +111,14 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
updateAccount.Password = cryptox.PwdHash(updateAccount.Password)
}
oldAcc := a.AccountApp.GetById(updateAccount.Id)
oldAcc, err := a.AccountApp.GetById(new(entity.Account), updateAccount.Id)
biz.ErrIsNil(err, "账号信息不存在")
// 账号创建十分钟内允许修改用户名兼容oauth2首次登录修改用户名否则不允许修改
if oldAcc.CreateTime.Add(10 * time.Minute).Before(time.Now()) {
// 禁止更新用户名,防止误传被更新
updateAccount.Username = ""
}
a.AccountApp.Update(updateAccount)
biz.ErrIsNil(a.AccountApp.Update(updateAccount))
}
/** 后台账号操作 **/
@@ -126,7 +127,9 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
func (a *Account) Accounts(rc *req.Ctx) {
condition := &entity.Account{}
condition.Username = rc.GinCtx.Query("username")
rc.ResData = a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
res, err := a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
biz.ErrIsNil(err)
rc.ResData = res
}
// @router /accounts
@@ -139,7 +142,7 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
account.SetBaseInfo(rc.LoginAccount)
if account.Id == 0 {
a.AccountApp.Create(account)
biz.ErrIsNil(a.AccountApp.Create(account))
} else {
if account.Password != "" {
biz.IsTrue(utils.CheckAccountPasswordLever(account.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
@@ -147,7 +150,7 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
}
// 更新操作不允许修改用户名、防止误传更新
account.Username = ""
a.AccountApp.Update(account)
biz.ErrIsNil(a.AccountApp.Update(account))
}
}
@@ -158,7 +161,7 @@ func (a *Account) ChangeStatus(rc *req.Ctx) {
account.Id = uint64(ginx.PathParamInt(g, "id"))
account.Status = int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = collx.Kvs("accountId", account.Id, "status", account.Status)
a.AccountApp.Update(account)
biz.ErrIsNil(a.AccountApp.Update(account))
}
func (a *Account) DeleteAccount(rc *req.Ctx) {
@@ -169,7 +172,7 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
a.AccountApp.Delete(uint64(value))
biz.ErrIsNilAppendErr(a.AccountApp.Delete(uint64(value)), "删除失败:%s")
}
}
@@ -188,7 +191,7 @@ func (a *Account) AccountRoles(rc *req.Ctx) {
func (a *Account) AccountResources(rc *req.Ctx) {
var resources vo.ResourceManageVOList
// 获取账号菜单资源
a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources)
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources))
rc.ResData = resources.ToTrees(0)
}
@@ -213,5 +216,5 @@ func (a *Account) ResetOtpSecret(rc *req.Ctx) {
accountId := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
account.Id = accountId
rc.ReqParam = collx.Kvs("accountId", accountId)
a.AccountApp.Update(account)
biz.ErrIsNil(a.AccountApp.Update(account))
}

View File

@@ -1,12 +1,14 @@
package api
import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
func GenerateCaptcha(rc *req.Ctx) {
id, image := captcha.Generate()
id, image, err := captcha.Generate()
biz.ErrIsNilAppendErr(err, "获取验证码错误: %s")
rc.ResData = collx.M{"base64Captcha": image, "cid": id}
}

View File

@@ -17,7 +17,9 @@ func (c *Config) Configs(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.Config{Key: g.Query("key")}
condition.Permission = rc.LoginAccount.Username
rc.ResData = c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
res, err := c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
biz.ErrIsNil(err)
rc.ResData = res
}
func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
@@ -39,5 +41,5 @@ func (c *Config) SaveConfig(rc *req.Ctx) {
config := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Config))
rc.ReqParam = form
config.SetBaseInfo(rc.LoginAccount)
c.ConfigApp.Save(config)
biz.ErrIsNil(c.ConfigApp.Save(config))
}

View File

@@ -6,6 +6,7 @@ import (
"mayfly-go/internal/sys/api/vo"
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
@@ -17,12 +18,14 @@ type Resource struct {
func (r *Resource) GetAllResourceTree(rc *req.Ctx) {
var resources vo.ResourceManageVOList
r.ResourceApp.GetResourceList(new(entity.Resource), &resources, "weight asc")
r.ResourceApp.ListByCondOrder(new(entity.Resource), &resources, "weight asc")
rc.ResData = resources.ToTrees(0)
}
func (r *Resource) GetById(rc *req.Ctx) {
rc.ResData = r.ResourceApp.GetById(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(ginx.PathParamInt(rc.GinCtx, "id")))
biz.ErrIsNil(err, "该资源不存在")
rc.ResData = res
}
func (r *Resource) SaveResource(rc *req.Ctx) {
@@ -37,18 +40,18 @@ func (r *Resource) SaveResource(rc *req.Ctx) {
entity.Meta = string(bytes)
entity.SetBaseInfo(rc.LoginAccount)
r.ResourceApp.Save(entity)
biz.ErrIsNil(r.ResourceApp.Save(entity))
}
func (r *Resource) DelResource(rc *req.Ctx) {
r.ResourceApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
biz.ErrIsNil(r.ResourceApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
}
func (r *Resource) ChangeStatus(rc *req.Ctx) {
rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
status := int8(ginx.PathParamInt(rc.GinCtx, "status"))
rc.ReqParam = collx.Kvs("id", rid, "status", status)
r.ResourceApp.ChangeStatus(rid, status)
biz.ErrIsNil(r.ResourceApp.ChangeStatus(rid, status))
}
func (r *Resource) Sort(rc *req.Ctx) {

View File

@@ -22,7 +22,9 @@ type Role struct {
func (r *Role) Roles(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.Role{Name: g.Query("name")}
rc.ResData = r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role))
res, err := r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role))
biz.ErrIsNil(err)
rc.ResData = res
}
// 保存角色信息

View File

@@ -3,6 +3,7 @@ package api
import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
)
@@ -13,5 +14,7 @@ type Syslog struct {
func (r *Syslog) Syslogs(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage[*entity.SysLogQuery](rc.GinCtx, new(entity.SysLogQuery))
rc.ResData = r.SyslogApp.GetPageList(queryCond, page, new([]entity.SysLog), "create_time DESC")
res, err := r.SyslogApp.GetPageList(queryCond, page, new([]entity.SysLog), "create_time DESC")
biz.ErrIsNil(err)
rc.ResData = res
}

View File

@@ -20,7 +20,7 @@ func (s *System) ConnectWs(g *gin.Context) {
defer func() {
if err := recover(); err != nil {
errInfo := anyx.ToString(err)
logx.Error("websocket连接失败: ", errInfo)
logx.Errorf("websocket连接失败: %s", errInfo)
if wsConn != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(errInfo))
wsConn.Close()

View File

@@ -3,7 +3,8 @@ package application
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/cryptox"
@@ -12,71 +13,60 @@ import (
)
type Account interface {
GetAccount(condition *entity.Account, cols ...string) error
base.App[*entity.Account]
GetById(id uint64) *entity.Account
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Create(account *entity.Account) error
Create(account *entity.Account)
Update(account *entity.Account) error
Update(account *entity.Account)
Delete(id uint64)
Delete(id uint64) error
}
func newAccountApp(accountRepo repository.Account) Account {
return &accountAppImpl{
accountRepo: accountRepo,
}
return &accountAppImpl{base.AppImpl[*entity.Account, repository.Account]{Repo: accountRepo}}
}
type accountAppImpl struct {
accountRepo repository.Account
base.AppImpl[*entity.Account, repository.Account]
}
// 根据条件获取账号信息
func (a *accountAppImpl) GetAccount(condition *entity.Account, cols ...string) error {
return a.accountRepo.GetAccount(condition, cols...)
func (a *accountAppImpl) GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return a.GetRepo().GetPageList(condition, pageParam, toEntity)
}
func (a *accountAppImpl) GetById(id uint64) *entity.Account {
return a.accountRepo.GetById(id)
}
func (a *accountAppImpl) GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return a.accountRepo.GetPageList(condition, pageParam, toEntity)
}
func (a *accountAppImpl) Create(account *entity.Account) {
biz.IsTrue(a.GetAccount(&entity.Account{Username: account.Username}) != nil, "该账号用户名已存在")
func (a *accountAppImpl) Create(account *entity.Account) error {
if a.GetBy(&entity.Account{Username: account.Username}) == nil {
return errorx.NewBiz("该账号用户名已存在")
}
// 默认密码为账号用户名
account.Password = cryptox.PwdHash(account.Username)
account.Status = entity.AccountEnableStatus
a.accountRepo.Insert(account)
return a.Insert(account)
}
func (a *accountAppImpl) Update(account *entity.Account) {
func (a *accountAppImpl) Update(account *entity.Account) error {
if account.Username != "" {
unAcc := &entity.Account{Username: account.Username}
err := a.GetAccount(unAcc)
biz.IsTrue(err != nil || unAcc.Id == account.Id, "该用户名已存在")
err := a.GetBy(unAcc)
if err == nil && unAcc.Id != account.Id {
return errorx.NewBiz("该用户名已存在")
}
}
a.accountRepo.Update(account)
return a.UpdateById(account)
}
func (a *accountAppImpl) Delete(id uint64) {
err := gormx.Tx(
func (a *accountAppImpl) Delete(id uint64) error {
return gormx.Tx(
func(db *gorm.DB) error {
// 删除account信息
return db.Delete(new(entity.Account), "id = ?", id).Error
// 删除account信息
return a.DeleteByIdWithDb(db, id)
},
func(db *gorm.DB) error {
// 删除账号关联的角色信息
accountRole := &entity.AccountRole{AccountId: id}
return db.Where(accountRole).Delete(accountRole).Error
return gormx.DeleteByWithDb(db, &entity.AccountRole{AccountId: id})
},
)
biz.ErrIsNilAppendErr(err, "删除失败:%s")
}

View File

@@ -4,8 +4,9 @@ import (
"encoding/json"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/jsonx"
@@ -15,40 +16,48 @@ import (
const SysConfigKeyPrefix = "mayfly:sys:config:"
type Config interface {
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.App[*entity.Config]
Save(config *entity.Config)
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Save(config *entity.Config) error
// GetConfig 获取指定key的配置信息, 不会返回nil, 若不存在则值都默认值即空字符串
GetConfig(key string) *entity.Config
}
func newConfigApp(configRepo repository.Config) Config {
return &configAppImpl{
configRepo: configRepo,
}
configApp := new(configAppImpl)
configApp.Repo = configRepo
return configApp
// return &configAppImpl{base.AppImpl[*entity.Config, repository.Config]{Repo: configRepo}}
}
type configAppImpl struct {
configRepo repository.Config
base.AppImpl[*entity.Config, repository.Config]
}
func (a *configAppImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return a.configRepo.GetPageList(condition, pageParam, toEntity)
func (a *configAppImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return a.GetRepo().GetPageList(condition, pageParam, toEntity)
}
func (a *configAppImpl) Save(config *entity.Config) {
func (a *configAppImpl) Save(config *entity.Config) error {
if config.Id == 0 {
a.configRepo.Insert(config)
if err := a.Insert(config); err != nil {
return err
}
} else {
oldConfig := a.GetConfig(config.Key)
if oldConfig.Permission != "all" {
biz.IsTrue(strings.Contains(oldConfig.Permission, config.Modifier), "您无权修改该配置")
if oldConfig.Permission != "all" && !strings.Contains(oldConfig.Permission, config.Modifier) {
return errorx.NewBiz("您无权修改该配置")
}
a.configRepo.Update(config)
if err := a.UpdateById(config); err != nil {
return err
}
}
cache.Del(SysConfigKeyPrefix + config.Key)
return nil
}
func (a *configAppImpl) GetConfig(key string) *entity.Config {
@@ -60,7 +69,7 @@ func (a *configAppImpl) GetConfig(key string) *entity.Config {
return config
}
if err := a.configRepo.GetConfig(config, "Id", "Key", "Value", "Permission"); err != nil {
if err := a.GetBy(config, "Id", "Key", "Value", "Permission"); err != nil {
logx.Warnf("不存在key = [%s] 的系统配置", key)
} else {
cache.SetStr(SysConfigKeyPrefix+key, jsonx.ToStr(config), -1)

View File

@@ -4,7 +4,8 @@ import (
"mayfly-go/internal/common/consts"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/utils/stringx"
"strings"
@@ -12,58 +13,54 @@ import (
)
type Resource interface {
GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string)
base.App[*entity.Resource]
GetById(id uint64, cols ...string) *entity.Resource
Save(entity *entity.Resource) error
Save(entity *entity.Resource)
Delete(id uint64) error
ChangeStatus(resourceId uint64, status int8)
ChangeStatus(resourceId uint64, status int8) error
Sort(re *entity.Resource)
Sort(re *entity.Resource) error
Delete(id uint64)
GetAccountResources(accountId uint64, toEntity any)
GetAccountResources(accountId uint64, toEntity any) error
}
func newResourceApp(resourceRepo repository.Resource) Resource {
return &resourceAppImpl{
resourceRepo: resourceRepo,
base.AppImpl[*entity.Resource, repository.Resource]{Repo: resourceRepo},
}
}
type resourceAppImpl struct {
resourceRepo repository.Resource
base.AppImpl[*entity.Resource, repository.Resource]
}
func (r *resourceAppImpl) GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string) {
r.resourceRepo.GetResourceList(condition, toEntity, orderBy...)
}
func (r *resourceAppImpl) GetById(id uint64, cols ...string) *entity.Resource {
return r.resourceRepo.GetById(id, cols...)
}
func (r *resourceAppImpl) Save(resource *entity.Resource) {
func (r *resourceAppImpl) Save(resource *entity.Resource) error {
// 更新操作
if resource.Id != 0 {
if resource.Code != "" {
oldRes := r.GetById(resource.Id, "Code")
oldRes, err := r.GetById(new(entity.Resource), resource.Id, "Code")
if err != nil {
return errorx.NewBiz("更新失败, 该资源不存在")
}
// 如果修改了code则校验新code是否存在
if oldRes.Code != resource.Code {
r.checkCode(resource.Code)
if err := r.checkCode(resource.Code); err != nil {
return err
}
}
}
gormx.UpdateById(resource)
return
return gormx.UpdateById(resource)
}
// 生成随机八位唯一标识符
ui := stringx.Rand(8)
if pid := resource.Pid; pid != 0 {
pResource := r.GetById(uint64(pid))
biz.IsTrue(pResource != nil, "该父资源不存在")
pResource, err := r.GetById(new(entity.Resource), uint64(pid))
if err != nil {
return errorx.NewBiz("该父资源不存在")
}
resource.UiPath = pResource.UiPath + ui + entity.ResourceUiPathSp
} else {
resource.UiPath = ui + entity.ResourceUiPathSp
@@ -72,27 +69,33 @@ func (r *resourceAppImpl) Save(resource *entity.Resource) {
if resource.Status == 0 {
resource.Status = entity.ResourceStatusEnable
}
r.checkCode(resource.Code)
if err := r.checkCode(resource.Code); err != nil {
return err
}
resource.Weight = int(time.Now().Unix())
gormx.Insert(resource)
return gormx.Insert(resource)
}
func (r *resourceAppImpl) ChangeStatus(resourceId uint64, status int8) {
resource := r.resourceRepo.GetById(resourceId)
biz.NotNil(resource, "资源不存在")
func (r *resourceAppImpl) ChangeStatus(resourceId uint64, status int8) error {
resource, err := r.GetById(new(entity.Resource), resourceId)
if err != nil {
return errorx.NewBiz("资源不存在")
}
resource.Status = status
r.resourceRepo.UpdateByUiPathLike(resource)
return r.GetRepo().UpdateByUiPathLike(resource)
}
func (r *resourceAppImpl) Sort(sortResource *entity.Resource) {
resource := r.resourceRepo.GetById(sortResource.Id)
biz.NotNil(resource, "资源不存在")
func (r *resourceAppImpl) Sort(sortResource *entity.Resource) error {
resource, err := r.GetById(new(entity.Resource), sortResource.Id)
if err != nil {
return errorx.NewBiz("资源不存在")
}
// 未改变父节点,则更新排序值即可
if sortResource.Pid == resource.Pid {
saveE := &entity.Resource{Weight: sortResource.Weight}
saveE.Id = sortResource.Id
r.Save(saveE)
return
return r.Save(saveE)
}
// 若资源原本唯一标识路径为xxxx/yyyy/zzzz/,则获取其父节点路径标识 xxxx/yyyy/ 与自身节点标识 zzzz/
@@ -109,12 +112,14 @@ func (r *resourceAppImpl) Sort(sortResource *entity.Resource) {
newParentResourceUiPath := ""
if sortResource.Pid != 0 {
newParentResource := r.resourceRepo.GetById(uint64(sortResource.Pid))
biz.NotNil(newParentResource, "父资源不存在")
newParentResource, err := r.GetById(new(entity.Resource), uint64(sortResource.Pid))
if err != nil {
return errorx.NewBiz("父资源不存在")
}
newParentResourceUiPath = newParentResource.UiPath
}
children := r.resourceRepo.GetChildren(resource.UiPath)
children := r.GetRepo().GetChildren(resource.UiPath)
for _, v := range children {
if v.Id == sortResource.Id {
continue
@@ -137,36 +142,43 @@ func (r *resourceAppImpl) Sort(sortResource *entity.Resource) {
}
condition := new(entity.Resource)
condition.Id = sortResource.Id
gormx.Updates(condition, updateMap)
return gormx.Updates(condition, updateMap)
}
func (r *resourceAppImpl) checkCode(code string) {
biz.IsTrue(!strings.Contains(code, ","), "code不能包含','")
biz.IsEquals(gormx.CountBy(&entity.Resource{Code: code}), int64(0), "code已存在")
func (r *resourceAppImpl) checkCode(code string) error {
if strings.Contains(code, ",") {
return errorx.NewBiz("code不能包含','")
}
if gormx.CountBy(&entity.Resource{Code: code}) == 0 {
return errorx.NewBiz("该code已存在")
}
return nil
}
func (r *resourceAppImpl) Delete(id uint64) {
resource := r.resourceRepo.GetById(id)
biz.NotNil(resource, "资源不存在")
func (r *resourceAppImpl) Delete(id uint64) error {
resource, err := r.GetById(new(entity.Resource), id)
if err != nil {
return errorx.NewBiz("资源不存在")
}
// 删除当前节点及其所有子节点
children := r.resourceRepo.GetChildren(resource.UiPath)
children := r.GetRepo().GetChildren(resource.UiPath)
for _, v := range children {
r.resourceRepo.Delete(v.Id)
r.GetRepo().DeleteById(v.Id)
// 删除角色关联的资源信息
gormx.DeleteByCondition(&entity.RoleResource{ResourceId: v.Id})
gormx.DeleteBy(&entity.RoleResource{ResourceId: v.Id})
}
return nil
}
func (r *resourceAppImpl) GetAccountResources(accountId uint64, toEntity any) {
func (r *resourceAppImpl) GetAccountResources(accountId uint64, toEntity any) error {
// 超级管理员返回所有
if accountId == consts.AdminId {
cond := &entity.Resource{
Status: entity.ResourceStatusEnable,
}
r.resourceRepo.GetResourceList(cond, toEntity, "pid asc", "weight asc")
return
return r.ListByCondOrder(cond, toEntity, "pid asc", "weight asc")
}
r.resourceRepo.GetAccountResources(accountId, toEntity)
return r.GetRepo().GetAccountResources(accountId, toEntity)
}

View File

@@ -10,14 +10,16 @@ import (
"mayfly-go/pkg/utils/collx"
"strings"
"time"
"gorm.io/gorm"
)
type Role interface {
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
SaveRole(role *entity.Role)
SaveRole(role *entity.Role) error
DeleteRole(id uint64)
DeleteRole(id uint64) error
GetRoleResourceIds(roleId uint64) []uint64
@@ -50,26 +52,32 @@ type roleAppImpl struct {
roleRepo repository.Role
}
func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *roleAppImpl) GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.roleRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func (m *roleAppImpl) SaveRole(role *entity.Role) {
func (m *roleAppImpl) SaveRole(role *entity.Role) error {
role.Code = strings.ToUpper(role.Code)
if role.Id != 0 {
// code不可更改防止误传
role.Code = ""
gormx.UpdateById(role)
} else {
role.Status = 1
gormx.Insert(role)
return gormx.UpdateById(role)
}
role.Status = 1
return gormx.Insert(role)
}
func (m *roleAppImpl) DeleteRole(id uint64) {
m.roleRepo.Delete(id)
func (m *roleAppImpl) DeleteRole(id uint64) error {
// 删除角色与资源的关联关系
gormx.DeleteByCondition(&entity.RoleResource{RoleId: id})
return gormx.Tx(
func(db *gorm.DB) error {
return m.roleRepo.DeleteByIdWithDb(db, id)
},
func(db *gorm.DB) error {
return gormx.DeleteByWithDb(db, &entity.RoleResource{RoleId: id})
},
)
}
func (m *roleAppImpl) GetRoleResourceIds(roleId uint64) []uint64 {

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/anyx"
@@ -13,7 +13,7 @@ import (
)
type Syslog interface {
GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 从请求上下文的参数保存系统日志
SaveFromReq(req *req.Ctx)
@@ -29,7 +29,7 @@ type syslogAppImpl struct {
syslogRepo repository.Syslog
}
func (m *syslogAppImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (m *syslogAppImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return m.syslogRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
@@ -64,7 +64,7 @@ func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
syslog.Type = entity.SyslogTypeError
var errMsg string
switch t := err.(type) {
case *biz.BizError:
case errorx.BizError:
errMsg = fmt.Sprintf("errCode: %d, errMsg: %s", t.Code(), t.Error())
case error:
errMsg = t.Error()

View File

@@ -2,18 +2,12 @@ package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Account interface {
// 根据条件获取账号信息
GetAccount(condition *entity.Account, cols ...string) error
base.Repo[*entity.Account]
GetById(id uint64) *entity.Account
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Insert(account *entity.Account)
Update(account *entity.Account)
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}

View File

@@ -2,17 +2,12 @@ package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Config interface {
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.Config]
Insert(config *entity.Config)
Update(config *entity.Config)
GetConfig(config *entity.Config, cols ...string) error
GetByCondition(condition *entity.Config, cols ...string) error
GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}

View File

@@ -2,24 +2,18 @@ package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/base"
)
type Resource interface {
// 获取资源列表
GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string)
GetById(id uint64, cols ...string) *entity.Resource
Delete(id uint64)
GetByCondition(condition *entity.Resource, cols ...string) error
base.Repo[*entity.Resource]
// 获取账号资源列表
GetAccountResources(accountId uint64, toEntity any)
GetAccountResources(accountId uint64, toEntity any) error
// 获取所有子节点id
GetChildren(uiPath string) []entity.Resource
// 根据uiPath右匹配更新所有相关类资源
UpdateByUiPathLike(resource *entity.Resource)
UpdateByUiPathLike(resource *entity.Resource) error
}

View File

@@ -2,13 +2,14 @@ package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Role interface {
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.Role]
Delete(id uint64)
GetPageList(condition *entity.Role, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
// 获取角色拥有的资源id数组从role_resource表获取
GetRoleResourceIds(roleId uint64) []uint64

View File

@@ -2,11 +2,12 @@ package repository
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Syslog interface {
GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
base.Repo[*entity.SysLog]
Insert(log *entity.SysLog)
GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}

View File

@@ -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), "更新账号信息失败")
}

View File

@@ -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...)
}

View File

@@ -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)
}

View File

@@ -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})
}
// 获取账号角色信息列表

View File

@@ -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)
}