From 5c61bdb147458adbb0a87b27c2299f86f739da0a Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 29 七月 2024 20:33:53 +0800
Subject: [PATCH] 新增出入库调拨操作时明细支持多单位传参

---
 models/operation_details.go |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/models/operation_details.go b/models/operation_details.go
index a00b670..f57c5b3 100644
--- a/models/operation_details.go
+++ b/models/operation_details.go
@@ -1,6 +1,7 @@
 package models
 
 import (
+	"encoding/json"
 	"fmt"
 	"github.com/shopspring/decimal"
 	"gorm.io/gorm"
@@ -39,6 +40,9 @@
 		// 鍢夎仈浠撳偍娣诲姞 SilkMarket銆丼ilkMarketClose
 		SilkMarket      string `json:"silkMarket" gorm:"type:varchar(255);comment:搴勫彛"`       // 搴勫彛
 		SilkMarketClose string `json:"silkMarketClose" gorm:"type:varchar(10);comment:搴勫彛鍏抽棴"` // 搴勫彛鍏抽棴
+
+		MoreUnitList  []UnitItems `json:"moreUnitList" gorm:"-"`
+		MoreUnitValue string      `json:"-" gorm:"type:varchar(255);comment:澶氬崟浣嶅��"`
 	}
 
 	OperationDetailsSearch struct {
@@ -62,6 +66,37 @@
 	return &OperationDetailsSearch{Orm: mysqlx.GetDB()}
 }
 
+func (slf *OperationDetails) AfterFind(tx *gorm.DB) (err error) {
+	if slf.MoreUnitValue != "" {
+		var arr []UnitItems
+		err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr)
+		if err != nil {
+			return err
+		}
+		slf.MoreUnitList = arr
+	}
+
+	return
+}
+
+func (slf *OperationDetails) BeforeCreate(tx *gorm.DB) (err error) {
+	if len(slf.MoreUnitList) != 0 {
+		items := make([]UnitItems, 0)
+		for k, item := range slf.MoreUnitList {
+			if item.Unit != "" && !item.Amount.IsZero() {
+				items = append(items, slf.MoreUnitList[k])
+			}
+		}
+
+		str, err := json.Marshal(items)
+		if err != nil {
+			return err
+		}
+		slf.MoreUnitValue = string(str)
+	}
+	return
+}
+
 func (slf *OperationDetailsSearch) SetOrm(tx *gorm.DB) *OperationDetailsSearch {
 	slf.Orm = tx
 	return slf

--
Gitblit v1.8.0