package service import ( "admin/apps/game/api" "admin/apps/game/domain" "admin/apps/game/model/dto" "admin/internal/consts" "admin/internal/errcode" "admin/internal/event" "context" "encoding/json" "gorm.io/gorm" ) type Service struct { db *gorm.DB resourceSvc *domain.CommonResourceService projectSvc *domain.ProjectService cdkeySvc *domain.CDKeyService } func New(db *gorm.DB) (*Service, error) { svc := &Service{ db: db, resourceSvc: domain.NewCommonResourceService(db), projectSvc: domain.NewProjectService(db), cdkeySvc: domain.NewCDKeyService(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 *dto.CommonListReq) (*dto.CommonDtoList, error) { 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) } } switch resourceName { case consts.ResourcesName_Project: default: params.ParsedWhereConditions.Conditions = append([]*dto.GetWhereCondition{&dto.GetWhereCondition{ Key: "ProjectId", Op: "eq", Value1: projectId, }}, params.ParsedWhereConditions.Conditions...) } totalCount, fieldsDescInfo, rows, err := svc.resourceSvc.List(projectId, resourceName, params) return &dto.CommonDtoList{FieldsDesc: fieldsDescInfo, TotalCount: totalCount, Rows: rows}, err } func (svc *Service) CommonPost(ctx context.Context, projectId int, resourceName string, params dto.CommonDtoValues) (dto.CommonDtoValues, error) { if resourceName != consts.ResourcesName_Project { params["ProjectId"] = projectId } 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 } func (svc *Service) CommonPut(ctx context.Context, projectId int, resourceName string, params dto.CommonDtoValues) error { if resourceName != consts.ResourcesName_Project { params["ProjectId"] = projectId } 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 } func (svc *Service) CommonDelete(ctx context.Context, projectId int, resourceName string, id int) error { 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) event.GetMgrInstance().Publish(event.EventTopic_UserExecute, &event.EventPayload_UserExecute{ UserId: userId, UserName: userName, ProjectId: projectId, Resource: resourceName, Method: "删除", NewData: deletedEt.ToCommonDto(), }) return err } func (svc *Service) GetSupportResourcesList(permissions []string) []*api.ResourceInitInfo { return svc.resourceSvc.GetSupportResourcesList(permissions) }