From 3abb522a610fa41a8d5570b643d88a23030e56db Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 06 五月 2024 17:24:11 +0800
Subject: [PATCH] 执行月度统计前先删除本月记录,防止重复数据

---
 controllers/report_forms_controller.go |   98 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 76 insertions(+), 22 deletions(-)

diff --git a/controllers/report_forms_controller.go b/controllers/report_forms_controller.go
index 25bb3f3..6870409 100644
--- a/controllers/report_forms_controller.go
+++ b/controllers/report_forms_controller.go
@@ -32,9 +32,33 @@
 // @Router    /api-wms/v1/forms/getInventoryForms [post]
 func (slf ReportFormsController) GetInventoryForms(c *gin.Context) {
 	var params request.GetInventoryForms
-	if err := c.BindJSON(&params); err != nil {
+	err := c.BindJSON(&params)
+	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
 		return
+	}
+	locationIds := make([]int, 0)
+	productIds := make([]string, 0)
+	productAmounts := make([]*models.LocationProductAmount, 0)
+	if params.WarehouseCode != "" {
+		locations, err := models.NewLocationSearch().SetJointName(params.WarehouseCode).FindNotTotal()
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ浠撳簱浣嶇疆澶辫触")
+			return
+		}
+
+		for _, location := range locations {
+			locationIds = append(locationIds, location.Id)
+		}
+		productAmounts, err = models.NewLocationProductAmountSearch().SetLocationIds(locationIds).SetQuery("amount > 0").Find()
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
+			return
+		}
+		for _, amount := range productAmounts {
+			productIds = append(productIds, amount.ProductId)
+		}
+
 	}
 	//鏌ヨ浜у搧
 	search := models.NewMaterialSearch()
@@ -46,6 +70,9 @@
 	}
 	if params.KeyWord != "" {
 		search.Orm.Where("material.name like ?", "%"+params.KeyWord+"%").Or("wms_product_category.name like ?", "%"+params.KeyWord+"%")
+	}
+	if len(productIds) > 0 {
+		search.Orm.Where("material.id in (?)", productIds)
 	}
 	var (
 		materials = make([]*models.Material, 0)
@@ -63,24 +90,16 @@
 		return
 	}
 
-	locations, err := models.NewLocationSearch().SetJointName(params.WarehouseCode).FindNotTotal()
-	if err != nil {
-		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ浠撳簱浣嶇疆澶辫触")
-		return
-	}
-	locationIds := make([]int, 0)
-	for _, location := range locations {
-		locationIds = append(locationIds, location.Id)
-	}
 	//鏌ヨ鍦ㄥ簱鏁伴噺
-	productIds := make([]string, 0)
-	for _, material := range materials {
-		productIds = append(productIds, material.ID)
-	}
-	productAmounts, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
-	if err != nil {
-		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
-		return
+	if len(productIds) == 0 {
+		for _, material := range materials {
+			productIds = append(productIds, material.ID)
+		}
+		productAmounts, err = models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
+			return
+		}
 	}
 
 	//鏌ヨ鍑哄叆搴撳氨缁暟閲�
@@ -97,8 +116,8 @@
 		Where("wms_operation.base_operation_type in (?)", []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}).
 		Where("wms_operation.status in (?)", []constvar.OperationStatus{constvar.OperationStatus_Ready, constvar.OperationStatus_Finish})
 	if len(locationIds) > 0 {
-		dbIn.Where("wms_operation.to_location_id in (?)", locationIds)
-		dbOut.Where("wms_operation.from_location_id in (?)", locationIds)
+		dbIn.Where("wms_operation_details.to_location_id in (?)", locationIds)
+		dbOut.Where("wms_operation_details.from_location_id in (?)", locationIds)
 	}
 	if len(productIds) > 0 {
 		dbIn.Where("wms_operation_details.product_id in (?)", productIds)
@@ -265,13 +284,23 @@
 	}
 
 	detailsSearch.Orm = detailsSearch.Orm.Model(&models.MoveHistory{}).
-		Select("number, updated_at as date, product_name as product_name, from_location_id, operation_id," +
-			"to_location_id, amount, unit, operator as contacted_name, base_operation_type, weight, product_id, from_location, to_location").Order("id desc")
+		Select("number, updated_at as date, product_name as product_name, from_location_id, operation_id,to_location_id, amount, " +
+			"unit, operator as contacted_name, base_operation_type, weight, product_id, from_location, to_location, operation_type_name, weight").Order("id desc")
 	if len(ids) > 0 {
 		detailsSearch.Orm = detailsSearch.Orm.Where("id in ?", ids)
 	}
 	if params.BaseOperationType != 0 {
 		detailsSearch.Orm = detailsSearch.Orm.Where("base_operation_type = ?", params.BaseOperationType)
+	}
+	var t int64
+	err = detailsSearch.Orm.Count(&t).Error
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ鎬绘潯鏁板け璐�: %v", err))
+		return
+	}
+	total = uint64(t)
+	if params.Page*params.PageSize > 0 {
+		detailsSearch.Orm = detailsSearch.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize)
 	}
 	err = detailsSearch.Orm.Find(&result).Error
 	if err != nil {
@@ -329,6 +358,7 @@
 	for _, amount := range amounts {
 		var resp response.LocationForms
 		resp.Amount = amount.Amount
+		resp.LocationId = amount.LocationId
 		resp.LocationName = amount.Location.Name
 		resp.ProduceId = amount.Product.ID
 		resp.ProductName = amount.Product.Name
@@ -340,3 +370,27 @@
 
 	util.ResponseFormatList(c, code.Success, result, int(total))
 }
+
+// MonthStats
+// @Tags      鎶ヨ〃
+// @Summary   鏈堝害缁熻搴撳瓨鎶ヨ〃
+// @Produce   application/json
+// @Param     object  body  request.GetMonthStats true  "鏌ヨ鍙傛暟"
+// @Param     Authorization	header string true "token"
+// @Success   200 {object} util.ResponseList{data=[]models.MonthStats}	"鎴愬姛"
+// @Router    /api-wms/v1/forms/monthStats [post]
+func (slf ReportFormsController) MonthStats(c *gin.Context) {
+	var params request.GetMonthStats
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+
+	list, total, err := models.NewMonthStatsSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetDate(params.Date).Find()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
+	}
+
+	util.ResponseFormatList(c, code.Success, list, int(total))
+}

--
Gitblit v1.8.0