| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "context" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/mitchellh/mapstructure" |
| | | "go.uber.org/zap" |
| | | "gorm.io/gorm" |
| | | "srm/global" |
| | |
| | | "srm/model/common/response" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/purchase_wms" |
| | | "strconv" |
| | | "strings" |
| | | "time" |
| | | |
| | | //"srm/model/purchase" |
| | | |
| | |
| | | var params purchaserequest.AddPurchase |
| | | err := c.ShouldBindJSON(¶ms) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("Add Purchase failed", zap.Error(err)) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | |
| | | var purchaseRecord purchase.Purchase |
| | | if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { |
| | | if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil { |
| | | global.GVA_LOG.Error("Add Purchase failed", zap.Error(err)) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | |
| | | purchaseRecord.Status = purchase.OrderStatusConfirmed |
| | | purchaseRecord.HandledBy = "admin" |
| | | 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) |
| | | return |
| | | } |
| | | |
| | | if !purchaseRecord.PriceAdjustmentType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.PriceAdjustment) { |
| | | response.FailWithMessage("价格调整数值不正确", c) |
| | | return |
| | | } |
| | | |
| | | err = service.NewPurchaseService().CreatePurchase(&purchaseRecord, params.ProductList) |
| | | |
| | | if err != nil { |
| | |
| | | return |
| | | } |
| | | global.GVA_LOG.Error("创建失败!", zap.Error(err)) |
| | | response.FailWithMessage("创建失败", c) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | response.OkWithMessage("创建成功", c) |
| | |
| | | } |
| | | |
| | | var purchaseRecord purchase.Purchase |
| | | if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { |
| | | if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil { |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | |
| | | return |
| | | } |
| | | respProductList := make([]*purchaseRes.PurchaseProducts, len(productList)) |
| | | err = mapstructure.Decode(productList, &respProductList) |
| | | err = utils.AssignTo(productList, &respProductList) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | |
| | | respProductList[k].Amount = item.Amount |
| | | respProductList[k].Price = item.Price |
| | | respProductList[k].Total = item.Total |
| | | err = mapstructure.Decode(item.Product, &respProductList[k]) |
| | | err = utils.AssignTo(item.Product, &respProductList[k]) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | 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) |
| | |
| | | } |
| | | |
| | | purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params)) |
| | | if err := mapstructure.Decode(params, &purchaseTypeList); err != nil { |
| | | if err := utils.AssignTo(params, &purchaseTypeList); err != nil { |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |