| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "context" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "go.uber.org/zap" |
| | |
| | | "srm/model/common/response" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/purchase_wms" |
| | | "strconv" |
| | | "strings" |
| | | "time" |
| | |
| | | purchaseRecord.Creator = "admin" |
| | | purchaseRecord.Number = fmt.Sprintf("CG%v", time.Now().Unix()) |
| | | purchaseRecord.Principal = "admin" |
| | | purchaseRecord.OrderType = "采购订单" |
| | | |
| | | if !purchaseRecord.WholeDiscountType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.WholeDiscount) { |
| | | response.FailWithMessage("整单折扣数值不正确", c) |
| | |
| | | // @Produce application/json |
| | | // @Param id path int true "采购单ID" |
| | | // @Success 200 {object} response.Response{msg=string} "提交采购单" |
| | | // @Router /purchase/submit/{id} [post] |
| | | // @Router /purchase/submit [post] |
| | | func (e *PurchaseApi) Submit(c *gin.Context) { |
| | | id, _ := strconv.Atoi(c.Param("id")) |
| | | if id == 0 { |
| | | response.FailWithMessage("参数缺失", c) |
| | | var params purchaserequest.SubmitPurchase |
| | | err := c.ShouldBindJSON(¶ms) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("Submit failed", zap.Error(err)) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | err := service.NewPurchaseService().Submit(uint(id)) |
| | | if params.Status == purchase.OrderStatusReceived { |
| | | data, err := service.NewPurchaseService().GetPurchase(uint(params.Id)) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(params.Id)) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | product := make([]*purchase_wms.PurchaseProduct, 0) |
| | | for _, products := range productList { |
| | | var p purchase_wms.PurchaseProduct |
| | | p.Id = products.Product.Number |
| | | p.Amount = products.Amount.IntPart() |
| | | product = append(product, &p) |
| | | } |
| | | client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn) |
| | | _, err = client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{ |
| | | Number: data.Number, |
| | | SupplierName: data.Supplier.Name, |
| | | Product: product, |
| | | }) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("grpc调用失败!", zap.Error(err)) |
| | | response.FailWithMessage("grpc调用失败", c) |
| | | return |
| | | } |
| | | } |
| | | |
| | | err = service.NewPurchaseService().Submit(params.Id, params.Status) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("更新失败!", zap.Error(err)) |
| | | response.FailWithMessage("更新失败", c) |