Exemplo n.º 1
0
        public ApiResult ApplyRefund([FromBody] RefundApplyModel apply)
        {
            Argument.ThrowIfNullOrEmpty(apply.Reason, "退款原因");
            if (apply.RefundAmount <= 0)
            {
                throw new WebApiInnerException("0009", "退款金额不能小于等于0");
            }

            if (apply.OrderId.Equals(Guid.Empty))
            {
                throw new WebApiInnerException("0001", "订单Id不合法");
            }

            var order = _currencyService.GetSingleById <Order>(apply.OrderId);

            if (order == null)
            {
                throw new WebApiInnerException("0002", "订单不存在");
            }
            if (order.OrderStatus == OrderStatus.WaitingForDelivery && apply.RefundType == RefundType.RefundAndReturn)
            {
                throw new WebApiInnerException("0003", "订单未发货,不能申请退款并退货");
            }
            if (order.OrderStatus != OrderStatus.WaitingForDelivery && order.OrderStatus != OrderStatus.WaitingForReceiving)
            {
                throw new WebApiInnerException("0004", "当前订单状态不能申请退款");
            }
            if (!order.MemberId.Equals(AuthorizedUser.Id))
            {
                throw new WebApiInnerException("0005", "只能对自己的订单申请退款");
            }

            var singleGoods = _currencyService.GetSingleByConditon <OrderGoods>(x => x.OrderId == apply.OrderId && x.SingleGoodsId == apply.SingleGoodsId);

            if (singleGoods == null)
            {
                throw new WebApiInnerException("0006", "商品不存在");
            }
            if (singleGoods.RefundStatus != OrderRefundStatus.NoRefund)
            {
                throw new WebApiInnerException("0007", "已申请退款不可重复申请");
            }

            var maxRefundAmount = singleGoods.Price * singleGoods.Quantity;

            if (order.IntegralMoney > 0)
            {
                maxRefundAmount -= order.IntegralMoney * (singleGoods.Price * singleGoods.Quantity / order.GoodsAmount);
            }
            if (apply.RefundAmount > maxRefundAmount)
            {
                throw new WebApiInnerException("0008", "退款金额不能大于单品实付金额");
            }

            var model = new OrderRefund()
            {
                OrderId       = apply.OrderId,
                SingleGoodsId = apply.SingleGoodsId,
                RefundType    = apply.RefundType,
                RefundAmount  = apply.RefundAmount,
                Reason        = apply.Reason
            };

            _refundService.CreateOrderRefund(model);

            ApiResult result = new ApiResult();

            return(result);
        }