181 lines
5.6 KiB
Go
181 lines
5.6 KiB
Go
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) {
|
||
commResourcesRepo = []*resourceRepoInfo{
|
||
{consts.ResourcesName_Project, "项目管理", repo.NewCommonResourceRepo(db, &model.Project{})},
|
||
{consts.ResourcesName_Server, "服务器管理", repo.NewCommonResourceRepo(db, &model.Server{})},
|
||
{consts.ResourcesName_WhiteList, "白名单", repo.NewCommonResourceRepo(db, &model.WhiteList{})},
|
||
{consts.ResourcesName_Ban, "封禁管理", repo.NewCommonResourceRepo(db, &model.Ban{})},
|
||
{consts.ResourcesName_MailRole, "个人邮件", repo.NewCommonResourceRepo(db, &model.RoleMail{})},
|
||
{consts.ResourcesName_MailGlobal, "全服邮件", repo.NewCommonResourceRepo(db, &model.GlobalMail{})},
|
||
{consts.ResourcesName_Notice, "公告", repo.NewCommonResourceRepo(db, &model.Notice{})},
|
||
{consts.ResourcesName_RewardCode, "奖励码", repo.NewCommonResourceRepo(db, &model.RewardCode{})},
|
||
{consts.ResourcesName_DevicePush, "设备推送", repo.NewCommonResourceRepo(db, &model.DevicePush{})},
|
||
}
|
||
}
|
||
|
||
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)
|
||
}
|
||
|
||
err = findCommResourceRepo(resource).Delete(projectId, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 执行各个项目特定的钩子方法
|
||
if hook, ok := projects.GetProjectResourceHook(projectId, resource).(projects.IPostResourceOpDeleteHook); ok {
|
||
err = hook.Delete(projectEt, resource, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (svc *CommonResourceService) GetSupportResourcesList() [][2]string {
|
||
list := make([][2]string, 0, len(commResourcesRepo))
|
||
for _, v := range commResourcesRepo {
|
||
list = append(list, [2]string{v.resource, v.desc})
|
||
}
|
||
return list
|
||
}
|
||
|
||
type resourceRepoInfo struct {
|
||
resource string
|
||
desc string
|
||
repo repo.ICommonResourceRepo
|
||
}
|
||
|
||
var commResourcesRepo = make([]*resourceRepoInfo, 0)
|
||
|
||
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
|
||
}
|