2025-04-30 15:46:14 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
apiUser "admin/apps/user/api"
|
|
|
|
"admin/apps/user/domain"
|
|
|
|
"admin/internal/context"
|
2025-05-16 15:17:10 +08:00
|
|
|
"admin/internal/errcode"
|
|
|
|
dto2 "admin/internal/model/dto"
|
|
|
|
"encoding/json"
|
2025-04-30 15:46:14 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
db *gorm.DB
|
|
|
|
resourceSvc *domain.CommonResourceService
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(db *gorm.DB) (*Service, error) {
|
|
|
|
svc := &Service{
|
|
|
|
db: db,
|
|
|
|
resourceSvc: domain.NewCommonResourceService(db),
|
|
|
|
}
|
|
|
|
err := svc.resourceSvc.EnsureInitDB()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apiUser.RegisterUserApiHandler(svc)
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
func (svc *Service) CommonList(ctx *context.WebContext, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalCount, fieldsDescInfo, rows, err := svc.resourceSvc.List(resourceName, params)
|
|
|
|
return &dto2.CommonDtoList{FieldsDesc: fieldsDescInfo, Rows: rows, TotalCount: totalCount}, err
|
2025-04-30 15:46:14 +08:00
|
|
|
}
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
func (svc *Service) CommonPost(ctx *context.WebContext, resourceName string, params dto2.CommonDtoValues) (dto2.CommonDtoValues, error) {
|
2025-04-30 15:46:14 +08:00
|
|
|
values, _, err := svc.resourceSvc.Create(resourceName, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return values, err
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
func (svc *Service) CommonPut(ctx *context.WebContext, resourceName string, params dto2.CommonDtoValues) error {
|
2025-04-30 15:46:14 +08:00
|
|
|
return svc.resourceSvc.Edit(resourceName, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) CommonDelete(ctx *context.WebContext, resourceName string, id int) error {
|
|
|
|
return svc.resourceSvc.Delete(resourceName, id)
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:17:10 +08:00
|
|
|
func (svc *Service) GetSupportResourcesList() []*dto2.ResourceInitInfo {
|
2025-04-30 15:46:14 +08:00
|
|
|
return svc.resourceSvc.GetSupportResourcesList()
|
|
|
|
}
|