From d20acf38c36c11ee4428c3e74a17f5870dc61b51 Mon Sep 17 00:00:00 2001 From: liujiandao <274878379@qq.com> Date: 星期四, 16 十一月 2023 09:51:00 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/SRM --- api/v1/purchase/purchase.go | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 160 insertions(+), 18 deletions(-) diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go index d68781c..c037be0 100644 --- a/api/v1/purchase/purchase.go +++ b/api/v1/purchase/purchase.go @@ -1,13 +1,20 @@ 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 +32,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 +113,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,7 +168,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 @@ -149,14 +211,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, @@ -171,20 +233,49 @@ // @Produce application/json // @Param id path int true "閲囪喘鍗旾D" // @Success 200 {object} response.Response{msg=string} "鎻愪氦閲囪喘鍗�" -// @Router /purchase/submit/{id} [post] +// @Router /purchase/submit [post] func (e *PurchaseApi) Submit(c *gin.Context) { - var params purchaserequest.AddPurchase + 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 } - err = utils.Verify(params.Purchase.GVA_MODEL, utils.IdVerify) - if err != nil { - response.FailWithMessage(err.Error(), c) - return + 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) + _, 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 + } } - err = service.NewPurchaseService().UpdatePurchase(¶ms) + + err = service.NewPurchaseService().Submit(params.Id, params.Status) if err != nil { global.GVA_LOG.Error("鏇存柊澶辫触!", zap.Error(err)) response.FailWithMessage("鏇存柊澶辫触", c) @@ -192,3 +283,54 @@ } 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