206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
package service
|
|
|
|
import (
|
|
"admin/apps/game/api"
|
|
"admin/apps/game/domain"
|
|
"admin/internal/consts"
|
|
"admin/internal/errcode"
|
|
"admin/internal/event"
|
|
dto2 "admin/internal/model/dto"
|
|
"context"
|
|
"encoding/json"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Service struct {
|
|
db *gorm.DB
|
|
resourceSvc *domain.CommonResourceService
|
|
projectSvc *domain.ProjectService
|
|
cdkeySvc *domain.CDKeyService
|
|
accountSvc *domain.AccountService
|
|
}
|
|
|
|
func New(db *gorm.DB) (*Service, error) {
|
|
svc := &Service{
|
|
db: db,
|
|
resourceSvc: domain.NewCommonResourceService(db),
|
|
projectSvc: domain.NewProjectService(db),
|
|
cdkeySvc: domain.NewCDKeyService(db),
|
|
accountSvc: domain.NewAccountService(db),
|
|
}
|
|
api.RegisterGameApi(svc)
|
|
//err := svc.ensureProjectsDBData()
|
|
//if err != nil {
|
|
// return nil, err
|
|
//}
|
|
return svc, nil
|
|
}
|
|
|
|
func (svc *Service) CommonList(ctx context.Context, projectId int, resourceName string, params *dto2.CommonListReq) (*dto2.CommonDtoList, error) {
|
|
|
|
params.ParsedWhereConditions = &dto2.ListWhereConditionInfo{}
|
|
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)
|
|
}
|
|
}
|
|
|
|
switch resourceName {
|
|
case consts.ResourcesName_Project:
|
|
default:
|
|
params.ParsedWhereConditions.Conditions = append([]*dto2.GetWhereCondition{&dto2.GetWhereCondition{
|
|
Key: "ProjectId",
|
|
Op: "eq",
|
|
Value1: projectId,
|
|
}}, params.ParsedWhereConditions.Conditions...)
|
|
}
|
|
totalCount, fieldsDescInfo, rows, err := svc.resourceSvc.List(projectId, resourceName, params)
|
|
itemBags, err := svc.projectSvc.GetAllItemBag(projectId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto2.CommonDtoList{FieldsDesc: fieldsDescInfo, TotalCount: totalCount, Rows: rows, ItemBags: itemBags}, err
|
|
}
|
|
|
|
func (svc *Service) CommonPost(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) (dto2.CommonDtoValues, error) {
|
|
if resourceName != consts.ResourcesName_Project {
|
|
params["ProjectId"] = projectId
|
|
}
|
|
|
|
project, values, err := svc.resourceSvc.Create(projectId, resourceName, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//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
|
|
}
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return values, err
|
|
}
|
|
|
|
func (svc *Service) CommonPut(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) error {
|
|
if resourceName != consts.ResourcesName_Project {
|
|
params["ProjectId"] = projectId
|
|
}
|
|
project, err := svc.resourceSvc.Edit(projectId, resourceName, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//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
|
|
}
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return err
|
|
}
|
|
|
|
func (svc *Service) CommonDelete(ctx context.Context, projectId int, resourceName string, id int) error {
|
|
project, deletedEt, err := svc.resourceSvc.Delete(projectId, resourceName, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//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)
|
|
|
|
return err
|
|
}
|
|
|
|
func (svc *Service) CommonRowsSelection(ctx context.Context, projectId int, resourceName string, param *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
|
|
project, rsp, err := svc.resourceSvc.RowsSelection(projectId, resourceName, param)
|
|
if err != nil {
|
|
return rsp, err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//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)
|
|
|
|
return rsp, err
|
|
}
|
|
|
|
func (svc *Service) GetSupportResourcesList(permissions []string) []*api.ResourceInitInfo {
|
|
return svc.resourceSvc.GetSupportResourcesList(permissions)
|
|
}
|