From a2302df7ce372249b617e483d83a75f3312fbac2 Mon Sep 17 00:00:00 2001 From: liujiandao <274878379@qq.com> Date: 星期一, 18 三月 2024 16:09:07 +0800 Subject: [PATCH] crm查询采购信息3 --- api/v1/purchase/purchase.go | 190 +++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 172 insertions(+), 18 deletions(-) diff --git a/api/v1/purchase/purchase.go b/api/v1/purchase/purchase.go index c82be66..c847f43 100644 --- a/api/v1/purchase/purchase.go +++ b/api/v1/purchase/purchase.go @@ -1,18 +1,17 @@ package purchase import ( + "context" "github.com/gin-gonic/gin" - "github.com/mitchellh/mapstructure" "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" - //"srm/model/purchase" //"srm/model/purchase" @@ -29,19 +28,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(¶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 } @@ -50,6 +51,18 @@ purchaseRecord.Status = purchase.OrderStatusConfirmed purchaseRecord.HandledBy = "admin" purchaseRecord.Creator = "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 { @@ -58,7 +71,7 @@ return } global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err)) - response.FailWithMessage("鍒涘缓澶辫触", c) + response.FailWithMessage(err.Error(), c) return } response.OkWithMessage("鍒涘缓鎴愬姛", c) @@ -106,7 +119,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 +162,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 @@ -158,11 +190,11 @@ // @Security ApiKeyAuth // @accept application/json // @Produce application/json -// @Param data query request.PageInfo true "椤电爜, 姣忛〉澶у皬" +// @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) @@ -173,14 +205,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, @@ -195,14 +227,52 @@ // @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) { - id, _ := strconv.Atoi(c.Param("id")) - if id == 0 { - response.FailWithMessage("鍙傛暟缂哄け", c) + 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 := service.NewPurchaseService().Submit(uint(id)) + 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, + Source: "SRM_PURCHASE", + }) + 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) @@ -229,7 +299,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 } @@ -261,3 +331,87 @@ } response.OkWithDetailed(list, "鑾峰彇鎴愬姛", c) } + +// GetWarehouseInfo +// @Tags Purchase +// @Summary 鑾峰彇浠撳簱鍒楄〃 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {object} response.Response{data=[]purchase_wms.SrmGetWarehouseInfoResponse} "鑾峰彇閲囪喘绫诲瀷鍒楄〃" +// @Router /purchase/getWarehouseInfo [get] +func (e *PurchaseApi) GetWarehouseInfo(c *gin.Context) { + client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn) + info, err := client.SrmGetWarehouseInfo(context.Background(), &purchase_wms.SrmGetWarehouseInfoRequest{}) + if err != nil { + global.GVA_LOG.Error("grpc璋冪敤澶辫触!", zap.Error(err)) + response.FailWithMessage("grpc璋冪敤澶辫触", c) + return + } + response.OkWithData(info, c) +} + +// GetOperationInfo +// @Tags Purchase +// @Summary 鑾峰彇鎿嶄綔淇℃伅 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {object} response.Response{data=[]purchaseRes.OperationInfo} "鑾峰彇鎿嶄綔淇℃伅" +// @Router /purchase/getOperationInfo/{id} [get] +func (e *PurchaseApi) GetOperationInfo(c *gin.Context) { + id, _ := strconv.Atoi(c.Param("id")) + if id == 0 { + response.FailWithMessage("鍙傛暟缂哄け", c) + return + } + data, err := service.NewPurchaseService().GetPurchase(uint(id)) + if err != nil { + global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err)) + response.FailWithMessage("鑾峰彇澶辫触", c) + return + } + productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(id)) + if err != nil { + global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err)) + response.FailWithMessage("鑾峰彇澶辫触", c) + return + } + client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn) + info, err := client.SrmGetOperationInfo(context.Background(), &purchase_wms.SrmGetOperationInfoRequest{Number: data.Number}) + if err != nil { + global.GVA_LOG.Error("grpc璋冪敤澶辫触!", zap.Error(err)) + response.FailWithMessage("grpc璋冪敤澶辫触", c) + return + } + + operationInfos := make([]purchaseRes.OperationInfo, 0) + for _, operation := range info.Operations { + var oi purchaseRes.OperationInfo + oi.Number = operation.ProductId + oi.Name = operation.ProductName + oi.OperationNumber = operation.Number + oi.Status = operation.Status + oi.Principal = data.Principal + oi.WarehouseName = data.Warehouse + if operation.OverTime != "" { + oi.OverTime = operation.OverTime + oi.OverAmount = operation.Amount + } else { + oi.SendAmount = operation.Amount + } + for _, products := range productList { + if products.Product.Number == operation.ProductId { + oi.Amount = products.Amount + oi.PurchasePrice = products.Price + oi.Total = products.Total + oi.Unit = products.Product.Unit + oi.Specifications = products.Product.Specifications + oi.ModelNumber = products.Product.ModelNumber + break + } + } + operationInfos = append(operationInfos, oi) + } + response.OkWithData(operationInfos, c) +} -- Gitblit v1.8.0