From 2f856eaa7e46c884f1cb7ad721919a086d7f34a3 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 13 六月 2024 15:57:45 +0800
Subject: [PATCH] 出入库明细列表和报表增加多单位

---
 response/report_forms_response.go |    1 
 models/move_history.go            |    5 +
 service/history_forms.go          |   58 ++++++++++++++++---
 service/more_units.go             |   52 +++++++++++++++++
 docs/swagger.yaml                 |   11 +++
 docs/docs.go                      |   16 +++++
 docs/swagger.json                 |   16 +++++
 7 files changed, 149 insertions(+), 10 deletions(-)

diff --git a/docs/docs.go b/docs/docs.go
index 7837c42..3556fb5 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -4149,6 +4149,12 @@
                         }
                     ]
                 },
+                "checkedAt": {
+                    "type": "string"
+                },
+                "checkedBy": {
+                    "type": "string"
+                },
                 "comment": {
                     "type": "string"
                 },
@@ -4162,6 +4168,9 @@
                     "type": "integer"
                 },
                 "contacterName": {
+                    "type": "string"
+                },
+                "createBy": {
                     "type": "string"
                 },
                 "createTime": {
@@ -5748,6 +5757,13 @@
                     "description": "鏁伴噺",
                     "type": "number"
                 },
+                "amountMoreUnits": {
+                    "description": "鍦ㄥ簱鏁伴噺澶氬崟浣�",
+                    "type": "array",
+                    "items": {
+                        "$ref": "#/definitions/models.UnitItems"
+                    }
+                },
                 "locationId": {
                     "description": "浣嶇疆id",
                     "type": "integer"
diff --git a/docs/swagger.json b/docs/swagger.json
index 36ca1e2..3b27c5b 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -4137,6 +4137,12 @@
                         }
                     ]
                 },
+                "checkedAt": {
+                    "type": "string"
+                },
+                "checkedBy": {
+                    "type": "string"
+                },
                 "comment": {
                     "type": "string"
                 },
@@ -4150,6 +4156,9 @@
                     "type": "integer"
                 },
                 "contacterName": {
+                    "type": "string"
+                },
+                "createBy": {
                     "type": "string"
                 },
                 "createTime": {
@@ -5736,6 +5745,13 @@
                     "description": "鏁伴噺",
                     "type": "number"
                 },
+                "amountMoreUnits": {
+                    "description": "鍦ㄥ簱鏁伴噺澶氬崟浣�",
+                    "type": "array",
+                    "items": {
+                        "$ref": "#/definitions/models.UnitItems"
+                    }
+                },
                 "locationId": {
                     "description": "浣嶇疆id",
                     "type": "integer"
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index ebfef94..c470564 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -809,6 +809,10 @@
         allOf:
         - $ref: '#/definitions/constvar.BaseOperationType'
         description: 鍩虹浣滀笟绫诲瀷
+      checkedAt:
+        type: string
+      checkedBy:
+        type: string
       comment:
         type: string
       companyID:
@@ -818,6 +822,8 @@
       contacterID:
         type: integer
       contacterName:
+        type: string
+      createBy:
         type: string
       createTime:
         type: string
@@ -1912,6 +1918,11 @@
       amount:
         description: 鏁伴噺
         type: number
+      amountMoreUnits:
+        description: 鍦ㄥ簱鏁伴噺澶氬崟浣�
+        items:
+          $ref: '#/definitions/models.UnitItems'
+        type: array
       locationId:
         description: 浣嶇疆id
         type: integer
diff --git a/models/move_history.go b/models/move_history.go
index a83a53a..4d7b10d 100644
--- a/models/move_history.go
+++ b/models/move_history.go
@@ -19,6 +19,7 @@
 		OperationTypeName string                     `json:"operationTypeName" gorm:"type:varchar(127);not null;comment:浣滀笟绫诲瀷鍚嶇О"` //浣滀笟绫诲瀷鍚嶇О
 		OperationId       int                        `json:"operationId" gorm:"type:int;not null;comment:鎿嶄綔id"`                  //鎿嶄綔id
 		ProductId         string                     `json:"productId" gorm:"type:varchar(255);not null;comment:浜у搧id"`           //浜у搧id
+		Product           Material                   `json:"-" gorm:"foreignKey:ProductId"`                                      //浜у搧
 		ProductName       string                     `json:"productName" gorm:"type:varchar(255);not null;comment:浜у搧鍚嶇О"`         //浜у搧鍚嶇О
 		Amount            decimal.Decimal            `json:"amount" gorm:"type:decimal(20,2);not null;comment:鏁伴噺"`               //鏁伴噺
 		Unit              string                     `json:"unit" gorm:"type:char(10);not null;comment:鍗曚綅"`                      //鍗曚綅
@@ -94,6 +95,10 @@
 		db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
 	}
 
+	if slf.Preload {
+		db = db.Model(&MoveHistory{}).Preload("Product")
+	}
+
 	return db
 }
 
diff --git a/response/report_forms_response.go b/response/report_forms_response.go
index 5d12343..a302424 100644
--- a/response/report_forms_response.go
+++ b/response/report_forms_response.go
@@ -31,6 +31,7 @@
 	FromLocation      string                     `json:"fromLocation"`      //婧愪綅缃�
 	ToLocation        string                     `json:"toLocation"`        //鐩爣浣嶇疆
 	Amount            decimal.Decimal            `json:"amount"`            //鏁伴噺
+	AmountMoreUnits   []models.UnitItems         `json:"amountMoreUnits"`   //鏁伴噺澶氬崟浣�
 	Unit              string                     `json:"unit"`              //鍗曚綅
 	ContactedName     string                     `json:"contactedName"`     //瀹屾垚鑰�
 	Status            string                     `json:"status"`            //鐘舵��
diff --git a/service/history_forms.go b/service/history_forms.go
index 14e212e..02b27bf 100644
--- a/service/history_forms.go
+++ b/service/history_forms.go
@@ -19,6 +19,7 @@
 
 func (slf *HistoryFormsService) Query(params request.GetInventoryHistory) (result []*response.InventoryHistory, err error) {
 	search, err := slf.BuildSearch(params)
+	search = search.SetPreload(true)
 	if err != nil {
 		return nil, err
 	}
@@ -27,7 +28,33 @@
 		search = search.SetPage(params.Page, params.PageSize)
 	}
 	result = make([]*response.InventoryHistory, 0, params.PageSize)
-	err = search.FindAs(&result)
+	list, err := search.FindNotTotal()
+
+	for _, v := range list {
+		data := &response.InventoryHistory{
+			Number:            v.Number,
+			Date:              v.UpdatedAt,
+			ProductName:       v.ProductName,
+			FromLocation:      v.FromLocation,
+			ToLocation:        v.ToLocation,
+			Amount:            v.Amount,
+			AmountMoreUnits:   nil,
+			Unit:              v.Unit,
+			ContactedName:     v.Operator,
+			BaseOperationType: v.BaseOperationType,
+			Weight:            v.Weight,
+			ProductId:         v.ProductId,
+			FromLocationId:    v.FromLocationId,
+			ToLocationId:      v.ToLocationId,
+			OperationId:       v.OperationId,
+			OperationTypeName: v.OperationTypeName,
+		}
+		moreUnit := v.Product.MoreUnit
+		if moreUnit != nil && *moreUnit {
+			data.AmountMoreUnits = CreateMoreUnit(v.Amount, v.Product.MoreUnitList)
+		}
+		result = append(result, data)
+	}
 
 	return result, nil
 }
@@ -48,8 +75,8 @@
 	}
 
 	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")
+		Select("number, updated_at, product_name, from_location_id, operation_id,to_location_id, amount, " +
+			"unit, operator, 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)
 	}
@@ -96,16 +123,26 @@
 	f := excelize.NewFile()
 
 	// 鑷畾涔夎〃澶�
-	headers := []string{"鏃ユ湡", "鍗曞彿", "浜у搧", "浜у搧缂栫爜", "涓氬姟绫诲瀷", "浠�", "鑷�", "鏁伴噺", "鍗曚綅", "閲嶉噺"}
+	headers := []interface{}{"鏃ユ湡", "鍗曞彿", "浜у搧", "浜у搧缂栫爜", "涓氬姟绫诲瀷", "浠�", "鑷�", map[string][]string{"鏁伴噺": {"浠�", "鍖�", "绫�", "閲嶉噺"}}, "鍗曚綅", "閲嶉噺"}
 
 	// 璁剧疆琛ㄥご
-	for i, header := range headers {
-		cell := getColumnAlphabet(i) + "1"
-		f.SetCellValue("Sheet1", cell, header)
+	if err := SetExcelHeader(headers, f); err != nil {
+		return "", err
 	}
 
+	style, err := SetHeaderStyle(f)
+	if err != nil {
+		return "", err
+	}
+
+	lastColumn := getColumnAlphabet(13)
+	f.SetCellStyle("Sheet1", "A1", lastColumn+"2", style)
+	// 璁剧疆鍒楀
+	f.SetColWidth("Sheet1", "A", "G", 30)
+	f.SetColWidth("Sheet1", "H", "K", 15)
+
 	for i, v := range dataList {
-		column := strconv.Itoa(i + 2)
+		column := strconv.Itoa(i + 3)
 		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)
@@ -114,8 +151,9 @@
 		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)
+		FillMoreUnitToExcel(v.Amount, v.AmountMoreUnits, 7, i+3, f)
+		f.SetCellValue("Sheet1", "L"+column, v.Unit)
+		f.SetCellValue("Sheet1", "M"+column, v.Weight)
 	}
 
 	if params.BaseOperationType == constvar.BaseOperationTypeIncoming {
diff --git a/service/more_units.go b/service/more_units.go
index 8671ce3..3a4cf49 100644
--- a/service/more_units.go
+++ b/service/more_units.go
@@ -1,6 +1,7 @@
 package service
 
 import (
+	"errors"
 	"github.com/shopspring/decimal"
 	"github.com/xuri/excelize/v2"
 	"strconv"
@@ -33,3 +34,54 @@
 	}
 	return
 }
+
+func SetExcelHeader(headers []interface{}, f *excelize.File) (err error) {
+	// 璁剧疆琛ㄥご
+	var i int
+	for _, h := range headers {
+		if v, ok := h.(string); ok {
+			err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", v)
+			if err != nil {
+				return
+			}
+			i++
+		} else if childHeaders, ok := h.(map[string][]string); ok {
+			for title, list := range childHeaders {
+				err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", title)
+				if err != nil {
+					return err
+				}
+				err = f.MergeCell("Sheet1", getColumnAlphabet(i)+"1", getColumnAlphabet(i-1+len(list))+"1") // 鍚堝苟鍗曞厓鏍�
+				for _, t := range list {
+					err = f.SetCellValue("Sheet1", getColumnAlphabet(i)+"2", t)
+					if err != nil {
+						return err
+					}
+					i++
+				}
+			}
+		} else {
+			return errors.New("unsupported header value")
+		}
+	}
+	return nil
+}
+
+func SetHeaderStyle(f *excelize.File) (styleInt int, err error) {
+	// 璁剧疆琛ㄥご鏍峰紡
+	style := &excelize.Style{
+		Border: nil,
+		Fill: excelize.Fill{
+			Type:    "pattern",
+			Pattern: 1,
+			Shading: 0,
+		},
+		Font: &excelize.Font{
+			Bold: true,
+		},
+		Alignment: &excelize.Alignment{
+			Horizontal: "center",
+		},
+	}
+	return f.NewStyle(style)
+}

--
Gitblit v1.8.0