2025-04-24 20:39:31 +08:00
|
|
|
|
package domain
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-30 15:46:14 +08:00
|
|
|
|
"admin/apps/game/api"
|
2025-04-24 20:39:31 +08:00
|
|
|
|
"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"
|
2025-05-09 18:28:15 +08:00
|
|
|
|
"admin/lib/xlog"
|
2025-04-24 20:39:31 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"gorm.io/gorm"
|
2025-05-09 18:28:15 +08:00
|
|
|
|
"strings"
|
2025-04-24 20:39:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CommonResourceService struct {
|
|
|
|
|
projectRepo repo.IProjectRepo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initCommonResourcesRepo(db *gorm.DB) {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
r(consts.ResourcesName_Project, "项目管理", repo.NewCommonResourceRepo(db, &model.Project{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-05-12 18:43:41 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
serverRepo := r(consts.ResourcesName_Server, "服务器管理", repo.NewCommonResourceRepo(db, &model.Server{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
|
|
|
|
serverRepo.GlobalBtns = []*api.ResourceBtnInfo{
|
2025-05-13 18:13:22 +08:00
|
|
|
|
{Key: consts.BtnKeyGlobal_Server_DownAll, Name: "一键停服", BtnColorType: "info"},
|
|
|
|
|
{Key: consts.BtnKeyGlobal_Server_UpAll, Name: "一键起服", BtnColorType: "warning"},
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
serverRepo.RowBtns = []*api.ResourceBtnInfo{
|
2025-05-13 18:13:22 +08:00
|
|
|
|
{Key: consts.BtnKeyRow_Server_Down, Name: "停服", BtnColorType: "info"},
|
|
|
|
|
{Key: consts.BtnKeyRow_Server_Up, Name: "起服", BtnColorType: "warning"},
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 10:18:06 +08:00
|
|
|
|
r(consts.ResourcesName_Account, "账号列表", repo.NewCommonResourceRepo(db, &model.Account{}), ShowMethod_Get) // 账号管理不需要在后台读写数据,都是通过项目api拉
|
2025-05-09 18:28:15 +08:00
|
|
|
|
r(consts.ResourcesName_SupportAccount, "扶持账号", repo.NewCommonResourceRepo(db, &model.SupportAccount{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Delete) // 扶持账号,只是标记哪些账号是扶持好
|
2025-05-10 10:18:06 +08:00
|
|
|
|
r(consts.ResourcesName_Role, "角色列表", repo.NewCommonResourceRepo(db, &model.Role{}), ShowMethod_Get) // 角色管理不需要在后台读写数据,都是通过项目api拉
|
2025-04-26 13:50:26 +08:00
|
|
|
|
r(consts.ResourcesName_WhiteList, "白名单", repo.NewCommonResourceRepo(db, &model.WhiteList{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Delete)
|
2025-04-28 15:56:04 +08:00
|
|
|
|
r(consts.ResourcesName_Ban, "封禁管理", repo.NewCommonResourceRepo(db, &model.Ban{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-04-30 15:46:14 +08:00
|
|
|
|
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) // 直接删除,别修改了,玩家收到的更乱
|
2025-05-07 18:25:31 +08:00
|
|
|
|
r(consts.ResourcesName_CDKey, "礼包码", repo.NewCommonResourceRepo(db, &model.CDKey{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-05-12 18:43:41 +08:00
|
|
|
|
r(consts.ResourcesName_Notice, "公告(暂无)", repo.NewCommonResourceRepo(db, &model.Notice{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-05-10 10:18:06 +08:00
|
|
|
|
r(consts.ResourcesName_DevicePush, "设备推送(暂无)", repo.NewCommonResourceRepo(db, &model.DevicePush{}), ShowMethod_Get)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCommonResourceService(db *gorm.DB) *CommonResourceService {
|
|
|
|
|
initCommonResourcesRepo(db)
|
|
|
|
|
svc := &CommonResourceService{
|
|
|
|
|
projectRepo: repo.NewProjectRepo(db),
|
|
|
|
|
}
|
|
|
|
|
return svc
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
func (svc *CommonResourceService) List(projectId int, resource string, listParams *dto.CommonListReq) (int, []*dto.CommonDtoFieldDesc, []dto.CommonDtoValues, error) {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-09 18:28:15 +08:00
|
|
|
|
return 0, nil, nil, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if resource != consts.ResourcesName_Project && !find {
|
2025-05-09 18:28:15 +08:00
|
|
|
|
return 0, nil, nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
rRepo := findCommResourceRepo(resource)
|
|
|
|
|
totalCount, fieldsDescInfo, etList, err := rRepo.List(projectEt, listParams)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-09 18:28:15 +08:00
|
|
|
|
return 0, nil, nil, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
retList := make([]dto.CommonDtoValues, 0, len(etList))
|
|
|
|
|
for _, v := range etList {
|
|
|
|
|
retList = append(retList, v.ToCommonDto())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if hook, ok := projects.GetProjectResourceHook(projectEt, resource).(projects.IPostResourceOpListHook); ok {
|
2025-05-12 11:09:11 +08:00
|
|
|
|
totalCount1, fieldsDescInfo1, retList1, err1 := hook.List(projectEt, resource, listParams, fieldsDescInfo, totalCount, retList)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
xlog.Warnf("list resource %v, hook return error:%v", resource, err1)
|
|
|
|
|
return totalCount, fieldsDescInfo, retList, nil
|
|
|
|
|
}
|
|
|
|
|
return totalCount1, fieldsDescInfo1, retList1, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
return totalCount, fieldsDescInfo, retList, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
func (svc *CommonResourceService) GetById(projectId int, resource string, id int) ([]*dto.CommonDtoFieldDesc, dto.CommonDtoValues, *entity.CommonResource, bool, error) {
|
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, false, err
|
|
|
|
|
}
|
|
|
|
|
if resource != consts.ResourcesName_Project && !find {
|
|
|
|
|
return nil, nil, nil, false, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldsDescInfo, et, find, err := findCommResourceRepo(resource).GetById(projectEt, id)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldsDescInfo, et.ToCommonDto(), et, find, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
func (svc *CommonResourceService) Create(projectId int, resource string, dtoObj dto.CommonDtoValues) (dto.CommonDtoValues, error) {
|
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if resource != consts.ResourcesName_Project && !find {
|
2025-04-24 20:39:31 +08:00
|
|
|
|
return nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
createOne := func(obj dto.CommonDtoValues) (dto.CommonDtoValues, error) {
|
2025-05-12 18:43:41 +08:00
|
|
|
|
et, err := findCommResourceRepo(resource).Create(projectEt, resource, obj)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-05-09 18:28:15 +08:00
|
|
|
|
|
|
|
|
|
// 返回新的实体,插入数据库之后会填入数据库id,所以要返给前端刷新id数据
|
|
|
|
|
// 这里转换一次新的数据传输对象,因为上一步走了创建,会给dto分配id
|
|
|
|
|
newObj := et.ToCommonDto()
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
|
|
|
|
if hook, ok := projects.GetProjectResourceHook(projectEt, resource).(projects.IPostResourceOpCreateHook); ok {
|
|
|
|
|
err = hook.Create(projectEt, resource, newObj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return newObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newDtoObj dto.CommonDtoValues
|
|
|
|
|
if resource == consts.ResourcesName_SupportAccount {
|
|
|
|
|
account, findAccount := dtoObj["Account"]
|
|
|
|
|
if findAccount {
|
|
|
|
|
for _, ac := range strings.Split(account.(string), ",") {
|
|
|
|
|
dtoObj["Account"] = ac
|
|
|
|
|
tmpObj, err := createOne(dtoObj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
xlog.Errorf("create support account %v db error:%v", ac, err)
|
|
|
|
|
err = nil // 忽略多个插入的报错
|
|
|
|
|
} else {
|
|
|
|
|
newDtoObj = tmpObj
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errcode.New(errcode.ParamsInvalid, "account empty:%+v", dtoObj)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
newDtoObj, err = createOne(dtoObj)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
return newDtoObj, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
func (svc *CommonResourceService) Edit(projectId int, resource string, dtoObj dto.CommonDtoValues) error {
|
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if resource != consts.ResourcesName_Project && !find {
|
2025-04-24 20:39:31 +08:00
|
|
|
|
return errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
err = findCommResourceRepo(resource).Edit(projectEt, dtoObj)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if hook, ok := projects.GetProjectResourceHook(projectEt, resource).(projects.IPostResourceOpEditHook); ok {
|
2025-04-24 20:39:31 +08:00
|
|
|
|
err = hook.Edit(projectEt, resource, dtoObj)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 10:18:06 +08:00
|
|
|
|
func (svc *CommonResourceService) Delete(projectId int, resource string, id int) (*entity.CommonResource, error) {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return nil, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if resource != consts.ResourcesName_Project && !find {
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
oldEt, find, err := findCommResourceRepo(resource).Delete(projectEt, id)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return nil, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
2025-04-26 13:50:26 +08:00
|
|
|
|
if find {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
if hook, ok := projects.GetProjectResourceHook(projectEt, resource).(projects.IPostResourceOpDeleteHook); ok {
|
2025-04-26 13:50:26 +08:00
|
|
|
|
err = hook.Delete(projectEt, resource, oldEt.ToCommonDto())
|
|
|
|
|
if err != nil {
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return oldEt, err
|
2025-04-26 13:50:26 +08:00
|
|
|
|
}
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return oldEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-26 13:50:26 +08:00
|
|
|
|
|
2025-05-10 10:18:06 +08:00
|
|
|
|
return oldEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 18:43:41 +08:00
|
|
|
|
func (svc *CommonResourceService) RowsSelection(projectId int, resourceName string, params *dto.CommonRowsSelectionReq) (*dto.CommonRowsSelectionRsp, error) {
|
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resourceName != consts.ResourcesName_Project && !find {
|
|
|
|
|
return nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
|
|
|
|
}
|
|
|
|
|
if hook, ok := projects.GetProjectResourceHook(projectEt, resourceName).(projects.IPostResourceOpRowsHook); ok {
|
|
|
|
|
return hook.RowsSelection(projectEt, resourceName, params.BtnKey, params.Rows)
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
func (svc *CommonResourceService) GetSupportResourcesList(permissions []string) []*api.ResourceInitInfo {
|
|
|
|
|
list := make([]*api.ResourceInitInfo, 0, len(commResourcesRepo))
|
2025-04-24 20:39:31 +08:00
|
|
|
|
for _, v := range commResourcesRepo {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
list = append(list, &api.ResourceInitInfo{
|
2025-04-26 13:50:26 +08:00
|
|
|
|
Resource: v.Resource,
|
|
|
|
|
Desc: v.Desc,
|
|
|
|
|
ShowMethods: v.ShowMethods,
|
2025-05-12 18:43:41 +08:00
|
|
|
|
GlobalBtns: v.GlobalBtns,
|
|
|
|
|
RowBtns: v.RowBtns,
|
2025-04-26 13:50:26 +08:00
|
|
|
|
})
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-26 13:50:26 +08:00
|
|
|
|
const (
|
|
|
|
|
ShowMethod_Get = 1
|
|
|
|
|
ShowMethod_Post = 1 << 1
|
|
|
|
|
ShowMethod_Put = 1 << 2
|
|
|
|
|
ShowMethod_Delete = 1 << 3
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
|
type resourceRepoInfo struct {
|
2025-04-26 13:50:26 +08:00
|
|
|
|
Resource string
|
|
|
|
|
Desc string
|
|
|
|
|
Repo repo.ICommonResourceRepo
|
|
|
|
|
ShowMethods []string
|
|
|
|
|
showMethods int
|
2025-05-12 18:43:41 +08:00
|
|
|
|
GlobalBtns []*api.ResourceBtnInfo
|
|
|
|
|
RowBtns []*api.ResourceBtnInfo
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var commResourcesRepo = make([]*resourceRepoInfo, 0)
|
|
|
|
|
|
2025-05-12 18:43:41 +08:00
|
|
|
|
func r(resource, desc string, repo repo.ICommonResourceRepo, showMethods int) *resourceRepoInfo {
|
2025-04-26 13:50:26 +08:00
|
|
|
|
curRepo := &resourceRepoInfo{
|
2025-04-30 15:46:14 +08:00
|
|
|
|
Resource: resource,
|
|
|
|
|
Desc: desc,
|
|
|
|
|
Repo: repo,
|
|
|
|
|
ShowMethods: make([]string, 0),
|
2025-04-26 13:50:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2025-05-12 18:43:41 +08:00
|
|
|
|
|
|
|
|
|
return curRepo
|
2025-04-26 13:50:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
|
func findCommResourceRepo(resource string) repo.ICommonResourceRepo {
|
|
|
|
|
for _, v := range commResourcesRepo {
|
2025-04-26 13:50:26 +08:00
|
|
|
|
if v.Resource == resource {
|
|
|
|
|
return v.Repo
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-26 13:50:26 +08:00
|
|
|
|
panic(fmt.Errorf("not found Resource:%v", resource))
|
2025-04-24 20:39:31 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|