diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/AbstractProgramLogic.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/AbstractProgramLogic.java index 22c238126..732547e47 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/AbstractProgramLogic.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/AbstractProgramLogic.java @@ -105,6 +105,9 @@ public abstract class AbstractProgramLogic implements InitializingBean { @Autowired protected MemberWalletInfoService memberWalletInfoService; + @Autowired + protected MemberGroupService memberGroupService; + @Autowired protected RedisCache redisCache; @@ -424,30 +427,46 @@ public abstract class AbstractProgramLogic implements InitializingBean { * @return */ protected Map calculateReturnAmount(BigDecimal principalPay, BigDecimal giftPay, BigDecimal orderAmount) { + return calculateReturnAmount(principalPay, giftPay, orderAmount, BigDecimal.ZERO); + } + + /** + * 余额支付 计算需要退回的金额 + * 【公共方法】 + * @param principalPay 本金支付金额 + * @param giftPay 赠送金额支付的金额 + * @param orderAmount 订单消费金额 + * @param discountAmount 订单折扣金额 + * @return + */ + protected Map calculateReturnAmount(BigDecimal principalPay, BigDecimal giftPay, BigDecimal orderAmount, BigDecimal discountAmount) { Map resultMap = Maps.newHashMap(); // 消费金额优先使用本金 BigDecimal returnPrincipal = null; // 退回本金金额 BigDecimal returnGift = null; // 退回赠送金额 + // 实际消费金额 = 订单消费金额 - 订单折扣金额 + BigDecimal amountDue = orderAmount.subtract(discountAmount); + // 余额支付 有3种情况 if (principalPay != null && giftPay == null) { // 只有本金支付 - BigDecimal subtract = principalPay.subtract(orderAmount); + BigDecimal subtract = principalPay.subtract(amountDue); if (subtract.compareTo(BigDecimal.ZERO) > 0) { returnPrincipal = subtract; } } if (principalPay == null && giftPay != null) { // 只有赠送金额支付 - BigDecimal subtract = giftPay.subtract(orderAmount); + BigDecimal subtract = giftPay.subtract(amountDue); if (subtract.compareTo(BigDecimal.ZERO) > 0) { returnGift = subtract; } } if (principalPay != null && giftPay != null) { // 本金+赠送支付 - BigDecimal subtract = principalPay.subtract(orderAmount); + BigDecimal subtract = principalPay.subtract(amountDue); if (subtract.compareTo(BigDecimal.ZERO) > 0) { // 本金减掉订单金额后还有剩余,那就把剩余的退回,赠送原封不动退回 returnPrincipal = subtract; diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/DelayMerchantProgramLogic.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/DelayMerchantProgramLogic.java index 5dd9e7af6..d2201f2a7 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/DelayMerchantProgramLogic.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/programlogic/DelayMerchantProgramLogic.java @@ -31,6 +31,7 @@ import com.jsowell.pile.dto.*; import com.jsowell.pile.transaction.dto.OrderTransactionDTO; import com.jsowell.pile.vo.uniapp.MemberVO; import com.jsowell.pile.vo.web.BalanceDeductionAmountVO; +import com.jsowell.pile.vo.web.MemberDiscountVO; import com.jsowell.pile.vo.web.OrderDetailInfoVO; import com.jsowell.pile.vo.web.UpdateMemberBalanceDTO; import com.jsowell.wxpay.dto.WechatSendMsgDTO; @@ -40,6 +41,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.ParseException; import java.time.LocalDateTime; import java.util.List; @@ -77,119 +79,6 @@ public class DelayMerchantProgramLogic extends AbstractProgramLogic { return saveOrder2Database(dto); } - /** - * 生成订单编号 - * - * @return - */ - // private String generateNewOrderCode() { - // while (true) { - // String orderCode = "C" + IdUtils.getOrderCode(); - // // 通过orderCode查询是否已经存在 - // OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode); - // if (orderBasicInfo == null) { - // return orderCode; - // } - // } - // } - - /** - * 保存订单信息到数据库 - * - * @param dto - * @return - */ - // protected OrderBasicInfo saveOrder2Database(GenerateOrderDTO dto) throws ParseException { - // String orderCode = generateNewOrderCode(); - // String transactionCode = IdUtils.generateTransactionCode(dto.getPileSn(), dto.getConnectorCode()); - // - // if (StringUtils.isBlank(dto.getStartType())) { - // dto.setStartType(StartTypeEnum.NOW.getValue()); - // } - // - // String stationId = dto.getPileConnector().getStationId(); - // // 查询站点信息 - // PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(Long.valueOf(stationId)); - // String merchantId = pileStationInfo != null ? String.valueOf(pileStationInfo.getMerchantId()) : ""; - // String plateNumber = dto.getPlateNumber() != null ? dto.getPlateNumber() : ""; - // // 订单基本信息 - // OrderBasicInfo orderBasicInfo = OrderBasicInfo.builder() - // .orderCode(orderCode) - // .transactionCode(transactionCode) - // .orderStatus(OrderStatusEnum.NOT_START.getValue()) - // .memberId(dto.getMemberId()) - // .stationId(stationId) - // .merchantId(merchantId) - // .pileSn(dto.getPileSn()) - // .connectorCode(dto.getConnectorCode()) - // .pileConnectorCode(dto.getPileSn() + dto.getConnectorCode()) - // .startMode(dto.getStartMode()) - // .payStatus(Constants.ZERO) - // .payAmount(dto.getChargeAmount()) - // .payMode(dto.getPayMode()) - // .plateNumber(plateNumber) - // .orderAmount(BigDecimal.ZERO) - // .virtualAmount(BigDecimal.ZERO) - // .settleAmount(BigDecimal.ZERO) - // .startType(dto.getStartType()) - // .build(); - // if (StringUtils.equals(dto.getStartMode(), StartModeEnum.AUTH_CARD.getValue())) { - // // 鉴权卡启动 - // orderBasicInfo.setLogicCard(dto.getPileAuthCardInfo().getLogicCard()); - // } - // if (StringUtils.equals(dto.getStartMode(), StartModeEnum.VIN_CODE.getValue())) { - // // vin启动 - // MemberPlateNumberRelation memberPlateNumberRelation = dto.getMemberPlateNumberRelation(); - // if (memberPlateNumberRelation != null) { - // if (StringUtils.isNotBlank(memberPlateNumberRelation.getVinCode())) { - // orderBasicInfo.setVinCode(memberPlateNumberRelation.getVinCode()); - // } - // if (StringUtils.isNotBlank(memberPlateNumberRelation.getLicensePlateNumber())) { - // orderBasicInfo.setPlateNumber(memberPlateNumberRelation.getLicensePlateNumber()); - // } - // } - // } - // - // if (StringUtils.equals(dto.getStartType(), StartTypeEnum.APPOINTMENT.getValue())) { - // orderBasicInfo.setAppointmentTime(DateUtils.parseDate(dto.getAppointmentTime(), DateUtils.YYYY_MM_DD_HH_MM_SS)); - // } - // - // // 订单详情 - // BillingTemplateVO billingTemplate = dto.getBillingTemplate(); - // logger.info("订单使用的计费模板-orderCode:{}, billingTemplate:{}", orderCode, JSONObject.toJSONString(billingTemplate)); - // BigDecimal sharpElectricityPrice = billingTemplate.getSharpElectricityPrice() != null ? billingTemplate.getSharpElectricityPrice() : BigDecimal.ZERO; - // BigDecimal sharpServicePrice = billingTemplate.getSharpServicePrice() != null ? billingTemplate.getSharpServicePrice() : BigDecimal.ZERO; - // BigDecimal peakElectricityPrice = billingTemplate.getPeakElectricityPrice() != null ? billingTemplate.getPeakElectricityPrice() : BigDecimal.ZERO; - // BigDecimal peakServicePrice = billingTemplate.getPeakServicePrice() != null ? billingTemplate.getPeakServicePrice() : BigDecimal.ZERO; - // BigDecimal flatElectricityPrice = billingTemplate.getFlatElectricityPrice() != null ? billingTemplate.getFlatElectricityPrice() : BigDecimal.ZERO; - // BigDecimal flatServicePrice = billingTemplate.getFlatServicePrice() != null ? billingTemplate.getFlatServicePrice() : BigDecimal.ZERO; - // BigDecimal valleyElectricityPrice = billingTemplate.getValleyElectricityPrice() != null ? billingTemplate.getValleyElectricityPrice() : BigDecimal.ZERO; - // BigDecimal valleyServicePrice = billingTemplate.getValleyServicePrice() != null ? billingTemplate.getValleyServicePrice() : BigDecimal.ZERO; - // - // OrderDetail orderDetail = OrderDetail.builder() - // .orderCode(orderCode) - // .sharpPrice(sharpElectricityPrice.add(sharpServicePrice)) - // .sharpElectricityPrice(sharpElectricityPrice) - // .sharpServicePrice(sharpServicePrice) - // .peakPrice(peakElectricityPrice.add(peakServicePrice)) - // .peakElectricityPrice(peakElectricityPrice) - // .peakServicePrice(peakServicePrice) - // .flatPrice(flatElectricityPrice.add(flatServicePrice)) - // .flatElectricityPrice(flatElectricityPrice) - // .flatServicePrice(flatServicePrice) - // .valleyPrice(valleyElectricityPrice.add(valleyServicePrice)) - // .valleyElectricityPrice(valleyElectricityPrice) - // .valleyServicePrice(valleyServicePrice) - // .build(); - // - // OrderTransactionDTO createOrderTransactionDTO = OrderTransactionDTO.builder() - // .orderBasicInfo(orderBasicInfo) - // .orderDetail(orderDetail) - // .build(); - // transactionService.doCreateOrder(createOrderTransactionDTO); - // return orderBasicInfo; - // } - @Override public Map payOrder(PayOrderDTO dto) { logger.info("【{}】-支付订单start, param:{}", this.getClass().getSimpleName(), JSON.toJSONString(dto)); @@ -488,7 +377,41 @@ public class DelayMerchantProgramLogic extends AbstractProgramLogic { * @param orderDetail 订单详情 */ private void calculateOrderDiscounts(OrderBasicInfo orderBasicInfo, OrderDetail orderDetail) { + String memberId = orderBasicInfo.getMemberId(); // 会员id + String merchantId = orderBasicInfo.getMerchantId(); // 运营商id + String stationId = orderBasicInfo.getStationId(); // 站点id + // 电费折扣金额 + BigDecimal discountElectricityAmount = BigDecimal.ZERO; + // 服务费折扣金额 + BigDecimal discountServiceAmount = BigDecimal.ZERO; + + // 查询会员在此站点会员折扣 + MemberDiscountVO memberDiscountVO = memberGroupService.queryMemberDiscount(merchantId, stationId, memberId); + if (memberDiscountVO != null) { + BigDecimal discount = memberDiscountVO.getDiscount(); // 折扣率 + String groupType = memberDiscountVO.getGroupType(); // 类型(1-服务费折扣,2-电费折扣 ,3-电费和服务费一起折扣) + BigDecimal totalElectricityAmount = orderDetail.getTotalElectricityAmount(); // 电费 + BigDecimal totalServiceAmount = orderDetail.getTotalServiceAmount(); // 服务费 + + BigDecimal afterServiceAmountDiscount = totalServiceAmount.multiply(discount).setScale(2, RoundingMode.DOWN); + BigDecimal afterElectricityAmountDiscount = totalElectricityAmount.multiply(discount).setScale(2, RoundingMode.DOWN); + if (Constants.ONE.equals(groupType)) { + discountServiceAmount = totalServiceAmount.subtract(afterServiceAmountDiscount); + } else if (Constants.TWO.equals(groupType)) { + discountElectricityAmount = totalElectricityAmount.subtract(afterElectricityAmountDiscount); + } else { + // BigDecimal afterServiceAmountDiscount = totalServiceAmount.multiply(discount).setScale(2, RoundingMode.DOWN); + discountServiceAmount = totalServiceAmount.subtract(afterServiceAmountDiscount); + // BigDecimal afterElectricityAmountDiscount = totalElectricityAmount.multiply(discount).setScale(2, RoundingMode.DOWN); + discountElectricityAmount = totalElectricityAmount.subtract(afterElectricityAmountDiscount); + } + } + // 订单折扣金额 + BigDecimal discountAmount = discountServiceAmount.add(discountElectricityAmount); + orderBasicInfo.setDiscountAmount(discountAmount); + orderDetail.setDiscountElectricityAmount(discountElectricityAmount); + orderDetail.setDiscountServiceAmount(discountServiceAmount); } // uniApp 发送停止充电订阅消息