fix: align admin behavior and Kratos integrations
This commit is contained in:
@@ -2,8 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service/dto"
|
||||
@@ -16,25 +14,12 @@ func dictionaryDomain(value *dto.DictionaryRequest) *biz.Dictionary {
|
||||
}
|
||||
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 {
|
||||
@@ -46,15 +31,8 @@ func dictionaryDTO(v *biz.Dictionary) map[string]any {
|
||||
}
|
||||
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)
|
||||
items, total, err := s.uc.ListDictionaries(ctx, page, size, name, typ, details)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -64,24 +42,52 @@ func (s *SettingsService) Dictionaries(ctx context.Context, page, size int, name
|
||||
}
|
||||
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)
|
||||
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.Repo().CreateDictionary(ctx, v)
|
||||
return s.uc.CreateDictionary(ctx, v)
|
||||
}
|
||||
func (s *SettingsService) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
|
||||
return s.Repo().UpdateDictionary(ctx, v)
|
||||
return s.uc.UpdateDictionary(ctx, v)
|
||||
}
|
||||
func (s *SettingsService) DeleteDictionary(ctx context.Context, id uint) error {
|
||||
return s.Repo().DeleteDictionary(ctx, id)
|
||||
return s.uc.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)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -92,14 +98,14 @@ func (s *SettingsService) DictionaryDetails(ctx context.Context, page, size int,
|
||||
return out, total, nil
|
||||
}
|
||||
func (s *SettingsService) DictionaryDetail(ctx context.Context, id uint) (map[string]any, error) {
|
||||
v, err := s.Repo().FindDictionaryDetail(ctx, id)
|
||||
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.Repo().DictionaryDetailTree(ctx, id, typ)
|
||||
items, err := s.uc.DictionaryDetailTree(ctx, id, typ)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -110,73 +116,11 @@ func (s *SettingsService) DictionaryTree(ctx context.Context, id uint, typ strin
|
||||
return out, nil
|
||||
}
|
||||
func (s *SettingsService) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
||||
return s.Repo().CreateDictionaryDetail(ctx, v)
|
||||
return s.uc.CreateDictionaryDetail(ctx, v)
|
||||
}
|
||||
func (s *SettingsService) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
|
||||
return s.Repo().UpdateDictionaryDetail(ctx, v)
|
||||
return s.uc.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
|
||||
return s.uc.DeleteDictionaryDetail(ctx, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user