From 88324f3d47dab99da5b4bd9422e50b63b97c242c Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期三, 30 八月 2023 19:51:00 +0800
Subject: [PATCH] fix
---
api/v1/purchase/purchase.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 49 insertions(+), 9 deletions(-)
diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go
index 1d220d6..0a5c918 100644
--- a/api/v1/purchase/purchase.go
+++ b/api/v1/purchase/purchase.go
@@ -1,8 +1,8 @@
package purchase
import (
+ "fmt"
"github.com/gin-gonic/gin"
- "github.com/mitchellh/mapstructure"
"go.uber.org/zap"
"gorm.io/gorm"
"srm/global"
@@ -12,6 +12,7 @@
purchaserequest "srm/model/purchase/request"
"strconv"
"strings"
+ "time"
//"srm/model/purchase"
@@ -29,24 +30,42 @@
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
-// @Param data body purchaserequest.AddPurchase true "閲囪喘鍗曠敤鎴峰悕, 閲囪喘鍗曟墜鏈哄彿鐮�"
+// @Param data body purchaserequest.AddPurchase true "閲囪喘鍗�,閲囪喘鍗曚骇鍝�"
// @Success 200 {object} response.Response{msg=string} "鍒涘缓閲囪喘鍗�"
// @Router /purchase/purchase [post]
func (e *PurchaseApi) CreatePurchase(c *gin.Context) {
var params purchaserequest.AddPurchase
err := c.ShouldBindJSON(¶ms)
if err != nil {
+ global.GVA_LOG.Error("Add Purchase failed", zap.Error(err))
response.FailWithMessage(err.Error(), c)
return
}
var purchaseRecord purchase.Purchase
- if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil {
+ if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil {
+ global.GVA_LOG.Error("Add Purchase failed", zap.Error(err))
response.FailWithMessage(err.Error(), c)
return
}
purchaseRecord.ID = 0
+ purchaseRecord.Status = purchase.OrderStatusConfirmed
+ purchaseRecord.HandledBy = "admin"
+ purchaseRecord.Creator = "admin"
+ purchaseRecord.Number = fmt.Sprintf("CG%v", time.Now().Unix())
+ purchaseRecord.Principal = "admin"
+
+ if !purchaseRecord.WholeDiscountType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.WholeDiscount) {
+ response.FailWithMessage("鏁村崟鎶樻墸鏁板�间笉姝g‘", c)
+ return
+ }
+
+ if !purchaseRecord.PriceAdjustmentType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.PriceAdjustment) {
+ response.FailWithMessage("浠锋牸璋冩暣鏁板�间笉姝g‘", c)
+ return
+ }
+
err = service.NewPurchaseService().CreatePurchase(&purchaseRecord, params.ProductList)
if err != nil {
@@ -55,7 +74,7 @@
return
}
global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err))
- response.FailWithMessage("鍒涘缓澶辫触", c)
+ response.FailWithMessage(err.Error(), c)
return
}
response.OkWithMessage("鍒涘缓鎴愬姛", c)
@@ -103,11 +122,13 @@
}
var purchaseRecord purchase.Purchase
- if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil {
+ if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
+ purchaseRecord.HandledBy = "admin"
+ purchaseRecord.Creator = "admin"
err = service.NewPurchaseService().UpdatePurchase(&purchaseRecord, params.ProductList)
if err != nil {
global.GVA_LOG.Error("鏇存柊澶辫触!", zap.Error(err))
@@ -144,7 +165,26 @@
response.FailWithMessage("鑾峰彇澶辫触", c)
return
}
- response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: productList}, "鑾峰彇鎴愬姛", c)
+ respProductList := make([]*purchaseRes.PurchaseProducts, len(productList))
+ err = utils.AssignTo(productList, &respProductList)
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触", c)
+ return
+ }
+ for k, item := range productList {
+ respProductList[k].Amount = item.Amount
+ respProductList[k].Price = item.Price
+ respProductList[k].Total = item.Total
+ err = utils.AssignTo(item.Product, &respProductList[k])
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触", c)
+ return
+ }
+ }
+
+ response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: respProductList}, "鑾峰彇鎴愬姛", c)
}
// GetPurchaseList
@@ -168,14 +208,14 @@
response.FailWithMessage(err.Error(), c)
return
}
- PurchaseList, total, err := service.NewPurchaseService().GetPurchaseList(pageInfo)
+ purchaseList, total, err := service.NewPurchaseService().GetPurchaseList(pageInfo)
if err != nil {
global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
response.FailWithMessage("鑾峰彇澶辫触"+err.Error(), c)
return
}
response.OkWithDetailed(response.PageResult{
- List: PurchaseList,
+ List: purchaseList,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
@@ -224,7 +264,7 @@
}
purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params))
- if err := mapstructure.Decode(params, &purchaseTypeList); err != nil {
+ if err := utils.AssignTo(params, &purchaseTypeList); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
--
Gitblit v1.8.0