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 | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 153 insertions(+), 12 deletions(-) diff --git a/service/salesRefund.go b/service/salesRefund.go index 78bd789..52a4a88 100644 --- a/service/salesRefund.go +++ b/service/salesRefund.go @@ -1,54 +1,195 @@ package service import ( + "aps_crm/constvar" "aps_crm/model" "aps_crm/pkg/ecode" + "errors" + "github.com/shopspring/decimal" + "gorm.io/gorm" ) type SalesRefundService struct{} func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int { - err := model.NewSalesRefundSearch().Create(salesRefund) + salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First() if err != nil { - return ecode.SalesRefundExist + return ecode.SalesReturnNotExist + } + + if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK { + return code + } + var amount decimal.Decimal + 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.SalesReturnId).Update(salesReturnRecord) + if err != nil { + code = ecode.DBErr + return err + } + return nil + }) + + if err != nil { + return code } return ecode.OK } -func (SalesRefundService) DeleteSalesRefund(id int) int { - _, err := model.NewSalesRefundSearch().SetId(id).Find() +func CheckProduct(returnProducts, refundProducts []*model.Product) int { + returnProductsMap := make(map[uint]*model.Product, len(returnProducts)) + for _, product := range returnProducts { + returnProductsMap[product.Id] = product + } + for _, product := range refundProducts { + if returnProductsMap[product.Id] == nil { + return ecode.SalesRefundProductNotExist + } + if !returnProductsMap[product.Id].Price.Equal(product.Price) || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) { + return ecode.SalesRefundProductPriceOrAmountErr + } + } + return ecode.OK +} + +func (slf SalesRefundService) DeleteSalesRefund(id int) int { + refund, err := model.NewSalesRefundSearch().SetId(id).First() if err != nil { return ecode.SalesRefundNotExist } + salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SalesReturnId).SetPreload(true).First() + if err != nil { + return ecode.SalesReturnNotExist + } - err = model.NewSalesRefundSearch().SetId(id).Delete() + var amount decimal.Decimal + for _, product := range refund.Products { + amount = amount.Add(product.Amount.Mul(product.Price)) + } + + err = model.WithTransaction(func(db *gorm.DB) error { + err = model.NewSalesRefundSearch().SetId(id).Delete() + if err != nil { + return err + } + salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(amount).Round(2) + salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) + err = model.NewSalesReturnSearch().SetId(refund.SalesReturnId).Update(salesReturnRecord) + if err != nil { + return err + } + return nil + }) + if err != nil { return ecode.SalesRefundNotExist } return ecode.OK +} + +func (slf SalesRefundService) BatchDeleteSalesRefund(ids []int) (failIds []int, code int) { + for _, id := range ids { + code = slf.DeleteSalesRefund(id) + if code != ecode.OK { + failIds = append(failIds, id) + } + } + return failIds, code } func (SalesRefundService) UpdateSalesRefund(salesRefund *model.SalesRefund) int { // check salesRefund exist - _, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).Find() + oldRefund, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).First() if err != nil { return ecode.SalesRefundNotExist } - err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund) + salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First() if err != nil { - return ecode.SalesRefundSetErr + return ecode.SalesReturnNotExist + } + + if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK { + return code + } + + var diffAmount decimal.Decimal + + for _, product := range salesRefund.Products { + diffAmount = diffAmount.Add(product.Amount.Mul(product.Price)) + } + for _, product := range oldRefund.Products { + 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) + + 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 code } return ecode.OK } -func (SalesRefundService) GetSalesRefundList(page, pageSize int, keyword string) ([]*model.SalesRefund, int) { +func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string, sourceId int, memberIds []int) ([]*model.SalesRefund, int64, int) { // get contact list - contacts, err := model.NewSalesRefundSearch().SetKeyword(keyword).SetPage(page, pageSize).FindAll() + contacts, total, err := model.NewSalesRefundSearch(). + SetKeywordType(keywordType). + SetKeyword(keyword). + SetSourceId(sourceId). + SetPreload(true). + SetMemberIds(memberIds). + SetPage(page, pageSize).FindAll() if err != nil { - return nil, ecode.SalesRefundListErr + return nil, 0, ecode.SalesRefundListErr } - return contacts, ecode.OK + return contacts, total, ecode.OK +} + +func (SalesRefundService) GetSalesRefundListByIds(ids []int) ([]*model.SalesRefund, int) { + // get contact list + salesRefunds, err := model.NewSalesRefundSearch(). + SetIds(ids). + Find() + if err != nil { + return nil, ecode.SalesReturnListErr + } + return salesRefunds, ecode.OK } -- Gitblit v1.8.0