package org.xmx0632.deliciousfruit.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderRequest.Payment;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderRequest.Payment.CashCouponPay;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderRequest.Payment.GiftCardPay;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderResponse.DeductionPay;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderResponse.PayByCashCoupon;
import org.xmx0632.deliciousfruit.api.v1.bo.OrderResponse.PayByGiftCard;
public class DeductionPayServiceHelper {
public static final String CASH_COUPON_ID_1 = "1";
public static final String CASH_COUPON_ID_2 = "2";
public static final String GIFT_CARD_ID_1 = "1";
public static final String GIFT_CARD_ID_2 = "2";
public static OrderRequest getOrderRequest4CashCoupon(
BigDecimal payAmount4CashCoupon, String cashCouponId) {
OrderRequest orderRequest = new OrderRequest();
List<CashCouponPay> cashCoupons = Arrays.asList(new CashCouponPay(
cashCouponId, payAmount4CashCoupon));
Payment payment = setupPayment(cashCoupons, null, BigDecimal.ZERO, 0,
BigDecimal.valueOf(0));
orderRequest.setPayment(payment);
return orderRequest;
}
private static Payment setupPayment(List<CashCouponPay> cashCoupons,
List<GiftCardPay> giftCards, BigDecimal payAmountFromAccount,
int payPonit, BigDecimal payAmountFromPoint) {
Payment payment = new Payment();
payment.setCashCoupons(cashCoupons);
payment.setGiftCards(giftCards);
payment.setPayAmountFromAccount(payAmountFromAccount);
payment.setPayAmountFromPonit(payAmountFromPoint);
payment.setPayPonit(payPonit);
return payment;
}
public static OrderRequest getOrderRequest4NoDeductionPay() {
return new OrderRequest();
}
public static OrderRequest getOrderRequest4PayAmountFromAccount(
BigDecimal payAmountFromAccount) {
OrderRequest orderRequest = new OrderRequest();
// 抵扣余额
Payment payment = setupPayment(null, null, payAmountFromAccount, 0,
BigDecimal.valueOf(0));
orderRequest.setPayment(payment);
return orderRequest;
}
public static OrderRequest getOrderRequest4PayByPointInfo(
BigDecimal payAmountFromPoint, int payPonit) {
OrderRequest orderRequest = new OrderRequest();
Payment payment = setupPayment(null, null, BigDecimal.ZERO, payPonit,
payAmountFromPoint);
orderRequest.setPayment(payment);
return orderRequest;
}
public static OrderRequest getOrderRequest4GiftCard(
BigDecimal giftPayAmount, String giftCardId) {
OrderRequest orderRequest = new OrderRequest();
List<GiftCardPay> giftCards = Arrays.asList(new GiftCardPay(giftCardId,
"passwd", giftPayAmount));
Payment payment = setupPayment(null, giftCards, BigDecimal.ZERO, 0,
BigDecimal.ZERO);
orderRequest.setPayment(payment);
return orderRequest;
}
public static OrderRequest getOrderRequest4GiftCardAndPayAmountFromAccount(
String giftCardId, BigDecimal giftPayAmount,
BigDecimal payAmountFromAccount) {
OrderRequest orderRequest = new OrderRequest();
// 抵扣礼品卡
List<GiftCardPay> giftCards = Arrays.asList(new GiftCardPay(giftCardId,
"passwd", giftPayAmount));
// 抵扣余额
Payment payment = setupPayment(null, giftCards, payAmountFromAccount,
0, BigDecimal.ZERO);
orderRequest.setPayment(payment);
return orderRequest;
}
public static OrderRequest getOrderRequest4GiftCardAndPayByPointInfo(
String giftCardId, BigDecimal giftPayAmount, int payPonit,
BigDecimal payAmountFromPoint) {
OrderRequest orderRequest = new OrderRequest();
// 抵扣礼品卡
List<GiftCardPay> giftCards = Arrays.asList(new GiftCardPay(giftCardId,
"passwd", giftPayAmount));
// 积分
Payment payment = setupPayment(null, giftCards, BigDecimal.ZERO,
payPonit, payAmountFromPoint);
orderRequest.setPayment(payment);
return orderRequest;
}
public static OrderRequest getOrderRequest4AllDeductionPay(
String cashCouponId, BigDecimal cashCouponPayAmount,
String giftCardId, BigDecimal giftPayAmount, int payPonit,
BigDecimal payAmountFromPoint, BigDecimal payAmountFromAccount) {
// 现金券
OrderRequest orderRequest = new OrderRequest();
List<CashCouponPay> cashCoupons = Arrays.asList(new CashCouponPay(
cashCouponId, cashCouponPayAmount));
// 抵扣礼品卡
List<GiftCardPay> giftCards = Arrays.asList(new GiftCardPay(giftCardId,
"passwd", giftPayAmount));
Payment payment = setupPayment(cashCoupons, giftCards,
payAmountFromAccount, payPonit, payAmountFromPoint);
orderRequest.setPayment(payment);
return orderRequest;
}
public static void assertDeductionPay(ExpectedDeductionPayResult param,
DeductionPay deductionPay) {
assertNotNull(deductionPay);
// 有现金券抵扣付款
assertEquals(param.payByCashCouponInfoSize, deductionPay
.getPayByCashCouponInfo().size());
if (param.payByCashCouponInfoSize > 0) {
PayByCashCoupon dp = deductionPay.getPayByCashCouponInfo().get(0);
assertEquals(param.payCashCouponAmount, dp.getDeductionAmount());
assertEquals(0, dp.getPayLeftTimes());
}
// 礼品卡抵扣付款
assertEquals(param.payByGiftCardInfoSize, deductionPay
.getPayByGiftCardInfo().size());
if (param.payByGiftCardInfoSize > 0) {
PayByGiftCard dp = deductionPay.getPayByGiftCardInfo().get(0);
assertEquals(param.giftDeductionAmount, dp.getDeductionAmount());
assertEquals(param.giftBalance, dp.getBalance());
}
// 用积分抵扣付款
BigDecimal payByPoint = deductionPay.getPayByPoint();
assertEquals(param.deductionPointAmount, payByPoint);
// 用账户余额抵扣付款
BigDecimal payFromAccount = deductionPay.getPayFromAccount();
assertEquals(param.deductionAccountAmount, payFromAccount);
}
}