1、优化采购报表查询慢的问题;

2、优化采购明细跟供应商采购汇总数据不一致的问题,采购明细不再排查"已终止"的订单;
This commit is contained in:
chenlin 2024-09-27 19:35:44 +08:00
parent 5fb54b36d8
commit 4e7aa8caed

View File

@ -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,19 +5093,19 @@ 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, 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)
data.RejectAmount = -data.Amount
@ -5120,12 +5123,24 @@ 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
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
resp.List[i].StoreName = purchaseOrderInfo.StoreName
@ -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, 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
}
@ -5463,13 +5479,13 @@ func GetReportDetail(req *ErpPurchaseReportDetailReq, c *gin.Context) (*ErpPurch
"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,11 +5559,19 @@ func GetReportDetail(req *ErpPurchaseReportDetailReq, c *gin.Context) (*ErpPurch
// 根据目前最新的需求,页面"采购/退货金额"合并到一起了但前端统一使用的时price字段
// 所以对 reject 采购退货单进行特殊处理将RejectPrice赋值给price
if reportList[i].PurchaseType == "reject" {
// 采购退货单查询采购价:根据采购入库编号
price, err := getPrice(reportList[i].SerialNumber)
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
reportList[i].RejectPrice = -reportList[i].RejectPrice