2025-10-07 11:48:29 +08:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-10-11 18:47:43 +08:00
|
|
|
"hm-server/app/main/model"
|
2025-10-07 11:48:29 +08:00
|
|
|
"hm-server/common/xerr"
|
|
|
|
|
|
2025-10-11 18:47:43 +08:00
|
|
|
"github.com/Masterminds/squirrel"
|
2025-10-07 11:48:29 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
|
"github.com/pkg/errors"
|
2025-10-11 18:47:43 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/mr"
|
2025-10-07 11:48:29 +08:00
|
|
|
|
|
|
|
|
"hm-server/app/main/api/internal/svc"
|
|
|
|
|
"hm-server/app/main/api/internal/types"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GetLinkDataLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGetLinkDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLinkDataLogic {
|
|
|
|
|
return &GetLinkDataLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *GetLinkDataLogic) GetLinkData(req *types.GetLinkDataReq) (resp *types.GetLinkDataResp, err error) {
|
|
|
|
|
agentLinkModel, err := l.svcCtx.AgentLinkModel.FindOneByLinkIdentifier(l.ctx, req.LinkIdentifier)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取代理链接数据, %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
productModel, err := l.svcCtx.ProductModel.FindOne(l.ctx, agentLinkModel.ProductId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取代理链接数据, %v", err)
|
|
|
|
|
}
|
2025-10-11 18:47:43 +08:00
|
|
|
|
|
|
|
|
// 查询产品关联的 feature
|
|
|
|
|
build := l.svcCtx.ProductFeatureModel.SelectBuilder().Where(squirrel.Eq{
|
|
|
|
|
"product_id": productModel.Id,
|
|
|
|
|
})
|
|
|
|
|
productFeatureAll, err := l.svcCtx.ProductFeatureModel.FindAll(l.ctx, build, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取代理链接数据, 查找产品关联错误: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 11:48:29 +08:00
|
|
|
var product types.Product
|
2025-10-11 18:47:43 +08:00
|
|
|
err = copier.Copy(&product, productModel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取代理链接数据, 产品信息结构体复制失败, %v", err)
|
|
|
|
|
}
|
2025-10-07 11:48:29 +08:00
|
|
|
product.SellPrice = agentLinkModel.Price
|
2025-10-11 18:47:43 +08:00
|
|
|
|
|
|
|
|
// 并发查询所有 feature 详情
|
|
|
|
|
mr.MapReduceVoid(func(source chan<- interface{}) {
|
|
|
|
|
for _, productFeature := range productFeatureAll {
|
|
|
|
|
source <- productFeature.FeatureId
|
|
|
|
|
}
|
|
|
|
|
}, func(item interface{}, writer mr.Writer[*model.Feature], cancel func(error)) {
|
|
|
|
|
id := item.(int64)
|
|
|
|
|
|
|
|
|
|
feature, findFeatureErr := l.svcCtx.FeatureModel.FindOne(l.ctx, id)
|
|
|
|
|
if findFeatureErr != nil {
|
|
|
|
|
logx.WithContext(l.ctx).Errorf("获取代理链接数据, 查找关联feature错误: %d, err:%v", id, findFeatureErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if feature != nil && feature.Id > 0 {
|
|
|
|
|
writer.Write(feature)
|
|
|
|
|
}
|
|
|
|
|
}, func(pipe <-chan *model.Feature, cancel func(error)) {
|
|
|
|
|
for item := range pipe {
|
|
|
|
|
var feature types.Feature
|
|
|
|
|
_ = copier.Copy(&feature, item)
|
|
|
|
|
product.Features = append(product.Features, feature)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-10-07 11:48:29 +08:00
|
|
|
return &types.GetLinkDataResp{
|
|
|
|
|
Product: product,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|