173 lines
6.8 KiB
Go
173 lines
6.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz"
|
|
)
|
|
|
|
type DictionaryService struct{ uc *biz.DictionaryUsecase }
|
|
|
|
func NewDictionaryService(uc *biz.DictionaryUsecase) *DictionaryService {
|
|
return &DictionaryService{uc: uc}
|
|
}
|
|
|
|
func dictionaryDomain(value *DictionaryRequest) *biz.Dictionary {
|
|
return &biz.Dictionary{ID: value.ID, Name: value.Name, Type: value.Type, Status: value.Status, Desc: value.Description, ParentID: value.ParentID}
|
|
}
|
|
func (s *DictionaryService) CreateDictionaryRequest(ctx context.Context, req *DictionaryRequest) (*DictionaryResponse, error) {
|
|
value := dictionaryDomain(req)
|
|
if err := s.CreateDictionary(ctx, value); err != nil {
|
|
return nil, err
|
|
}
|
|
return dictionaryDTO(value), nil
|
|
}
|
|
func (s *DictionaryService) UpdateDictionaryRequest(ctx context.Context, req *DictionaryRequest) error {
|
|
return s.UpdateDictionary(ctx, dictionaryDomain(req))
|
|
}
|
|
func dictionaryDTO(v *biz.Dictionary) *DictionaryResponse {
|
|
var children []*DictionaryResponse
|
|
if v.Children != nil {
|
|
children = make([]*DictionaryResponse, 0, len(v.Children))
|
|
for _, x := range v.Children {
|
|
children = append(children, dictionaryDTO(x))
|
|
}
|
|
}
|
|
var details []*DictionaryDetailResponse
|
|
if v.Details != nil {
|
|
details = make([]*DictionaryDetailResponse, 0, len(v.Details))
|
|
for _, x := range v.Details {
|
|
details = append(details, detailDTO(x))
|
|
}
|
|
}
|
|
return &DictionaryResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Name: v.Name, Type: v.Type, Status: v.Status, Desc: v.Desc, ParentID: v.ParentID, Children: children, SysDictionaryDetails: details}
|
|
}
|
|
func (s *DictionaryService) Dictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]*DictionaryResponse, int64, error) {
|
|
items, total, err := s.uc.ListDictionaries(ctx, page, size, name, typ, details)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*DictionaryResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, dictionaryDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *DictionaryService) Dictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (*DictionaryResponse, error) {
|
|
v, err := s.uc.FindDictionary(ctx, id, typ, status, details)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dictionaryDTO(v), nil
|
|
}
|
|
func (s *DictionaryService) ExportDictionary(ctx context.Context, id uint) (*DictionaryExportResponse, error) {
|
|
v, err := s.uc.ExportDictionary(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var details []*DictionaryDetailExportResponse
|
|
for _, detail := range v.Details {
|
|
details = append(details, &DictionaryDetailExportResponse{
|
|
Label: detail.Label, Value: detail.Value, Extend: detail.Extend,
|
|
Status: detail.Status, Sort: detail.Sort, Level: detail.Level, Path: detail.Path,
|
|
})
|
|
}
|
|
return &DictionaryExportResponse{
|
|
Name: v.Name, Type: v.Type, Status: v.Status, Desc: v.Desc,
|
|
SysDictionaryDetails: details,
|
|
}, nil
|
|
}
|
|
func (s *DictionaryService) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.uc.CreateDictionary(ctx, v)
|
|
}
|
|
func (s *DictionaryService) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.uc.UpdateDictionary(ctx, v)
|
|
}
|
|
func (s *DictionaryService) DeleteDictionary(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteDictionary(ctx, id)
|
|
}
|
|
|
|
func detailDomain(value *DictionaryDetailRequest) *biz.DictionaryDetail {
|
|
return &biz.DictionaryDetail{ID: value.ID, Label: value.Label, Value: value.Value, Extend: value.Extend, Status: value.Status, Sort: value.Sort, DictionaryID: value.DictionaryID, ParentID: value.ParentID, Level: value.Level, Path: value.Path}
|
|
}
|
|
func (s *DictionaryService) CreateDictionaryDetailRequest(ctx context.Context, req *DictionaryDetailRequest) error {
|
|
return s.CreateDictionaryDetail(ctx, detailDomain(req))
|
|
}
|
|
func (s *DictionaryService) UpdateDictionaryDetailRequest(ctx context.Context, req *DictionaryDetailRequest) error {
|
|
return s.UpdateDictionaryDetail(ctx, detailDomain(req))
|
|
}
|
|
func detailDTO(v *biz.DictionaryDetail) *DictionaryDetailResponse {
|
|
var children []*DictionaryDetailResponse
|
|
if v.Children != nil {
|
|
children = make([]*DictionaryDetailResponse, 0, len(v.Children))
|
|
for _, x := range v.Children {
|
|
children = append(children, detailDTO(x))
|
|
}
|
|
}
|
|
disabled := v.Status != nil && !*v.Status
|
|
return &DictionaryDetailResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Label: v.Label, Value: v.Value, Extend: v.Extend, Status: v.Status, Sort: v.Sort, DictionaryID: v.DictionaryID, ParentID: v.ParentID, Level: v.Level, Path: v.Path, Disabled: disabled, Children: children}
|
|
}
|
|
func (s *DictionaryService) DictionaryDetails(ctx context.Context, page, size int, filter biz.DictionaryDetailFilter) ([]*DictionaryDetailResponse, int64, error) {
|
|
items, total, err := s.uc.ListDictionaryDetails(ctx, page, size, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*DictionaryDetailResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, detailDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *DictionaryService) DictionaryDetailsRequest(ctx context.Context, page, size int, request DictionaryDetailListRequest) ([]*DictionaryDetailResponse, int64, error) {
|
|
return s.DictionaryDetails(ctx, page, size, biz.DictionaryDetailFilter{
|
|
DictionaryID: request.DictionaryID,
|
|
Label: request.Label,
|
|
Value: request.Value,
|
|
Status: request.Status,
|
|
ParentID: request.ParentID,
|
|
Level: request.Level,
|
|
})
|
|
}
|
|
func (s *DictionaryService) DictionaryDetail(ctx context.Context, id uint) (*DictionaryDetailResponse, error) {
|
|
v, err := s.uc.FindDictionaryDetail(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return detailDTO(v), nil
|
|
}
|
|
func (s *DictionaryService) DictionaryPath(ctx context.Context, id uint) ([]*DictionaryDetailResponse, error) {
|
|
path := make([]*DictionaryDetailResponse, 0)
|
|
for id != 0 {
|
|
value, err := s.uc.FindDictionaryDetail(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
path = append([]*DictionaryDetailResponse{detailDTO(value)}, path...)
|
|
if value.ParentID == nil {
|
|
break
|
|
}
|
|
id = *value.ParentID
|
|
}
|
|
return path, nil
|
|
}
|
|
func (s *DictionaryService) DictionaryTree(ctx context.Context, id uint, typ string) ([]*DictionaryDetailResponse, error) {
|
|
items, err := s.uc.DictionaryDetailTree(ctx, id, typ)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]*DictionaryDetailResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, detailDTO(v))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *DictionaryService) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
|
return s.uc.CreateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *DictionaryService) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
|
return s.uc.UpdateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *DictionaryService) DeleteDictionaryDetail(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteDictionaryDetail(ctx, id)
|
|
}
|