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

---
 service/inventory_report_forms.go      |   21 +-
 controllers/report_forms_controller.go |   95 +++++++------
 models/location_product_amount.go      |    3 
 docs/swagger.yaml                      |   36 +++++
 service/location_forms.go              |  121 +++++++++++++++++
 docs/docs.go                           |   59 ++++++++
 docs/swagger.json                      |   59 ++++++++
 router/router.go                       |    3 
 8 files changed, 344 insertions(+), 53 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   鏈堝害缁熻搴撳瓨鎶ヨ〃
diff --git a/docs/docs.go b/docs/docs.go
index 6a0b008..a334db6 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -520,6 +520,58 @@
                 }
             }
         },
+        "/api-wms/v1/forms/downloadLocationForms": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "鎶ヨ〃"
+                ],
+                "summary": "涓嬭浇浣嶇疆鎶ヨ〃",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
+                        "description": "鏌ヨ鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.GetLocationForms"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/response.LocationForms"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/forms/getHistory": {
             "post": {
                 "produces": [
@@ -628,6 +680,13 @@
                 "summary": "鑾峰彇浣嶇疆鎶ヨ〃",
                 "parameters": [
                     {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
                         "description": "鏌ヨ鍙傛暟",
                         "name": "object",
                         "in": "body",
diff --git a/docs/swagger.json b/docs/swagger.json
index 2fe0e55..040b004 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -508,6 +508,58 @@
                 }
             }
         },
+        "/api-wms/v1/forms/downloadLocationForms": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "鎶ヨ〃"
+                ],
+                "summary": "涓嬭浇浣嶇疆鎶ヨ〃",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
+                        "description": "鏌ヨ鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.GetLocationForms"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/response.LocationForms"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/forms/getHistory": {
             "post": {
                 "produces": [
@@ -616,6 +668,13 @@
                 "summary": "鑾峰彇浣嶇疆鎶ヨ〃",
                 "parameters": [
                     {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
                         "description": "鏌ヨ鍙傛暟",
                         "name": "object",
                         "in": "body",
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index f8b7f54..8177db7 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -2251,6 +2251,37 @@
       summary: 涓嬭浇搴撳瓨鎶ヨ〃
       tags:
       - 鎶ヨ〃
+  /api-wms/v1/forms/downloadLocationForms:
+    post:
+      parameters:
+      - description: token
+        in: header
+        name: Authorization
+        required: true
+        type: string
+      - description: 鏌ヨ鍙傛暟
+        in: body
+        name: object
+        required: true
+        schema:
+          $ref: '#/definitions/request.GetLocationForms'
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: 鎴愬姛
+          schema:
+            allOf:
+            - $ref: '#/definitions/util.ResponseList'
+            - properties:
+                data:
+                  items:
+                    $ref: '#/definitions/response.LocationForms'
+                  type: array
+              type: object
+      summary: 涓嬭浇浣嶇疆鎶ヨ〃
+      tags:
+      - 鎶ヨ〃
   /api-wms/v1/forms/getHistory:
     post:
       parameters:
@@ -2311,6 +2342,11 @@
   /api-wms/v1/forms/getLocationForms:
     post:
       parameters:
+      - description: token
+        in: header
+        name: Authorization
+        required: true
+        type: string
       - description: 鏌ヨ鍙傛暟
         in: body
         name: object
diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index f1f2dc3..efa1205 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -253,6 +253,9 @@
 		db      = slf.build()
 	)
 
+	if slf.PageNum*slf.PageSize > 0 {
+		db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
+	}
 	if err := db.Find(&records).Error; err != nil {
 		return records, fmt.Errorf("find records err: %v", err)
 	}
diff --git a/router/router.go b/router/router.go
index 83c7b8f..541d52f 100644
--- a/router/router.go
+++ b/router/router.go
@@ -157,8 +157,9 @@
 		reportFormsAPI.POST("downloadInventoryForms", reportFormsController.DownloadInventoryForms) //涓嬭浇搴撳瓨鎶ヨ〃
 		reportFormsAPI.POST("getHistory", reportFormsController.GetHistory)                         //鑾峰彇搴撳瓨鍘嗗彶
 		reportFormsAPI.POST("getLocationForms", reportFormsController.GetLocationForms)             //鑾峰彇浣嶇疆鎶ヨ〃
+		reportFormsAPI.POST("downloadLocationForms", reportFormsController.DownloadLocationForms)   //涓嬭浇浣嶇疆鎶ヨ〃
 		reportFormsAPI.POST("monthStats", reportFormsController.MonthStats)                         //鑾峰彇鏈堝害缁熻鎶ヨ〃
-		reportFormsAPI.POST("doMonthStats", reportFormsController.DoMonthStats)                     //鑾峰彇鏈堝害缁熻鎶ヨ〃
+		reportFormsAPI.POST("doMonthStats", reportFormsController.DoMonthStats)                     //鎵嬪姩璺戞湀搴︾粺璁″簱瀛樻姤琛�
 	}
 
 	//閲嶈璐ц鍒�
diff --git a/service/inventory_report_forms.go b/service/inventory_report_forms.go
index 82a5d2e..e83af14 100644
--- a/service/inventory_report_forms.go
+++ b/service/inventory_report_forms.go
@@ -174,7 +174,7 @@
 		return nil, err
 	}
 	list = make([]*response.InventoryForms, 0, total)
-	params.PageSize = 1000
+	params.PageSize = 500
 	page := 1
 	for {
 		params.Page = page
@@ -205,15 +205,16 @@
 	}
 
 	for i, v := range dataList {
-		f.SetCellValue("Sheet1", "A"+strconv.Itoa(i+2), v.ProductName)
-		f.SetCellValue("Sheet1", "B"+strconv.Itoa(i+2), v.ProductType)
-		f.SetCellValue("Sheet1", "C"+strconv.Itoa(i+2), v.Cost)
-		f.SetCellValue("Sheet1", "D"+strconv.Itoa(i+2), v.Value)
-		f.SetCellValue("Sheet1", "E"+strconv.Itoa(i+2), v.Amount)
-		f.SetCellValue("Sheet1", "F"+strconv.Itoa(i+2), v.AvailableNumber)
-		f.SetCellValue("Sheet1", "G"+strconv.Itoa(i+2), v.In)
-		f.SetCellValue("Sheet1", "H"+strconv.Itoa(i+2), v.Out)
-		f.SetCellValue("Sheet1", "I"+strconv.Itoa(i+2), v.Unit)
+		column := strconv.Itoa(i + 2)
+		f.SetCellValue("Sheet1", "A"+column, v.ProductName)
+		f.SetCellValue("Sheet1", "B"+column, v.ProductType)
+		f.SetCellValue("Sheet1", "C"+column, v.Cost)
+		f.SetCellValue("Sheet1", "D"+column, v.Value)
+		f.SetCellValue("Sheet1", "E"+column, v.Amount)
+		f.SetCellValue("Sheet1", "F"+column, v.AvailableNumber)
+		f.SetCellValue("Sheet1", "G"+column, v.In)
+		f.SetCellValue("Sheet1", "H"+column, v.Out)
+		f.SetCellValue("Sheet1", "I"+column, v.Unit)
 	}
 
 	fileName = fmt.Sprintf("搴撳瓨鎶ヨ〃%s.xlsx", time.Now().Format("2006-01-02-1504"))
diff --git a/service/location_forms.go b/service/location_forms.go
new file mode 100644
index 0000000..eafd283
--- /dev/null
+++ b/service/location_forms.go
@@ -0,0 +1,121 @@
+package service
+
+import (
+	"fmt"
+	"github.com/xuri/excelize/v2"
+	"strconv"
+	"time"
+	"wms/models"
+	"wms/request"
+	"wms/response"
+)
+
+type LocationFormsService struct{}
+
+func NewLocationFormsService() *LocationFormsService {
+	return &LocationFormsService{}
+}
+
+func (slf *LocationFormsService) Query(params request.GetLocationForms) (result []*response.LocationForms, err error) {
+	search, err := slf.BuildSearch(params)
+	search = search.SetPage(params.Page, params.PageSize)
+	amounts, err := search.Find()
+	if err != nil {
+		return
+	}
+	for _, amount := range amounts {
+		resp := new(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)
+	}
+
+	return
+}
+
+func (slf *LocationFormsService) BuildSearch(params request.GetLocationForms) (search *models.LocationProductAmountSearch, err error) {
+	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 {
+			return nil, err
+		}
+		for _, location := range locations {
+			ids = append(ids, location.Id)
+		}
+	}
+
+	search = models.NewLocationProductAmountSearch().SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids)
+
+	return search, nil
+}
+
+func (slf *LocationFormsService) Count(params request.GetLocationForms) (total int64, err error) {
+	search, err := slf.BuildSearch(params)
+	if err != nil {
+		return 0, err
+	}
+	return search.Count()
+}
+
+func (slf *LocationFormsService) FetchAll(params request.GetLocationForms) (list []*response.LocationForms, err error) {
+	total, err := slf.Count(params)
+	if err != nil {
+		return nil, err
+	}
+	list = make([]*response.LocationForms, 0, total)
+	params.PageSize = 500
+	page := 1
+	for {
+		params.Page = page
+		data, err := slf.Query(params)
+		if err != nil {
+			return nil, err
+		}
+		if len(data) == 0 {
+			break
+		}
+		list = append(list, data...)
+		page++
+	}
+	return
+}
+
+func (slf *LocationFormsService) Export(dataList []*response.LocationForms) (filename string, err error) {
+	var fileName string
+	f := excelize.NewFile()
+
+	// 鑷畾涔夎〃澶�
+	headers := []string{"浣嶇疆", "浜у搧", "浜у搧绫诲埆", "鍦ㄥ簱鏁伴噺", "浠峰��"}
+
+	// 璁剧疆琛ㄥご
+	for i, header := range headers {
+		cell := getColumnAlphabet(i+1) + "1"
+		f.SetCellValue("Sheet1", cell, header)
+	}
+
+	for i, v := range dataList {
+		column := strconv.Itoa(i + 2)
+		f.SetCellValue("Sheet1", "A"+column, v.LocationName)
+		f.SetCellValue("Sheet1", "B"+column, v.ProductName)
+		f.SetCellValue("Sheet1", "C"+column, v.ProductTypeName)
+		f.SetCellValue("Sheet1", "D"+column, v.Amount)
+		f.SetCellValue("Sheet1", "E"+column, v.Value)
+	}
+
+	fileName = fmt.Sprintf("浣嶇疆鎶ヨ〃%s.xlsx", time.Now().Format("2006-01-02-1504"))
+	if err := f.SaveAs(fileName); err != nil {
+		return fileName, err
+	}
+
+	return fileName, nil
+}

--
Gitblit v1.8.0