183 lines
7.0 KiB
Go
183 lines
7.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"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 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) 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 (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 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 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) Dictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]map[string]any, int64, error) {
|
|
items, total, err := s.Repo().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, details bool) (map[string]any, error) {
|
|
v, err := s.Repo().FindDictionary(ctx, id, typ, details)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dictionaryDTO(v), nil
|
|
}
|
|
func (s *SettingsService) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.Repo().CreateDictionary(ctx, v)
|
|
}
|
|
func (s *SettingsService) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
|
return s.Repo().UpdateDictionary(ctx, v)
|
|
}
|
|
func (s *SettingsService) DeleteDictionary(ctx context.Context, id uint) error {
|
|
return s.Repo().DeleteDictionary(ctx, id)
|
|
}
|
|
func (s *SettingsService) DictionaryDetails(ctx context.Context, page, size int, dictionaryID uint, label, value string) ([]map[string]any, int64, error) {
|
|
items, total, err := s.Repo().ListDictionaryDetails(ctx, page, size, dictionaryID, label, value)
|
|
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.Repo().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.Repo().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.Repo().CreateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *SettingsService) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
|
return s.Repo().UpdateDictionaryDetail(ctx, v)
|
|
}
|
|
func (s *SettingsService) DeleteDictionaryDetail(ctx context.Context, id uint) error {
|
|
return s.Repo().DeleteDictionaryDetail(ctx, id)
|
|
}
|
|
func (s *SettingsService) ImportDictionaryJSON(ctx context.Context, raw string) error {
|
|
var payload struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Status *bool `json:"status"`
|
|
Description string `json:"desc"`
|
|
Details []dto.DictionaryDetailRequest `json:"sysDictionaryDetails"`
|
|
}
|
|
if json.Unmarshal([]byte(raw), &payload) != nil || payload.Name == "" || payload.Type == "" {
|
|
return errors.New("JSON 格式错误")
|
|
}
|
|
dictionary := dictionaryDomain(&dto.DictionaryRequest{Name: payload.Name, Type: payload.Type, Status: payload.Status, Description: payload.Description})
|
|
if err := s.CreateDictionary(ctx, dictionary); err != nil {
|
|
return err
|
|
}
|
|
mapping := map[uint]uint{}
|
|
pending := append([]dto.DictionaryDetailRequest(nil), payload.Details...)
|
|
for len(pending) > 0 {
|
|
progress := false
|
|
next := pending[:0]
|
|
for _, item := range pending {
|
|
oldID := item.ID
|
|
if item.ParentID != nil && *item.ParentID != 0 {
|
|
parent, exists := mapping[*item.ParentID]
|
|
if !exists {
|
|
next = append(next, item)
|
|
continue
|
|
}
|
|
item.ParentID = &parent
|
|
}
|
|
value := detailDomain(&item)
|
|
value.ID = 0
|
|
value.DictionaryID = dictionary.ID
|
|
if err := s.CreateDictionaryDetail(ctx, value); err != nil {
|
|
return errors.New("导入详情失败")
|
|
}
|
|
if oldID != 0 {
|
|
mapping[oldID] = value.ID
|
|
}
|
|
progress = true
|
|
}
|
|
if !progress {
|
|
return errors.New("字典层级关系无效")
|
|
}
|
|
pending = next
|
|
}
|
|
return nil
|
|
}
|
|
func (s *SettingsService) DictionaryDetailsByParent(ctx context.Context, dictionaryID, parentID uint) ([]map[string]any, error) {
|
|
items, _, err := s.DictionaryDetails(ctx, 1, 10000, dictionaryID, "", "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := []map[string]any{}
|
|
for _, item := range items {
|
|
parent, _ := item["parentID"].(*uint)
|
|
if (parentID == 0 && parent == nil) || (parent != nil && *parent == parentID) {
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|