package complete import ( "context" "time" filePb "sundynix-micro-go/app/file/rpc/file" "sundynix-micro-go/app/plant/api/internal/svc" plantModel "sundynix-micro-go/app/plant/model" "github.com/zeromicro/go-zero/core/logx" ) type GetBadgeConfigTreeLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetBadgeConfigTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBadgeConfigTreeLogic { return &GetBadgeConfigTreeLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } func (l *GetBadgeConfigTreeLogic) GetBadgeConfigTree() (interface{}, error) { var allBadges []plantModel.BadgeConfig err := l.svcCtx.DB.Order("dimension desc, group_id asc, tier asc, sort asc").Find(&allBadges).Error if err != nil { return nil, err } iconIds := make([]string, 0) iconIdMap := make(map[string]bool) for _, b := range allBadges { if b.IconID != "" && !iconIdMap[b.IconID] { iconIdMap[b.IconID] = true iconIds = append(iconIds, b.IconID) } } fileMap := make(map[string]map[string]interface{}) if len(iconIds) > 0 { resp, err := l.svcCtx.FileRpc.GetFilesByIds(l.ctx, &filePb.GetFilesByIdsReq{Ids: iconIds}) if err == nil && resp != nil { for _, f := range resp.Files { fileMap[f.Id] = map[string]interface{}{ "id": f.Id, "name": f.Name, "url": f.Url, "tag": f.Tag, "key": f.Key, "suffix": f.Suffix, "md5": f.Md5, "createdAt": time.Unix(f.CreatedAt, 0).Format(time.RFC3339), } } } } dimLabelMap := map[string]string{ "PERSISTENCE": "勤勉成就", "EXPERTISE": "专家成就", "JOURNAL": "岁月记录", "DISCOVERY": "探索发现", } groupLabelMap := map[string]string{ "water_master": "雨露均沾", "alive_master": "长情陪伴", "fert_master": "炼金术士", "prune_master": "园艺理发师", "repot_master": "乔迁之喜", "doctor_master": "植物医生", "photo_master": "光影捕手", "night_owl": "守夜人", } dimOrder := []string{"PERSISTENCE", "EXPERTISE", "JOURNAL", "DISCOVERY"} // Build dimension -> groupId -> badge list tempMap := make(map[string]map[string][]map[string]interface{}) for _, b := range allBadges { dim := b.Dimension group := b.GroupID badgeData := map[string]interface{}{ "id": b.ID, "name": b.Name, "description": b.Description, "iconId": b.IconID, "dimension": b.Dimension, "groupId": b.GroupID, "tier": b.Tier, "targetAction": b.TargetAction, "threshold": b.Threshold, "comparator": b.Comparator, "rewardSunlight": b.RewardSunlight, "sort": b.Sort, "createdAt": b.CreatedAt.Format("2006-01-02 15:04:05"), } if iconData, ok := fileMap[b.IconID]; ok { badgeData["icon"] = iconData } else { badgeData["icon"] = nil } if tempMap[dim] == nil { tempMap[dim] = make(map[string][]map[string]interface{}) } tempMap[dim][group] = append(tempMap[dim][group], badgeData) } var tree []map[string]interface{} for _, dimKey := range dimOrder { if groupMap, exists := tempMap[dimKey]; exists { var groupNodes []map[string]interface{} for groupKey, badges := range groupMap { gLabel := groupLabelMap[groupKey] if gLabel == "" { gLabel = groupKey } groupNodes = append(groupNodes, map[string]interface{}{ "groupId": groupKey, "groupLabel": gLabel, "badges": badges, }) } dLabel := dimLabelMap[dimKey] if dLabel == "" { dLabel = dimKey } tree = append(tree, map[string]interface{}{ "dimension": dimKey, "label": dLabel, "groups": groupNodes, }) delete(tempMap, dimKey) } } // Handle leftover dimensions for dimKey, groupMap := range tempMap { var groupNodes []map[string]interface{} for groupKey, badges := range groupMap { gLabel := groupLabelMap[groupKey] if gLabel == "" { gLabel = groupKey } groupNodes = append(groupNodes, map[string]interface{}{ "groupId": groupKey, "groupLabel": gLabel, "badges": badges, }) } tree = append(tree, map[string]interface{}{ "dimension": dimKey, "label": dimKey, "groups": groupNodes, }) } return tree, nil }