Files
ocean/gateway/internal/logic/getsystemcategorieslogic.go

98 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"context"
"strings"
"time"
"godemo/category/category"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type GetSystemCategoriesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetSystemCategoriesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSystemCategoriesLogic {
return &GetSystemCategoriesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetSystemCategoriesLogic) GetSystemCategories(req *types.GetSystemCategoriesReq) (resp *types.ListCategoriesResp, err error) {
// 1. 验证系统ID
if strings.TrimSpace(req.SystemID) == "" {
return nil, status.Error(codes.InvalidArgument, "system_id is required")
}
// 2. 准备 RPC 请求
rpcReq := &category.GetSystemCategoriesRequest{
SystemId: req.SystemID,
IncludeDescendants: req.IncludeDescendants,
}
// 添加父分类ID过滤如果提供
if req.ParentID != "" {
rpcReq.ParentId = req.ParentID
}
// 3. 调用 RPC 服务
rpcResp, rpcErr := l.svcCtx.CategoryRpc.GetSystemCategories(l.ctx, rpcReq)
if rpcErr != nil {
st, ok := status.FromError(rpcErr)
if ok && st.Code() == codes.NotFound {
// 返回空列表而不是错误
return &types.ListCategoriesResp{
Total: 0,
List: []types.BaseCategory{},
}, nil
}
l.Logger.Errorf("RPC error: %v", rpcErr)
return nil, rpcErr
}
// 4. 转换响应数据
categories := make([]types.BaseCategory, 0, len(rpcResp.Categories))
for _, cat := range rpcResp.Categories {
// 转换时间格式
createdAt := ""
if cat.CreatedAt > 0 {
createdAt = time.Unix(cat.CreatedAt, 0).Format(time.RFC3339)
}
updatedAt := ""
if cat.UpdatedAt > 0 {
updatedAt = time.Unix(cat.UpdatedAt, 0).Format(time.RFC3339)
}
categories = append(categories, types.BaseCategory{
ID: cat.Id,
SystemID: cat.SystemId,
Name: cat.Name,
Alias: cat.Alias,
ParentID: cat.ParentId,
Description: cat.Description,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
})
}
// 5. 构建响应
resp = &types.ListCategoriesResp{
Total: rpcResp.Total,
List: categories,
}
l.Logger.Infof("Retrieved %d categories for system %s (parent: %s)", len(categories), req.SystemID, req.ParentID)
return resp, nil
}