| | |
| | | |
| | | import ( |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/spf13/cast" |
| | | "go.uber.org/zap" |
| | | "google.golang.org/grpc" |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/common/response" |
| | | "srm/model/test" |
| | | testReq "srm/model/test/request" |
| | | "srm/proto/product" |
| | | "srm/service" |
| | | ) |
| | | |
| | |
| | | }, "获取成功", c) |
| | | } |
| | | } |
| | | |
| | | var ( |
| | | productServiceConn *grpc.ClientConn |
| | | ) |
| | | |
| | | //func InitProductServiceConn() { |
| | | // var err error |
| | | // productServiceConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Aps, 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 |
| | | //productList[k].MinimumStock = &v.SalePrice |
| | | //productList[k].MaximumStock = &v.SalePrice |
| | | //productList[k].Remark = &v.SalePrice |
| | | productList[k].ProductType = v.MaterialMode |
| | | } |
| | | |
| | | if err != nil || getProductListResponse.Code != 0 { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | |
| | | response.OkWithDetailed(response.PageResult{ |
| | | List: productList, |
| | | Total: 0, |
| | | Page: pageInfo.Page, |
| | | PageSize: pageInfo.PageSize, |
| | | }, "获取成功", c) |
| | | |
| | | } |