zhangqian
2024-03-15 2eb20330dfbda6136f1b8cc804cc21438ae4dc13
api/v1/salesDetails.go
@@ -15,6 +15,7 @@
   "aps_crm/utils"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "strconv"
   "strings"
)
@@ -240,7 +241,7 @@
// GetProductInventoryInfo
//
// @Tags      SalesDetails
// @Summary   获取产品库存信息
// @Summary   获取产品发货信息
// @Produce   application/json
// @Param      number   path      string   true   "明细编码"
// @Success   200   {object}   response.ListResponse
@@ -272,6 +273,172 @@
   ctx.OkWithDetailed(list)
}
type GetWarehouseProductInfoReq struct {
   SaleDetailID     int    `json:"saleDetailID,omitempty"`
   SaleDetailNumber string `json:"saleDetailNumber,omitempty"`
}
// GetDeliveryPrepareInfo
// @Tags      SalesDetails
// @Summary   获取产品入库信息
// @Produce   application/json
// @Param      object    body GetWarehouseProductInfoReq   true   "明细编码"
// @Success   200   {object}   response.ListResponse
// @Router      /api/salesDetails/getDeliveryPrepareInfo [post]
func (s *SalesDetailsApi) GetDeliveryPrepareInfo(c *gin.Context) {
   var params GetWarehouseProductInfoReq
   ctx, ok := contextx.NewContext(c, &params)
   if !ok {
      return
   }
   if params.SaleDetailID == 0 {
      ctx.FailWithMsg(ecode.ParamsErr, "参数缺失")
      return
   }
   salesDetails, err := salesDetailsService.GetSalesDetails(params.SaleDetailID)
   if err == gorm.ErrRecordNotFound || salesDetails.Number != params.SaleDetailNumber {
      ctx.FailWithMsg(ecode.ParamsErr, "销售订单不存在")
      return
   }
   productMap := model.ProductMap(salesDetails.Products)
   client := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
   grpcResp, err := client.GetOrderInputAndOutputInfo(ctx.GetCtx(), &product_inventory.GetOrderInputAndOutputInfoRequest{
      Number: params.SaleDetailNumber,
   })
   if err != nil {
      if strings.Contains(err.Error(), "record not found") {
         ctx.Ok()
         return
      }
      logx.Errorf("GetOrderInputAndOutputInfo err: %v", err.Error())
      ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误")
      return
   }
   grpcOutputList := grpcResp.OutputList
   grpcInputList := grpcResp.InputList
   inputProductMap := make(map[string]*response.StoreInfo)
   outputProductMap := make(map[string]*response.OutputSimpleInfo)
   for _, v := range grpcOutputList {
      if outputProductMap[v.Number] == nil {
         simpleInfo := &response.OutputSimpleInfo{
            Number: v.Number,
         }
         amount, _ := decimal.NewFromString(v.Amount)
         simpleInfo.Amount = amount
         outputProductMap[v.Number] = simpleInfo
      } else {
         amount, _ := decimal.NewFromString(v.Amount)
         outputProductMap[v.Number].Amount = outputProductMap[v.Number].Amount.Add(amount)
      }
   }
   for _, v := range grpcInputList {
      if inputProductMap[v.Number] == nil {
         storeInfo := &response.StoreInfo{
            Number:      v.Number,
            Name:        v.Name,
            OrderAmount: productMap[v.Number].Amount,
         }
         finishAmount, _ := decimal.NewFromString(v.Amount)
         storeInfo.FinishAmount = finishAmount
         storeInfo.AvailableAmount = finishAmount
         storeInfo.LeftAmount = storeInfo.OrderAmount
         inputProductMap[v.Number] = storeInfo
      } else {
         finishAmount, _ := decimal.NewFromString(v.Amount)
         inputProductMap[v.Number].FinishAmount = inputProductMap[v.Number].FinishAmount.Add(finishAmount)
         inputProductMap[v.Number].AvailableAmount = inputProductMap[v.Number].FinishAmount
      }
   }
   storeList := make([]*response.StoreInfo, 0, len(salesDetails.Products))
   for _, product := range salesDetails.Products {
      storeInfo := inputProductMap[product.Number]
      if storeInfo == nil { //没有入库信息
         storeInfo = &response.StoreInfo{
            Name:            product.Number,
            Number:          product.Number,
            OrderAmount:     product.Amount,
            FinishAmount:    decimal.Decimal{},
            LeftAmount:      product.Amount,
            AvailableAmount: decimal.Decimal{},
         }
      } else { //有入库数量再查出库,算出未发货数量
         if outputProductMap[product.Number] != nil {
            outputInfo := outputProductMap[product.Number]
            storeInfo.LeftAmount = storeInfo.LeftAmount.Sub(outputInfo.Amount)           //剩余发货数量 = 订单数量 - 已发货数量
            storeInfo.AvailableAmount = storeInfo.AvailableAmount.Sub(outputInfo.Amount) //可用数量 = 入库完成数量 - 已发货数量
         }
      }
      storeList = append(storeList, storeInfo)
   }
   ctx.OkWithDetailed(storeList)
}
// GetDeliveryList
// @Tags      SalesDetails
// @Summary   发货明细
// @Produce   application/json
// @Param      object    body GetWarehouseProductInfoReq   true   "明细编码"
// @Success   200   {object}   response.ListResponse
// @Router      /api/salesDetails/getDeliveryList [post]
func (s *SalesDetailsApi) GetDeliveryList(c *gin.Context) {
   var params GetWarehouseProductInfoReq
   ctx, ok := contextx.NewContext(c, &params)
   if !ok {
      return
   }
   if params.SaleDetailID == 0 {
      ctx.FailWithMsg(ecode.ParamsErr, "参数缺失")
      return
   }
   salesDetails, err := salesDetailsService.GetSalesDetails(params.SaleDetailID)
   if err == gorm.ErrRecordNotFound || salesDetails.Number != params.SaleDetailNumber {
      ctx.FailWithMsg(ecode.ParamsErr, "销售订单不存在")
      return
   }
   productMap := model.ProductMap(salesDetails.Products)
   client := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
   grpcResp, err := client.GetOrderInputAndOutputInfo(ctx.GetCtx(), &product_inventory.GetOrderInputAndOutputInfoRequest{
      Number: params.SaleDetailNumber,
   })
   if err != nil {
      if strings.Contains(err.Error(), "record not found") {
         ctx.Ok()
         return
      }
      logx.Errorf("GetOrderInputAndOutputInfo err: %v", err.Error())
      ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误")
      return
   }
   grpcOutputList := grpcResp.OutputList
   outputList := make([]*response.OutputInfo, 0, len(grpcOutputList))
   for _, v := range grpcOutputList {
      outputInfo := &response.OutputInfo{
         Number:      v.Number,
         Name:        v.Name,
         OrderAmount: productMap[v.Number].Amount.String(),
         Unit:        v.Unit,
         Invoice:     v.Invoice,
         Carrier:     v.Carrier,
         Waybill:     v.Waybill,
         SalePrice:   v.SalePrice,
         Valorem:     v.Valorem,
         Warehouse:   v.Warehouse,
         Amount:      v.Amount,
         Status:      int(v.Status),
         Specs:       productMap[v.Number].Specs,
         CreateTime:  v.CreateTime,
      }
      outputList = append(outputList, outputInfo)
   }
   ctx.OkWithDetailed(outputList)
}
// GetApsProjectList
//
// @Tags      SalesDetails