2025-04-18 17:17:23 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"admin/apps/game/domain"
|
|
|
|
"admin/apps/game/model/dto"
|
2025-04-24 20:39:31 +08:00
|
|
|
"admin/internal/consts"
|
2025-04-18 17:17:23 +08:00
|
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2025-04-24 20:39:31 +08:00
|
|
|
db *gorm.DB
|
|
|
|
resourceSvc *domain.CommonResourceService
|
|
|
|
projectSvc *domain.ProjectService
|
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-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
err := svc.ensureProjectsDBData()
|
2025-04-18 17:17:23 +08:00
|
|
|
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-24 20:39:31 +08:00
|
|
|
func (svc *Service) CommonList(ctx context.Context, projectId string, resourceName string, params *dto.CommonListReq) (*dto.CommonDtoList, error) {
|
|
|
|
var (
|
|
|
|
query string
|
|
|
|
args []any
|
|
|
|
)
|
|
|
|
switch resourceName {
|
|
|
|
case consts.ResourcesName_Project:
|
|
|
|
default:
|
|
|
|
query = "project_id = ?"
|
|
|
|
args = append(args, projectId)
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
fieldsDescInfo, rows, err := svc.resourceSvc.List(projectId, resourceName, params.PageNo, params.PageLen, query, args)
|
|
|
|
return &dto.CommonDtoList{FieldsDesc: fieldsDescInfo, Rows: rows}, err
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
|
|
|
|
func (svc *Service) CommonPost(ctx context.Context, projectId string, resourceName string, params dto.CommonDtoValues) (dto.CommonDtoValues, error) {
|
|
|
|
if resourceName != consts.ResourcesName_Project {
|
|
|
|
params["ProjectId"] = projectId
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
return svc.resourceSvc.Create(projectId, resourceName, params)
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
|
|
|
|
func (svc *Service) CommonPut(ctx context.Context, projectId string, resourceName string, params dto.CommonDtoValues) error {
|
|
|
|
if resourceName != consts.ResourcesName_Project {
|
|
|
|
params["ProjectId"] = projectId
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|
2025-04-24 20:39:31 +08:00
|
|
|
return svc.resourceSvc.Edit(projectId, resourceName, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) CommonDelete(ctx context.Context, projectId string, resourceName string, id int) error {
|
|
|
|
return svc.resourceSvc.Delete(projectId, resourceName, id)
|
|
|
|
}
|
|
|
|
|
2025-04-26 13:50:26 +08:00
|
|
|
func (svc *Service) GetSupportResourcesList() []*dto.ResourceInitInfo {
|
2025-04-24 20:39:31 +08:00
|
|
|
return svc.resourceSvc.GetSupportResourcesList()
|
2025-04-18 17:17:23 +08:00
|
|
|
}
|