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-05-13 18:13:22 +08:00
|
|
|
"fmt"
|
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
|
|
|
|
|
|
|
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-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-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-10 10:18:06 +08:00
|
|
|
deletedEt, err := svc.resourceSvc.Delete(projectId, resourceName, id)
|
|
|
|
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)
|
|
|
|
userName := ctx.Value("user_name").(string)
|
|
|
|
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{
|
|
|
|
UserId: userId,
|
|
|
|
UserName: userName,
|
|
|
|
ProjectId: projectId,
|
|
|
|
Resource: resourceName,
|
|
|
|
Method: "删除",
|
2025-05-10 10:18:06 +08:00
|
|
|
NewData: deletedEt.ToCommonDto(),
|
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-12 18:43:41 +08:00
|
|
|
rsp, err := svc.resourceSvc.RowsSelection(projectId, resourceName, param)
|
|
|
|
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)
|
|
|
|
userName := ctx.Value("user_name").(string)
|
|
|
|
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{
|
|
|
|
UserId: userId,
|
|
|
|
UserName: userName,
|
|
|
|
ProjectId: projectId,
|
|
|
|
Resource: resourceName,
|
2025-05-13 18:13:22 +08:00
|
|
|
Method: fmt.Sprintf("选择行:%v", param.BtnKey),
|
2025-05-12 18:43:41 +08:00
|
|
|
Any: param,
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
}
|