From 2531387e99a35c0717df28da79fcc1f46dbf7a4c Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 26 六月 2024 16:29:47 +0800
Subject: [PATCH] 定时任务时区改local

---
 controllers/operation.go |  292 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 257 insertions(+), 35 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index b9053f9..2cc1671 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -3,11 +3,13 @@
 import (
 	"context"
 	"errors"
+	"fmt"
 	"github.com/gin-gonic/gin"
 	uuid "github.com/satori/go.uuid"
 	"github.com/shopspring/decimal"
 	"github.com/xuri/excelize/v2"
 	"gorm.io/gorm"
+	"io/ioutil"
 	"sort"
 	"strconv"
 	"strings"
@@ -485,19 +487,19 @@
 		}
 
 		if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing || operation.BaseOperationType == constvar.BaseOperationTypeDisuse {
-			if err := service.FinishOperationOutput(tx, listDetails, mapLocAmount); err != nil {
+			if err := service.FinishOperationOutput(tx, listDetails, mapLocAmount, operation); err != nil {
 				return err
 			}
 		}
 
 		if operation.BaseOperationType == constvar.BaseOperationTypeInternal {
-			if err := service.FinishOperationInternal(tx, listDetails); err != nil {
+			if err := service.FinishOperationInternal(tx, listDetails, operation); err != nil {
 				return err
 			}
 		}
 
 		if operation.BaseOperationType == constvar.BaseOperationTypeAdjust {
-			if err := service.FinishOperationAdjust(tx, listDetails, mapLocAmount); err != nil {
+			if err := service.FinishOperationAdjust(tx, listDetails, mapLocAmount, operation); err != nil {
 				return err
 			}
 		}
@@ -696,7 +698,7 @@
 	util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛")
 }
 
-// outputOperation
+// OutputOperation
 //
 //	@Tags		鍏ュ簱/鍑哄簱
 //	@Summary	鎵撳嵃
@@ -714,6 +716,7 @@
 		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())
@@ -725,18 +728,47 @@
 	}
 
 	var fileUrl string
+	// 鑾峰彇鍏徃鍚嶇О
 	companyName := conf.WebConf.CompanyName
 	if companyName == "" {
 		util.ResponseFormat(c, code.RequestParamError, "缂哄皯宸ュ巶鍚嶇О閰嶇疆")
 		return
 	}
+	// BaseOperationTypeIncoming 鏀惰揣
 	if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
 		if companyName == "jialian" {
-			fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianInput, operation)
+			// 鍏ュ簱
+			oT, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
 			if err != nil {
 				util.ResponseFormat(c, code.RequestParamError, err.Error())
 				return
 			}
+			warehouse, err := models.NewWarehouseSearch().SetID(oT.WarehouseId).First()
+			if err != nil {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+			if err != nil {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+
+			if warehouse.FileTemplateCategoryIn == constvar.FileTemplateCategory_JialianInput1 {
+				fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryIn, operation)
+				if err != nil {
+					util.ResponseFormat(c, code.RequestParamError, err.Error())
+					return
+				}
+
+			} else if warehouse.FileTemplateCategoryIn == constvar.FileWarehouseCategory_JialianInput2 {
+				fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryIn, operation)
+				if err != nil {
+					util.ResponseFormat(c, code.RequestParamError, err.Error())
+					return
+				}
+
+			}
+
 		} else if companyName == "geruimi" {
 			fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Selfmade, operation)
 			if err != nil {
@@ -744,12 +776,37 @@
 				return
 			}
 		}
-	} else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
+	} else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing { // 浜よ揣
 		if companyName == "jialian" {
-			fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput, operation)
+			oT, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
 			if err != nil {
 				util.ResponseFormat(c, code.RequestParamError, err.Error())
 				return
+			}
+			warehouse, err := models.NewWarehouseSearch().SetID(oT.WarehouseId).First()
+			if err != nil {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+			if err != nil {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+
+			if warehouse.FileTemplateCategoryOut == constvar.FileTemplateCategory_JialianOutput1 {
+				fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryOut, operation)
+				if err != nil {
+					util.ResponseFormat(c, code.RequestParamError, err.Error())
+					return
+				}
+
+			} else if warehouse.FileTemplateCategoryOut == constvar.FileWarehouseCategory_JialianOutput2 {
+				fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryOut, operation)
+				if err != nil {
+					util.ResponseFormat(c, code.RequestParamError, err.Error())
+					return
+				}
+
 			}
 		} else if companyName == "geruimi" {
 			fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Output, operation)
@@ -769,39 +826,88 @@
 	util.ResponseFormat(c, code.Success, m)
 }
 
-func JialianOperation(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
+func JialianOperation1(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
+	// 鑾峰彇妯℃澘璁板綍
 	template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First()
 	if err != nil {
 		return "", errors.New("鑾峰彇妯$増璁板綍澶辫触:" + err.Error())
 	}
+	fmt.Println(template)
+	// 鏍规嵁妯℃澘璺緞鑾峰彇妯℃澘娑堟伅
 	readerCloser, err := http.HttpGetWithReadCloser(template.FileUrl)
 	if err != nil {
 		return "", errors.New("鑾峰彇妯$増澶辫触:" + err.Error())
 	}
 
+	// 璇诲彇妯℃澘鍐呭
 	f, err := excelize.OpenReader(readerCloser)
+	//fl, err := os.Open("E:\\瀹剁汉鍑哄叆搴撳崟.xlsx")
+	//f, err := excelize.OpenReader(fl)
 	if err != nil {
 		return "", errors.New("璇诲彇excel妯$増澶辫触锛�" + err.Error())
 	}
 	readerCloser.Close()
+
 	defer f.Close()
 
-	f.SetCellValue("Sheet1", "H2", operation.OperationDate)
-	f.SetCellValue("Sheet1", "O2", operation.Number)
+	sheet := "Sheet1"
+	if category == constvar.FileWarehouseCategory_JialianInput2 {
+		sheet = "鍏ュ簱鍗�"
+	} else if category == constvar.FileWarehouseCategory_JialianOutput2 {
+		sheet = "鍑哄簱鍗�"
+	}
 
-	rowIndex := 5
-	totalAmount := decimal.NewFromInt(0)
-	totalPrice := decimal.NewFromInt(0)
+	// 绗簩琛�
+	f.SetCellValue(sheet, "K2", operation.Number) // 鍗曞彿
+	// 绗笁琛�
+	f.SetCellValue(sheet, "D3", operation.CompanyName) // 鍏徃
+	f.SetCellValue(sheet, "G3", "")                    // 閮ㄩ棬
+
+	// 鏃堕棿
+	date := operation.OperationDate
+	f.SetCellValue(sheet, "I3", date[0:4])  // 骞�
+	f.SetCellValue(sheet, "L3", date[5:7])  // 鏈�
+	f.SetCellValue(sheet, "N3", date[8:10]) // 鏃�
+
+	var entityIDs []string
+	for _, v := range operation.Details {
+		entityIDs = append(entityIDs, v.ProductId)
+	}
+	attributeMap := make(map[string]uint)
+	attributeValueMap := make(map[string]string)
+	attributes, err := models.NewAttributeSearch().SetEntityType(1).FindNotTotal()
+	if err != nil {
+		return "", errors.New("鑾峰彇鍔ㄦ�佸睘鎬уけ璐ワ細" + err.Error())
+	}
+	for _, v := range attributes {
+		attributeMap[v.Name] = v.ID
+	}
+	attributeValues, err := models.NewAttributeValueSearch().SetEntityIDs(entityIDs).FindNotTotal()
+	if err != nil {
+		return "", errors.New("鑾峰彇鍔ㄦ�佸睘鎬у�煎け璐ワ細" + err.Error())
+	}
+	for _, v := range attributeValues {
+		attributeValueMap[fmt.Sprintf("%d%s", v.AttributeID, v.EntityID)] = v.Value
+	}
+
+	// 绗叚琛� 寰幆濉��
+	totalAmount := decimal.NewFromInt(0) // 鎬婚噾棰�
+	totalPrice := decimal.NewFromInt(0)  // 鎬讳环
+	rowIndex := 6
 	for i, v := range operation.Details {
-		//璁剧疆琛ㄥ崟鏈�澶�9鏉℃暟鎹�
-		if i > 8 {
+		//璁剧疆琛ㄥ崟鏈�澶�6鏉℃暟鎹�
+		if i > 6 {
 			break
 		}
-		f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), v.Product.Name)
-		f.SetCellValue("Sheet1", "B"+strconv.Itoa(rowIndex), v.Product.Type)
-		f.SetCellValue("Sheet1", "C"+strconv.Itoa(rowIndex), v.Product.Unit)
-		f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), v.Amount.String())
-		f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), v.Product.SalePrice.String())
+		f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Name)                                                          // 浜у搧鍚嶇О
+		f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Specs)                                                         //瑙勬牸
+		f.SetCellValue(sheet, "D"+strconv.Itoa(rowIndex), attributeValueMap[fmt.Sprintf("%d%s", attributeMap["棰滆壊"], v.ProductId)]) //棰滆壊
+		f.SetCellValue(sheet, "E"+strconv.Itoa(rowIndex), attributeValueMap[fmt.Sprintf("%d%s", attributeMap["闈㈡枡"], v.ProductId)]) //闈㈡枡
+		f.SetCellValue(sheet, "F"+strconv.Itoa(rowIndex), v.TotalGrossWeight.String())                                             //閲嶉噺
+		f.SetCellValue(sheet, "G"+strconv.Itoa(rowIndex), v.Amount.String())                                                       //鏁伴噺
+		f.SetCellValue(sheet, "H"+strconv.Itoa(rowIndex), v.Product.SalePrice.String())                                            // 鍗曚环
+
+		// 閲戦
 		if !v.Product.SalePrice.IsZero() {
 			ap := v.Amount.Mul(v.Product.SalePrice)
 			totalPrice = totalPrice.Add(ap)
@@ -809,64 +915,180 @@
 			split := strings.Split(price, ".")
 			if len(split) == 2 {
 				for n, a := range split[1] {
-					f.SetCellValue("Sheet1", string('M'+n)+strconv.Itoa(rowIndex), string(a))
+					f.SetCellValue(sheet, string('N'+n)+strconv.Itoa(rowIndex), string(a))
 				}
 			}
 			s := split[0]
 			n := 0
 			for j := len(s) - 1; j >= 0; j-- {
 				if n == 6 {
-					f.SetCellValue("Sheet1", string('L'-n)+strconv.Itoa(rowIndex), s[:j])
+					f.SetCellValue(sheet, string('M'-n)+strconv.Itoa(rowIndex), s[:j])
 					break
 				}
-				f.SetCellValue("Sheet1", string('L'-n)+strconv.Itoa(rowIndex), string(s[j]))
+				f.SetCellValue(sheet, string('M'-n)+strconv.Itoa(rowIndex), string(s[j]))
 				n++
 			}
 		}
-
 		rowIndex++
 		totalAmount = totalAmount.Add(v.Amount)
 	}
+
 	//鍚堣
 	if !totalPrice.IsZero() {
 		price := totalPrice.String()
 		split := strings.Split(price, ".")
 		if len(split) == 2 {
 			for n, a := range split[1] {
-				f.SetCellValue("Sheet1", string('M'+n)+"14", string(a))
+				f.SetCellValue(sheet, string('M'+n)+"12", string(a))
 			}
 		} else {
-			f.SetCellValue("Sheet1", "M14", "0")
-			f.SetCellValue("Sheet1", "N14", "0")
+			f.SetCellValue(sheet, "N12", "0")
+			f.SetCellValue(sheet, "O12", "0")
 		}
 		s := split[0]
 		n := 0
 		for j := len(s) - 1; j >= 0; j-- {
 			if n == 6 {
-				f.SetCellValue("Sheet1", string('L'-n)+"14", s[:j])
+				f.SetCellValue(sheet, string('M'-n)+"12", s[:j])
 				break
 			}
-			f.SetCellValue("Sheet1", string('L'-n)+"14", string(s[j]))
+			f.SetCellValue(sheet, string('M'-n)+"12", string(s[j]))
 			n++
 		}
 	}
-	f.SetCellValue("Sheet1", "D14", totalAmount)
 
-	f.SetCellValue("Sheet1", "B15", operation.Manager)
-	f.SetCellValue("Sheet1", "D15", operation.Accountant)
-	f.SetCellValue("Sheet1", "F15", operation.Custodian)
-
+	// 澶囨敞 绗�13琛�
+	f.SetCellValue(sheet, "B13", "澶囨敞锛�"+operation.Remark)
+	// 绗�14琛�
+	f.SetCellValue(sheet, "C14", operation.Manager)    // 瀹℃牳
+	f.SetCellValue(sheet, "F14", operation.Accountant) // 淇濈
+	f.SetCellValue(sheet, "I14", operation.Custodian)  //缁忓姙浜�
 	buf, err := f.WriteToBuffer()
 	if err != nil {
 		return "", err
 	}
 
 	fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes())
+	ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
+	if err != nil {
+		//ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
+		logx.Errorf("file upload err: %v", err)
+		return "", err
+	}
+	return fileUrl, nil
+}
+
+func JialianOperation2(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
+	// 鑾峰彇妯℃澘璁板綍
+	template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First()
+	if err != nil {
+		return "", errors.New("鑾峰彇妯$増璁板綍澶辫触:" + err.Error())
+	}
+	fmt.Println(template)
+	// 鏍规嵁妯℃澘璺緞鑾峰彇妯℃澘娑堟伅
+	readerCloser, err := http.HttpGetWithReadCloser(template.FileUrl)
+	if err != nil {
+		return "", errors.New("鑾峰彇妯$増澶辫触:" + err.Error())
+	}
+
+	// 璇诲彇妯℃澘鍐呭
+	f, err := excelize.OpenReader(readerCloser)
+	if err != nil {
+		return "", errors.New("璇诲彇excel妯$増澶辫触锛�" + err.Error())
+	}
+	readerCloser.Close()
+
+	defer f.Close()
+
+	sheet := "Sheet1"
+	f.SetCellValue(sheet, "A2", "鍗曚綅(閮ㄩ棬):"+operation.CompanyName) // 鍗曚綅閮ㄩ棬
+	f.SetCellValue(sheet, "H2", operation.OperationDate)         // 鏃堕棿
+	f.SetCellValue(sheet, "O2", operation.Number)                // 缂栧彿
+
+	totalAmount := decimal.NewFromInt(0) // 鎬婚噾棰�
+	totalPrice := decimal.NewFromInt(0)  // 鎬讳环
+	rowIndex := 5
+	for i, v := range operation.Details {
+		//璁剧疆琛ㄥ崟鏈�澶�9鏉℃暟鎹�
+		if i > 9 {
+			break
+		}
+		f.SetCellValue(sheet, "A"+strconv.Itoa(rowIndex), v.Product.Name)               // 浜у搧鍚嶇О
+		f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Specs)              //瑙勬牸
+		f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.TotalGrossWeight.String())  //閲嶉噺
+		f.SetCellValue(sheet, "D"+strconv.Itoa(rowIndex), v.Amount.String())            //鏁伴噺
+		f.SetCellValue(sheet, "E"+strconv.Itoa(rowIndex), v.AuxiliaryAmount.String())   //杈呭姪鏁伴噺
+		f.SetCellValue(sheet, "F"+strconv.Itoa(rowIndex), v.AuxiliaryUnit)              //杈呭姪鍗曚綅
+		f.SetCellValue(sheet, "G"+strconv.Itoa(rowIndex), v.Product.SalePrice.String()) // 鍗曚环
+
+		// 閲戦
+		if !v.Product.SalePrice.IsZero() {
+			ap := v.Amount.Mul(v.Product.SalePrice)
+			totalPrice = totalPrice.Add(ap)
+			price := ap.String()
+			split := strings.Split(price, ".")
+			if len(split) == 2 {
+				for n, a := range split[1] {
+					f.SetCellValue(sheet, string('O'+n)+strconv.Itoa(rowIndex), string(a))
+				}
+			}
+			s := split[0]
+			n := 0
+			for j := len(s) - 1; j >= 0; j-- {
+				if n == 6 {
+					f.SetCellValue(sheet, string('N'-n)+strconv.Itoa(rowIndex), s[:j])
+					break
+				}
+				f.SetCellValue(sheet, string('N'-n)+strconv.Itoa(rowIndex), string(s[j]))
+				n++
+			}
+		}
+		rowIndex++
+		totalAmount = totalAmount.Add(v.Amount)
+	}
+
+	//鍚堣
+	if !totalPrice.IsZero() {
+		price := totalPrice.String()
+		split := strings.Split(price, ".")
+		if len(split) == 2 {
+			for n, a := range split[1] {
+				f.SetCellValue(sheet, string('N'+n)+"14", string(a))
+			}
+		} else {
+			f.SetCellValue(sheet, "O14", "0")
+			f.SetCellValue(sheet, "P14", "0")
+		}
+		s := split[0]
+		n := 0
+		for j := len(s) - 1; j >= 0; j-- {
+			if n == 6 {
+				f.SetCellValue(sheet, string('N'-n)+"14", s[:j])
+				break
+			}
+			f.SetCellValue(sheet, string('N'-n)+"14", string(s[j]))
+			n++
+		}
+	}
+
+	f.SetCellValue(sheet, "Q5", operation.Remark) // 澶囨敞
+
+	// 绗�14琛�
+	f.SetCellValue(sheet, "B15", operation.Manager)    // 涓荤
+	f.SetCellValue(sheet, "D15", operation.Accountant) // 浼氳
+	f.SetCellValue(sheet, "H15", operation.Custodian)  //淇濈鍛�
+	f.SetCellValue(sheet, "Q15", operation.Custodian)  //缁忔墜
+	buf, err := f.WriteToBuffer()
+	if err != nil {
+		return "", err
+	}
+
+	fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes())
+	ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
 	if err != nil {
 		logx.Errorf("file upload err: %v", err)
 		return "", err
 	}
-
 	return fileUrl, nil
 }
 

--
Gitblit v1.8.0