125 lines
3.5 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"
"admin/apps/game/model/dto"
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-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-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-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-04-30 15:46:14 +08:00
func (svc *Service) CommonList(ctx context.Context, projectId int, resourceName string, params *dto.CommonListReq) (*dto.CommonDtoList, error) {
2025-05-04 22:07:13 +08:00
params.ParsedWhereConditions = &dto.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)
}
}
2025-04-24 20:39:31 +08:00
switch resourceName {
case consts.ResourcesName_Project:
default:
2025-05-04 22:07:13 +08:00
params.ParsedWhereConditions.Conditions = append([]*dto.GetWhereCondition{&dto.GetWhereCondition{
Key: "ProjectId",
Op: "eq",
Value1: projectId,
}}, params.ParsedWhereConditions.Conditions...)
2025-04-18 17:17:23 +08:00
}
2025-05-04 22:07:13 +08:00
fieldsDescInfo, rows, err := svc.resourceSvc.List(projectId, resourceName, params)
2025-04-24 20:39:31 +08:00
return &dto.CommonDtoList{FieldsDesc: fieldsDescInfo, Rows: rows}, err
2025-04-18 17:17:23 +08:00
}
2025-04-24 20:39:31 +08:00
2025-04-30 15:46:14 +08:00
func (svc *Service) CommonPost(ctx context.Context, projectId int, resourceName string, params dto.CommonDtoValues) (dto.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
values, err := svc.resourceSvc.Create(projectId, resourceName, params)
userId := ctx.Value("user_id").(int)
userName := ctx.Value("user_name").(string)
event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{
UserId: userId,
UserName: userName,
ProjectId: projectId,
Resource: resourceName,
Method: "新增",
NewData: params,
})
return values, err
2025-04-18 17:17:23 +08:00
}
2025-04-24 20:39:31 +08:00
2025-04-30 15:46:14 +08:00
func (svc *Service) CommonPut(ctx context.Context, projectId int, resourceName string, params dto.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
err := svc.resourceSvc.Edit(projectId, resourceName, params)
userId := ctx.Value("user_id").(int)
userName := ctx.Value("user_name").(string)
event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{
UserId: userId,
UserName: userName,
ProjectId: projectId,
Resource: resourceName,
Method: "编辑",
NewData: params,
})
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-05 10:30:33 +08:00
err := svc.resourceSvc.Delete(projectId, resourceName, id)
userId := ctx.Value("user_id").(int)
userName := ctx.Value("user_name").(string)
event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{
UserId: userId,
UserName: userName,
ProjectId: projectId,
Resource: resourceName,
Method: "删除",
NewData: id,
})
return err
2025-04-24 20:39:31 +08:00
}
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
}