From 4885cc79aae34a011602807a41051e36e4b54e4f Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期一, 27 十一月 2023 16:50:32 +0800
Subject: [PATCH] grpc创建采购单修改
---
api/v1/purchase/purchase.go | 205 +++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 189 insertions(+), 16 deletions(-)
diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go
index 4151621..edec24e 100644
--- a/api/v1/purchase/purchase.go
+++ b/api/v1/purchase/purchase.go
@@ -1,13 +1,19 @@
package purchase
import (
+ "context"
+ "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"
+ "srm/proto/purchase_wms"
"strconv"
+ "strings"
+ "time"
//"srm/model/purchase"
@@ -25,20 +31,52 @@
// @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"
+ purchaseRecord.OrderType = "閲囪喘璁㈠崟"
+
+ 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 +112,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,20 +167,39 @@
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} "鍒嗛〉鑾峰彇鏉冮檺閲囪喘鍗曞垪琛�,杩斿洖鍖呮嫭鍒楄〃,鎬绘暟,椤电爜,姣忛〉鏁伴噺"
+// @Param data query purchaserequest.PurchaseSearch true "鍙傛暟"
+// @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
+ var pageInfo purchaserequest.PurchaseSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
@@ -149,16 +210,128 @@
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 [post]
+func (e *PurchaseApi) Submit(c *gin.Context) {
+ var params purchaserequest.SubmitPurchase
+ err := c.ShouldBindJSON(¶ms)
+ if err != nil {
+ global.GVA_LOG.Error("Submit failed", zap.Error(err))
+ response.FailWithMessage(err.Error(), c)
+ return
+ }
+ warehouse := ""
+ if params.Status == purchase.OrderStatusReceived {
+ data, err := service.NewPurchaseService().GetPurchase(uint(params.Id))
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触", c)
+ return
+ }
+ productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(params.Id))
+ if err != nil {
+ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
+ response.FailWithMessage("鑾峰彇澶辫触", c)
+ return
+ }
+ product := make([]*purchase_wms.PurchaseProduct, 0)
+ for _, products := range productList {
+ var p purchase_wms.PurchaseProduct
+ p.Id = products.Product.Number
+ p.Amount = products.Amount.IntPart()
+ product = append(product, &p)
+ }
+ client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
+ resp, err := client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{
+ Number: data.Number,
+ SupplierName: data.Supplier.Name,
+ Product: product,
+ })
+ if err != nil {
+ global.GVA_LOG.Error("grpc璋冪敤澶辫触!", zap.Error(err))
+ response.FailWithMessage("grpc璋冪敤澶辫触", c)
+ return
+ }
+ warehouse = resp.Warehouse
+ }
+
+ err = service.NewPurchaseService().Submit(params.Id, params.Status, warehouse)
+ 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