From 3545b1af80395204e96f748cb6c52cf1dc572606 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 30 八月 2023 14:34:35 +0800
Subject: [PATCH] merge

---
 api/v1/purchase/purchase.go |   53 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go
index c82be66..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,19 +30,21 @@
 // @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(&params)
 	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
 	}
@@ -50,6 +53,19 @@
 	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 {
@@ -58,7 +74,7 @@
 			return
 		}
 		global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err))
-		response.FailWithMessage("鍒涘缓澶辫触", c)
+		response.FailWithMessage(err.Error(), c)
 		return
 	}
 	response.OkWithMessage("鍒涘缓鎴愬姛", c)
@@ -106,7 +122,7 @@
 	}
 
 	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
 	}
@@ -149,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
@@ -173,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,
@@ -229,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