From 985e3b7a74962d1aae5cba3451f3bff35daf5794 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 29 七月 2024 20:08:45 +0800 Subject: [PATCH] 优化查询 --- controllers/product_controller.go | 12 ++++-------- 1 files changed, 4 insertions(+), 8 deletions(-) diff --git a/controllers/product_controller.go b/controllers/product_controller.go index 56f6e68..9b81e63 100644 --- a/controllers/product_controller.go +++ b/controllers/product_controller.go @@ -142,23 +142,19 @@ product.CategoryName = category.Name } } - var reorderAmount request.ProductStatisticsAmount + var totalAmount decimal.Decimal if err := models.NewOperationSearch().Orm. Table("wms_operation_details"). InnerJoins("INNER JOIN wms_operation on wms_operation_details.operation_id=wms_operation.id"). - Select("wms_operation_details.product_id,SUM(wms_operation_details.amount) as total_count"). + Select("IFNULL(SUM(wms_operation_details.amount), 0) as total_count"). Where("wms_operation_details.product_id=? and wms_operation.`status`=? and wms_operation.base_operation_type in (?)", product.ID, constvar.OperationStatus_Ready, []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeDisuse}). - Group("wms_operation_details.product_id"). - //Order("wms_operation_details.product_id"). - First(&reorderAmount).Error; err != nil { + Scan(&totalAmount).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { util.ResponseFormat(c, code.RequestParamError, "浜у搧鏁版嵁缁熻澶辫触") return - } else { - reorderAmount.TotalAmount = decimal.NewFromInt(0) } } - product.PredictionAmount = product.Amount.Add(reorderAmount.TotalAmount) + product.PredictionAmount = product.Amount.Add(totalAmount) var statisticsList []*request.ProductStatistics if err := models.NewOperationSearch().Orm.Table("wms_operation"). -- Gitblit v1.8.0