|
|
|
@ -1,5 +1,6 @@ |
|
|
|
|
package com.hfkj.service.order.impl; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.hfkj.common.exception.BaseException; |
|
|
|
|
import com.hfkj.common.exception.ErrorCode; |
|
|
|
@ -35,6 +36,8 @@ import com.hfkj.sysenum.mer.PayPlatformEnum; |
|
|
|
|
import com.hfkj.sysenum.order.*; |
|
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Isolation; |
|
|
|
@ -55,6 +58,7 @@ import java.util.stream.Collectors; |
|
|
|
|
@Service("orderRefundService") |
|
|
|
|
public class BsOrderRefundServiceImpl implements BsOrderRefundService { |
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(BsOrderRefundServiceImpl.class); |
|
|
|
|
@Resource |
|
|
|
|
public BsOrderRefundMapper orderRefundMapper; |
|
|
|
|
@Resource |
|
|
|
@ -192,22 +196,53 @@ public class BsOrderRefundServiceImpl implements BsOrderRefundService { |
|
|
|
|
orderRefund.setRefundPayChannel(order.getPayChannel()); |
|
|
|
|
orderRefund.setReufndPayType(order.getPayType()); |
|
|
|
|
|
|
|
|
|
if (orderChild.getSurplusRefundCount().equals(productCount)) { |
|
|
|
|
// 全部退款
|
|
|
|
|
orderRefund.setRefundPrice(orderChild.getSurplusRefundPrice()); |
|
|
|
|
orderRefund.setRefundIntegral(orderChild.getSurplusRefundIntegral()); |
|
|
|
|
} else { |
|
|
|
|
// 部分退款
|
|
|
|
|
// 计算单价
|
|
|
|
|
BigDecimal unitPrice = orderChild.getSurplusRefundPrice().divide(new BigDecimal(orderChild.getProductCount().toString()), 2, BigDecimal.ROUND_DOWN).setScale(2); |
|
|
|
|
BigDecimal unitPriceIntegral = new BigDecimal(orderChild.getSurplusRefundIntegral().toString()).divide(new BigDecimal("100")) |
|
|
|
|
.divide(new BigDecimal(orderChild.getProductCount().toString()), 2, BigDecimal.ROUND_DOWN).setScale(2); |
|
|
|
|
|
|
|
|
|
orderRefund.setRefundPrice(unitPrice.multiply(new BigDecimal(productCount.toString()))); |
|
|
|
|
orderRefund.setRefundIntegral(unitPriceIntegral.multiply(new BigDecimal("100")).longValue()); |
|
|
|
|
if (goodsVpdSource(orderChild)) { |
|
|
|
|
// 已经使用金额
|
|
|
|
|
BigDecimal useAmount = getRefundAmount(orderChild); |
|
|
|
|
if (useAmount == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款失败!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (useAmount.compareTo(new BigDecimal(0)) == 0) { |
|
|
|
|
// 全部退款
|
|
|
|
|
orderRefund.setRefundPrice(orderChild.getSurplusRefundPrice()); |
|
|
|
|
orderRefund.setRefundIntegral(orderChild.getSurplusRefundIntegral()); |
|
|
|
|
} else { |
|
|
|
|
// 判断支付金额
|
|
|
|
|
if (orderChild.getProductActualPrice().compareTo(useAmount) > 0) { |
|
|
|
|
// 部分退款
|
|
|
|
|
// 计算单价
|
|
|
|
|
BigDecimal unitPrice = orderChild.getProductActualPrice().subtract(useAmount); |
|
|
|
|
orderRefund.setRefundPrice(unitPrice.multiply(new BigDecimal(productCount.toString()))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (orderChild.getProductActualPrice().compareTo(useAmount) < 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "金额不足,退款失败"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
if (orderChild.getSurplusRefundCount().equals(productCount)) { |
|
|
|
|
// 全部退款
|
|
|
|
|
orderRefund.setRefundPrice(orderChild.getSurplusRefundPrice()); |
|
|
|
|
orderRefund.setRefundIntegral(orderChild.getSurplusRefundIntegral()); |
|
|
|
|
} else { |
|
|
|
|
// 部分退款
|
|
|
|
|
// 计算单价
|
|
|
|
|
BigDecimal unitPrice = orderChild.getSurplusRefundPrice().divide(new BigDecimal(orderChild.getProductCount().toString()), 2, BigDecimal.ROUND_DOWN).setScale(2); |
|
|
|
|
BigDecimal unitPriceIntegral = new BigDecimal(orderChild.getSurplusRefundIntegral().toString()).divide(new BigDecimal("100")) |
|
|
|
|
.divide(new BigDecimal(orderChild.getProductCount().toString()), 2, BigDecimal.ROUND_DOWN).setScale(2); |
|
|
|
|
|
|
|
|
|
orderRefund.setRefundPrice(unitPrice.multiply(new BigDecimal(productCount.toString()))); |
|
|
|
|
orderRefund.setRefundIntegral(unitPriceIntegral.multiply(new BigDecimal("100")).longValue()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (orderRefund.getRefundIntegral() > 0L) { |
|
|
|
|
Map<String,Object> opUser = new HashMap<>(); |
|
|
|
|
opUser.put("opUserType", orderRefund.getOpUserType()); |
|
|
|
@ -463,36 +498,70 @@ public class BsOrderRefundServiceImpl implements BsOrderRefundService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BigDecimal getRefundAmount(OrderChildModel orderChild) throws Exception{ |
|
|
|
|
|
|
|
|
|
private Boolean goodsVpdSource(OrderChildModel orderChild) { |
|
|
|
|
// 查询卡券订单
|
|
|
|
|
BsOrderCoupon orderCoupon = orderCouponService.getDetailByChildOrderNo(orderChild.getChildOrderNo()); |
|
|
|
|
// 查询卡券订单的卡密
|
|
|
|
|
List<BsOrderCouponNo> couponNoList = orderCouponNoService.getListByCouponOrderId(orderCoupon.getId()); |
|
|
|
|
|
|
|
|
|
BigDecimal refundAmount = BigDecimal.ZERO; |
|
|
|
|
|
|
|
|
|
// 查询内部优惠券包剩余情况
|
|
|
|
|
for (BsOrderCouponNo couponNo : couponNoList) { |
|
|
|
|
// 内部优惠券
|
|
|
|
|
if (couponNo.getGoodsVpdSource() == GoodsVpdSourceEnum.type1.getCode()) { |
|
|
|
|
// 查询用户优惠券使用情况
|
|
|
|
|
Map<String , Object> map = new HashMap<>(); |
|
|
|
|
map.put("couponOrderId" , couponNo.getId()); |
|
|
|
|
List<CouponDiscountUserRel> userRelList = discountUserRelService.getList(map); |
|
|
|
|
refundAmount = userRelList.stream().filter(s->s.getStatus().equals(2)).map(CouponDiscountUserRel::getDiscountPrice).reduce(BigDecimal.ZERO, BigDecimal::add).add(userRelList.stream().filter(s->s.getStatus().equals(1)).map(CouponDiscountUserRel::getDiscountPrice).reduce(BigDecimal.ZERO, BigDecimal::add)); |
|
|
|
|
} else if (couponNo.getGoodsVpdSource() == GoodsVpdSourceEnum.type3.getCode()) { |
|
|
|
|
// 查询嗨加油优惠券使用剩余情况
|
|
|
|
|
Map<String , Object> map = new HashMap<>(); |
|
|
|
|
map.put("reqId" ,couponNo.getChannelOrderNo()); |
|
|
|
|
map.put("discountPkStockCode" , couponNo.getGoodsVpdSourceCouNo()); |
|
|
|
|
JSONObject returnParam = HaiOilService.queryPkDetailByStockCode(map); |
|
|
|
|
return couponNoList.get(0).getGoodsVpdSource().equals(GoodsVpdSourceEnum.type1.getCode()) || couponNoList.get(0).getGoodsVpdSource().equals(GoodsVpdSourceEnum.type3.getCode()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private BigDecimal getRefundAmount(OrderChildModel orderChild){ |
|
|
|
|
try { |
|
|
|
|
// 查询卡券订单
|
|
|
|
|
BsOrderCoupon orderCoupon = orderCouponService.getDetailByChildOrderNo(orderChild.getChildOrderNo()); |
|
|
|
|
// 查询卡券订单的卡密
|
|
|
|
|
List<BsOrderCouponNo> couponNoList = orderCouponNoService.getListByCouponOrderId(orderCoupon.getId()); |
|
|
|
|
|
|
|
|
|
// 定义已经使用的金额
|
|
|
|
|
BigDecimal useAmount = BigDecimal.ZERO; |
|
|
|
|
|
|
|
|
|
// 查询内部优惠券包剩余情况
|
|
|
|
|
for (BsOrderCouponNo couponNo : couponNoList) { |
|
|
|
|
// 内部优惠券
|
|
|
|
|
if (couponNo.getGoodsVpdSource() == GoodsVpdSourceEnum.type1.getCode()) { |
|
|
|
|
// 查询用户优惠券使用情况
|
|
|
|
|
Map<String , Object> map = new HashMap<>(); |
|
|
|
|
map.put("couponOrderId" , couponNo.getId()); |
|
|
|
|
List<CouponDiscountUserRel> userRelList = discountUserRelService.getList(map); |
|
|
|
|
useAmount = userRelList.stream().filter(s->s.getStatus().equals(2)).map(CouponDiscountUserRel::getDiscountPrice).reduce(BigDecimal.ZERO, BigDecimal::add).add(userRelList.stream().filter(s->s.getStatus().equals(0)).map(CouponDiscountUserRel::getDiscountPrice).reduce(BigDecimal.ZERO, BigDecimal::add)); |
|
|
|
|
} else if (couponNo.getGoodsVpdSource() == GoodsVpdSourceEnum.type3.getCode()) { |
|
|
|
|
// 查询嗨加油优惠券使用剩余情况
|
|
|
|
|
Map<String , Object> map = new HashMap<>(); |
|
|
|
|
map.put("discountPkStockCode" , couponNo.getGoodsVpdSourceCouNo()); |
|
|
|
|
JSONObject returnParam = HaiOilService.queryPkDetailByStockCode(map); |
|
|
|
|
if (returnParam.getString("return_code").equals("000000")) { |
|
|
|
|
JSONArray data = returnParam.getJSONObject("return_data").getJSONArray("codeList"); |
|
|
|
|
// 1:未使用 2:已使用 3:已过期 4:已作废
|
|
|
|
|
for (int i = 0; i < data.size(); i++) { |
|
|
|
|
JSONObject jsonObject = data.getJSONObject(i); |
|
|
|
|
if (!jsonObject.getInteger("status").equals(1)) { |
|
|
|
|
useAmount = useAmount.add(jsonObject.getBigDecimal("discountPrice")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款失败!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 回收券包
|
|
|
|
|
Map<String , Object> mapRefund = new HashMap<>(); |
|
|
|
|
mapRefund.put("discountPkStockCode" , couponNo.getGoodsVpdSourceCouNo()); |
|
|
|
|
JSONObject refundParam = HaiOilService.refundPk(mapRefund); |
|
|
|
|
|
|
|
|
|
if (!refundParam.getString("return_code").equals("000000") || !refundParam.getString("return_data").equals("ok")) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "退款失败!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 查询嗨加油优惠券包剩余情况
|
|
|
|
|
// 查询嗨加油优惠券包剩余情况
|
|
|
|
|
|
|
|
|
|
return refundAmount; |
|
|
|
|
return useAmount; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
System.out.println(e); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|