From 4e7aa8caed3cfc1375c4606ab2860664591a1803 Mon Sep 17 00:00:00 2001 From: chenlin Date: Fri, 27 Sep 2024 19:35:44 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E9=87=87=E8=B4=AD?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E6=9F=A5=E8=AF=A2=E6=85=A2=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B=202=E3=80=81=E4=BC=98=E5=8C=96=E9=87=87?= =?UTF-8?q?=E8=B4=AD=E6=98=8E=E7=BB=86=E8=B7=9F=E4=BE=9B=E5=BA=94=E5=95=86?= =?UTF-8?q?=E9=87=87=E8=B4=AD=E6=B1=87=E6=80=BB=E6=95=B0=E6=8D=AE=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E9=87=87?= =?UTF-8?q?=E8=B4=AD=E6=98=8E=E7=BB=86=E4=B8=8D=E5=86=8D=E6=8E=92=E6=9F=A5?= =?UTF-8?q?"=E5=B7=B2=E7=BB=88=E6=AD=A2"=E7=9A=84=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/models/purchase.go | 149 +++++++++++++++++++++-------------- 1 file changed, 88 insertions(+), 61 deletions(-) diff --git a/app/admin/models/purchase.go b/app/admin/models/purchase.go index b63aa85..74bb8fb 100644 --- a/app/admin/models/purchase.go +++ b/app/admin/models/purchase.go @@ -5066,6 +5066,9 @@ func GetReportBySupplier(req *ErpPurchaseReportBySupplierReq, c *gin.Context) ( return nil, err } + // 创建缓存 map 存储 ErpCommodityId 和 SerialNumber 对应的价格信息 + priceCache := make(map[string]ResultPrice) + // 循环遍历采购订单 for _, po := range purchaseOrder { // 在resp.List中查找匹配的订单 @@ -5090,18 +5093,18 @@ func GetReportBySupplier(req *ErpPurchaseReportBySupplierReq, c *gin.Context) ( } if data.PurchaseType == "reject" { - //// 查询退货商品的采购金额 - //var oldOrderInfo ErpPurchaseOrder - //err = orm.Eloquent.Where("serial_number = ?", po.SerialNumber).First(&oldOrderInfo).Error - //if err != nil { - // return nil, err - //} + cacheKey := fmt.Sprintf("%d-%s", respItem.ErpCommodityId, respItem.SerialNumber) - // 查找对应的采购入库记录 - var resultInfo ResultPrice - resultInfo, err = GetTotalWholesalePrice(respItem.ErpCommodityId, respItem.SerialNumber) - if err != nil { - return nil, err + // 检查缓存中是否已有对应的采购记录 + resultInfo, exists := priceCache[cacheKey] + if !exists { + // 缓存中没有,调用 GetTotalWholesalePrice 获取数据,并存入缓存 + var err error + resultInfo, err = GetTotalWholesalePrice(respItem.ErpCommodityId, respItem.SerialNumber) + if err != nil { + return nil, err + } + priceCache[cacheKey] = resultInfo } data.Difference = -(resultInfo.TotalWholesalePrice - data.Amount) @@ -5120,11 +5123,23 @@ func GetReportBySupplier(req *ErpPurchaseReportBySupplierReq, c *gin.Context) ( resp.List = esData } else { // 补充关联的供应商和店铺信息 + // 创建缓存 map 存储 ErpPurchaseOrderId 和 ErpPurchaseOrder 结果 + purchaseOrderCache := make(map[uint32]ErpPurchaseOrder) + // 创建缓存 map 存储 ErpCommodityId 和 SerialNumber 对应的价格信息 + priceCache := make(map[string]ResultPrice) + for i := range resp.List { - var purchaseOrderInfo ErpPurchaseOrder - err = orm.Eloquent.Where("id = ?", resp.List[i].ErpPurchaseOrderId).First(&purchaseOrderInfo).Error - if err != nil { - return nil, err + erpPurchaseOrderId := resp.List[i].ErpPurchaseOrderId + + // 检查缓存中是否已有对应的采购订单信息 + purchaseOrderInfo, exists := purchaseOrderCache[erpPurchaseOrderId] + if !exists { + // 缓存中没有,查询采购订单信息并存入缓存 + err = orm.Eloquent.Where("id = ?", erpPurchaseOrderId).First(&purchaseOrderInfo).Error + if err != nil { + return nil, err + } + purchaseOrderCache[erpPurchaseOrderId] = purchaseOrderInfo } resp.List[i].StoreId = purchaseOrderInfo.StoreId @@ -5133,25 +5148,26 @@ func GetReportBySupplier(req *ErpPurchaseReportBySupplierReq, c *gin.Context) ( resp.List[i].ErpSupplierName = purchaseOrderInfo.ErpSupplierName if resp.List[i].PurchaseType == "reject" { - //// 查询退货商品的采购金额 - //var oldOrderInfo ErpPurchaseOrder - //err = orm.Eloquent.Where("serial_number = ?", purchaseOrderInfo.SerialNumber).First(&oldOrderInfo).Error - //if err != nil { - // return nil, err - //} + cacheKey := fmt.Sprintf("%d-%s", resp.List[i].ErpCommodityId, resp.List[i].SerialNumber) - // 查找对应的采购入库记录 - var resultInfo ResultPrice - resultInfo, err = GetTotalWholesalePrice(resp.List[i].ErpCommodityId, resp.List[i].SerialNumber) - if err != nil { - return nil, err + // 检查缓存中是否已有对应的采购记录 + resultInfo, exists := priceCache[cacheKey] + if !exists { + // 缓存中没有,调用 GetTotalWholesalePrice 获取数据,并存入缓存 + var err error + resultInfo, err = GetTotalWholesalePrice(resp.List[i].ErpCommodityId, resp.List[i].SerialNumber) + if err != nil { + return nil, err + } + priceCache[cacheKey] = resultInfo } + + // 计算差额,并处理退货金额和数量的负值 resp.List[i].Difference = -(resultInfo.TotalWholesalePrice - resp.List[i].Amount) - resp.List[i].RejectAmount = -resp.List[i].Amount + resp.List[i].RejectAmount = -math.Abs(resp.List[i].Amount) resp.List[i].Count = -resp.List[i].Count - // 前端合并了采购价退货价,都使用的amount字段,所以当是采购退货时,将RejectAmount赋值给Amount - //resp.List[i].Amount = -resultInfo.TotalWholesalePrice + // 将 RejectAmount 赋值给 Amount resp.List[i].Amount = resp.List[i].RejectAmount } @@ -5438,38 +5454,38 @@ func GetReportDetail(req *ErpPurchaseReportDetailReq, c *gin.Context) (*ErpPurch // 差额只有采购退货单才存在,差额=采购价-退货价;这里sql查询的不准确 qs := orm.Eloquent.Debug().Table("erp_purchase_order"). - Select("erp_purchase_order.id as erp_purchase_order_id, "+ - "erp_purchase_order.serial_number as order_serial_number, "+ - "erp_purchase_inventory.serial_number, "+ - "erp_purchase_order.purchase_type, "+ - "erp_purchase_inventory.created_at as execute_time, "+ - "erp_purchase_order.store_id, "+ - "erp_purchase_order.store_name, "+ - "erp_purchase_order.erp_supplier_id, "+ - "erp_purchase_order.erp_supplier_name, "+ - "erp_purchase_inventory.erp_commodity_id, "+ - "erp_purchase_inventory.erp_commodity_name, "+ - "erp_purchase_inventory.erp_category_id, "+ - "erp_purchase_inventory.erp_category_name, "+ - "erp_purchase_inventory.imei_type, "+ - "erp_purchase_inventory.imei, "+ - "CASE "+ - "WHEN erp_purchase_order.purchase_type = 'procure' THEN erp_purchase_inventory.implementation_price "+ - "WHEN erp_purchase_order.purchase_type = 'reject' THEN erp_purchase_inventory.implementation_price "+ - "ELSE 0 END AS price, "+ - "CASE "+ - "WHEN erp_purchase_order.purchase_type = 'procure' THEN 0 "+ - "WHEN erp_purchase_order.purchase_type = 'reject' THEN erp_purchase_inventory.implementation_price "+ - "ELSE 0 END AS reject_price, "+ - "erp_purchase_inventory.employee_price, "+ + Select("erp_purchase_order.id as erp_purchase_order_id, " + + "erp_purchase_order.serial_number as order_serial_number, " + + "erp_purchase_inventory.serial_number, " + + "erp_purchase_order.purchase_type, " + + "erp_purchase_inventory.created_at as execute_time, " + + "erp_purchase_order.store_id, " + + "erp_purchase_order.store_name, " + + "erp_purchase_order.erp_supplier_id, " + + "erp_purchase_order.erp_supplier_name, " + + "erp_purchase_inventory.erp_commodity_id, " + + "erp_purchase_inventory.erp_commodity_name, " + + "erp_purchase_inventory.erp_category_id, " + + "erp_purchase_inventory.erp_category_name, " + + "erp_purchase_inventory.imei_type, " + + "erp_purchase_inventory.imei, " + + "CASE " + + "WHEN erp_purchase_order.purchase_type = 'procure' THEN erp_purchase_inventory.implementation_price " + + "WHEN erp_purchase_order.purchase_type = 'reject' THEN erp_purchase_inventory.implementation_price " + + "ELSE 0 END AS price, " + + "CASE " + + "WHEN erp_purchase_order.purchase_type = 'procure' THEN 0 " + + "WHEN erp_purchase_order.purchase_type = 'reject' THEN erp_purchase_inventory.implementation_price " + + "ELSE 0 END AS reject_price, " + + "erp_purchase_inventory.employee_price, " + "(erp_purchase_inventory.implementation_price - erp_purchase_inventory.employee_price) as difference_price"). - Joins("JOIN erp_purchase_inventory ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id"). - Where("erp_purchase_order.state <> ?", 5) // 排除已终止的订单 + Joins("JOIN erp_purchase_inventory ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id") + //Where("erp_purchase_order.state <> ?", 5) // 排除已终止的订单 // 创建一个新的查询对象,用于 count 查询 countQuery := orm.Eloquent.Debug().Table("erp_purchase_order"). - Joins("JOIN erp_purchase_inventory ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id"). - Where("erp_purchase_order.state <> ?", 5) // 排除已终止的订单 + Joins("JOIN erp_purchase_inventory ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id") + //Where("erp_purchase_order.state <> ?", 5) // 排除已终止的订单 if req.SerialNumber != "" { qs = qs.Where("erp_purchase_order.serial_number = ?", req.SerialNumber) @@ -5533,6 +5549,9 @@ func GetReportDetail(req *ErpPurchaseReportDetailReq, c *gin.Context) (*ErpPurch return nil, err } + // 定义一个缓存map存储 SerialNumber 对应的 price + priceCache := make(map[string]float64) + // 根据订单类型调整价格的正负性 for i := range reportList { reportList[i].DifferencePrice = 0 // 采购单不存在差额 @@ -5540,10 +5559,18 @@ func GetReportDetail(req *ErpPurchaseReportDetailReq, c *gin.Context) (*ErpPurch // 根据目前最新的需求,页面"采购/退货金额"合并到一起了,但前端统一使用的时price字段 // 所以对 reject 采购退货单进行特殊处理,将RejectPrice赋值给price if reportList[i].PurchaseType == "reject" { - // 采购退货单查询采购价:根据采购入库编号 - price, err := getPrice(reportList[i].SerialNumber) - if err != nil { - return nil, err + serialNumber := reportList[i].SerialNumber + + // 如果缓存中已经有该 SerialNumber 对应的价格,直接使用 + price, exists := priceCache[serialNumber] + if !exists { + // 缓存中没有,调用 getPrice 获取价格,并存入缓存 + var err error + price, err = getPrice(serialNumber) + if err != nil { + return nil, err + } + priceCache[serialNumber] = price } reportList[i].DifferencePrice = -reportList[i].DifferencePrice