From 2bfc37cbc679ceb5cef3b714e93ce4ebf55f9512 Mon Sep 17 00:00:00 2001
From: jiangshuai <291802688@qq.com>
Date: 星期三, 08 十一月 2023 17:24:58 +0800
Subject: [PATCH] 修改缩略图尺寸 config文件覆盖修复

---
 controllers/operation.go |  369 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 304 insertions(+), 65 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 21b953d..5bd16ef 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -1,11 +1,13 @@
 package controllers
 
 import (
+	"encoding/json"
 	"errors"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/shopspring/decimal"
 	"gorm.io/gorm"
+	"os"
 	"sort"
 	"strconv"
 	"time"
@@ -13,6 +15,7 @@
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
+	"wms/opa"
 	"wms/pkg/logx"
 	"wms/pkg/structx"
 	"wms/request"
@@ -71,7 +74,7 @@
 }
 
 func CheckDetailsRepeat(details []*models.OperationDetails) bool {
-	detailIDs := []string{}
+	var detailIDs []string
 	var tempID string
 	for _, v := range details {
 		detailIDs = append(detailIDs, v.ProductId)
@@ -314,7 +317,7 @@
 		util.ResponseFormat(c, code.RequestParamError, "id涓�0")
 		return
 	}
-	operation, err := models.NewOperationSearch().SetID(id).First()
+	operation, err := models.NewOperationSearch().SetPreload(true).SetID(id).First()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error())
 		return
@@ -323,17 +326,12 @@
 		util.ResponseFormat(c, code.RequestError, "璇ュ嚭鍏ュ簱淇℃伅鏃犳硶瀹屾垚")
 		return
 	}
-	operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
-	if err != nil {
-		util.ResponseFormat(c, code.RequestError, err.Error())
-		return
-	}
 	if err := models.WithTransaction(func(tx *gorm.DB) error {
 
 		if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
 			return err
 		}
-		//if operationType.BaseOperationType != constvar.BaseOperationTypeInternal {
+
 		var listProdtId []string
 		var listProdt []*models.Material
 		mapProdt := make(map[string]decimal.Decimal)
@@ -345,96 +343,337 @@
 			listProdtId = append(listProdtId, v.ProductId)
 			mapProdt[v.ProductId] = v.Amount
 		}
+
 		if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil {
 			return err
 		}
-		if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
+
+		if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
+			var operationInputs []*models.Operation
 			for k, v := range listProdt {
-				if value, ok := mapProdt[v.ID]; !ok {
+				value, ok := mapProdt[v.ID]
+				if !ok {
 					return errors.New("浜у搧绉嶇被寮傚父")
-				} else {
-					listProdt[k].Amount = listProdt[k].Amount.Add(value)
-					if err := tx.Save(listProdt[k]).Error; err != nil {
-						return err
+				}
+
+				listProdt[k].Amount = listProdt[k].Amount.Add(value)
+				if err := tx.Save(listProdt[k]).Error; err != nil {
+					return err
+				}
+
+				locationRule, err := models.NewLocationProductSearch().SetProductId(v.ID).SetAreaId(operation.ToLocationID).First()
+				if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+					return err
+				}
+
+				if err == nil {
+					operationTransfer := &models.Operation{
+						Number:            operation.Number,
+						SourceNumber:      operation.SourceNumber,
+						OperationTypeId:   0,
+						OperationTypeName: operation.OperationTypeName,
+						Status:            constvar.OperationStatus_Finish,
+						FromLocationID:    locationRule.AreaId,
+						ToLocationID:      locationRule.LocationId,
+						OperationDate:     operation.OperationDate,
+						ContacterID:       operation.ContacterID,
+						ContacterName:     operation.ContacterName,
+						CompanyID:         operation.CompanyID,
+						CompanyName:       operation.CompanyName,
+						Comment:           operation.Comment,
+						BaseOperationType: constvar.BaseOperationTypeInternal,
+						Details: []*models.OperationDetails{
+							{
+								ProductId: v.ID,
+								Amount:    value,
+							},
+						},
 					}
+					operationInputs = append(operationInputs, operationTransfer)
+
+					//if err := tx.Create(&operationTransfer).Error; err != nil {
+					//	return err
+					//}
+
 					//TODO:鍑哄叆搴撶殑finish鍜屾姤搴熺殑finish閮借澧炲姞瀵筶ocation_product_amount琛ㄦ暟閲忕殑鏇存柊,鍥犱负姝よ〃鏈塒roductCategory瀛楁锛屾墍浠peration_details琛ㄤ腑瑕佸鍔燩roductCategoryId瀛楁
-					var locAmount models.LocationProductAmount
-					if err := models.NewLocationProductAmountSearch().Orm.
-						Table("wms_location_product_amount").
-						Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").
-						Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.ToLocationID).
-						First(&locAmount).Error; err != nil {
+					locAmount, err := models.NewLocationProductAmountSearch().
+						SetProductId(v.ID).
+						SetLocationId(locationRule.LocationId).
+						First()
+					if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 						return err
 					}
+					locAmount.LocationId = locationRule.LocationId
+					locAmount.ProductId = v.ID
+					locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
 					locAmount.Amount = locAmount.Amount.Add(value)
-					if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(&locAmount); err != nil {
+					if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+						return res.Error
+					}
+				} else {
+					locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First()
+					if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 						return err
 					}
+					if err == nil {
+						operationTransfer := &models.Operation{
+							Number:            operation.Number,
+							SourceNumber:      operation.SourceNumber,
+							OperationTypeId:   0,
+							OperationTypeName: operation.OperationTypeName,
+							Status:            constvar.OperationStatus_Finish,
+							FromLocationID:    locationRule.AreaId,
+							ToLocationID:      locationRule.LocationId,
+							OperationDate:     operation.OperationDate,
+							ContacterID:       operation.ContacterID,
+							ContacterName:     operation.ContacterName,
+							CompanyID:         operation.CompanyID,
+							CompanyName:       operation.CompanyName,
+							Comment:           operation.Comment,
+							BaseOperationType: constvar.BaseOperationTypeInternal,
+							Details: []*models.OperationDetails{
+								{
+									ProductId: v.ID,
+									Amount:    value,
+								},
+							},
+						}
+						operationInputs = append(operationInputs, operationTransfer)
+
+						//if err := tx.Create(&operationTransfer).Error; err != nil {
+						//	return err
+						//}
+
+						//TODO:鍑哄叆搴撶殑finish鍜屾姤搴熺殑finish閮借澧炲姞瀵筶ocation_product_amount琛ㄦ暟閲忕殑鏇存柊,鍥犱负姝よ〃鏈塒roductCategory瀛楁锛屾墍浠peration_details琛ㄤ腑瑕佸鍔燩roductCategoryId瀛楁
+						locAmount, err := models.NewLocationProductAmountSearch().
+							SetProductId(v.ID).
+							SetLocationId(locationRule.LocationId).
+							First()
+						if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+							return err
+						}
+						locAmount.LocationId = locationRule.LocationId
+						locAmount.ProductId = v.ID
+						locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
+						locAmount.Amount = locAmount.Amount.Add(value)
+						if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+							return res.Error
+						}
+					} else {
+						//TODO:鍑哄叆搴撶殑finish鍜屾姤搴熺殑finish閮借澧炲姞瀵筶ocation_product_amount琛ㄦ暟閲忕殑鏇存柊,鍥犱负姝よ〃鏈塒roductCategory瀛楁锛屾墍浠peration_details琛ㄤ腑瑕佸鍔燩roductCategoryId瀛楁
+						locAmount, err := models.NewLocationProductAmountSearch().
+							SetProductId(v.ID).
+							SetLocationId(operation.ToLocationID).
+							First()
+						if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+							return err
+						}
+						locAmount.LocationId = operation.ToLocationID
+						locAmount.ProductId = v.ID
+						locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
+						locAmount.Amount = locAmount.Amount.Add(value)
+						if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+							return res.Error
+						}
+					}
+				}
+			}
+			if len(operationInputs) > 0 {
+				if err := tx.Create(&operationInputs).Error; err != nil {
+					return err
 				}
 			}
 
 		}
-		if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
+
+		if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
 			for k, v := range listProdt {
-				if value, ok := mapProdt[v.ID]; !ok {
+				value, ok := mapProdt[v.ID]
+				if !ok {
 					return errors.New("浜у搧绉嶇被寮傚父")
-				} else {
+				}
+				//todo 婕旂ず娴嬭瘯鏁版嵁
+				data, err := os.ReadFile("conf/input.json")
+				if err != nil {
+					return errors.New("鏂囦欢璇诲彇澶辫触")
+				}
+				m := make(map[string]interface{})
+				err = json.Unmarshal(data, &m)
+				if err != nil {
+					return errors.New("鏍煎紡杞崲澶辫触")
+				}
+				if opa.OpaCheck(c, m, "operation") {
 					if v.Amount.LessThan(value) {
 						return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,鍑哄簱锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愬嚭搴撴搷浣�", v.Name, v.Amount.String(), value.String()))
 					}
-					listProdt[k].Amount = listProdt[k].Amount.Sub(value)
-					if err := tx.Save(listProdt[k]).Error; err != nil {
-						return err
-					}
-					var locAmount models.LocationProductAmount
-					if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.FromLocationID).First(&locAmount).Error; err != nil {
-						return err
-					}
-					if locAmount.Amount.LessThan(value) {
-						return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,鍑哄簱锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愬嚭搴撴搷浣�", v.Name, v.Amount.String(), value.String()))
-					}
-					locAmount.Amount = locAmount.Amount.Sub(value)
-					if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(&locAmount); err != nil {
-						return err
-					}
+				}
+				listProdt[k].Amount = listProdt[k].Amount.Sub(value)
+				if err := tx.Save(listProdt[k]).Error; err != nil {
+					return err
+				}
+				locAmount, res := models.NewLocationProductAmountSearch().
+					SetProductId(v.ID).
+					SetLocationId(operation.ToLocationID).
+					FirstRes()
+				if res.Error != nil {
+					return err
+				}
+				if locAmount.Amount.LessThan(value) {
+					return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,鍑哄簱锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愬嚭搴撴搷浣�", v.Name, v.Amount.String(), value.String()))
+				}
+				locAmount.Amount = locAmount.Amount.Sub(value)
+				if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
+					return err
 				}
 			}
 		}
-		if operationType.BaseOperationType == constvar.BaseOperationTypeInternal {
-			for k, v := range listProdt {
-				if value, ok := mapProdt[v.ID]; !ok {
+
+		if operation.BaseOperationType == constvar.BaseOperationTypeInternal {
+			var operationInputs []*models.Operation
+			for _, v := range listProdt {
+				value, ok := mapProdt[v.ID]
+				if !ok {
 					return errors.New("浜у搧绉嶇被寮傚父")
-				} else {
-					listProdt[k].Amount = listProdt[k].Amount.Add(value)
-					if err := tx.Save(listProdt[k]).Error; err != nil {
-						return err
-					}
-					//TODO:鍑哄叆搴撶殑finish鍜屾姤搴熺殑finish閮借澧炲姞瀵筶ocation_product_amount琛ㄦ暟閲忕殑鏇存柊,鍥犱负姝よ〃鏈塒roductCategory瀛楁锛屾墍浠peration_details琛ㄤ腑瑕佸鍔燩roductCategoryId瀛楁
-					var fromAmount, toAmount models.LocationProductAmount
-					if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.FromLocationID).Find(&fromAmount).Error; err != nil {
-						return err
-					}
-					if fromAmount.Amount.LessThan(value) {
-						return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,璋冩嫧锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愯皟鎷ㄦ搷浣�", v.Name, v.Amount.String(), value.String()))
-					}
-					fromAmount.Amount = fromAmount.Amount.Sub(value)
-					if err := models.NewLocationProductAmountSearch().SetID(fromAmount.Id).Update(&fromAmount); err != nil {
-						return err
-					}
+				}
+				//listProdt[k].Amount = listProdt[k].Amount.Add(value)
+				//if err := tx.Save(listProdt[k]).Error; err != nil {
+				//	return err
+				//}
+				fromLocAmount, res := models.NewLocationProductAmountSearch().
+					SetProductId(v.ID).
+					SetLocationId(operation.FromLocationID).
+					FirstRes()
+				if res.Error != nil {
+					return err
+				}
+				if fromLocAmount.Amount.LessThan(value) {
+					return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,璋冩嫧锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愯皟鎷ㄦ搷浣�", v.Name, v.Amount.String(), value.String()))
+				}
+				fromLocAmount.Amount = fromLocAmount.Amount.Sub(value)
+				if err := models.NewLocationProductAmountSearch().SetID(fromLocAmount.Id).Update(fromLocAmount); err != nil {
+					return err
+				}
 
-					if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.ToLocationID).Find(&toAmount).Error; err != nil {
+				locationRule, err := models.NewLocationProductSearch().SetProductId(v.ID).SetAreaId(operation.ToLocationID).First()
+				if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+					return err
+				}
+
+				if err == nil {
+					operationTransfer := &models.Operation{
+						Number:            operation.Number,
+						SourceNumber:      operation.SourceNumber,
+						OperationTypeId:   0,
+						OperationTypeName: operation.OperationTypeName,
+						Status:            constvar.OperationStatus_Finish,
+						FromLocationID:    locationRule.AreaId,
+						ToLocationID:      locationRule.LocationId,
+						OperationDate:     operation.OperationDate,
+						ContacterID:       operation.ContacterID,
+						ContacterName:     operation.ContacterName,
+						CompanyID:         operation.CompanyID,
+						CompanyName:       operation.CompanyName,
+						Comment:           operation.Comment,
+						BaseOperationType: constvar.BaseOperationTypeInternal,
+						Details: []*models.OperationDetails{
+							{
+								ProductId: v.ID,
+								Amount:    value,
+							},
+						},
+					}
+					operationInputs = append(operationInputs, operationTransfer)
+
+					//if err := tx.Create(&operationTransfer).Error; err != nil {
+					//	return err
+					//}
+
+					locAmount, err := models.NewLocationProductAmountSearch().
+						SetProductId(v.ID).
+						SetLocationId(locationRule.LocationId).
+						First()
+					if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 						return err
 					}
-					toAmount.Amount = toAmount.Amount.Add(value)
-					if err := models.NewLocationProductAmountSearch().SetID(toAmount.Id).Update(&toAmount); err != nil {
+					locAmount.LocationId = locationRule.LocationId
+					locAmount.ProductId = v.ID
+					locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
+					locAmount.Amount = locAmount.Amount.Add(value)
+					if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+						return res.Error
+					}
+				} else {
+					locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First()
+					if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 						return err
+					}
+					if err == nil {
+						operationTransfer := &models.Operation{
+							Number:            operation.Number,
+							SourceNumber:      operation.SourceNumber,
+							OperationTypeId:   0,
+							OperationTypeName: operation.OperationTypeName,
+							Status:            constvar.OperationStatus_Finish,
+							FromLocationID:    locationRule.AreaId,
+							ToLocationID:      locationRule.LocationId,
+							OperationDate:     operation.OperationDate,
+							ContacterID:       operation.ContacterID,
+							ContacterName:     operation.ContacterName,
+							CompanyID:         operation.CompanyID,
+							CompanyName:       operation.CompanyName,
+							Comment:           operation.Comment,
+							BaseOperationType: constvar.BaseOperationTypeInternal,
+							Details: []*models.OperationDetails{
+								{
+									ProductId: v.ID,
+									Amount:    value,
+								},
+							},
+						}
+						operationInputs = append(operationInputs, operationTransfer)
+
+						//if err := tx.Create(&operationTransfer).Error; err != nil {
+						//	return err
+						//}
+
+						locAmount, err := models.NewLocationProductAmountSearch().
+							SetProductId(v.ID).
+							SetLocationId(locationRule.LocationId).
+							First()
+						if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+							return err
+						}
+						locAmount.LocationId = locationRule.LocationId
+						locAmount.ProductId = v.ID
+						locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
+						locAmount.Amount = locAmount.Amount.Add(value)
+						if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+							return res.Error
+						}
+					} else {
+						locAmount, err := models.NewLocationProductAmountSearch().
+							SetProductId(v.ID).
+							SetLocationId(operation.ToLocationID).
+							First()
+						if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+							return err
+						}
+						locAmount.LocationId = operation.ToLocationID
+						locAmount.ProductId = v.ID
+						locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
+						locAmount.Amount = locAmount.Amount.Add(value)
+						if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
+							return res.Error
+						}
 					}
 				}
 			}
-
+			if len(operationInputs) > 0 {
+				if err := tx.Create(&operationInputs).Error; err != nil {
+					return err
+				}
+			}
 		}
-
-		//}
 		return nil
 	}); err != nil {
 		util.ResponseFormat(c, code.RequestError, err.Error())

--
Gitblit v1.8.0