From 6b2c4936814854f658b501e87cdcca454937a786 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 06 六月 2024 19:29:41 +0800
Subject: [PATCH] 下载出入库明细报表接口

---
 response/report_forms_response.go      |    3 
 models/move_history.go                 |   43 ++
 service/history_forms.go               |  132 +++++++++
 controllers/report_forms_controller.go |   90 +++---
 docs/swagger.yaml                      |  143 ++++++---
 docs/docs.go                           |  210 +++++++++-----
 docs/swagger.json                      |  210 +++++++++-----
 router/router.go                       |    1 
 8 files changed, 587 insertions(+), 245 deletions(-)

diff --git a/controllers/report_forms_controller.go b/controllers/report_forms_controller.go
index 37bcc1c..6d7b0ce 100644
--- a/controllers/report_forms_controller.go
+++ b/controllers/report_forms_controller.go
@@ -1,7 +1,6 @@
 package controllers
 
 import (
-	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/shopspring/decimal"
 	"net/url"
@@ -12,7 +11,6 @@
 	"wms/models"
 	"wms/pkg/logx"
 	"wms/request"
-	"wms/response"
 	"wms/service"
 	"wms/task"
 )
@@ -102,10 +100,11 @@
 
 // GetHistory
 // @Tags      鎶ヨ〃
-// @Summary   鑾峰彇鍘嗗彶淇℃伅
+// @Summary   鑾峰彇鍑哄叆搴撴槑缁�
 // @Produce   application/json
+// @Param     Authorization	header string true "token"
 // @Param     object  body  request.GetInventoryHistory true  "鏌ヨ鍙傛暟"
-// @Success   200 {object} util.ResponseList{data=[]response.InventoryHistory}	"鎴愬姛"
+// @Success   200 {object} util.ResponseList{data=[]models.MoveHistory}	"鎴愬姛"
 // @Router    /api-wms/v1/forms/getHistory [post]
 func (slf ReportFormsController) GetHistory(c *gin.Context) {
 	slf.GetHistoryNew(c)
@@ -195,53 +194,58 @@
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
 		return
 	}
-	//鑾峰彇鎿嶄綔璇︽儏
-	detailsSearch := models.NewMoveHistorySearch()
-	var (
-		result []*response.InventoryHistory
-		total  uint64
-		ids    []int
-		err    error
-	)
-	if params.KeyWord != "" {
-		ids, total, err = service.SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize)
-		if err != nil {
-			util.ResponseFormat(c, code.InternalError, err.Error())
-			return
-		}
-		if len(ids) == 0 {
-			util.ResponseFormatList(c, code.Success, result, 0)
-			return
-		}
+
+	historyFormsService := service.NewHistoryFormsService()
+	result, err := historyFormsService.Query(params)
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鍐呴儴閿欒")
+		return
 	}
 
-	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, 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
+	total, err := historyFormsService.Count(params)
 	if err != nil {
-		util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ鎬绘潯鏁板け璐�: %v", err))
+		util.ResponseFormat(c, code.InternalError, "鍐呴儴閿欒")
 		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 {
-		util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ鎿嶄綔鏄庣粏澶辫触: %v", err))
-		return
-	}
+
 	util.ResponseFormatList(c, code.Success, result, int(total))
 }
 
+// DownloadHistory
+// @Tags      鎶ヨ〃
+// @Summary   涓嬭浇鍑哄叆搴撴槑缁嗘姤琛�
+// @Produce   application/json
+// @Param     Authorization	header string true "token"
+// @Param     object  body  request.GetInventoryHistory true  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.ResponseList{data=[]models.MoveHistory}	"鎴愬姛"
+// @Router    /api-wms/v1/forms/downloadHistory [post]
+func (slf ReportFormsController) DownloadHistory(c *gin.Context) {
+	var params request.GetInventoryHistory
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	historyFormsService := service.NewHistoryFormsService()
+	list, err := historyFormsService.FetchAll(params)
+	if err != nil {
+		logx.Errorf("DownloadHistory FetchAll err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
+	}
+	filename, err := historyFormsService.Export(list, params)
+	if err != nil {
+		logx.Errorf("DownloadHistory Export err:%v", err)
+		util.ResponseFormat(c, code.InternalError, "瀵煎嚭鏁版嵁鍒版枃浠跺け璐�")
+		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)
+}
+
 // GetLocationForms
 // @Tags      鎶ヨ〃
 // @Summary   鑾峰彇浣嶇疆鎶ヨ〃
diff --git a/docs/docs.go b/docs/docs.go
index a334db6..24e7450 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -468,6 +468,58 @@
                 }
             }
         },
+        "/api-wms/v1/forms/downloadHistory": {
+            "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.GetInventoryHistory"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/models.MoveHistory"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/forms/downloadInventoryForms": {
             "post": {
                 "produces": [
@@ -580,8 +632,15 @@
                 "tags": [
                     "鎶ヨ〃"
                 ],
-                "summary": "鑾峰彇鍘嗗彶淇℃伅",
+                "summary": "鑾峰彇鍑哄叆搴撴槑缁�",
                 "parameters": [
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
                     {
                         "description": "鏌ヨ鍙傛暟",
                         "name": "object",
@@ -606,7 +665,7 @@
                                         "data": {
                                             "type": "array",
                                             "items": {
-                                                "$ref": "#/definitions/response.InventoryHistory"
+                                                "$ref": "#/definitions/models.MoveHistory"
                                             }
                                         }
                                     }
@@ -3940,6 +3999,84 @@
                 }
             }
         },
+        "models.MoveHistory": {
+            "type": "object",
+            "properties": {
+                "amount": {
+                    "description": "鏁伴噺",
+                    "type": "number"
+                },
+                "baseOperationType": {
+                    "description": "鍩虹浣滀笟绫诲瀷",
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.BaseOperationType"
+                        }
+                    ]
+                },
+                "createTime": {
+                    "type": "string"
+                },
+                "fromLocation": {
+                    "description": "婧愪綅缃�",
+                    "type": "string"
+                },
+                "fromLocationId": {
+                    "description": "婧愪綅缃甶d",
+                    "type": "integer"
+                },
+                "id": {
+                    "type": "integer"
+                },
+                "number": {
+                    "description": "鍗曞彿",
+                    "type": "string"
+                },
+                "operationId": {
+                    "description": "鎿嶄綔id",
+                    "type": "integer"
+                },
+                "operationTypeId": {
+                    "description": "浣滀笟绫诲瀷id",
+                    "type": "integer"
+                },
+                "operationTypeName": {
+                    "description": "浣滀笟绫诲瀷鍚嶇О",
+                    "type": "string"
+                },
+                "operator": {
+                    "description": "鎿嶄綔鑰�",
+                    "type": "string"
+                },
+                "productId": {
+                    "description": "浜у搧id",
+                    "type": "string"
+                },
+                "productName": {
+                    "description": "浜у搧鍚嶇О",
+                    "type": "string"
+                },
+                "toLocation": {
+                    "description": "鐩爣浣嶇疆",
+                    "type": "string"
+                },
+                "toLocationId": {
+                    "description": "鐩爣浣嶇疆id",
+                    "type": "integer"
+                },
+                "unit": {
+                    "description": "鍗曚綅",
+                    "type": "string"
+                },
+                "updateTime": {
+                    "type": "string"
+                },
+                "weight": {
+                    "description": "閲嶉噺",
+                    "type": "number"
+                }
+            }
+        },
         "models.Operation": {
             "type": "object",
             "properties": {
@@ -5519,75 +5656,6 @@
                 },
                 "value": {
                     "description": "鎬讳环鍊�",
-                    "type": "number"
-                }
-            }
-        },
-        "response.InventoryHistory": {
-            "type": "object",
-            "properties": {
-                "amount": {
-                    "description": "鏁伴噺",
-                    "type": "number"
-                },
-                "baseOperationType": {
-                    "description": "鍩虹浣滀笟绫诲瀷",
-                    "allOf": [
-                        {
-                            "$ref": "#/definitions/constvar.BaseOperationType"
-                        }
-                    ]
-                },
-                "contactedName": {
-                    "description": "瀹屾垚鑰�",
-                    "type": "string"
-                },
-                "date": {
-                    "description": "鏃ユ湡",
-                    "type": "string"
-                },
-                "fromLocation": {
-                    "description": "婧愪綅缃�",
-                    "type": "string"
-                },
-                "fromLocationId": {
-                    "type": "integer"
-                },
-                "number": {
-                    "description": "鍗曞彿",
-                    "type": "string"
-                },
-                "operationId": {
-                    "type": "integer"
-                },
-                "operationTypeName": {
-                    "description": "浣滀笟绫诲瀷鍚嶇О",
-                    "type": "string"
-                },
-                "productId": {
-                    "type": "string"
-                },
-                "productName": {
-                    "description": "浜у搧鍚嶇О",
-                    "type": "string"
-                },
-                "status": {
-                    "description": "鐘舵��",
-                    "type": "string"
-                },
-                "toLocation": {
-                    "description": "鐩爣浣嶇疆",
-                    "type": "string"
-                },
-                "toLocationId": {
-                    "type": "integer"
-                },
-                "unit": {
-                    "description": "鍗曚綅",
-                    "type": "string"
-                },
-                "weight": {
-                    "description": "閲嶉噺",
                     "type": "number"
                 }
             }
diff --git a/docs/swagger.json b/docs/swagger.json
index 040b004..73aef55 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -456,6 +456,58 @@
                 }
             }
         },
+        "/api-wms/v1/forms/downloadHistory": {
+            "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.GetInventoryHistory"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/models.MoveHistory"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/forms/downloadInventoryForms": {
             "post": {
                 "produces": [
@@ -568,8 +620,15 @@
                 "tags": [
                     "鎶ヨ〃"
                 ],
-                "summary": "鑾峰彇鍘嗗彶淇℃伅",
+                "summary": "鑾峰彇鍑哄叆搴撴槑缁�",
                 "parameters": [
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
                     {
                         "description": "鏌ヨ鍙傛暟",
                         "name": "object",
@@ -594,7 +653,7 @@
                                         "data": {
                                             "type": "array",
                                             "items": {
-                                                "$ref": "#/definitions/response.InventoryHistory"
+                                                "$ref": "#/definitions/models.MoveHistory"
                                             }
                                         }
                                     }
@@ -3928,6 +3987,84 @@
                 }
             }
         },
+        "models.MoveHistory": {
+            "type": "object",
+            "properties": {
+                "amount": {
+                    "description": "鏁伴噺",
+                    "type": "number"
+                },
+                "baseOperationType": {
+                    "description": "鍩虹浣滀笟绫诲瀷",
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.BaseOperationType"
+                        }
+                    ]
+                },
+                "createTime": {
+                    "type": "string"
+                },
+                "fromLocation": {
+                    "description": "婧愪綅缃�",
+                    "type": "string"
+                },
+                "fromLocationId": {
+                    "description": "婧愪綅缃甶d",
+                    "type": "integer"
+                },
+                "id": {
+                    "type": "integer"
+                },
+                "number": {
+                    "description": "鍗曞彿",
+                    "type": "string"
+                },
+                "operationId": {
+                    "description": "鎿嶄綔id",
+                    "type": "integer"
+                },
+                "operationTypeId": {
+                    "description": "浣滀笟绫诲瀷id",
+                    "type": "integer"
+                },
+                "operationTypeName": {
+                    "description": "浣滀笟绫诲瀷鍚嶇О",
+                    "type": "string"
+                },
+                "operator": {
+                    "description": "鎿嶄綔鑰�",
+                    "type": "string"
+                },
+                "productId": {
+                    "description": "浜у搧id",
+                    "type": "string"
+                },
+                "productName": {
+                    "description": "浜у搧鍚嶇О",
+                    "type": "string"
+                },
+                "toLocation": {
+                    "description": "鐩爣浣嶇疆",
+                    "type": "string"
+                },
+                "toLocationId": {
+                    "description": "鐩爣浣嶇疆id",
+                    "type": "integer"
+                },
+                "unit": {
+                    "description": "鍗曚綅",
+                    "type": "string"
+                },
+                "updateTime": {
+                    "type": "string"
+                },
+                "weight": {
+                    "description": "閲嶉噺",
+                    "type": "number"
+                }
+            }
+        },
         "models.Operation": {
             "type": "object",
             "properties": {
@@ -5507,75 +5644,6 @@
                 },
                 "value": {
                     "description": "鎬讳环鍊�",
-                    "type": "number"
-                }
-            }
-        },
-        "response.InventoryHistory": {
-            "type": "object",
-            "properties": {
-                "amount": {
-                    "description": "鏁伴噺",
-                    "type": "number"
-                },
-                "baseOperationType": {
-                    "description": "鍩虹浣滀笟绫诲瀷",
-                    "allOf": [
-                        {
-                            "$ref": "#/definitions/constvar.BaseOperationType"
-                        }
-                    ]
-                },
-                "contactedName": {
-                    "description": "瀹屾垚鑰�",
-                    "type": "string"
-                },
-                "date": {
-                    "description": "鏃ユ湡",
-                    "type": "string"
-                },
-                "fromLocation": {
-                    "description": "婧愪綅缃�",
-                    "type": "string"
-                },
-                "fromLocationId": {
-                    "type": "integer"
-                },
-                "number": {
-                    "description": "鍗曞彿",
-                    "type": "string"
-                },
-                "operationId": {
-                    "type": "integer"
-                },
-                "operationTypeName": {
-                    "description": "浣滀笟绫诲瀷鍚嶇О",
-                    "type": "string"
-                },
-                "productId": {
-                    "type": "string"
-                },
-                "productName": {
-                    "description": "浜у搧鍚嶇О",
-                    "type": "string"
-                },
-                "status": {
-                    "description": "鐘舵��",
-                    "type": "string"
-                },
-                "toLocation": {
-                    "description": "鐩爣浣嶇疆",
-                    "type": "string"
-                },
-                "toLocationId": {
-                    "type": "integer"
-                },
-                "unit": {
-                    "description": "鍗曚綅",
-                    "type": "string"
-                },
-                "weight": {
-                    "description": "閲嶉噺",
                     "type": "number"
                 }
             }
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 8177db7..23eb568 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -742,6 +742,61 @@
         description: 閲嶉噺
         type: number
     type: object
+  models.MoveHistory:
+    properties:
+      amount:
+        description: 鏁伴噺
+        type: number
+      baseOperationType:
+        allOf:
+        - $ref: '#/definitions/constvar.BaseOperationType'
+        description: 鍩虹浣滀笟绫诲瀷
+      createTime:
+        type: string
+      fromLocation:
+        description: 婧愪綅缃�
+        type: string
+      fromLocationId:
+        description: 婧愪綅缃甶d
+        type: integer
+      id:
+        type: integer
+      number:
+        description: 鍗曞彿
+        type: string
+      operationId:
+        description: 鎿嶄綔id
+        type: integer
+      operationTypeId:
+        description: 浣滀笟绫诲瀷id
+        type: integer
+      operationTypeName:
+        description: 浣滀笟绫诲瀷鍚嶇О
+        type: string
+      operator:
+        description: 鎿嶄綔鑰�
+        type: string
+      productId:
+        description: 浜у搧id
+        type: string
+      productName:
+        description: 浜у搧鍚嶇О
+        type: string
+      toLocation:
+        description: 鐩爣浣嶇疆
+        type: string
+      toLocationId:
+        description: 鐩爣浣嶇疆id
+        type: integer
+      unit:
+        description: 鍗曚綅
+        type: string
+      updateTime:
+        type: string
+      weight:
+        description: 閲嶉噺
+        type: number
+    type: object
   models.Operation:
     properties:
       accountant:
@@ -1831,54 +1886,6 @@
         description: 鎬讳环鍊�
         type: number
     type: object
-  response.InventoryHistory:
-    properties:
-      amount:
-        description: 鏁伴噺
-        type: number
-      baseOperationType:
-        allOf:
-        - $ref: '#/definitions/constvar.BaseOperationType'
-        description: 鍩虹浣滀笟绫诲瀷
-      contactedName:
-        description: 瀹屾垚鑰�
-        type: string
-      date:
-        description: 鏃ユ湡
-        type: string
-      fromLocation:
-        description: 婧愪綅缃�
-        type: string
-      fromLocationId:
-        type: integer
-      number:
-        description: 鍗曞彿
-        type: string
-      operationId:
-        type: integer
-      operationTypeName:
-        description: 浣滀笟绫诲瀷鍚嶇О
-        type: string
-      productId:
-        type: string
-      productName:
-        description: 浜у搧鍚嶇О
-        type: string
-      status:
-        description: 鐘舵��
-        type: string
-      toLocation:
-        description: 鐩爣浣嶇疆
-        type: string
-      toLocationId:
-        type: integer
-      unit:
-        description: 鍗曚綅
-        type: string
-      weight:
-        description: 閲嶉噺
-        type: number
-    type: object
   response.LocationForms:
     properties:
       amount:
@@ -2220,6 +2227,37 @@
       summary: 鎵嬪姩璺戞湀搴︾粺璁″簱瀛樻姤琛�
       tags:
       - 鎶ヨ〃
+  /api-wms/v1/forms/downloadHistory:
+    post:
+      parameters:
+      - description: token
+        in: header
+        name: Authorization
+        required: true
+        type: string
+      - description: 鏌ヨ鍙傛暟
+        in: body
+        name: object
+        required: true
+        schema:
+          $ref: '#/definitions/request.GetInventoryHistory'
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: 鎴愬姛
+          schema:
+            allOf:
+            - $ref: '#/definitions/util.ResponseList'
+            - properties:
+                data:
+                  items:
+                    $ref: '#/definitions/models.MoveHistory'
+                  type: array
+              type: object
+      summary: 涓嬭浇鍑哄叆搴撴槑缁嗘姤琛�
+      tags:
+      - 鎶ヨ〃
   /api-wms/v1/forms/downloadInventoryForms:
     post:
       parameters:
@@ -2285,6 +2323,11 @@
   /api-wms/v1/forms/getHistory:
     post:
       parameters:
+      - description: token
+        in: header
+        name: Authorization
+        required: true
+        type: string
       - description: 鏌ヨ鍙傛暟
         in: body
         name: object
@@ -2302,10 +2345,10 @@
             - properties:
                 data:
                   items:
-                    $ref: '#/definitions/response.InventoryHistory'
+                    $ref: '#/definitions/models.MoveHistory'
                   type: array
               type: object
-      summary: 鑾峰彇鍘嗗彶淇℃伅
+      summary: 鑾峰彇鍑哄叆搴撴槑缁�
       tags:
       - 鎶ヨ〃
   /api-wms/v1/forms/getInventoryForms:
diff --git a/models/move_history.go b/models/move_history.go
index ef75cbd..a83a53a 100644
--- a/models/move_history.go
+++ b/models/move_history.go
@@ -79,7 +79,7 @@
 	return slf
 }
 
-func (slf *MoveHistorySearch) build() *gorm.DB {
+func (slf *MoveHistorySearch) Build() *gorm.DB {
 	var db = slf.Orm.Model(&MoveHistory{})
 
 	if slf.ID != 0 {
@@ -99,7 +99,7 @@
 
 // Create 鍗曟潯鎻掑叆
 func (slf *MoveHistorySearch) Create(record *MoveHistory) error {
-	var db = slf.build()
+	var db = slf.Build()
 
 	if err := db.Create(record).Error; err != nil {
 		return err
@@ -110,7 +110,7 @@
 
 // CreateBatch 鎵归噺鎻掑叆
 func (slf *MoveHistorySearch) CreateBatch(records []*MoveHistory) error {
-	var db = slf.build()
+	var db = slf.Build()
 
 	if err := db.Create(&records).Error; err != nil {
 		return fmt.Errorf("create batch err: %v, records: %+v", err, records)
@@ -120,7 +120,7 @@
 }
 
 func (slf *MoveHistorySearch) Update(record *MoveHistory) error {
-	var db = slf.build()
+	var db = slf.Build()
 
 	if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
 		return fmt.Errorf("save err: %v, record: %+v", err, record)
@@ -131,7 +131,7 @@
 
 func (slf *MoveHistorySearch) UpdateByMap(upMap map[string]interface{}) error {
 	var (
-		db = slf.build()
+		db = slf.Build()
 	)
 
 	if err := db.Updates(upMap).Error; err != nil {
@@ -154,14 +154,14 @@
 }
 
 func (slf *MoveHistorySearch) Delete() error {
-	var db = slf.build()
+	var db = slf.Build()
 	return db.Delete(&MoveHistory{}).Error
 }
 
 func (slf *MoveHistorySearch) First() (*MoveHistory, error) {
 	var (
 		record = new(MoveHistory)
-		db     = slf.build()
+		db     = slf.Build()
 	)
 
 	if err := db.First(record).Error; err != nil {
@@ -175,7 +175,7 @@
 	var (
 		records = make([]*MoveHistory, 0)
 		total   int64
-		db      = slf.build()
+		db      = slf.Build()
 	)
 
 	if err := db.Count(&total).Error; err != nil {
@@ -191,10 +191,25 @@
 	return records, total, nil
 }
 
+// FindAs 鎸夋寚瀹氬舰寮�
+func (slf *MoveHistorySearch) FindAs(obj interface{}) (err error) {
+	var (
+		db = slf.Build()
+	)
+	if slf.PageNum*slf.PageSize > 0 {
+		db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
+	}
+	if err := db.Find(obj).Error; err != nil {
+		return fmt.Errorf("find records err: %v", err)
+	}
+
+	return nil
+}
+
 func (slf *MoveHistorySearch) FindNotTotal() ([]*MoveHistory, error) {
 	var (
 		records = make([]*MoveHistory, 0)
-		db      = slf.build()
+		db      = slf.Build()
 	)
 
 	if slf.PageNum*slf.PageSize > 0 {
@@ -244,3 +259,13 @@
 
 	return records, nil
 }
+
+func (slf *MoveHistorySearch) Count() (int64, error) {
+	var (
+		total int64
+		db    = slf.Build()
+	)
+
+	err := db.Count(&total).Error
+	return total, err
+}
diff --git a/response/report_forms_response.go b/response/report_forms_response.go
index 0aafe8e..5d4f159 100644
--- a/response/report_forms_response.go
+++ b/response/report_forms_response.go
@@ -2,6 +2,7 @@
 
 import (
 	"github.com/shopspring/decimal"
+	"time"
 	"wms/constvar"
 )
 
@@ -20,7 +21,7 @@
 
 type InventoryHistory struct {
 	Number            string                     `json:"number"`            //鍗曞彿
-	Date              string                     `json:"date"`              //鏃ユ湡
+	Date              time.Time                  `json:"date"`              //鏃ユ湡
 	ProductName       string                     `json:"productName"`       //浜у搧鍚嶇О
 	FromLocation      string                     `json:"fromLocation"`      //婧愪綅缃�
 	ToLocation        string                     `json:"toLocation"`        //鐩爣浣嶇疆
diff --git a/router/router.go b/router/router.go
index 541d52f..6cd7916 100644
--- a/router/router.go
+++ b/router/router.go
@@ -156,6 +156,7 @@
 		reportFormsAPI.POST("getInventoryForms", reportFormsController.GetInventoryForms)           //鑾峰彇搴撳瓨鎶ヨ〃
 		reportFormsAPI.POST("downloadInventoryForms", reportFormsController.DownloadInventoryForms) //涓嬭浇搴撳瓨鎶ヨ〃
 		reportFormsAPI.POST("getHistory", reportFormsController.GetHistory)                         //鑾峰彇搴撳瓨鍘嗗彶
+		reportFormsAPI.POST("downloadHistory", reportFormsController.DownloadHistory)               //涓嬭浇搴撳瓨鎶ヨ〃
 		reportFormsAPI.POST("getLocationForms", reportFormsController.GetLocationForms)             //鑾峰彇浣嶇疆鎶ヨ〃
 		reportFormsAPI.POST("downloadLocationForms", reportFormsController.DownloadLocationForms)   //涓嬭浇浣嶇疆鎶ヨ〃
 		reportFormsAPI.POST("monthStats", reportFormsController.MonthStats)                         //鑾峰彇鏈堝害缁熻鎶ヨ〃
diff --git a/service/history_forms.go b/service/history_forms.go
new file mode 100644
index 0000000..dad0ce4
--- /dev/null
+++ b/service/history_forms.go
@@ -0,0 +1,132 @@
+package service
+
+import (
+	"fmt"
+	"github.com/xuri/excelize/v2"
+	"strconv"
+	"time"
+	"wms/constvar"
+	"wms/models"
+	"wms/request"
+	"wms/response"
+)
+
+type HistoryFormsService struct{}
+
+func NewHistoryFormsService() *HistoryFormsService {
+	return &HistoryFormsService{}
+}
+
+func (slf *HistoryFormsService) Query(params request.GetInventoryHistory) (result []*response.InventoryHistory, err error) {
+	search, err := slf.BuildSearch(params)
+	if err != nil {
+		return nil, err
+	}
+
+	if params.Page > 0 && params.PageSize > 0 {
+		search = search.SetPage(params.Page, params.PageSize)
+	}
+	result = make([]*response.InventoryHistory, 0, params.PageSize)
+	err = search.FindAs(&result)
+
+	return result, nil
+}
+
+func (slf *HistoryFormsService) BuildSearch(params request.GetInventoryHistory) (search *models.MoveHistorySearch, err error) {
+	search = models.NewMoveHistorySearch()
+	var (
+		ids []int
+	)
+	if params.KeyWord != "" {
+		ids, _, err = SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize)
+		if err != nil {
+			return
+		}
+		if len(ids) == 0 {
+			return nil, nil
+		}
+	}
+
+	search.Orm = search.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, operation_type_name, weight").Order("id desc")
+	if len(ids) > 0 {
+		search.Orm = search.Orm.Where("id in ?", ids)
+	}
+	if params.BaseOperationType != 0 {
+		search.Orm = search.Orm.Where("base_operation_type = ?", params.BaseOperationType)
+	}
+	return search, err
+}
+
+func (slf *HistoryFormsService) Count(params request.GetInventoryHistory) (total int64, err error) {
+	search, err := slf.BuildSearch(params)
+	if err != nil {
+		return 0, err
+	}
+	total, err = search.Count()
+	return
+}
+
+func (slf *HistoryFormsService) FetchAll(params request.GetInventoryHistory) (list []*response.InventoryHistory, err error) {
+	total, err := slf.Count(params)
+	if err != nil {
+		return nil, err
+	}
+	list = make([]*response.InventoryHistory, 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 *HistoryFormsService) Export(dataList []*response.InventoryHistory, params request.GetInventoryHistory) (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.Date.Format("2006-01-02"))
+		f.SetCellValue("Sheet1", "B"+column, v.Number)
+		f.SetCellValue("Sheet1", "C"+column, v.ProductName)
+		f.SetCellValue("Sheet1", "D"+column, v.ProductId)
+		f.SetCellValue("Sheet1", "E"+column, v.OperationTypeName)
+		f.SetCellValue("Sheet1", "F"+column, v.FromLocation)
+		f.SetCellValue("Sheet1", "G"+column, v.ToLocation)
+		f.SetCellValue("Sheet1", "H"+column, v.Amount)
+		f.SetCellValue("Sheet1", "I"+column, v.Unit)
+		f.SetCellValue("Sheet1", "J"+column, v.Weight)
+	}
+
+	if params.BaseOperationType == constvar.BaseOperationTypeIncoming {
+		fileName = fmt.Sprintf("鍏ュ簱鏄庣粏鎶ヨ〃%s.xlsx", time.Now().Format("2006-01-02-1504"))
+	} else if params.BaseOperationType == constvar.BaseOperationTypeOutgoing {
+		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