From 727d16a6d0336daa6c2f9541564d1500444e44a2 Mon Sep 17 00:00:00 2001 From: liujiandao <274878379@qq.com> Date: 星期四, 16 十一月 2023 09:43:47 +0800 Subject: [PATCH] 采购单与wms通信 --- api/v1/test/product.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 97 insertions(+), 3 deletions(-) diff --git a/api/v1/test/product.go b/api/v1/test/product.go index c7b7457..dfb82c9 100644 --- a/api/v1/test/product.go +++ b/api/v1/test/product.go @@ -2,12 +2,16 @@ import ( "github.com/gin-gonic/gin" + "github.com/spf13/cast" "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "srm/global" "srm/model/common/request" "srm/model/common/response" "srm/model/test" testReq "srm/model/test/request" + "srm/proto/product" "srm/service" ) @@ -22,17 +26,30 @@ // @Security ApiKeyAuth // @accept application/json // @Produce application/json -// @Param data body test.Product true "鍒涘缓Product" +// @Param data body request.ProductCreate true "鍒涘缓Product" // @Success 200 {string} string "{"success":true,"data":{},"msg":"鑾峰彇鎴愬姛"}" // @Router /p/createProduct [post] func (pApi *ProductApi) CreateProduct(c *gin.Context) { - var p test.Product + var p testReq.ProductCreate err := c.ShouldBindJSON(&p) if err != nil { response.FailWithMessage(err.Error(), c) return } - if err := pService.CreateProduct(&p); err != nil { + for _, t := range p.List { + var num int64 + db := global.GVA_DB.Model(&test.Product{}) + err := db.Where("number = ?", t.Number).Where("supplier_id = ?", t.SupplierId).Count(&num).Error + if err != nil { + response.FailWithMessage(err.Error(), c) + return + } + if num > 0 { + response.FailWithMessage("褰撳墠渚涘簲鍟嗗凡缁忔彁渚涙浜у搧", c) + return + } + } + if err := pService.CreateProduct(p.List); err != nil { global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err)) response.FailWithMessage("鍒涘缓澶辫触", c) } else { @@ -156,6 +173,10 @@ global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err)) response.FailWithMessage("鑾峰彇澶辫触", c) } else { + //娣诲姞渚涘簲鍟嗗悕绉� + for i := 0; i < len(list); i++ { + list[i].SupplierName = list[i].Supplier.Name + } response.OkWithDetailed(response.PageResult{ List: list, Total: total, @@ -164,3 +185,76 @@ }, "鑾峰彇鎴愬姛", c) } } + +var ( + productServiceConn *grpc.ClientConn +) + +func InitProductServiceConn() { + var err error + productServiceConn, err = grpc.Dial(global.GVA_CONFIG.System.GrpcUrl, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + //logx.Errorf("grpc dial product service error: %v", err.Error()) + return + } +} + +func CloseProductServiceConn() { + if productServiceConn != nil { + productServiceConn.Close() + } +} + +// GetProductListFromGrpc 鍒嗛〉鑾峰彇Product鍒楄〃 +// @Tags Product +// @Summary 鍒嗛〉鑾峰彇Product鍒楄〃 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Param data query testReq.ProductSearch true "鍒嗛〉鑾峰彇Product鍒楄〃" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"鑾峰彇鎴愬姛"}" +// @Router /p/getProductListFromGrpc [get] +func (pApi *ProductApi) GetProductListFromGrpc(c *gin.Context) { + var pageInfo testReq.ProductSearch + err := c.ShouldBindQuery(&pageInfo) + if err != nil { + response.FailWithMessage(err.Error(), c) + return + } + + cli := product.NewProductServiceClient(productServiceConn) + getProductListResponse, err := cli.GetProductList(c, &product.GetProductListRequest{ + Page: cast.ToInt32(pageInfo.Page), + PageSize: cast.ToInt32(pageInfo.PageSize), + ProductNumber: pageInfo.Number, + ProductName: pageInfo.Name, + }) + rawProductList := getProductListResponse.List + productList := make([]test.Product, len(rawProductList)) + + for k, v := range rawProductList { + productList[k].Number = v.Number + productList[k].Name = v.Name + productList[k].Unit = v.Unit + productList[k].PurchasePrice = v.SalePrice + min := int(v.MinInventory) + productList[k].MinimumStock = min + max := int(v.MaxInventory) + productList[k].MaximumStock = max + productList[k].Remark = v.Node + productList[k].ProductType = v.MaterialMode + } + + if err != nil || getProductListResponse.Code != 0 { + global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err)) + response.FailWithMessage("鑾峰彇澶辫触", c) + } else { + response.OkWithDetailed(response.PageResult{ + List: productList, + Total: int64(len(productList)), + Page: pageInfo.Page, + PageSize: pageInfo.PageSize, + }, "鑾峰彇鎴愬姛", c) + } + +} -- Gitblit v1.8.0