From b327b91db1c7015845ad293e8ccc48c4611819c6 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 05 六月 2024 20:56:34 +0800
Subject: [PATCH] 位置报表下载接口

---
 controllers/report_forms_controller.go |   95 ++++++++++++++++++++++++++---------------------
 1 files changed, 53 insertions(+), 42 deletions(-)

diff --git a/controllers/report_forms_controller.go b/controllers/report_forms_controller.go
index 03cc2b0..37bcc1c 100644
--- a/controllers/report_forms_controller.go
+++ b/controllers/report_forms_controller.go
@@ -246,6 +246,7 @@
 // @Tags      鎶ヨ〃
 // @Summary   鑾峰彇浣嶇疆鎶ヨ〃
 // @Produce   application/json
+// @Param     Authorization	header string true "token"
 // @Param     object  body  request.GetLocationForms true  "鏌ヨ鍙傛暟"
 // @Success   200 {object} util.ResponseList{data=[]response.LocationForms}	"鎴愬姛"
 // @Router    /api-wms/v1/forms/getLocationForms [post]
@@ -255,54 +256,64 @@
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
 		return
 	}
-	ids := make([]int, 0)
-	if params.LocationId != 0 {
-		ids = append(ids, params.LocationId)
-	} else {
-		//鏌ヨ浣嶇疆
-		locations, err := models.NewLocationSearch().SetJointName(params.WareHouseCode).SetType(3).FindAll()
-		if err != nil {
-			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ浣嶇疆澶辫触")
-			return
-		}
-		for _, location := range locations {
-			ids = append(ids, location.Id)
-		}
-	}
-	var (
-		amounts    []*models.LocationProductAmount
-		total      uint64
-		totalInt64 int64
-		err        error
-	)
-	if params.KeyWord != "" {
-		amounts, total, err = service.SearchLocationReport(params.KeyWord, params.Page, params.PageSize)
-	} else {
-		amounts, totalInt64, err = models.NewLocationProductAmountSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids).FindByPage()
-		if err != nil {
-			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鏁伴噺澶辫触")
-			return
-		}
-		total = uint64(totalInt64)
-	}
 
-	var result []response.LocationForms
-	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
-		resp.ProductTypeName = amount.ProductCategory.Name
-		resp.Unit = amount.Product.Unit
-		resp.Value = resp.Amount.Mul(amount.Product.Cost)
-		result = append(result, resp)
+	locationFormsService := service.NewLocationFormsService()
+	total, err := locationFormsService.Count(params)
+	if err != nil {
+		logx.Errorf("GetLocationForms count err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ鎬绘暟澶辫触")
+		return
+	}
+	result, err := locationFormsService.Query(params)
+	if err != nil {
+		logx.Errorf("GetLocationForms query err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
 	}
 
 	util.ResponseFormatList(c, code.Success, result, int(total))
 }
 
+// DownloadLocationForms
+// @Tags      鎶ヨ〃
+// @Summary   涓嬭浇浣嶇疆鎶ヨ〃
+// @Produce   application/json
+// @Param     Authorization	header string true "token"
+// @Param     object  body  request.GetLocationForms true  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.ResponseList{data=[]response.LocationForms}	"鎴愬姛"
+// @Router    /api-wms/v1/forms/downloadLocationForms [post]
+func (slf ReportFormsController) DownloadLocationForms(c *gin.Context) {
+	var params request.GetLocationForms
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	locationFormsService := service.NewLocationFormsService()
+	list, err := locationFormsService.FetchAll(params)
+	if err != nil {
+		logx.Errorf("DownloadLocationForms FetchAll err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
+	}
+	filename, err := locationFormsService.Export(list)
+	if err != nil {
+		logx.Errorf("DownloadLocationForms Export err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "瀵煎嚭鏁版嵁鍒版枃浠跺け璐�")
+		return
+	}
+
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "瀵煎嚭澶辫触")
+		return
+	}
+
+	fileContentDisposition := "attachment;filename=\"" + url.QueryEscape(filename) + "\""
+	c.Header("Content-Type", "application/xlsx")
+	c.Header("Content-Disposition", fileContentDisposition)
+	c.File(filename)
+	defer os.Remove(filename)
+}
+
 // MonthStats
 // @Tags      鎶ヨ〃
 // @Summary   鏈堝害缁熻搴撳瓨鎶ヨ〃

--
Gitblit v1.8.0