uniugm/admin/apps/game/domain/comm_resource.go
2025-04-26 13:50:26 +08:00

220 lines
7.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"admin/apps/game/domain/entity"
"admin/apps/game/domain/projects"
"admin/apps/game/domain/repo"
"admin/apps/game/model"
"admin/apps/game/model/dto"
"admin/internal/consts"
"admin/internal/errcode"
"fmt"
"gorm.io/gorm"
)
type CommonResourceService struct {
projectRepo repo.IProjectRepo
}
func initCommonResourcesRepo(db *gorm.DB) {
r(consts.ResourcesName_Project, "项目管理", repo.NewCommonResourceRepo(db, &model.Project{}), ShowMethod_Get|ShowMethod_Put)
r(consts.ResourcesName_Server, "服务器管理", repo.NewCommonResourceRepo(db, &model.Server{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
r(consts.ResourcesName_Account, "账号管理", repo.NewCommonResourceRepo(db, &model.Account{}), ShowMethod_Get) // 账号管理不需要在后台读写数据都是通过项目api拉
r(consts.ResourcesName_Role, "角色管理", repo.NewCommonResourceRepo(db, &model.Role{}), ShowMethod_Get) // 角色管理不需要在后台读写数据都是通过项目api拉
r(consts.ResourcesName_WhiteList, "白名单", repo.NewCommonResourceRepo(db, &model.WhiteList{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Delete)
r(consts.ResourcesName_Ban, "封禁管理", repo.NewCommonResourceRepo(db, &model.Ban{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Delete)
r(consts.ResourcesName_MailRole, "个人邮件", repo.NewCommonResourceRepo(db, &model.RoleMail{}), ShowMethod_Get|ShowMethod_Post) // 个人邮件发放就没法撤回?
r(consts.ResourcesName_MailGlobal, "全服邮件", repo.NewCommonResourceRepo(db, &model.GlobalMail{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Delete) // 直接删除,别修改了,玩家收到的更乱
r(consts.ResourcesName_Notice, "公告", repo.NewCommonResourceRepo(db, &model.Notice{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
r(consts.ResourcesName_RewardCode, "奖励码", repo.NewCommonResourceRepo(db, &model.RewardCode{}), ShowMethod_Get)
r(consts.ResourcesName_DevicePush, "设备推送", repo.NewCommonResourceRepo(db, &model.DevicePush{}), ShowMethod_Get)
}
func NewCommonResourceService(db *gorm.DB) *CommonResourceService {
initCommonResourcesRepo(db)
svc := &CommonResourceService{
projectRepo: repo.NewProjectRepo(db),
}
return svc
}
func (svc *CommonResourceService) List(projectId string, resource string, pageNo, pageLen int, extraQuery string, args ...any) ([]*dto.CommonDtoFieldDesc, []dto.CommonDtoValues, error) {
projectEt, find, err := svc.projectRepo.GetByProjectId(projectId)
if err != nil {
return nil, nil, err
}
if !find {
return nil, nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
}
fieldsDescInfo, etList, err := findCommResourceRepo(resource).List(projectId, pageNo, pageLen, extraQuery, args...)
if err != nil {
return nil, nil, err
}
retList := make([]dto.CommonDtoValues, 0, len(etList))
for _, v := range etList {
retList = append(retList, v.ToCommonDto())
}
// 执行各个项目特定的钩子方法
if hook, ok := projects.GetProjectResourceHook(projectId, resource).(projects.IPostResourceOpListHook); ok {
return hook.List(projectEt, resource, pageNo, pageLen, fieldsDescInfo, retList, extraQuery, args...)
}
return fieldsDescInfo, retList, nil
}
func (svc *CommonResourceService) GetById(projectId string, resource string, id int) ([]*dto.CommonDtoFieldDesc, dto.CommonDtoValues, *entity.CommonResource, bool, error) {
fieldsDescInfo, et, find, err := findCommResourceRepo(resource).GetById(projectId, id)
if err != nil {
return nil, nil, nil, false, err
}
return fieldsDescInfo, et.ToCommonDto(), et, find, nil
}
func (svc *CommonResourceService) Create(projectId string, resource string, dtoObj dto.CommonDtoValues) (dto.CommonDtoValues, error) {
projectEt, find, err := svc.projectRepo.GetByProjectId(projectId)
if err != nil {
return nil, err
}
if !find {
return nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
}
et, err := findCommResourceRepo(resource).Create(projectId, dtoObj)
if err != nil {
return nil, err
}
// 返回新的实体插入数据库之后会填入数据库id所以要返给前端刷新id数据
// 执行各个项目特定的钩子方法
if hook, ok := projects.GetProjectResourceHook(projectId, resource).(projects.IPostResourceOpCreateHook); ok {
dtoObj := et.ToCommonDto()
err = hook.Create(projectEt, resource, dtoObj)
if err != nil {
return nil, err
}
return dtoObj, nil
}
return et.ToCommonDto(), err
}
func (svc *CommonResourceService) Edit(projectId string, resource string, dtoObj dto.CommonDtoValues) error {
projectEt, find, err := svc.projectRepo.GetByProjectId(projectId)
if err != nil {
return err
}
if !find {
return errcode.New(errcode.ServerError, "not found project %v db data", projectId)
}
err = findCommResourceRepo(resource).Edit(projectId, dtoObj)
if err != nil {
return err
}
// 执行各个项目特定的钩子方法
if hook, ok := projects.GetProjectResourceHook(projectId, resource).(projects.IPostResourceOpEditHook); ok {
err = hook.Edit(projectEt, resource, dtoObj)
if err != nil {
return err
}
return nil
}
return nil
}
func (svc *CommonResourceService) Delete(projectId string, resource string, id int) error {
projectEt, find, err := svc.projectRepo.GetByProjectId(projectId)
if err != nil {
return err
}
if !find {
return errcode.New(errcode.ServerError, "not found project %v db data", projectId)
}
oldEt, find, err := findCommResourceRepo(resource).Delete(projectId, id)
if err != nil {
return err
}
// 执行各个项目特定的钩子方法
if find {
if hook, ok := projects.GetProjectResourceHook(projectId, resource).(projects.IPostResourceOpDeleteHook); ok {
err = hook.Delete(projectEt, resource, oldEt.ToCommonDto())
if err != nil {
return err
}
return nil
}
}
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
}