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/internal/consts"
|
|
|
|
|
"admin/internal/errcode"
|
2025-05-15 17:30:33 +08:00
|
|
|
|
"admin/internal/event"
|
2025-05-16 15:17:10 +08:00
|
|
|
|
dto2 "admin/internal/model/dto"
|
2025-05-09 18:28:15 +08:00
|
|
|
|
"admin/lib/xlog"
|
2025-05-15 17:30:33 +08:00
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
2025-04-24 20:39:31 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"gorm.io/gorm"
|
2025-05-15 17:30:33 +08:00
|
|
|
|
"reflect"
|
2025-05-19 17:51:09 +08:00
|
|
|
|
"strconv"
|
2025-05-09 18:28:15 +08:00
|
|
|
|
"strings"
|
2025-05-15 17:30:33 +08:00
|
|
|
|
"time"
|
2025-04-24 20:39:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CommonResourceService struct {
|
|
|
|
|
projectRepo repo.IProjectRepo
|
2025-06-06 18:30:12 +08:00
|
|
|
|
serverRepo repo.IServerRepo
|
|
|
|
|
noticeRepo repo.INoticeRepo
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 13:50:00 +08:00
|
|
|
|
func (svc *CommonResourceService) 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)
|
2025-06-09 13:50:00 +08:00
|
|
|
|
serverRepo.GlobalBtns = []*ResourceBtnInfo{
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Server_DownAll, Name: "一键停服", BtnColorType: "info"}, svc.handleServerUpOrDown},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Server_UpAll, Name: "一键起服", BtnColorType: "warning"}, svc.handleServerUpOrDown},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Server_ExportCdn, Name: "一键导出cdn", BtnColorType: "danger"}, svc.handleServerExportCdn},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Server_ExportCdn, Name: "预览导出cdn", BtnColorType: "default"}, svc.handleServerExportCdn},
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
2025-06-09 13:50:00 +08:00
|
|
|
|
serverRepo.RowBtns = []*ResourceBtnInfo{
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyRow_Server_Down, Name: "停服", BtnColorType: "info"}, svc.handleServerUpOrDown},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyRow_Server_Up, Name: "起服", BtnColorType: "warning"}, svc.handleServerUpOrDown},
|
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-05-15 17:30:33 +08:00
|
|
|
|
r(consts.ResourcesName_MailRole, "个人邮件", repo.NewCommonResourceRepo(db, &model.RoleMail{}), ShowMethod_Get|ShowMethod_Post) // 个人邮件发放就没法撤回?
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
globalMailRepo := r(consts.ResourcesName_MailGlobal, "全服邮件", repo.NewCommonResourceRepo(db, &model.GlobalMail{}), ShowMethod_Get|ShowMethod_Post)
|
|
|
|
|
globalMailRepo.HasDelayInvokeCreateHook = true
|
|
|
|
|
} // 直接删除,别修改了,玩家收到的更乱
|
|
|
|
|
|
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-14 18:09:20 +08:00
|
|
|
|
r(consts.ResourcesName_ItemBag, "道具礼包", repo.NewCommonResourceRepo(db, &model.ItemBag{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-06-06 18:30:12 +08:00
|
|
|
|
{
|
|
|
|
|
noticeRepo := r(consts.ResourcesName_Notice, "公告", repo.NewCommonResourceRepo(db, &model.Notice{}), ShowMethod_Get|ShowMethod_Post|ShowMethod_Put|ShowMethod_Delete)
|
2025-06-09 13:50:00 +08:00
|
|
|
|
noticeRepo.GlobalBtns = []*ResourceBtnInfo{
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Notice_DisableAll, Name: "一键禁用", BtnColorType: "info"}, svc.handleNoticeDisable},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Notice_EnableAll, Name: "一键启用", BtnColorType: "warning"}, svc.handleNoticeEnable},
|
2025-06-06 18:30:12 +08:00
|
|
|
|
}
|
2025-06-09 13:50:00 +08:00
|
|
|
|
noticeRepo.RowBtns = []*ResourceBtnInfo{
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Notice_Disable, Name: "禁用", BtnColorType: "info"}, svc.handleNoticeDisable},
|
|
|
|
|
{&api.ResourceBtnInfo{Key: consts.BtnKeyGlobal_Notice_Enable, Name: "启用", BtnColorType: "warning"}, svc.handleNoticeEnable},
|
2025-06-06 18:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
|
|
|
|
svc := &CommonResourceService{
|
|
|
|
|
projectRepo: repo.NewProjectRepo(db),
|
2025-06-06 18:30:12 +08:00
|
|
|
|
serverRepo: repo.NewServerRepo(db),
|
|
|
|
|
noticeRepo: repo.NewNoticeRepo(db),
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
2025-06-09 13:50:00 +08:00
|
|
|
|
svc.initCommonResourcesRepo(db)
|
2025-05-15 17:30:33 +08:00
|
|
|
|
svc.startEventSubscriber()
|
|
|
|
|
svc.startLoadAllDelayInvokeDbData()
|
2025-04-24 20:39:31 +08:00
|
|
|
|
return svc
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 17:30:33 +08:00
|
|
|
|
func (svc *CommonResourceService) startLoadAllDelayInvokeDbData() {
|
|
|
|
|
for _, repo := range commResourcesRepo {
|
|
|
|
|
if repo.HasDelayInvokeCreateHook {
|
|
|
|
|
repo.Repo.ListPagination("`delay_invoke_create_hook` is not NULL and `delay_invoke_create_hook` > ?", []any{time.Now()}, func(po model.IModel) {
|
|
|
|
|
var projectId int
|
|
|
|
|
var resource string = repo.Resource
|
|
|
|
|
var delayAt time.Time
|
|
|
|
|
//xlog.Infof("过滤数据:%+v", po)
|
|
|
|
|
switch repo.Resource {
|
|
|
|
|
case consts.ResourcesName_MailGlobal:
|
|
|
|
|
dbData := po.(*model.GlobalMail)
|
|
|
|
|
projectId = dbData.ProjectId
|
|
|
|
|
if !dbData.DelayInvokeCreateHook.Valid || dbData.DelayInvokeCreateHook.Time.Before(time.Now()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
delayAt = dbData.DelayInvokeCreateHook.Time
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 延迟执行钩子调用
|
|
|
|
|
xlog.Infof("起服查看全局邮件:%+v延迟到%v发送,添加到全局定时器。。", po, delayAt.Format(time.DateTime))
|
|
|
|
|
objBin, _ := json.Marshal(po)
|
|
|
|
|
payload := &event.EventPayload_DelayInvokeCreateHook{
|
|
|
|
|
ProjectId: projectId,
|
|
|
|
|
Resource: resource,
|
|
|
|
|
DelayAt: delayAt,
|
|
|
|
|
Obj: objBin,
|
|
|
|
|
}
|
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_DelayInvokeCreateHook, payload)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
|
func (svc *CommonResourceService) List(projectId int, resource string, listParams *dto2.CommonListReq) (int, []*dto2.CommonDtoFieldDesc, []dto2.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)
|
2025-05-15 17:30:33 +08:00
|
|
|
|
totalCount, fieldsDescInfo, etList, err := rRepo.Repo.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
|
|
|
|
}
|
2025-05-16 15:17:10 +08:00
|
|
|
|
retList := make([]dto2.CommonDtoValues, 0, len(etList))
|
2025-04-24 20:39:31 +08:00
|
|
|
|
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-05-16 15:17:10 +08:00
|
|
|
|
func (svc *CommonResourceService) GetById(projectId int, resource string, id int) ([]*dto2.CommonDtoFieldDesc, dto2.CommonDtoValues, *entity.CommonResource, bool, error) {
|
2025-04-30 15:46:14 +08:00
|
|
|
|
_, 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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 17:30:33 +08:00
|
|
|
|
fieldsDescInfo, et, find, err := findCommResourceRepo(resource).Repo.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-05-19 17:51:09 +08:00
|
|
|
|
func (svc *CommonResourceService) Create(projectId int, resource string, dtoObj dto2.CommonDtoValues) (*entity.Project, dto2.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-19 17:51:09 +08:00
|
|
|
|
return projectEt, 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-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
|
createOne := func(obj dto2.CommonDtoValues) (dto2.CommonDtoValues, error) {
|
2025-05-15 17:30:33 +08:00
|
|
|
|
resourceRepo := findCommResourceRepo(resource)
|
|
|
|
|
et, err := resourceRepo.Repo.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()
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
2025-05-15 17:30:33 +08:00
|
|
|
|
if resourceRepo.HasDelayInvokeCreateHook {
|
|
|
|
|
field := reflect.ValueOf(et.Po).Elem().FieldByName("DelayInvokeCreateHook")
|
|
|
|
|
delayAt := field.Interface().(sql.NullTime)
|
|
|
|
|
if delayAt.Valid && delayAt.Time.After(time.Now()) {
|
|
|
|
|
// 延迟执行钩子调用
|
|
|
|
|
objBin, _ := json.Marshal(et.Po)
|
|
|
|
|
payload := &event.EventPayload_DelayInvokeCreateHook{
|
|
|
|
|
ProjectId: projectId,
|
|
|
|
|
Resource: resource,
|
|
|
|
|
DelayAt: delayAt.Time,
|
|
|
|
|
Obj: objBin,
|
|
|
|
|
}
|
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_DelayInvokeCreateHook, payload)
|
|
|
|
|
return newObj, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 18:28:15 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
|
var newDtoObj dto2.CommonDtoValues
|
2025-05-09 18:28:15 +08:00
|
|
|
|
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 {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil, errcode.New(errcode.ParamsInvalid, "account empty:%+v", dtoObj)
|
2025-05-09 18:28:15 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
newDtoObj, err = createOne(dtoObj)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, newDtoObj, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
func (svc *CommonResourceService) Edit(projectId int, resource string, dtoObj dto2.CommonDtoValues) (*entity.Project, 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-19 17:51:09 +08:00
|
|
|
|
return projectEt, 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-19 17:51:09 +08:00
|
|
|
|
return projectEt, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 17:30:33 +08:00
|
|
|
|
err = findCommResourceRepo(resource).Repo.Edit(projectEt, dtoObj)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行各个项目特定的钩子方法
|
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 {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, err
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
func (svc *CommonResourceService) Delete(projectId int, resource string, id int) (*entity.Project, *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-19 17:51:09 +08:00
|
|
|
|
return projectEt, 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-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 17:30:33 +08:00
|
|
|
|
oldEt, find, err := findCommResourceRepo(resource).Repo.Delete(projectEt, id)
|
2025-04-24 20:39:31 +08:00
|
|
|
|
if err != nil {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, 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-19 17:51:09 +08:00
|
|
|
|
return projectEt, oldEt, err
|
2025-04-26 13:50:26 +08:00
|
|
|
|
}
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, oldEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-26 13:50:26 +08:00
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, oldEt, nil
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 17:51:09 +08:00
|
|
|
|
func (svc *CommonResourceService) RowsSelection(projectId int, resourceName string, params *dto2.CommonRowsSelectionReq) (*entity.Project, *dto2.CommonRowsSelectionRsp, error) {
|
2025-05-12 18:43:41 +08:00
|
|
|
|
_, projectEt, find, err := svc.projectRepo.GetById(projectId)
|
|
|
|
|
if err != nil {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil, err
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
if resourceName != consts.ResourcesName_Project && !find {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return projectEt, nil, errcode.New(errcode.ServerError, "not found project %v db data", projectId)
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
2025-06-06 18:30:12 +08:00
|
|
|
|
|
2025-06-09 13:50:00 +08:00
|
|
|
|
rRepo := findCommResourceRepo(resourceName)
|
|
|
|
|
btnInfo, find := rRepo.findRowsSelectionBtn(params.BtnKey)
|
|
|
|
|
if !find {
|
|
|
|
|
return projectEt, nil, errcode.New(errcode.ServerError, "not found project %v resource %v rows section btn:%v",
|
|
|
|
|
projectId, resourceName, params.BtnKey)
|
2025-05-19 17:51:09 +08:00
|
|
|
|
}
|
2025-06-09 13:50:00 +08:00
|
|
|
|
rsp, err := btnInfo.RowsSelectionHandler(projectEt, resourceName, params)
|
|
|
|
|
return projectEt, rsp, err
|
2025-05-19 17:51:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (svc *CommonResourceService) GetResourceKeyByDto(resource string, dtoObj []dto2.CommonDtoValues) (string, string) {
|
|
|
|
|
resourceInfo := findCommResourceRepo(resource)
|
|
|
|
|
desc := resourceInfo.Desc
|
|
|
|
|
etList := resourceInfo.Repo.MakeEntitiesByDtoList(dtoObj)
|
|
|
|
|
keyList := make([]string, 0, len(dtoObj))
|
|
|
|
|
for _, et := range etList {
|
|
|
|
|
po := et.Po.(model.IModel)
|
|
|
|
|
method := reflect.ValueOf(po).MethodByName("GetShowKey")
|
|
|
|
|
var key string
|
|
|
|
|
if !method.IsValid() {
|
|
|
|
|
key = strconv.Itoa(po.GetId())
|
|
|
|
|
} else {
|
|
|
|
|
rets := method.Call([]reflect.Value{})
|
|
|
|
|
key = rets[0].Interface().(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyList = append(keyList, key)
|
|
|
|
|
}
|
|
|
|
|
if len(keyList) > 1 {
|
|
|
|
|
return desc, "[" + strings.Join(keyList, ",") + "]"
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
2025-05-19 17:51:09 +08:00
|
|
|
|
return desc, strings.Join(keyList, ",")
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 13:50:00 +08:00
|
|
|
|
func (svc *CommonResourceService) GetResourceSpecBtnKey(resource string, btnKey string) *ResourceBtnInfo {
|
2025-05-19 17:51:09 +08:00
|
|
|
|
resourceInfo := findCommResourceRepo(resource)
|
|
|
|
|
for _, v := range resourceInfo.GlobalBtns {
|
|
|
|
|
if v.Key == btnKey {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, v := range resourceInfo.RowBtns {
|
|
|
|
|
if v.Key == btnKey {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2025-05-12 18:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
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-06-09 13:50:00 +08:00
|
|
|
|
initInfo := &api.ResourceInitInfo{
|
2025-04-26 13:50:26 +08:00
|
|
|
|
Resource: v.Resource,
|
|
|
|
|
Desc: v.Desc,
|
|
|
|
|
ShowMethods: v.ShowMethods,
|
2025-06-09 13:50:00 +08:00
|
|
|
|
}
|
|
|
|
|
for _, v := range v.GlobalBtns {
|
|
|
|
|
initInfo.GlobalBtns = append(initInfo.GlobalBtns, v.ResourceBtnInfo)
|
|
|
|
|
}
|
|
|
|
|
for _, v := range v.RowBtns {
|
|
|
|
|
initInfo.RowBtns = append(initInfo.RowBtns, v.ResourceBtnInfo)
|
|
|
|
|
}
|
|
|
|
|
list = append(list, initInfo)
|
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-06-09 13:50:00 +08:00
|
|
|
|
type ResourceBtnInfo struct {
|
|
|
|
|
*api.ResourceBtnInfo
|
|
|
|
|
RowsSelectionHandler func(projectEt *entity.Project, resourceName string, params *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
|
type resourceRepoInfo struct {
|
2025-05-15 17:30:33 +08:00
|
|
|
|
Resource string
|
|
|
|
|
Desc string
|
|
|
|
|
Repo repo.ICommonResourceRepo
|
|
|
|
|
ShowMethods []string
|
|
|
|
|
showMethods int
|
2025-06-09 13:50:00 +08:00
|
|
|
|
GlobalBtns []*ResourceBtnInfo
|
|
|
|
|
RowBtns []*ResourceBtnInfo
|
2025-05-15 17:30:33 +08:00
|
|
|
|
HasDelayInvokeCreateHook bool
|
2025-04-24 20:39:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 13:50:00 +08:00
|
|
|
|
func (repoInfo *resourceRepoInfo) findRowsSelectionBtn(clickedBtn string) (*ResourceBtnInfo, bool) {
|
|
|
|
|
for _, btn := range repoInfo.GlobalBtns {
|
|
|
|
|
if btn.Key == clickedBtn {
|
|
|
|
|
return btn, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, btn := range repoInfo.RowBtns {
|
|
|
|
|
if btn.Key == clickedBtn {
|
|
|
|
|
return btn, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
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-05-15 17:30:33 +08:00
|
|
|
|
func findCommResourceRepo(resource string) *resourceRepoInfo {
|
2025-04-24 20:39:31 +08:00
|
|
|
|
for _, v := range commResourcesRepo {
|
2025-04-26 13:50:26 +08:00
|
|
|
|
if v.Resource == resource {
|
2025-05-15 17:30:33 +08:00
|
|
|
|
return v
|
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
|
|
|
|
|
}
|