jiangshuai
2023-11-22 0022d062fc8c72061b4d62dc5e192e75588b6979
controllers/reorder_rule_controller.go
@@ -3,15 +3,21 @@
import (
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "google.golang.org/grpc"
   "google.golang.org/grpc/credentials/insecure"
   "gorm.io/gorm"
   "strconv"
   "strings"
   "time"
   "wms/conf"
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
   "wms/models"
   "wms/pkg/logx"
   "wms/pkg/timex"
   "wms/proto/inventory_order"
   "wms/proto/purchase_wms"
   "wms/request"
)
@@ -114,7 +120,7 @@
      rule.Prediction = rule.Amount.Add(rule.Prediction)
   }
   //出库就绪
   operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
   operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}
   amount, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
@@ -216,7 +222,7 @@
   }
   prediction = amount.Add(prediction)
   //出库就绪
   operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
   operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}
   list, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败")
@@ -254,6 +260,23 @@
   util.ResponseFormat(c, code.Success, "更新成功")
}
var InventoryOrderServiceConn *grpc.ClientConn
func InitInventoryOrderServiceConn() {
   var err error
   InventoryOrderServiceConn, err = grpc.Dial(conf.GrpcServerConf.ApsAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
   if err != nil {
      logx.Errorf("grpc dial product service error: %v", err.Error())
      return
   }
}
func CloseInventoryOrderServiceConn() {
   if InventoryOrderServiceConn != nil {
      InventoryOrderServiceConn.Close()
   }
}
// OrderAgain
// @Tags      重订货规则
// @Summary   再订一次
@@ -267,18 +290,46 @@
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if params.Route == "采购" {
      client := purchase_wms.NewPurchaseServiceClient(PurchaseServiceConn)
      resp, err := client.GetSupplierListByProductId(c, &purchase_wms.GetSupplierListByProductIdRequest{ProductId: params.ProductId})
      if err != nil {
         util.ResponseFormat(c, code.RequestParamError, "grpc调用失败")
         return
      }
      util.ResponseFormat(c, code.Success, resp.List)
      return
   }
   client := inventory_order.NewInventoryOrderServiceClient(InventoryOrderServiceConn)
   order, err := client.CreateNewOrder(c, &inventory_order.CreateNewOrderRequest{
      OrderNumber: params.OrderNumber.IntPart(),
      Unit:        params.Unit,
      ProductId:   params.ProductId,
      Customer:    "WMS推送",
   })
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "grpc调用失败")
      return
   }
   err = orderAgain(params, order.OrderId)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "重订失败")
      return
   }
   util.ResponseFormat(c, code.Success, "重订成功")
}
func orderAgain(params models.ReorderRule, SourceNumber string) error {
   location, err := models.NewLocationSearch().SetID(params.LocationId).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败")
      return
      return err
   }
   houseCode := strings.Split(location.JointName, "/")[0]
   var operationType models.OperationType
   err = models.NewOperationTypeSearch().Orm.Model(&models.OperationType{}).Joins("left join wms_warehouse on wms_job_type.warehouse_id = wms_warehouse.id").
      Where("wms_job_type.base_operation_type = 1 and wms_warehouse.code = ?", houseCode).First(&operationType).Error
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败")
      return
      return err
   }
   var operation models.Operation
   var details models.OperationDetails
@@ -294,18 +345,42 @@
   operation.FromLocationID = 1
   operation.Number = strconv.FormatInt(time.Now().Unix(), 10)
   operation.ToLocationID = params.LocationId
   operation.SourceNumber = SourceNumber
   err = models.WithTransaction(func(db *gorm.DB) error {
      if err = models.NewOperationSearch().SetOrm(db).Create(&operation); err != nil {
         return err
      }
      params.OrderNumber = decimal.NewFromInt(0)
      err = models.NewReorderRuleSearch().SetID(params.Id).Update(&params)
      err = models.NewReorderRuleSearch().SetOrm(db).SetID(params.Id).Update(&params)
      return err
   })
   return err
}
// SubmitOrder
// @Tags      重订货规则
// @Summary   再订一次
// @Produce   application/json
// @Param     object  body  models.ReorderRule true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/reorderRule/submitOrder [post]
func (slf ReorderRuleController) SubmitOrder(c *gin.Context) {
   var params models.ReorderRule
   client := purchase_wms.NewPurchaseServiceClient(PurchaseServiceConn)
   resp, err := client.CreatePurchaseByWms(c, &purchase_wms.CreatePurchaseByWmsRequest{
      SupplierId: params.SupplierId,
      ProductId:  params.ProductId,
      Amount:     params.OrderNumber.IntPart(),
   })
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "grpc调用失败")
      return
   }
   err = orderAgain(params, resp.PurchaseNumber)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "重订失败")
      return
   }
   util.ResponseFormat(c, code.Success, "重订成功")
}