zhangqian
2023-08-29 d3a78e4c791821b298075b700e9fc14030d12ec2
service/purchase/purchase.go
@@ -2,6 +2,7 @@
import (
   "context"
   "github.com/shopspring/decimal"
   "github.com/spf13/cast"
   "gorm.io/gorm"
   "srm/global"
@@ -24,6 +25,11 @@
func (slf *PurchaseService) CreatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) {
   err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
      var quantity decimal.Decimal
      for _, product := range productList {
         quantity = quantity.Add(product.Amount)
      }
      params.Quantity = quantity
      err = tx.Create(&params).Error
      if err != nil {
         return err
@@ -60,6 +66,11 @@
func (slf *PurchaseService) UpdatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) {
   err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
      var quantity decimal.Decimal
      for _, product := range productList {
         quantity = quantity.Add(product.Amount)
      }
      params.Quantity = quantity
      err = tx.Where("id = ?", params.ID).Updates(params).Error
      if err != nil {
         return err