127 lines
5.1 KiB
Go
127 lines
5.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
func dictionaryDomain(value *dto.DictionaryRequest) *biz.Dictionary {
|
|
status := true
|
|
if value.Status != nil {
|
|
status = *value.Status
|
|
}
|
|
return &biz.Dictionary{ID: value.ID, Name: value.Name, Type: value.Type, Status: status, Desc: value.Description, ParentID: value.ParentID}
|
|
}
|
|
func (s *SettingsService) CreateDictionaryRequest(ctx context.Context, req *dto.DictionaryRequest) error {
|
|
return s.CreateDictionary(ctx, dictionaryDomain(req))
|
|
}
|
|
func (s *SettingsService) UpdateDictionaryRequest(ctx context.Context, req *dto.DictionaryRequest) error {
|
|
return s.UpdateDictionary(ctx, dictionaryDomain(req))
|
|
}
|
|
func dictionaryDTO(v *biz.Dictionary) map[string]any {
|
|
children := make([]map[string]any, 0, len(v.Children))
|
|
for _, x := range v.Children {
|
|
children = append(children, dictionaryDTO(x))
|
|
}
|
|
details := make([]map[string]any, 0, len(v.Details))
|
|
for _, x := range v.Details {
|
|
details = append(details, detailDTO(x))
|
|
}
|
|
return map[string]any{"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 *SettingsService) Dictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.ListDictionaries(ctx, page, size, name, typ, details)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, dictionaryDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *SettingsService) Dictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (map[string]any, error) {
|
|
v, err := s.uc.FindDictionary(ctx, id, typ, status, details)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dictionaryDTO(v), nil
|
|
}
|
|
func (s *SettingsService) ExportDictionary(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.ExportDictionary(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dictionaryDTO(v), nil
|
|
}
|
|
func (s *SettingsService) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.uc.CreateDictionary(ctx, v)
|
|
}
|
|
func (s *SettingsService) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.uc.UpdateDictionary(ctx, v)
|
|
}
|
|
func (s *SettingsService) DeleteDictionary(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteDictionary(ctx, id)
|
|
}
|
|
|
|
func detailDomain(value *dto.DictionaryDetailRequest) *biz.DictionaryDetail {
|
|
status := true
|
|
if value.Status != nil {
|
|
status = *value.Status
|
|
}
|
|
return &biz.DictionaryDetail{ID: value.ID, Label: value.Label, Value: value.Value, Extend: value.Extend, Status: status, Sort: value.Sort, DictionaryID: value.DictionaryID, ParentID: value.ParentID, Level: value.Level, Path: value.Path}
|
|
}
|
|
func (s *SettingsService) CreateDictionaryDetailRequest(ctx context.Context, req *dto.DictionaryDetailRequest) error {
|
|
return s.CreateDictionaryDetail(ctx, detailDomain(req))
|
|
}
|
|
func (s *SettingsService) UpdateDictionaryDetailRequest(ctx context.Context, req *dto.DictionaryDetailRequest) error {
|
|
return s.UpdateDictionaryDetail(ctx, detailDomain(req))
|
|
}
|
|
func detailDTO(v *biz.DictionaryDetail) map[string]any {
|
|
children := make([]map[string]any, 0, len(v.Children))
|
|
for _, x := range v.Children {
|
|
children = append(children, detailDTO(x))
|
|
}
|
|
return map[string]any{"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, "sysDictionaryID": v.DictionaryID, "parentID": v.ParentID, "level": v.Level, "path": v.Path, "disabled": !v.Status, "children": children}
|
|
}
|
|
func (s *SettingsService) DictionaryDetails(ctx context.Context, page, size int, filter biz.DictionaryDetailFilter) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.ListDictionaryDetails(ctx, page, size, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, detailDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *SettingsService) DictionaryDetail(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.FindDictionaryDetail(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return detailDTO(v), nil
|
|
}
|
|
func (s *SettingsService) DictionaryTree(ctx context.Context, id uint, typ string) ([]map[string]any, error) {
|
|
items, err := s.uc.DictionaryDetailTree(ctx, id, typ)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, detailDTO(v))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *SettingsService) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
|
return s.uc.CreateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *SettingsService) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
|
return s.uc.UpdateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *SettingsService) DeleteDictionaryDetail(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteDictionaryDetail(ctx, id)
|
|
}
|