liujiandao
2024-03-22 0fd3231799d9185af352dcbc4f8e14df5e0f0233
api/v1/purchase/purchase.go
@@ -3,6 +3,7 @@
import (
   "context"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "go.uber.org/zap"
   "gorm.io/gorm"
   "srm/global"
@@ -415,3 +416,223 @@
   }
   response.OkWithData(operationInfos, c)
}
// NewSubmit
// @Tags      Purchase
// @Summary   新版提交
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param      id   path      int   true   "采购单ID"                                           true  "采购单ID"
// @Success   200   {object}  response.Response{msg=string}  "新版提交"
// @Router    /purchase/newSubmit/{id} [get]
func (e *PurchaseApi) NewSubmit(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
   }
   list := make([]*purchase.PurchaseProductConfirm, 0)
   for _, products := range productList {
      var ppc purchase.PurchaseProductConfirm
      ppc.PurchaseNumber = data.Number
      ppc.Principal = data.Principal
      ppc.ProductId = products.Product.Number
      ppc.ProductName = products.Product.Name
      ppc.Unit = products.Product.Unit
      ppc.Specs = products.Product.Specifications
      ppc.Type = products.Product.ModelNumber
      ppc.Amount = products.Amount
      ppc.OverReceiveAmount = decimal.NewFromInt(0)
      ppc.NotReceiveAmount = products.Amount
      ppc.NowReceiveAmount = decimal.NewFromInt(0)
      ppc.SurplusReceiveAmount = products.Amount
      list = append(list, &ppc)
   }
   err = service.NewPurchaseService().SavePurchaseProductConfirm(list)
   if err != nil {
      global.GVA_LOG.Error("提交失败!", zap.Error(err))
      response.FailWithMessage("提交失败", c)
      return
   }
   response.OkWithMessage("提交成功", c)
}
// GetPurchaseProductConfirmInfo
// @Tags      Purchase
// @Summary   获取确认信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     number  path     string   true  "采购单编码"
// @Success   200   {object}  response.Response{data=[]purchase.PurchaseProductConfirm}  "获取确认信息"
// @Router    /purchase/getPurchaseProductConfirmInfo/{number} [get]
func (e *PurchaseApi) GetPurchaseProductConfirmInfo(c *gin.Context) {
   number := c.Param("number")
   if number == "" {
      response.FailWithMessage("参数缺失", c)
      return
   }
   info, err := service.NewPurchaseService().GetPurchaseProductConfirmInfo(number)
   if err != nil {
      global.GVA_LOG.Error("获取确认信息失败!", zap.Error(err))
      response.FailWithMessage("获取确认信息失败", c)
      return
   }
   response.OkWithData(info, c)
}
// SavePurchaseProductConfirm
// @Tags      Purchase
// @Summary   确认收货
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body     []purchaserequest.PurchaseProductConfirmInfo   true  "list"
// @Success   200   {object}  response.Response{msg=string}  "确认收货"
// @Router    /purchase/savePurchaseProductConfirm [post]
func (e *PurchaseApi) SavePurchaseProductConfirm(c *gin.Context) {
   var params []*purchaserequest.PurchaseProductConfirmInfo
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   list := make([]*purchase.PurchaseProductConfirm, 0, len(params))
   if err := utils.AssignTo(params, &list); err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   server := service.NewPurchaseService()
   err = server.SavePurchaseProductConfirm(list)
   if err != nil {
      global.GVA_LOG.Error("保存失败!", zap.Error(err))
      response.FailWithMessage("保存失败", c)
      return
   }
   err = server.SavePurchaseQualityInspection(list)
   if err != nil {
      global.GVA_LOG.Error("保存失败!", zap.Error(err))
      response.FailWithMessage("保存失败", c)
      return
   }
   response.OkWithMessage("保存成功", c)
}
// GetPurchaseQualityInspectionInfo
// @Tags      Purchase
// @Summary   获取采购质检信息信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body     purchaserequest.GetQualityInspectionInfo   true  "参数"
// @Success   200   {object}  response.Response{data=[]purchase.PurchaseProductConfirm}  "获取确认信息"
// @Router    /purchase/getPurchaseQualityInspectionInfo [post]
func (e *PurchaseApi) GetPurchaseQualityInspectionInfo(c *gin.Context) {
   var params purchaserequest.GetQualityInspectionInfo
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   if params.PurchaseNumber == "" {
      response.FailWithMessage("采购单编码不能为空", c)
      return
   }
   infos, err := service.NewPurchaseService().GetPurchaseQualityInspection(params)
   if err != nil {
      global.GVA_LOG.Error("获取采购质检信息信息!", zap.Error(err))
      response.FailWithMessage("获取采购质检信息信息", c)
      return
   }
   response.OkWithData(infos, c)
}
// SavePurchaseQualityInspectionInfo
// @Tags      Purchase
// @Summary   保存采购质检信息信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body     purchaserequest.GetQualityInspectionInfo   true  "参数"
// @Success   200   {object}  response.Response{data=[]purchase.PurchaseProductConfirm}  "获取确认信息"
// @Router    /purchase/savePurchaseQualityInspectionInfo [post]
func (e *PurchaseApi) SavePurchaseQualityInspectionInfo(c *gin.Context) {
   var params purchaserequest.SaveQualityInspectionInfo
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   if params.PurchaseId == 0 || len(params.Ids) == 0 {
      response.FailWithMessage("采购单编码和id不能为空", c)
      return
   }
   server := service.PurchaseService{}
   if params.Status == purchase.Unqualified {
      err := server.UpdatePurchaseQualityInspection(params.Ids, params.Status)
      if err != nil {
         global.GVA_LOG.Error("质检失败!", zap.Error(err))
         response.FailWithMessage("质检失败", c)
         return
      }
   } else if params.Status == purchase.InWarehouse {
      purchaseData, err := server.GetPurchase(params.PurchaseId)
      if err != nil {
         global.GVA_LOG.Error("获取失败!", zap.Error(err))
         response.FailWithMessage("获取失败", c)
         return
      }
      inspectionList, err := server.GetPurchaseQualityInspectionList(params.Ids)
      if err != nil {
         global.GVA_LOG.Error("获取失败!", zap.Error(err))
         response.FailWithMessage("获取失败", c)
         return
      }
      product := make([]*purchase_wms.PurchaseProduct, 0)
      for _, inspection := range inspectionList {
         var p purchase_wms.PurchaseProduct
         p.Id = inspection.ProductId
         p.Amount = inspection.Amount.IntPart()
         product = append(product, &p)
      }
      client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
      _, err = client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{
         Number:        purchaseData.Number,
         SupplierName:  purchaseData.Supplier.Name,
         SupplierId:    int64(purchaseData.SupplierId),
         Product:       product,
         Source:        "SRM_PURCHASE",
         WarehouseName: purchaseData.Warehouse,
      })
      if err != nil {
         global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
         response.FailWithMessage("grpc调用失败", c)
         return
      }
      err = server.UpdatePurchaseQualityInspection(params.Ids, params.Status)
      if err != nil {
         global.GVA_LOG.Error("质检失败!", zap.Error(err))
         response.FailWithMessage("质检失败", c)
         return
      }
   }
   response.OkWithMessage("质检成功", c)
}