From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 01 十一月 2023 19:20:21 +0800
Subject: [PATCH] 公海member_id使用0

---
 service/salesRefund.go |   40 +++++++++++++++++++++++++++++-----------
 1 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/service/salesRefund.go b/service/salesRefund.go
index cf548d9..52a4a88 100644
--- a/service/salesRefund.go
+++ b/service/salesRefund.go
@@ -4,6 +4,7 @@
 	"aps_crm/constvar"
 	"aps_crm/model"
 	"aps_crm/pkg/ecode"
+	"errors"
 	"github.com/shopspring/decimal"
 	"gorm.io/gorm"
 )
@@ -11,7 +12,7 @@
 type SalesRefundService struct{}
 
 func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int {
-	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First()
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First()
 	if err != nil {
 		return ecode.SalesReturnNotExist
 	}
@@ -23,23 +24,29 @@
 	for _, product := range salesRefund.Products {
 		amount = amount.Add(product.Amount.Mul(product.Price))
 	}
-
+	code := ecode.OK
 	err = model.WithTransaction(func(db *gorm.DB) error {
 		err = model.NewSalesRefundSearch().Create(salesRefund)
 		if err != nil {
+			code = ecode.DBErr
 			return err
 		}
 		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Add(amount).Round(2)
+		if salesReturnRecord.AmountHasRefund.GreaterThan(salesReturnRecord.AmountTotal) {
+			code = ecode.SalesRefundProductBeyondTotalAmount
+			return errors.New("閫�璐т骇鍝佸搴旀�讳环瓒呭嚭搴旈��鎬讳环")
+		}
 		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
-		err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord)
+		err = model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).Update(salesReturnRecord)
 		if err != nil {
+			code = ecode.DBErr
 			return err
 		}
 		return nil
 	})
 
 	if err != nil {
-		return ecode.SalesRefundExist
+		return code
 	}
 
 	return ecode.OK
@@ -54,7 +61,7 @@
 		if returnProductsMap[product.Id] == nil {
 			return ecode.SalesRefundProductNotExist
 		}
-		if returnProductsMap[product.Id].Price != product.Price || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) {
+		if !returnProductsMap[product.Id].Price.Equal(product.Price) || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) {
 			return ecode.SalesRefundProductPriceOrAmountErr
 		}
 	}
@@ -66,7 +73,7 @@
 	if err != nil {
 		return ecode.SalesRefundNotExist
 	}
-	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SourceId).SetPreload(true).First()
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SalesReturnId).SetPreload(true).First()
 	if err != nil {
 		return ecode.SalesReturnNotExist
 	}
@@ -83,7 +90,7 @@
 		}
 		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(amount).Round(2)
 		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
-		err = model.NewSalesReturnSearch().SetId(refund.SourceId).Update(salesReturnRecord)
+		err = model.NewSalesReturnSearch().SetId(refund.SalesReturnId).Update(salesReturnRecord)
 		if err != nil {
 			return err
 		}
@@ -113,7 +120,7 @@
 		return ecode.SalesRefundNotExist
 	}
 
-	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First()
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First()
 	if err != nil {
 		return ecode.SalesReturnNotExist
 	}
@@ -131,33 +138,44 @@
 		diffAmount = diffAmount.Sub(product.Amount.Mul(product.Price))
 	}
 
+	code := ecode.OK
 	err = model.WithTransaction(func(db *gorm.DB) error {
 		err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund)
 		if err != nil {
+			code = ecode.DBErr
 			return err
 		}
 		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(diffAmount).Round(2)
 		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
-		err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord)
+
+		if salesReturnRecord.AmountHasRefund.GreaterThan(salesReturnRecord.AmountTotal) {
+			code = ecode.SalesRefundProductBeyondTotalAmount
+			return errors.New("閫�璐т骇鍝佸搴旀�讳环瓒呭嚭搴旈��鎬讳环")
+		}
+
+		err = model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).Update(salesReturnRecord)
 		if err != nil {
+			code = ecode.DBErr
 			return err
 		}
 		return nil
 	})
 
 	if err != nil {
-		return ecode.SalesRefundSetErr
+		return code
 	}
 
 	return ecode.OK
 }
 
-func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string) ([]*model.SalesRefund, int64, int) {
+func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string, sourceId int, memberIds []int) ([]*model.SalesRefund, int64, int) {
 	// get contact list
 	contacts, total, err := model.NewSalesRefundSearch().
 		SetKeywordType(keywordType).
 		SetKeyword(keyword).
+		SetSourceId(sourceId).
 		SetPreload(true).
+		SetMemberIds(memberIds).
 		SetPage(page, pageSize).FindAll()
 	if err != nil {
 		return nil, 0, ecode.SalesRefundListErr

--
Gitblit v1.8.0