package subscribe import ( "context" "encoding/json" "strings" "github.com/perfect-panel/server/internal/model/subscribe" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/logger" "github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/pkg/xerr" "github.com/pkg/errors" ) type QuerySubscribeListLogic struct { logger.Logger ctx context.Context svcCtx *svc.ServiceContext } // Get subscribe list func NewQuerySubscribeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuerySubscribeListLogic { return &QuerySubscribeListLogic{ Logger: logger.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *QuerySubscribeListLogic) QuerySubscribeList(req *types.QuerySubscribeListRequest) (resp *types.QuerySubscribeListResponse, err error) { total, data, err := l.svcCtx.SubscribeModel.FilterList(l.ctx, &subscribe.FilterParams{ Page: 1, Size: 9999, Language: req.Language, Sell: true, DefaultLanguage: true, }) if err != nil { l.Errorw("[QuerySubscribeListLogic] Database Error", logger.Field("error", err.Error())) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "QuerySubscribeList error: %v", err.Error()) } resp = &types.QuerySubscribeListResponse{ Total: total, } list := make([]types.Subscribe, len(data)) for i, item := range data { var sub types.Subscribe tool.DeepCopy(&sub, item) if item.Discount != "" { var discount []types.SubscribeDiscount _ = json.Unmarshal([]byte(item.Discount), &discount) sub.Discount = discount } // 计算节点数量 var nodeIds []int64 var tags []string // 解析节点ID if item.Nodes != "" { nodeIds = tool.StringToInt64Slice(item.Nodes) } // 解析标签 if item.NodeTags != "" { tagStrs := strings.Split(item.NodeTags, ",") for _, tag := range tagStrs { if tag != "" { tags = append(tags, tag) } } } // 获取节点数量 nodeCount, err := l.svcCtx.NodeModel.CountNodesByIdsAndTags(l.ctx, nodeIds, tags) if err != nil { l.Errorw("[QuerySubscribeListLogic] Count nodes failed", logger.Field("error", err.Error()), logger.Field("subscribeId", item.Id)) nodeCount = 0 // 出错时设置为0 } sub.NodeCount = nodeCount list[i] = sub } resp.List = list return }