From f6d3b806597479eca126df3035a510a378f12993 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 2 Mar 2026 14:41:13 +0800 Subject: [PATCH] f --- app/main/api/desc/admin/admin_product.api | 1 + .../admin_product/admingetproductlistlogic.go | 108 +++++++++++++++--- app/main/api/internal/types/types.go | 1 + 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/app/main/api/desc/admin/admin_product.api b/app/main/api/desc/admin/admin_product.api index 9aebc52..c35a76c 100644 --- a/app/main/api/desc/admin/admin_product.api +++ b/app/main/api/desc/admin/admin_product.api @@ -98,6 +98,7 @@ type ( Description string `json:"description"` // 描述 Notes string `json:"notes"` // 备注 SellPrice float64 `json:"sell_price"` // 售价 + CostPrice float64 `json:"cost_price"` // 成本价(由功能成本累加得出) CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间 } diff --git a/app/main/api/internal/logic/admin_product/admingetproductlistlogic.go b/app/main/api/internal/logic/admin_product/admingetproductlistlogic.go index 6c5c536..4d5e510 100644 --- a/app/main/api/internal/logic/admin_product/admingetproductlistlogic.go +++ b/app/main/api/internal/logic/admin_product/admingetproductlistlogic.go @@ -2,12 +2,13 @@ package admin_product import ( "context" + + "github.com/Masterminds/squirrel" + "github.com/pkg/errors" + "github.com/zeromicro/go-zero/core/logx" "ycc-server/app/main/api/internal/svc" "ycc-server/app/main/api/internal/types" "ycc-server/common/xerr" - - "github.com/pkg/errors" - "github.com/zeromicro/go-zero/core/logx" ) type AdminGetProductListLogic struct { @@ -25,28 +26,106 @@ func NewAdminGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext } func (l *AdminGetProductListLogic) AdminGetProductList(req *types.AdminGetProductListReq) (resp *types.AdminGetProductListResp, err error) { - // 1. 构建查询条件 - builder := l.svcCtx.ProductModel.SelectBuilder() + // 1. 构建产品查询条件 + productBuilder := l.svcCtx.ProductModel.SelectBuilder() // 2. 添加查询条件 if req.ProductName != nil && *req.ProductName != "" { - builder = builder.Where("product_name LIKE ?", "%"+*req.ProductName+"%") + productBuilder = productBuilder.Where("product_name LIKE ?", "%"+*req.ProductName+"%") } if req.ProductEn != nil && *req.ProductEn != "" { - builder = builder.Where("product_en LIKE ?", "%"+*req.ProductEn+"%") + productBuilder = productBuilder.Where("product_en LIKE ?", "%"+*req.ProductEn+"%") } - // 3. 执行分页查询 - list, total, err := l.svcCtx.ProductModel.FindPageListByPageWithTotal( - l.ctx, builder, req.Page, req.PageSize, "id DESC") + // 3. 执行产品分页查询 + productList, total, err := l.svcCtx.ProductModel.FindPageListByPageWithTotal( + l.ctx, productBuilder, req.Page, req.PageSize, "id DESC") if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询产品列表失败, err: %v, req: %+v", err, req) } - // 4. 构建响应列表 - items := make([]types.ProductListItem, 0, len(list)) - for _, item := range list { + if len(productList) == 0 { + return &types.AdminGetProductListResp{ + Total: total, + Items: []types.ProductListItem{}, + }, nil + } + + // 4. 统计每个产品关联功能的成本价之和 + // 4.1 收集产品ID列表 + productIdList := make([]string, 0, len(productList)) + for _, p := range productList { + productIdList = append(productIdList, p.Id) + } + + // 4.2 查询 product_feature 记录 + productFeatureBuilder := l.svcCtx.ProductFeatureModel.SelectBuilder(). + Where(squirrel.Eq{"product_id": productIdList}) + productFeatureList, err := l.svcCtx.ProductFeatureModel.FindAll(l.ctx, productFeatureBuilder, "") + if err != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), + "查询产品功能关联失败, err: %v, productIds: %+v", err, productIdList) + } + + // 4.3 没有关联功能时直接返回 + if len(productFeatureList) == 0 { + items := make([]types.ProductListItem, 0, len(productList)) + for _, item := range productList { + listItem := types.ProductListItem{ + Id: item.Id, + ProductName: item.ProductName, + ProductEn: item.ProductEn, + Description: item.Description, + Notes: item.Notes.String, + SellPrice: item.SellPrice, + CostPrice: 0, + CreateTime: item.CreateTime.Format("2006-01-02 15:04:05"), + UpdateTime: item.UpdateTime.Format("2006-01-02 15:04:05"), + } + items = append(items, listItem) + } + + return &types.AdminGetProductListResp{ + Total: total, + Items: items, + }, nil + } + + // 4.4 收集 featureId 列表 + featureIdList := make([]string, 0, len(productFeatureList)) + for _, pf := range productFeatureList { + featureIdList = append(featureIdList, pf.FeatureId) + } + + // 4.5 查询功能表,取出每个功能的成本价 + featureBuilder := l.svcCtx.FeatureModel.SelectBuilder(). + Where(squirrel.Eq{"id": featureIdList}) + featureList, err := l.svcCtx.FeatureModel.FindAll(l.ctx, featureBuilder, "") + if err != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), + "查询功能成本价失败, err: %v, featureIds: %+v", err, featureIdList) + } + + // 4.6 构建 featureId -> cost_price 映射 + featureCostMap := make(map[string]float64, len(featureList)) + for _, f := range featureList { + featureCostMap[f.Id] = f.CostPrice + } + + // 4.7 计算每个产品的成本价(所有关联 feature 的 cost_price 之和) + productCostMap := make(map[string]float64, len(productList)) + for _, pf := range productFeatureList { + cost, ok := featureCostMap[pf.FeatureId] + if !ok { + continue + } + productCostMap[pf.ProductId] += cost + } + + // 5. 构建响应列表 + items := make([]types.ProductListItem, 0, len(productList)) + for _, item := range productList { listItem := types.ProductListItem{ Id: item.Id, ProductName: item.ProductName, @@ -54,13 +133,14 @@ func (l *AdminGetProductListLogic) AdminGetProductList(req *types.AdminGetProduc Description: item.Description, Notes: item.Notes.String, SellPrice: item.SellPrice, + CostPrice: productCostMap[item.Id], CreateTime: item.CreateTime.Format("2006-01-02 15:04:05"), UpdateTime: item.UpdateTime.Format("2006-01-02 15:04:05"), } items = append(items, listItem) } - // 5. 返回结果 + // 6. 返回结果 return &types.AdminGetProductListResp{ Total: total, Items: items, diff --git a/app/main/api/internal/types/types.go b/app/main/api/internal/types/types.go index f6c25d7..c29f542 100644 --- a/app/main/api/internal/types/types.go +++ b/app/main/api/internal/types/types.go @@ -512,6 +512,7 @@ type ProductListItem struct { ProductEn string `json:"product_en"` // 英文名 Description string `json:"description"` // 描述 Notes string `json:"notes"` // 备注 + CostPrice float64 `json:"cost_price"` // 成本价(由功能成本累加得出) SellPrice float64 `json:"sell_price"` // 售价 CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间