2025-04-30 15:46:14 +08:00
|
|
|
|
package domain
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"admin/apps/user/domain/entity"
|
|
|
|
|
"admin/apps/user/domain/repo"
|
|
|
|
|
"admin/apps/user/model"
|
|
|
|
|
"admin/apps/user/model/dto"
|
|
|
|
|
"admin/internal/consts"
|
|
|
|
|
"fmt"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CommonResourceService struct {
|
|
|
|
|
userRepo repo.IUserRepo
|
|
|
|
|
tokenRepo repo.ITokenRepo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initCommonResourcesRepo(db *gorm.DB) {
|
|
|
|
|
r(consts.ResourcesName_SysUser, "用户管理", repo.NewCommonResourceRepo(db, &model.User{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
|
|
|
|
r(consts.ResourcesName_SysCharacter, "角色管理", repo.NewCommonResourceRepo(db, &model.Character{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCommonResourceService(db *gorm.DB) *CommonResourceService {
|
|
|
|
|
initCommonResourcesRepo(db)
|
|
|
|
|
svc := &CommonResourceService{
|
|
|
|
|
userRepo: repo.NewUserRepo(db),
|
|
|
|
|
tokenRepo: repo.NewTokenRepo(db),
|
|
|
|
|
}
|
|
|
|
|
return svc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) List(resource string, pageNo, pageLen int, extraQuery string, args ...any) ([]*dto.CommonDtoFieldDesc, []dto.CommonDtoValues, error) {
|
|
|
|
|
fieldsDescInfo, etList, err := findCommResourceRepo(resource).List(pageNo, pageLen, extraQuery, args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
retList := make([]dto.CommonDtoValues, 0, len(etList))
|
|
|
|
|
for _, v := range etList {
|
2025-05-06 09:07:44 +08:00
|
|
|
|
dtoObj := v.ToCommonDto()
|
|
|
|
|
if resource == consts.ResourcesName_SysUser {
|
|
|
|
|
if name, find := dtoObj["UserName"]; find && name == "admin" {
|
|
|
|
|
// 不显示初始管理员数据
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if resource == consts.ResourcesName_SysCharacter {
|
|
|
|
|
if name, find := dtoObj["Name"]; find && name == "admin" {
|
|
|
|
|
// 不显示初始管理员数据
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-30 15:46:14 +08:00
|
|
|
|
retList = append(retList, v.ToCommonDto())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldsDescInfo, retList, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) GetById(resource string, id int) ([]*dto.CommonDtoFieldDesc, dto.CommonDtoValues, *entity.CommonResource, bool, error) {
|
|
|
|
|
fieldsDescInfo, et, find, err := findCommResourceRepo(resource).GetById(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldsDescInfo, et.ToCommonDto(), et, find, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) Create(resource string, dtoObj dto.CommonDtoValues) (dto.CommonDtoValues, *entity.CommonResource, error) {
|
|
|
|
|
if resource == consts.ResourcesName_SysUser {
|
2025-05-06 09:07:44 +08:00
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
et, err := findCommResourceRepo(resource).Create(dtoObj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, et, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回新的实体,插入数据库之后会填入数据库id,所以要返给前端刷新id数据
|
|
|
|
|
return et.ToCommonDto(), et, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) Edit(resource string, dtoObj dto.CommonDtoValues) error {
|
|
|
|
|
err := findCommResourceRepo(resource).Edit(dtoObj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) Delete(resource string, id int) error {
|
|
|
|
|
_, _, err := findCommResourceRepo(resource).Delete(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) GetSupportResourcesList() []*dto.ResourceInitInfo {
|
|
|
|
|
list := make([]*dto.ResourceInitInfo, 0, len(commResourcesRepo))
|
|
|
|
|
for _, v := range commResourcesRepo {
|
|
|
|
|
list = append(list, &dto.ResourceInitInfo{
|
|
|
|
|
Resource: v.Resource,
|
|
|
|
|
Desc: v.Desc,
|
|
|
|
|
ShowMethods: v.ShowMethods,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ShowMethod_Get = 1
|
|
|
|
|
ShowMethod_Post = 1 << 1
|
|
|
|
|
ShowMethod_Put = 1 << 2
|
|
|
|
|
ShowMethod_Delete = 1 << 3
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type resourceRepoInfo struct {
|
|
|
|
|
Resource string
|
|
|
|
|
Desc string
|
|
|
|
|
Repo repo.ICommonResourceRepo
|
|
|
|
|
ShowMethods []string
|
|
|
|
|
showMethods int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var commResourcesRepo = make([]*resourceRepoInfo, 0)
|
|
|
|
|
|
|
|
|
|
func r(resource, desc string, repo repo.ICommonResourceRepo, showMethods int) {
|
|
|
|
|
curRepo := &resourceRepoInfo{
|
|
|
|
|
Resource: resource,
|
|
|
|
|
Desc: desc,
|
|
|
|
|
Repo: repo,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if showMethods&ShowMethod_Get == ShowMethod_Get {
|
|
|
|
|
curRepo.ShowMethods = append(curRepo.ShowMethods, "get")
|
|
|
|
|
}
|
|
|
|
|
if showMethods&ShowMethod_Post == ShowMethod_Post {
|
|
|
|
|
curRepo.ShowMethods = append(curRepo.ShowMethods, "post")
|
|
|
|
|
}
|
|
|
|
|
if showMethods&ShowMethod_Put == ShowMethod_Put {
|
|
|
|
|
curRepo.ShowMethods = append(curRepo.ShowMethods, "put")
|
|
|
|
|
}
|
|
|
|
|
if showMethods&ShowMethod_Delete == ShowMethod_Delete {
|
|
|
|
|
curRepo.ShowMethods = append(curRepo.ShowMethods, "delete")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commResourcesRepo = append(commResourcesRepo, curRepo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findCommResourceRepo(resource string) repo.ICommonResourceRepo {
|
|
|
|
|
for _, v := range commResourcesRepo {
|
|
|
|
|
if v.Resource == resource {
|
|
|
|
|
return v.Repo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
panic(fmt.Errorf("not found Resource:%v", resource))
|
|
|
|
|
return nil
|
|
|
|
|
}
|