liujiandao
2023-11-02 f9ce7300c4ce1673d06a052e43cb462f8a69c13c
api/v1/test/product.go
@@ -5,6 +5,7 @@
   "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"
@@ -25,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 {
@@ -159,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,
@@ -172,14 +190,14 @@
   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 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 {
@@ -218,24 +236,25 @@
      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].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)
      return
   } else {
      response.OkWithDetailed(response.PageResult{
         List:     productList,
         Total:    int64(len(productList)),
         Page:     pageInfo.Page,
         PageSize: pageInfo.PageSize,
      }, "获取成功", c)
   }
   response.OkWithDetailed(response.PageResult{
      List:     productList,
      Total:    0,
      Page:     pageInfo.Page,
      PageSize: pageInfo.PageSize,
   }, "获取成功", c)
}