From bb5d729a62fa5e6875114018562458ea29ea1649 Mon Sep 17 00:00:00 2001
From: jiangshuai <291802688@qq.com>
Date: 星期五, 17 十一月 2023 18:07:43 +0800
Subject: [PATCH] 打印入库-自制导出excel 增加对取消的统计

---
 controllers/operation.go |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 1e56fd1..b673452 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -7,6 +7,7 @@
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/shopspring/decimal"
+	"github.com/xuri/excelize/v2"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/credentials/insecure"
 	"gorm.io/gorm"
@@ -25,6 +26,8 @@
 	"wms/proto/product_inventory"
 	"wms/proto/purchase_wms"
 	"wms/request"
+	"wms/utils/http"
+	"wms/utils/upload"
 )
 
 type OperationController struct {
@@ -827,3 +830,106 @@
 	}
 	util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛")
 }
+
+// inputSelfmade
+//
+//	@Tags		鍏ュ簱/鍑哄簱
+//	@Summary	鎵撳嵃-鑷埗-鍏ュ簱
+//	@Produce	application/json
+//	@Param		id	path		int			true	"id"
+//	@Success	200	{object}	util.Response	"鎴愬姛"
+//	@Router		/api-wms/v1/operation/inputSelfmade/{id} [put]
+func (slf OperationController) InputSelfmade(c *gin.Context) {
+	id, err := strconv.Atoi(c.Param("id"))
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "閿欒鐨刬d鍊�")
+		return
+	}
+	if id == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "id涓�0")
+		return
+	}
+	operation, err := models.NewOperationSearch().SetPreload(true).SetID(id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error())
+		return
+	}
+
+	readerCloser, err := http.HttpGetWithReadCloser(conf.FileTemplateConf.InputSelfmadeAddr)
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鑾峰彇妯$増澶辫触:"+err.Error())
+		return
+	}
+
+	f, err := excelize.OpenReader(readerCloser)
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "璇诲彇excel妯$増澶辫触锛�"+err.Error())
+		return
+	}
+	readerCloser.Close()
+
+	style, _ := f.NewStyle(&excelize.Style{
+		Border: []excelize.Border{
+			{Type: "left", Color: "#000000", Style: 2},
+			{Type: "top", Color: "#000000", Style: 2},
+			{Type: "bottom", Color: "#000000", Style: 2},
+			{Type: "right", Color: "#000000", Style: 2},
+		},
+		Alignment: &excelize.Alignment{
+			Horizontal: "center",
+			Vertical:   "center",
+		},
+	})
+	styleLeft, _ := f.NewStyle(&excelize.Style{
+		Alignment: &excelize.Alignment{
+			Horizontal: "left",
+			Vertical:   "center",
+		},
+	})
+
+	f.SetCellValue("Sheet1", "B3", operation.CompanyName)
+	f.SetCellValue("Sheet1", "E3", operation.OperationDate)
+	f.SetCellValue("Sheet1", "H3", operation.Number)
+
+	rowIndex := 5
+	totalAmount := decimal.NewFromInt(0)
+	for _, v := range operation.Details {
+		f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), v.ProductId)
+		f.SetCellValue("Sheet1", "B"+strconv.Itoa(rowIndex), v.Product.Name)
+		f.SetCellValue("Sheet1", "C"+strconv.Itoa(rowIndex), v.Product.Specs)
+		f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), v.Product.Unit)
+		f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), v.Amount.String())
+		f.SetCellValue("Sheet1", "H"+strconv.Itoa(rowIndex), operation.ToLocation.Name)
+		f.SetCellValue("Sheet1", "I"+strconv.Itoa(rowIndex), operation.Comment)
+		rowIndex++
+		totalAmount = totalAmount.Add(v.Amount)
+	}
+	if err := f.MergeCell("Sheet1", "A"+strconv.Itoa(rowIndex), "C"+strconv.Itoa(rowIndex)); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+	f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), "鍚堣")
+	f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), totalAmount.String())
+	f.SetCellStyle("Sheet1", "A4", "I"+strconv.Itoa(rowIndex), style)
+	rowIndex++
+
+	f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), "瀹℃牳锛�")
+	f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), "妫�楠岋細")
+	f.SetCellValue("Sheet1", "G"+strconv.Itoa(rowIndex), "淇濈锛�")
+	f.SetCellStyle("Sheet1", "A"+strconv.Itoa(rowIndex), "G"+strconv.Itoa(rowIndex), styleLeft)
+
+	buf, err := f.WriteToBuffer()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), "xlsx", buf.Bytes())
+	if err != nil {
+		logx.Errorf("file upload err: %v", err)
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	util.ResponseFormat(c, code.Success, fileUrl)
+}

--
Gitblit v1.8.0