206 lines
6.1 KiB
Go
Raw Normal View History

2025-04-18 17:17:23 +08:00
package service
import (
2025-04-30 15:46:14 +08:00
"admin/apps/game/api"
2025-04-18 17:17:23 +08:00
"admin/apps/game/domain"
2025-04-24 20:39:31 +08:00
"admin/internal/consts"
2025-05-04 22:07:13 +08:00
"admin/internal/errcode"
2025-05-05 10:30:33 +08:00
"admin/internal/event"
2025-05-16 15:17:10 +08:00
dto2 "admin/internal/model/dto"
2025-04-18 17:17:23 +08:00
"context"
2025-05-04 22:07:13 +08:00
"encoding/json"
2025-04-18 17:17:23 +08:00
"gorm.io/gorm"
)
type Service struct {
2025-04-24 20:39:31 +08:00
db *gorm.DB
resourceSvc *domain.CommonResourceService
projectSvc *domain.ProjectService
2025-05-07 18:25:31 +08:00
cdkeySvc *domain.CDKeyService
2025-05-13 18:13:22 +08:00
accountSvc *domain.AccountService
2025-04-18 17:17:23 +08:00
}
2025-04-24 20:39:31 +08:00
func New(db *gorm.DB) (*Service, error) {
svc := &Service{
db: db,
resourceSvc: domain.NewCommonResourceService(db),
projectSvc: domain.NewProjectService(db),
2025-05-07 18:25:31 +08:00
cdkeySvc: domain.NewCDKeyService(db),
2025-05-13 18:13:22 +08:00
accountSvc: domain.NewAccountService(db),
2025-04-18 17:17:23 +08:00
}
2025-04-30 15:46:14 +08:00
api.RegisterGameApi(svc)
//err := svc.ensureProjectsDBData()
//if err != nil {
// return nil, err
//}
2025-04-24 20:39:31 +08:00
return svc, nil
2025-04-18 17:17:23 +08:00
}
2025-05-16 15:17:10 +08:00
func (svc *Service) CommonList(ctx context.Context, projectId int, resourceName string, params *dto2.CommonListReq) (*dto2.CommonDtoList, error) {
2025-05-04 22:07:13 +08:00
2025-05-16 15:17:10 +08:00
params.ParsedWhereConditions = &dto2.ListWhereConditionInfo{}
2025-05-04 22:07:13 +08:00
if params.WhereConditions != "" {
err := json.Unmarshal([]byte(params.WhereConditions), params.ParsedWhereConditions)
if err != nil {
return nil, errcode.New(errcode.ParamsInvalid, "unmarshal list condition:%v error:%v",
params.WhereConditions, err)
}
}
2025-04-24 20:39:31 +08:00
switch resourceName {
case consts.ResourcesName_Project:
default:
2025-05-16 15:17:10 +08:00
params.ParsedWhereConditions.Conditions = append([]*dto2.GetWhereCondition{&dto2.GetWhereCondition{
2025-05-04 22:07:13 +08:00
Key: "ProjectId",
Op: "eq",
Value1: projectId,
}}, params.ParsedWhereConditions.Conditions...)
2025-04-18 17:17:23 +08:00
}
2025-05-09 18:28:15 +08:00
totalCount, fieldsDescInfo, rows, err := svc.resourceSvc.List(projectId, resourceName, params)
2025-05-15 17:30:33 +08:00
itemBags, err := svc.projectSvc.GetAllItemBag(projectId)
if err != nil {
return nil, err
}
2025-05-16 15:17:10 +08:00
return &dto2.CommonDtoList{FieldsDesc: fieldsDescInfo, TotalCount: totalCount, Rows: rows, ItemBags: itemBags}, err
2025-04-18 17:17:23 +08:00
}
2025-04-24 20:39:31 +08:00
2025-05-16 15:17:10 +08:00
func (svc *Service) CommonPost(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) (dto2.CommonDtoValues, error) {
2025-04-24 20:39:31 +08:00
if resourceName != consts.ResourcesName_Project {
params["ProjectId"] = projectId
2025-04-18 17:17:23 +08:00
}
2025-05-05 10:30:33 +08:00
2025-05-19 17:51:09 +08:00
project, values, err := svc.resourceSvc.Create(projectId, resourceName, params)
if err != nil {
return nil, err
}
2025-05-05 10:30:33 +08:00
userId := ctx.Value("user_id").(int)
2025-05-19 17:51:09 +08:00
//userName := ctx.Value("user_name").(string)
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{params})
evPayload := &event.EventPayload_UserGameExecute{
UserId: userId,
ProjectId: projectId,
ProjectName: project.Po.Name,
OpResourceType: resourceDesc,
OpResourceGroup: "系统",
OpResourceKey: keyDesc,
Method: "新增",
SrcDataList: []any{params},
}
if resourceName != consts.ResourcesName_Project {
evPayload.OpResourceGroup = project.Po.Name
}
2025-05-05 10:30:33 +08:00
2025-05-19 17:51:09 +08:00
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
2025-05-05 10:30:33 +08:00
return values, err
2025-04-18 17:17:23 +08:00
}
2025-04-24 20:39:31 +08:00
2025-05-16 15:17:10 +08:00
func (svc *Service) CommonPut(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) error {
2025-04-24 20:39:31 +08:00
if resourceName != consts.ResourcesName_Project {
params["ProjectId"] = projectId
2025-04-18 17:17:23 +08:00
}
2025-05-19 17:51:09 +08:00
project, err := svc.resourceSvc.Edit(projectId, resourceName, params)
if err != nil {
return err
}
2025-05-05 10:30:33 +08:00
userId := ctx.Value("user_id").(int)
2025-05-19 17:51:09 +08:00
//userName := ctx.Value("user_name").(string)
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{params})
evPayload := &event.EventPayload_UserGameExecute{
UserId: userId,
ProjectId: projectId,
ProjectName: project.Po.Name,
OpResourceType: resourceDesc,
OpResourceGroup: "系统",
OpResourceKey: keyDesc,
Method: "编辑",
SrcDataList: []any{params},
}
if resourceName != consts.ResourcesName_Project {
evPayload.OpResourceGroup = project.Po.Name
}
2025-05-05 10:30:33 +08:00
2025-05-19 17:51:09 +08:00
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
2025-05-05 10:30:33 +08:00
return err
2025-04-24 20:39:31 +08:00
}
2025-04-30 15:46:14 +08:00
func (svc *Service) CommonDelete(ctx context.Context, projectId int, resourceName string, id int) error {
2025-05-19 17:51:09 +08:00
project, deletedEt, err := svc.resourceSvc.Delete(projectId, resourceName, id)
2025-05-10 10:18:06 +08:00
if err != nil {
return err
}
2025-05-12 18:43:41 +08:00
2025-05-05 10:30:33 +08:00
userId := ctx.Value("user_id").(int)
2025-05-19 17:51:09 +08:00
//userName := ctx.Value("user_name").(string)
delObj := deletedEt.ToCommonDto()
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{delObj})
evPayload := &event.EventPayload_UserGameExecute{
UserId: userId,
ProjectId: projectId,
ProjectName: project.Po.Name,
OpResourceType: resourceDesc,
OpResourceGroup: "系统",
OpResourceKey: keyDesc,
Method: "删除",
SrcDataList: []any{delObj},
}
if resourceName != consts.ResourcesName_Project {
evPayload.OpResourceGroup = project.Po.Name
}
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
2025-05-05 10:30:33 +08:00
return err
2025-04-24 20:39:31 +08:00
}
2025-05-16 15:17:10 +08:00
func (svc *Service) CommonRowsSelection(ctx context.Context, projectId int, resourceName string, param *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
2025-05-19 17:51:09 +08:00
project, rsp, err := svc.resourceSvc.RowsSelection(projectId, resourceName, param)
2025-05-12 18:43:41 +08:00
if err != nil {
2025-05-13 18:13:22 +08:00
return rsp, err
2025-05-12 18:43:41 +08:00
}
userId := ctx.Value("user_id").(int)
2025-05-19 17:51:09 +08:00
//userName := ctx.Value("user_name").(string)
srcDataList := make([]any, 0, len(param.Rows))
for _, v := range param.Rows {
srcDataList = append(srcDataList, v)
}
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, param.Rows)
btnInfo := svc.resourceSvc.GetResourceSpecBtnKey(resourceName, param.BtnKey)
evPayload := &event.EventPayload_UserGameExecute{
UserId: userId,
ProjectId: projectId,
ProjectName: project.Po.Name,
OpResourceType: resourceDesc,
OpResourceGroup: "系统",
OpResourceKey: keyDesc,
Method: param.BtnKey,
SrcDataList: srcDataList,
}
if btnInfo != nil {
evPayload.Method = btnInfo.Name
}
if resourceName != consts.ResourcesName_Project {
evPayload.OpResourceGroup = project.Po.Name
}
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
2025-05-12 18:43:41 +08:00
return rsp, err
}
2025-04-30 15:46:14 +08:00
func (svc *Service) GetSupportResourcesList(permissions []string) []*api.ResourceInitInfo {
return svc.resourceSvc.GetSupportResourcesList(permissions)
2025-04-18 17:17:23 +08:00
}