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 | 160 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 147 insertions(+), 13 deletions(-)
diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go
index 4151621..0a5c918 100644
--- a/api/v1/purchase/purchase.go
+++ b/api/v1/purchase/purchase.go
@@ -1,13 +1,18 @@
package purchase
import (
+ "fmt"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
+ "gorm.io/gorm"
"srm/global"
"srm/model/common/request"
"srm/model/common/response"
+ "srm/model/purchase"
purchaserequest "srm/model/purchase/request"
"strconv"
+ "strings"
+ "time"
//"srm/model/purchase"
@@ -25,20 +30,51 @@
// @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
}
- err = service.NewPurchaseService().CreatePurchase(params)
+
+ var purchaseRecord purchase.Purchase
+ 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 {
+ if err == gorm.ErrDuplicatedKey || strings.Contains(err.Error(), "Duplicate entry") {
+ response.FailWithMessage("缂栧彿閲嶅", c)
+ return
+ }
global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err))
- response.FailWithMessage("鍒涘缓澶辫触", c)
+ response.FailWithMessage(err.Error(), c)
return
}
response.OkWithMessage("鍒涘缓鎴愬姛", c)
@@ -74,22 +110,26 @@
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
-// @Param data body purchaserequest.AddPurchase true "閲囪喘鍗旾D, 閲囪喘鍗曚俊鎭�"
+// @Param data body purchaserequest.UpdatePurchase true "閲囪喘鍗旾D, 閲囪喘鍗曚俊鎭�"
// @Success 200 {object} response.Response{msg=string} "鏇存柊閲囪喘鍗曚俊鎭�"
// @Router /purchase/purchase [put]
func (e *PurchaseApi) UpdatePurchase(c *gin.Context) {
- var params purchaserequest.AddPurchase
+ var params purchaserequest.UpdatePurchase
err := c.ShouldBindJSON(¶ms)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
- err = utils.Verify(params.Purchase.GVA_MODEL, utils.IdVerify)
- if err != nil {
+
+ var purchaseRecord purchase.Purchase
+ if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
- err = service.NewPurchaseService().UpdatePurchase(¶ms)
+
+ purchaseRecord.HandledBy = "admin"
+ purchaseRecord.Creator = "admin"
+ err = service.NewPurchaseService().UpdatePurchase(&purchaseRecord, params.ProductList)
if err != nil {
global.GVA_LOG.Error("鏇存柊澶辫触!", zap.Error(err))
response.FailWithMessage("鏇存柊澶辫触", c)
@@ -125,17 +165,36 @@
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
// @Tags Purchase
-// @Summary 鍒嗛〉鑾峰彇鏉冮檺閲囪喘鍗曞垪琛�
+// @Summary 鍒嗛〉鑾峰彇閲囪喘鍗曞垪琛�
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.PageInfo true "椤电爜, 姣忛〉澶у皬"
-// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "鍒嗛〉鑾峰彇鏉冮檺閲囪喘鍗曞垪琛�,杩斿洖鍖呮嫭鍒楄〃,鎬绘暟,椤电爜,姣忛〉鏁伴噺"
+// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "鍒嗛〉鑾峰彇閲囪喘鍗曞垪琛�,杩斿洖鍖呮嫭鍒楄〃,鎬绘暟,椤电爜,姣忛〉鏁伴噺"
// @Router /purchase/purchaseList [get]
func (e *PurchaseApi) GetPurchaseList(c *gin.Context) {
var pageInfo request.PageInfo
@@ -149,16 +208,91 @@
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,
}, "鑾峰彇鎴愬姛", c)
}
+
+// Submit
+// @Tags Purchase
+// @Summary 鎻愪氦閲囪喘鍗�
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param id path int true "閲囪喘鍗旾D"
+// @Success 200 {object} response.Response{msg=string} "鎻愪氦閲囪喘鍗�"
+// @Router /purchase/submit/{id} [post]
+func (e *PurchaseApi) Submit(c *gin.Context) {
+ id, _ := strconv.Atoi(c.Param("id"))
+ if id == 0 {
+ response.FailWithMessage("鍙傛暟缂哄け", c)
+ return
+ }
+ err := service.NewPurchaseService().Submit(uint(id))
+ if err != nil {
+ global.GVA_LOG.Error("鏇存柊澶辫触!", zap.Error(err))
+ response.FailWithMessage("鏇存柊澶辫触", c)
+ return
+ }
+ response.OkWithMessage("鏇存柊鎴愬姛", c)
+}
+
+// SavePurchaseType
+// @Tags Purchase
+// @Summary 鍒涘缓閲囪喘绫诲瀷
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body []purchaserequest.PurchaseType true "閲囪喘绫诲瀷list"
+// @Success 200 {object} response.Response{msg=string} "鍒涘缓閲囪喘绫诲瀷"
+// @Router /purchase/purchaseType [post]
+func (e *PurchaseApi) SavePurchaseType(c *gin.Context) {
+ var params []*purchaserequest.PurchaseType
+ err := c.ShouldBindJSON(¶ms)
+ if err != nil {
+ response.FailWithMessage(err.Error(), c)
+ return
+ }
+
+ purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params))
+ if err := utils.AssignTo(params, &purchaseTypeList); err != nil {
+ response.FailWithMessage(err.Error(), c)
+ return
+ }
+
+ err = service.NewPurchaseService().SavePurchaseType(purchaseTypeList)
+
+ if err != nil {
+ global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err))
+ response.FailWithMessage("鍒涘缓澶辫触", c)
+ return
+ }
+ response.OkWithMessage("鍒涘缓鎴愬姛", c)
+}
+
+// GetPurchaseTypeList
+// @Tags Purchase
+// @Summary 鑾峰彇閲囪喘绫诲瀷鍒楄〃
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Success 200 {object} response.Response{data=[]purchase.PurchaseType,msg=string} "鑾峰彇閲囪喘绫诲瀷鍒楄〃"
+// @Router /purchase/purchaseTypeList [get]
+func (e *PurchaseApi) GetPurchaseTypeList(c *gin.Context) {
+ list, err := service.NewPurchaseService().GetPurchaseTypeList()
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触"+err.Error(), c)
+ return
+ }
+ response.OkWithDetailed(list, "鑾峰彇鎴愬姛", c)
+}
--
Gitblit v1.8.0