mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-17 23:00:24 +08:00
refactor: 新增base.Repo与base.App,重构repo与app层代码
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user