diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsersPayBeforController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsersPayBeforController.java index d2a61f5..4dd7c1e 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsersPayBeforController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsersPayBeforController.java @@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse; import java.util.Map; import com.alibaba.fastjson.JSONObject; +import com.ruoyi.system.ControllerUtil.BenefitPointsUtil; import com.ruoyi.system.ControllerUtil.OrderUtil; import com.ruoyi.system.ControllerUtil.RefundUtil; import com.ruoyi.system.domain.GoodsOrder; @@ -164,11 +165,11 @@ public class UsersPayBeforController extends BaseController { try { System.out.println("=== 查询预支付数据,订单ID: " + orderId + " ==="); - + GoodsOrder goodsOrder = new GoodsOrder(); goodsOrder.setMainOrderId(orderId); List orders = goodsOrderService.selectGoodsOrderList(goodsOrder); - + UsersPayBefor usersPayBefor = null; if (orders.size() > 0) { // 优先查询主订单ID对应的预支付记录 @@ -286,9 +287,9 @@ public class UsersPayBeforController extends BaseController // System.out.println(" 会员优惠: " + resultPayBefor.getMembermoney() + "元"); // System.out.println(" 实际总金额: " + resultPayBefor.getAllmoney() + "元"); // System.out.println(" 已退款: " + resultPayBefor.getReturnmoney() + "元"); - - + + } catch (Exception e) { System.err.println("❌ 查询预支付数据异常: " + e.getMessage()); e.printStackTrace(); @@ -673,10 +674,7 @@ public class UsersPayBeforController extends BaseController // 修复:计算该记录服务金的剩余可退款金额 BigDecimal serviceRemaining = record.getServicemoney().subtract(currentRefunded); if (serviceRemaining.compareTo(BigDecimal.ZERO) > 0 && remainingServiceGoldRefund.compareTo(BigDecimal.ZERO) > 0) { - record.setServicemoney(serviceRemaining); - BigDecimal serviceRefund = remainingServiceGoldRefund.min(serviceRemaining); - recordRefundAmount = recordRefundAmount.add(serviceRefund); - remainingServiceGoldRefund = remainingServiceGoldRefund.subtract(serviceRefund); + BenefitPointsUtil.theBenefitPointsServiceMoneyChangeResult(record,remainingServiceGoldRefund,1,"订单退款"); } } @@ -684,10 +682,8 @@ public class UsersPayBeforController extends BaseController // 修复:计算该记录优惠券的剩余可退款金额 BigDecimal couponRemaining = record.getCouponmoney().subtract(currentRefunded); if (couponRemaining.compareTo(BigDecimal.ZERO) > 0 && remainingCouponRefund.compareTo(BigDecimal.ZERO) > 0) { - record.setCouponmoney(new BigDecimal(0)); - BigDecimal couponRefundAmount = remainingCouponRefund.min(couponRemaining); - recordRefundAmount = recordRefundAmount.add(couponRefundAmount); - remainingCouponRefund = remainingCouponRefund.subtract(couponRefundAmount); + BenefitPointsUtil.theBenefitPointsGoodsMoneyChangeResult(record,remainingShoppingGoldRefund,1,"订单退款"); + } } @@ -732,17 +728,17 @@ public class UsersPayBeforController extends BaseController System.out.println(" balanceRefund值: " + balanceRefund); System.out.println(" balanceRefund比较0: " + balanceRefund.compareTo(BigDecimal.ZERO)); - Map refundResult = refundUtil.processUnifiedRefund( - orderId, wechatRefund, balanceRefund, shoppingGoldRefund, - serviceGoldRefund, memberDiscountRefund, couponRefund, refundRemark - ); - - if (refundResult != null && (Boolean) refundResult.get("success")) { - return success("退款成功"); - } else { - String errorMsg = (String) refundResult.get("message"); - return error(errorMsg != null ? errorMsg : "退款处理失败"); - } +// Map refundResult = refundUtil.processUnifiedRefund( +// orderId, wechatRefund, balanceRefund, shoppingGoldRefund, +// serviceGoldRefund, memberDiscountRefund, couponRefund, refundRemark +// ); + return success("退款成功"); +// if (refundResult != null && (Boolean) refundResult.get("success")) { +// return success("退款成功"); +// } else { +// String errorMsg = (String) refundResult.get("message"); +// return error(errorMsg != null ? errorMsg : "退款处理失败"); +// } } catch (Exception e) { return error("退款失败:" + e.getMessage()); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/BenefitPointsUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/BenefitPointsUtil.java index 9db07e4..125392b 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/BenefitPointsUtil.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/BenefitPointsUtil.java @@ -6,6 +6,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; +import com.ruoyi.system.controller.UserMemberRechargeLogController; import com.ruoyi.system.domain.*; import com.ruoyi.system.service.*; import org.slf4j.Logger; @@ -16,17 +17,19 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.utils.spring.SpringUtils; +import static java.math.BigDecimal.*; + /** * 购物金服务金工具类 - * + * * @author ruoyi * @date 2025-01-27 */ @Component public class BenefitPointsUtil { - + private static final Logger log = LoggerFactory.getLogger(BenefitPointsUtil.class); - + // 服务注入 private static final IOrderService orderService = SpringUtils.getBean(IOrderService.class); private static final IGoodsOrderService goodsOrderService = SpringUtils.getBean(IGoodsOrderService.class); @@ -34,16 +37,19 @@ public class BenefitPointsUtil { private static final ISiteConfigService siteConfigService = SpringUtils.getBean(ISiteConfigService.class); private static final IUserBenefitPointsService userBenefitPointsService = SpringUtils.getBean(IUserBenefitPointsService.class); private static final IUserUseSecondaryCardService userUseSecondaryCardService = SpringUtils.getBean(IUserUseSecondaryCardService.class); + private static final IUsersPayBeforService usersPayBeforService = SpringUtils.getBean(IUsersPayBeforService.class); + private static final IUserMemnerConsumptionLogService userMemnerConsumptionLogService = SpringUtils.getBean(IUserMemnerConsumptionLogService.class); + private static final IPayMoneyLogService payMoneyLogService = SpringUtils.getBean(IPayMoneyLogService.class); + - // 配置常量 private static final String CONFIG_NAME = "config_one"; private static final String CONSUMPTION_KEY = "consumption"; private static final String SERVICEFEE_KEY = "servicefee"; private static final String CONSUMPTION_DEDUCTION_KEY = "consumption_deduction"; private static final String SERVICE_DEDUCTION_KEY = "service_deduction"; - + // 类型常量 private static final Long TYPE_SERVICE_FEE = 1L; // 服务金 private static final Long TYPE_CONSUMPTION = 2L; // 消费金 @@ -56,7 +62,7 @@ public class BenefitPointsUtil { /** * 处理购物金服务金转换 - * + * * @param orderId 订单ID * @param money 金额 * @return 处理结果 @@ -147,60 +153,63 @@ public class BenefitPointsUtil { } return null; } - + + + + /** * 处理服务订单 - 增加购物金 */ private static BenefitPointsResult processServiceOrder(Order order, Users user, BigDecimal money, Map config) { try { log.info("【服务订单处理】订单ID: {}, 用户ID: {}, 金额: {}", order.getId(), user.getId(), money); - + // 获取服务金转换为消费金的比例 BigDecimal servicefeeRatio = getConfigValue(config, SERVICEFEE_KEY, new BigDecimal("0")); - if (servicefeeRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (servicefeeRatio.compareTo(ZERO) <= 0) { log.warn("【警告】服务金转换比例未配置或为0"); return new BenefitPointsResult(false, "服务金转换比例未配置", null); } - + // 计算增加的消费金金额 BigDecimal consumptionIncrease = money.multiply(servicefeeRatio).divide(new BigDecimal("100")); - + // 更新用户消费金余额 - BigDecimal beforeConsumption = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; + BigDecimal beforeConsumption = user.getConsumption() != null ? user.getConsumption() : ZERO; BigDecimal afterConsumption = beforeConsumption.add(consumptionIncrease); user.setConsumption(afterConsumption); - + // 更新用户信息 int updateResult = usersService.updateUsers(user); if (updateResult <= 0) { log.error("【错误】用户消费金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户消费金更新失败", null); } - + // 记录日志 UserBenefitPoints benefitLog = createBenefitPointsLog( - order.getId(), - user.getId(), - TYPE_CONSUMPTION, - ORDER_TYPE_INCOME, - money, - consumptionIncrease, - beforeConsumption, - afterConsumption, + order.getId(), + user.getId(), + TYPE_CONSUMPTION, + ORDER_TYPE_INCOME, + money, + consumptionIncrease, + beforeConsumption, + afterConsumption, "服务订单增加消费金" ); - + int insertResult = userBenefitPointsService.insertUserBenefitPoints(benefitLog); if (insertResult <= 0) { log.error("【错误】消费金日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "消费金日志记录失败", null); } - + log.info("【服务订单处理完成】用户ID: {}, 消费金增加: {}, 更新前: {}, 更新后: {}", user.getId(), consumptionIncrease, beforeConsumption, afterConsumption); - + return new BenefitPointsResult(true, "服务订单消费金处理成功", benefitLog); - + } catch (Exception e) { log.error("【错误】服务订单处理异常,订单ID: {}", order.getId(), e); return new BenefitPointsResult(false, "服务订单处理异常: " + e.getMessage(), null); @@ -215,7 +224,7 @@ public class BenefitPointsUtil { // 获取服务金转换为消费金的比例 BigDecimal servicefeeRatio = getConfigValue(config, SERVICEFEE_KEY, new BigDecimal("0")); - if (servicefeeRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (servicefeeRatio.compareTo(ZERO) <= 0) { log.warn("【警告】服务金转换比例未配置或为0"); return new BenefitPointsResult(false, "服务金转换比例未配置", null); } @@ -224,7 +233,7 @@ public class BenefitPointsUtil { BigDecimal consumptionIncrease = money.multiply(servicefeeRatio).divide(new BigDecimal("100")); // 更新用户消费金余额 - BigDecimal beforeConsumption = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; + BigDecimal beforeConsumption = user.getConsumption() != null ? user.getConsumption() : ZERO; BigDecimal afterConsumption = beforeConsumption.add(consumptionIncrease); user.setConsumption(afterConsumption); @@ -273,56 +282,56 @@ public class BenefitPointsUtil { user=usersService.selectUsersById(order.getUid()); // 获取消费金转换为服务金的比例 BigDecimal consumptionRatio = getConfigValue(config, CONSUMPTION_KEY, new BigDecimal("0")); - if (consumptionRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (consumptionRatio.compareTo(ZERO) <= 0) { log.warn("【警告】消费金转换比例未配置或为0"); return new BenefitPointsResult(false, "消费金转换比例未配置", null); } - + // 计算增加的服务金金额 BigDecimal servicefeeIncrease = money.multiply(consumptionRatio).divide(new BigDecimal("100")); - + // 更新用户服务金余额 - BigDecimal beforeServicefee = user.getServicefee() != null ? user.getServicefee() : BigDecimal.ZERO; + BigDecimal beforeServicefee = user.getServicefee() != null ? user.getServicefee() : ZERO; BigDecimal afterServicefee = beforeServicefee.add(servicefeeIncrease); user.setServicefee(afterServicefee); - + // 更新用户信息 int updateResult = usersService.updateUsers(user); if (updateResult <= 0) { log.error("【错误】用户服务金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户服务金更新失败", null); } - + // 记录日志 UserBenefitPoints benefitLog = createBenefitPointsLog( - order.getId(), - user.getId(), - TYPE_SERVICE_FEE, - ORDER_TYPE_INCOME, - money, - servicefeeIncrease, - beforeServicefee, - afterServicefee, + order.getId(), + user.getId(), + TYPE_SERVICE_FEE, + ORDER_TYPE_INCOME, + money, + servicefeeIncrease, + beforeServicefee, + afterServicefee, "商品订单增加服务金" ); - + int insertResult = userBenefitPointsService.insertUserBenefitPoints(benefitLog); if (insertResult <= 0) { log.error("【错误】服务金日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "服务金日志记录失败", null); } - + log.info("【商品订单处理完成】用户ID: {}, 服务金增加: {}, 更新前: {}, 更新后: {}", user.getId(), servicefeeIncrease, beforeServicefee, afterServicefee); - + return new BenefitPointsResult(true, "商品订单服务金处理成功", benefitLog); - + } catch (Exception e) { log.error("【错误】商品订单处理异常,订单ID: {}", order.getId(), e); return new BenefitPointsResult(false, "商品订单处理异常: " + e.getMessage(), null); } } - + /** * 获取系统配置 */ @@ -333,25 +342,25 @@ public class BenefitPointsUtil { log.error("【错误】系统配置为空,配置名称: {}", CONFIG_NAME); return null; } - + JSONObject configJson = JSON.parseObject(siteConfig.getValue()); Map config = new HashMap<>(); - + // 解析配置项 config.put(CONSUMPTION_KEY, configJson.getBigDecimal(CONSUMPTION_KEY)); config.put(SERVICEFEE_KEY, configJson.getBigDecimal(SERVICEFEE_KEY)); config.put(CONSUMPTION_DEDUCTION_KEY, configJson.getBigDecimal(CONSUMPTION_DEDUCTION_KEY)); config.put(SERVICE_DEDUCTION_KEY, configJson.getBigDecimal(SERVICE_DEDUCTION_KEY)); - + log.info("【配置获取】系统配置解析完成: {}", config); return config; - + } catch (Exception e) { log.error("【错误】系统配置解析失败", e); return null; } } - + /** * 获取配置值 */ @@ -371,21 +380,21 @@ public class BenefitPointsUtil { } return defaultValue; } - + /** * 创建福利金日志记录 */ private static UserBenefitPoints createBenefitPointsLog( - Long orderId, - Long userId, - Long type, - Long orderType, - BigDecimal orderMoney, - BigDecimal money, - BigDecimal beforeMoney, - BigDecimal afterMoney, + Long orderId, + Long userId, + Long type, + Long orderType, + BigDecimal orderMoney, + BigDecimal money, + BigDecimal beforeMoney, + BigDecimal afterMoney, String remark) { - + UserBenefitPoints benefitLog = new UserBenefitPoints(); benefitLog.setOrderid(orderId); benefitLog.setUid(userId); @@ -397,14 +406,14 @@ public class BenefitPointsUtil { benefitLog.setAftremoney(afterMoney); benefitLog.setReamk(remark); benefitLog.setDotime(new Date()); - + return benefitLog; } - + /** * 获取服务金消费金抵扣金额 * 用于PayBeforeUtil.createPayBefore方法中的抵扣换算 - * + * * @param user 用户信息 * @param amount 订单金额 * @param serviceType 服务类型:1=服务金抵扣,2=消费金抵扣 @@ -413,66 +422,66 @@ public class BenefitPointsUtil { public static BenefitDeductionResult getBenefitDeduction(Users user, BigDecimal amount, Long serviceType) { try { log.info("【获取抵扣金额】用户ID: {}, 订单金额: {}, 服务类型: {}", user.getId(), amount, serviceType); - + // 获取系统配置 Map config = getSystemConfig(); if (config == null) { log.error("【错误】系统配置获取失败"); - return new BenefitDeductionResult(false, "系统配置获取失败", BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); + return new BenefitDeductionResult(false, "系统配置获取失败", ZERO, ZERO, ZERO); } - - BigDecimal serviceMoney = BigDecimal.ZERO; - BigDecimal shopMoney = BigDecimal.ZERO; + + BigDecimal serviceMoney = ZERO; + BigDecimal shopMoney = ZERO; BigDecimal finalAmount = amount; - + if (serviceType != null && serviceType == 1) { // 服务金抵扣 BigDecimal serviceDeductionRatio = getConfigValue(config, SERVICE_DEDUCTION_KEY, new BigDecimal("0")); - if (serviceDeductionRatio.compareTo(BigDecimal.ZERO) > 0) { + if (serviceDeductionRatio.compareTo(ZERO) > 0) { // 获取用户最新信息 Users userDb = usersService.selectUsersById(user.getId()); - if (userDb != null && userDb.getServicefee() != null && userDb.getServicefee().compareTo(BigDecimal.ZERO) > 0) { - BigDecimal serviceRate = serviceDeductionRatio.divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP); + if (userDb != null && userDb.getServicefee() != null && userDb.getServicefee().compareTo(ZERO) > 0) { + BigDecimal serviceRate = serviceDeductionRatio.divide(new BigDecimal("100"), 4, ROUND_HALF_UP); serviceMoney = userDb.getServicefee().multiply(serviceRate); - log.info("【服务金抵扣】用户ID: {}, 服务金余额: {}, 抵扣比例: {}, 抵扣金额: {}", + log.info("【服务金抵扣】用户ID: {}, 服务金余额: {}, 抵扣比例: {}, 抵扣金额: {}", user.getId(), userDb.getServicefee(), serviceDeductionRatio, serviceMoney); } } } else if (serviceType != null && serviceType == 2) { // 消费金抵扣 BigDecimal consumptionDeductionRatio = getConfigValue(config, CONSUMPTION_DEDUCTION_KEY, new BigDecimal("0")); - if (consumptionDeductionRatio.compareTo(BigDecimal.ZERO) > 0) { + if (consumptionDeductionRatio.compareTo(ZERO) > 0) { // 获取用户最新信息 Users userDb = usersService.selectUsersById(user.getId()); - if (userDb != null && userDb.getConsumption() != null && userDb.getConsumption().compareTo(BigDecimal.ZERO) > 0) { - BigDecimal consumptionRate = consumptionDeductionRatio.divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP); + if (userDb != null && userDb.getConsumption() != null && userDb.getConsumption().compareTo(ZERO) > 0) { + BigDecimal consumptionRate = consumptionDeductionRatio.divide(new BigDecimal("100"), 4, ROUND_HALF_UP); shopMoney = userDb.getConsumption().multiply(consumptionRate); - log.info("【消费金抵扣】用户ID: {}, 消费金余额: {}, 抵扣比例: {}, 抵扣金额: {}", + log.info("【消费金抵扣】用户ID: {}, 消费金余额: {}, 抵扣比例: {}, 抵扣金额: {}", user.getId(), userDb.getConsumption(), consumptionDeductionRatio, shopMoney); } } } - + // 计算最终支付金额 finalAmount = amount.subtract(serviceMoney).subtract(shopMoney); - if (finalAmount.compareTo(BigDecimal.ZERO) < 0) { - finalAmount = BigDecimal.ZERO; + if (finalAmount.compareTo(ZERO) < 0) { + finalAmount = ZERO; } - - log.info("【抵扣计算完成】用户ID: {}, 原金额: {}, 服务金抵扣: {}, 消费金抵扣: {}, 最终金额: {}", + + log.info("【抵扣计算完成】用户ID: {}, 原金额: {}, 服务金抵扣: {}, 消费金抵扣: {}, 最终金额: {}", user.getId(), amount, serviceMoney, shopMoney, finalAmount); - + return new BenefitDeductionResult(true, "抵扣计算成功", serviceMoney, shopMoney, finalAmount); - + } catch (Exception e) { log.error("【错误】获取抵扣金额异常,用户ID: {}, 金额: {}, 服务类型: {}", user.getId(), amount, serviceType, e); - return new BenefitDeductionResult(false, "抵扣计算异常: " + e.getMessage(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO); + return new BenefitDeductionResult(false, "抵扣计算异常: " + e.getMessage(), ZERO, ZERO, ZERO); } } - + /** * 增加购物金或服务金 - * + * * @param money 金额 * @param type 类型 1=服务金 2=购物金 * @param orderId 订单号 @@ -481,106 +490,106 @@ public class BenefitPointsUtil { public static BenefitPointsResult increaseBenefitPoints(BigDecimal money, Long type, Long orderId) { try { log.info("【增加福利金】金额: {}, 类型: {}, 订单ID: {}", money, type, orderId); - + // 获取订单信息 Order order = orderService.selectOrderById(orderId); if (order == null) { log.error("【错误】订单不存在,订单ID: {}", orderId); return new BenefitPointsResult(false, "订单不存在", null); } - + // 获取用户信息 Users user = usersService.selectUsersById(order.getUid()); if (user == null) { log.error("【错误】用户不存在,用户ID: {}", order.getUid()); return new BenefitPointsResult(false, "用户不存在", null); } - + // 获取系统配置 Map config = getSystemConfig(); if (config == null) { log.error("【错误】系统配置获取失败"); return new BenefitPointsResult(false, "系统配置获取失败", null); } - - BigDecimal increaseAmount = BigDecimal.ZERO; - BigDecimal beforeAmount = BigDecimal.ZERO; - BigDecimal afterAmount = BigDecimal.ZERO; + + BigDecimal increaseAmount = ZERO; + BigDecimal beforeAmount = ZERO; + BigDecimal afterAmount = ZERO; String remark = ""; - + if (type == 1) { // 增加服务金 BigDecimal servicefeeRatio = getConfigValue(config, SERVICEFEE_KEY, new BigDecimal("0")); - if (servicefeeRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (servicefeeRatio.compareTo(ZERO) <= 0) { log.warn("【警告】服务金转换比例未配置或为0"); return new BenefitPointsResult(false, "服务金转换比例未配置", null); } - + increaseAmount = money.multiply(servicefeeRatio).divide(new BigDecimal("100")); - beforeAmount = user.getServicefee() != null ? user.getServicefee() : BigDecimal.ZERO; + beforeAmount = user.getServicefee() != null ? user.getServicefee() : ZERO; afterAmount = beforeAmount.add(increaseAmount); user.setServicefee(afterAmount); remark = "增加服务金"; - + } else if (type == 2) { // 增加购物金 BigDecimal consumptionRatio = getConfigValue(config, CONSUMPTION_KEY, new BigDecimal("0")); - if (consumptionRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (consumptionRatio.compareTo(ZERO) <= 0) { log.warn("【警告】购物金转换比例未配置或为0"); return new BenefitPointsResult(false, "购物金转换比例未配置", null); } - + increaseAmount = money.multiply(consumptionRatio).divide(new BigDecimal("100")); - beforeAmount = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; + beforeAmount = user.getConsumption() != null ? user.getConsumption() : ZERO; afterAmount = beforeAmount.add(increaseAmount); user.setConsumption(afterAmount); remark = "增加购物金"; - + } else { log.error("【错误】未知类型: {}", type); return new BenefitPointsResult(false, "未知类型", null); } - + // 更新用户信息 int updateResult = usersService.updateUsers(user); if (updateResult <= 0) { log.error("【错误】用户福利金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户福利金更新失败", null); } - + // 记录日志 UserBenefitPoints benefitLog = createBenefitPointsLog( - orderId, - user.getId(), - type, - ORDER_TYPE_INCOME, - money, - increaseAmount, - beforeAmount, - afterAmount, + orderId, + user.getId(), + type, + ORDER_TYPE_INCOME, + money, + increaseAmount, + beforeAmount, + afterAmount, remark ); - + int insertResult = userBenefitPointsService.insertUserBenefitPoints(benefitLog); if (insertResult <= 0) { log.error("【错误】福利金日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "福利金日志记录失败", null); } - + log.info("【福利金增加完成】用户ID: {}, 类型: {}, 增加金额: {}, 更新前: {}, 更新后: {}", user.getId(), type, increaseAmount, beforeAmount, afterAmount); - + return new BenefitPointsResult(true, "福利金增加成功", benefitLog); - + } catch (Exception e) { log.error("【错误】增加福利金异常,金额: {}, 类型: {}, 订单ID: {}", money, type, orderId, e); return new BenefitPointsResult(false, "增加福利金异常: " + e.getMessage(), null); } } - + /** * 抵扣购物金或服务金 - * + * * @param money 金额 * @param type 类型 1=服务金 2=购物金 * @param orderId 订单号 @@ -589,123 +598,123 @@ public class BenefitPointsUtil { public static BenefitPointsResult deductBenefitPoints(BigDecimal money, Long type, Long orderId) { try { log.info("【抵扣福利金】金额: {}, 类型: {}, 订单ID: {}", money, type, orderId); - + // 获取订单信息 Order order = orderService.selectOrderById(orderId); if (order == null) { log.error("【错误】订单不存在,订单ID: {}", orderId); return new BenefitPointsResult(false, "订单不存在", null); } - + // 获取用户信息 Users user = usersService.selectUsersById(order.getUid()); if (user == null) { log.error("【错误】用户不存在,用户ID: {}", order.getUid()); return new BenefitPointsResult(false, "用户不存在", null); } - + // 获取系统配置 Map config = getSystemConfig(); if (config == null) { log.error("【错误】系统配置获取失败"); return new BenefitPointsResult(false, "系统配置获取失败", null); } - - BigDecimal deductAmount = BigDecimal.ZERO; - BigDecimal beforeAmount = BigDecimal.ZERO; - BigDecimal afterAmount = BigDecimal.ZERO; + + BigDecimal deductAmount = ZERO; + BigDecimal beforeAmount = ZERO; + BigDecimal afterAmount = ZERO; String remark = ""; - + if (type == 1) { // 抵扣服务金 BigDecimal serviceDeductionRatio = getConfigValue(config, SERVICE_DEDUCTION_KEY, new BigDecimal("0")); - if (serviceDeductionRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (serviceDeductionRatio.compareTo(ZERO) <= 0) { log.warn("【警告】服务金抵扣比例未配置或为0"); return new BenefitPointsResult(false, "服务金抵扣比例未配置", null); } - - beforeAmount = user.getServicefee() != null ? user.getServicefee() : BigDecimal.ZERO; - BigDecimal serviceRate = serviceDeductionRatio.divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP); + + beforeAmount = user.getServicefee() != null ? user.getServicefee() : ZERO; + BigDecimal serviceRate = serviceDeductionRatio.divide(new BigDecimal("100"), 4, ROUND_HALF_UP); deductAmount = beforeAmount.multiply(serviceRate); - - if (deductAmount.compareTo(BigDecimal.ZERO) > 0) { + + if (deductAmount.compareTo(ZERO) > 0) { afterAmount = beforeAmount.subtract(deductAmount); - if (afterAmount.compareTo(BigDecimal.ZERO) < 0) { - afterAmount = BigDecimal.ZERO; + if (afterAmount.compareTo(ZERO) < 0) { + afterAmount = ZERO; } user.setServicefee(afterAmount); remark = "抵扣服务金"; } - + } else if (type == 2) { // 抵扣购物金 BigDecimal consumptionDeductionRatio = getConfigValue(config, CONSUMPTION_DEDUCTION_KEY, new BigDecimal("0")); - if (consumptionDeductionRatio.compareTo(BigDecimal.ZERO) <= 0) { + if (consumptionDeductionRatio.compareTo(ZERO) <= 0) { log.warn("【警告】购物金抵扣比例未配置或为0"); return new BenefitPointsResult(false, "购物金抵扣比例未配置", null); } - - beforeAmount = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; - BigDecimal consumptionRate = consumptionDeductionRatio.divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP); + + beforeAmount = user.getConsumption() != null ? user.getConsumption() : ZERO; + BigDecimal consumptionRate = consumptionDeductionRatio.divide(new BigDecimal("100"), 4, ROUND_HALF_UP); deductAmount = beforeAmount.multiply(consumptionRate); - - if (deductAmount.compareTo(BigDecimal.ZERO) > 0) { + + if (deductAmount.compareTo(ZERO) > 0) { afterAmount = beforeAmount.subtract(deductAmount); - if (afterAmount.compareTo(BigDecimal.ZERO) < 0) { - afterAmount = BigDecimal.ZERO; + if (afterAmount.compareTo(ZERO) < 0) { + afterAmount = ZERO; } user.setConsumption(afterAmount); remark = "抵扣购物金"; } - + } else { log.error("【错误】未知类型: {}", type); return new BenefitPointsResult(false, "未知类型", null); } - + // 如果没有抵扣金额,直接返回成功 - if (deductAmount.compareTo(BigDecimal.ZERO) <= 0) { + if (deductAmount.compareTo(ZERO) <= 0) { log.info("【抵扣完成】用户ID: {}, 类型: {}, 无抵扣金额", user.getId(), type); return new BenefitPointsResult(true, "无抵扣金额", null); } - + // 更新用户信息 int updateResult = usersService.updateUsers(user); if (updateResult <= 0) { log.error("【错误】用户福利金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户福利金更新失败", null); } - + // 记录日志 UserBenefitPoints benefitLog = createBenefitPointsLog( - orderId, - user.getId(), - type, - ORDER_TYPE_EXPENSE, - money, - deductAmount, - beforeAmount, - afterAmount, + orderId, + user.getId(), + type, + ORDER_TYPE_EXPENSE, + money, + deductAmount, + beforeAmount, + afterAmount, remark ); - + int insertResult = userBenefitPointsService.insertUserBenefitPoints(benefitLog); if (insertResult <= 0) { log.error("【错误】福利金日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "福利金日志记录失败", null); } - + log.info("【福利金抵扣完成】用户ID: {}, 类型: {}, 抵扣金额: {}, 更新前: {}, 更新后: {}", user.getId(), type, deductAmount, beforeAmount, afterAmount); - + return new BenefitPointsResult(true, "福利金抵扣成功", benefitLog); - + } catch (Exception e) { log.error("【错误】抵扣福利金异常,金额: {}, 类型: {}, 订单ID: {}", money, type, orderId, e); return new BenefitPointsResult(false, "抵扣福利金异常: " + e.getMessage(), null); } } - + /** * 扣减服务金和消费金 * 扣减逻辑: @@ -722,159 +731,159 @@ public class BenefitPointsUtil { */ public static BenefitPointsResult deductServiceAndConsumption(Long orderId, Users user, BigDecimal usedServiceAmount, BigDecimal usedConsumptionAmount) { try { - log.info("【扣减服务金和消费金】订单ID: {}, 用户ID: {}, 使用服务金抵扣: {}, 使用消费金抵扣: {}", + log.info("【扣减服务金和消费金】订单ID: {}, 用户ID: {}, 使用服务金抵扣: {}, 使用消费金抵扣: {}", orderId, user.getId(), usedServiceAmount, usedConsumptionAmount); - + // 获取系统配置 Map config = getSystemConfig(); if (config == null) { log.error("【错误】获取系统配置失败"); return new BenefitPointsResult(false, "获取系统配置失败", null); } - + // 获取服务金扣减比例 BigDecimal serviceDeductionRatio = getConfigValue(config, SERVICE_DEDUCTION_KEY, new BigDecimal("100")); log.info("【服务金扣减比例】用户ID: {}, 服务金扣减比例: {}%", user.getId(), serviceDeductionRatio); - + // 获取消费金扣减比例 BigDecimal consumptionDeductionRatio = getConfigValue(config, CONSUMPTION_DEDUCTION_KEY, new BigDecimal("100")); log.info("【消费金扣减比例】用户ID: {}, 消费金扣减比例: {}%", user.getId(), consumptionDeductionRatio); - + // 获取用户最新信息 Users userDb = usersService.selectUsersById(user.getId()); if (userDb == null) { log.error("【错误】获取用户最新信息失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "获取用户最新信息失败", null); } - - BigDecimal beforeServicefee = userDb.getServicefee() != null ? userDb.getServicefee() : BigDecimal.ZERO; - BigDecimal beforeConsumption = userDb.getConsumption() != null ? userDb.getConsumption() : BigDecimal.ZERO; - - log.info("【扣减前余额】用户ID: {}, 服务金余额: {}, 消费金余额: {}", + + BigDecimal beforeServicefee = userDb.getServicefee() != null ? userDb.getServicefee() : ZERO; + BigDecimal beforeConsumption = userDb.getConsumption() != null ? userDb.getConsumption() : ZERO; + + log.info("【扣减前余额】用户ID: {}, 服务金余额: {}, 消费金余额: {}", user.getId(), beforeServicefee, beforeConsumption); - + BigDecimal afterServicefee = beforeServicefee; BigDecimal afterConsumption = beforeConsumption; - + // 服务金抵扣时,扣减金额 = usedServiceAmount / (service_deduction 百分比) - if (usedServiceAmount != null && usedServiceAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedServiceAmount != null && usedServiceAmount.compareTo(ZERO) > 0) { // 计算实际扣减金额:usedServiceAmount / (service_deduction 百分比) // 例如:service_deduction=80,usedServiceAmount=100,则实际扣减=100/80=1.25倍 BigDecimal actualServiceDeduction = usedServiceAmount.divide(serviceDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - log.info("【服务金扣减计算】用户ID: {}, 服务金抵扣金额: {}, 服务金扣减比例: {}%, 实际扣减金额: {}", + log.info("【服务金扣减计算】用户ID: {}, 服务金抵扣金额: {}, 服务金扣减比例: {}%, 实际扣减金额: {}", user.getId(), usedServiceAmount, serviceDeductionRatio, actualServiceDeduction); - + if (beforeServicefee.compareTo(actualServiceDeduction) >= 0) { // 余额充足,正常扣减 afterServicefee = beforeServicefee.subtract(actualServiceDeduction); userDb.setServicefee(afterServicefee); - log.info("【服务金扣减成功】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", + log.info("【服务金扣减成功】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", user.getId(), beforeServicefee, actualServiceDeduction, afterServicefee); } else { // 余额不足,扣完所有余额 - log.warn("【警告】服务金余额不足,用户ID: {}, 余额: {}, 需要扣减: {}, 将扣完所有余额", + log.warn("【警告】服务金余额不足,用户ID: {}, 余额: {}, 需要扣减: {}, 将扣完所有余额", user.getId(), beforeServicefee, actualServiceDeduction); - afterServicefee = BigDecimal.ZERO; + afterServicefee = ZERO; userDb.setServicefee(afterServicefee); - log.info("【服务金扣减完成】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", + log.info("【服务金扣减完成】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", user.getId(), beforeServicefee, beforeServicefee, afterServicefee); } } - + // 消费金抵扣时,扣减金额 = usedConsumptionAmount / (consumption_deduction 百分比) - if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(ZERO) > 0) { // 计算实际扣减金额:usedConsumptionAmount / (consumption_deduction 百分比) // 例如:consumption_deduction=80,usedConsumptionAmount=100,则实际扣减=100/80=1.25倍 BigDecimal actualConsumptionDeduction = usedConsumptionAmount.divide(consumptionDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - log.info("【消费金扣减计算】用户ID: {}, 消费金抵扣金额: {}, 消费金扣减比例: {}%, 实际扣减金额: {}", + log.info("【消费金扣减计算】用户ID: {}, 消费金抵扣金额: {}, 消费金扣减比例: {}%, 实际扣减金额: {}", user.getId(), usedConsumptionAmount, consumptionDeductionRatio, actualConsumptionDeduction); - + if (beforeConsumption.compareTo(actualConsumptionDeduction) >= 0) { // 余额充足,正常扣减 afterConsumption = beforeConsumption.subtract(actualConsumptionDeduction); userDb.setConsumption(afterConsumption); - log.info("【消费金扣减成功】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", + log.info("【消费金扣减成功】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", user.getId(), beforeConsumption, actualConsumptionDeduction, afterConsumption); } else { // 余额不足,扣完所有余额 - log.warn("【警告】消费金余额不足,用户ID: {}, 余额: {}, 需要扣减: {}, 将扣完所有余额", + log.warn("【警告】消费金余额不足,用户ID: {}, 余额: {}, 需要扣减: {}, 将扣完所有余额", user.getId(), beforeConsumption, actualConsumptionDeduction); - afterConsumption = BigDecimal.ZERO; + afterConsumption = ZERO; userDb.setConsumption(afterConsumption); - log.info("【消费金扣减完成】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", + log.info("【消费金扣减完成】用户ID: {}, 扣减前: {}, 实际扣减金额: {}, 扣减后: {}", user.getId(), beforeConsumption, beforeConsumption, afterConsumption); } } - + // 更新用户信息 int updateResult = usersService.updateUsers(userDb); if (updateResult <= 0) { log.error("【错误】用户福利金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户福利金更新失败", null); } - + // 记录服务金扣减日志 UserBenefitPoints serviceLog = null; - if (usedServiceAmount != null && usedServiceAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedServiceAmount != null && usedServiceAmount.compareTo(ZERO) > 0) { // 计算实际扣减金额用于日志记录 BigDecimal actualServiceDeduction = usedServiceAmount.divide(serviceDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); // 如果余额不足,实际扣减金额为余额 BigDecimal realServiceDeduction = beforeServicefee.compareTo(actualServiceDeduction) >= 0 ? actualServiceDeduction : beforeServicefee; - + serviceLog = createBenefitPointsLog( - orderId, - user.getId(), - TYPE_SERVICE_FEE, - ORDER_TYPE_EXPENSE, + orderId, + user.getId(), + TYPE_SERVICE_FEE, + ORDER_TYPE_EXPENSE, usedServiceAmount, // 记录服务金抵扣金额 realServiceDeduction, // 记录实际扣减的服务金金额 - beforeServicefee, - afterServicefee, + beforeServicefee, + afterServicefee, "服务金抵扣订单,扣除服务金" ); - + int serviceLogResult = userBenefitPointsService.insertUserBenefitPoints(serviceLog); if (serviceLogResult <= 0) { log.error("【错误】服务金扣减日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "服务金扣减日志记录失败", null); } } - + // 记录消费金扣减日志 UserBenefitPoints consumptionLog = null; - if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(ZERO) > 0) { // 计算实际扣减金额用于日志记录 BigDecimal actualConsumptionDeduction = usedConsumptionAmount.divide(consumptionDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); // 如果余额不足,实际扣减金额为余额 BigDecimal realConsumptionDeduction = beforeConsumption.compareTo(actualConsumptionDeduction) >= 0 ? actualConsumptionDeduction : beforeConsumption; - + consumptionLog = createBenefitPointsLog( - orderId, - user.getId(), - TYPE_CONSUMPTION, - ORDER_TYPE_EXPENSE, + orderId, + user.getId(), + TYPE_CONSUMPTION, + ORDER_TYPE_EXPENSE, usedConsumptionAmount, // 记录消费金抵扣金额 realConsumptionDeduction, // 记录实际扣减的消费金金额 - beforeConsumption, - afterConsumption, + beforeConsumption, + afterConsumption, "消费金抵扣订单,扣除消费金" ); - + int consumptionLogResult = userBenefitPointsService.insertUserBenefitPoints(consumptionLog); if (consumptionLogResult <= 0) { log.error("【错误】消费金扣减日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "消费金扣减日志记录失败", null); } } - + log.info("【扣减完成】用户ID: {}, 服务金抵扣金额: {}, 消费金抵扣金额: {}, 服务金余额: {}, 消费金余额: {}", user.getId(), usedServiceAmount, usedConsumptionAmount, afterServicefee, afterConsumption); - + // 返回服务金日志作为主要结果 return new BenefitPointsResult(true, "扣减服务金和消费金成功", serviceLog != null ? serviceLog : consumptionLog); - + } catch (Exception e) { - log.error("【错误】扣减服务金和消费金异常,订单ID: {}, 用户ID: {}, 使用服务金: {}, 使用消费金: {}", + log.error("【错误】扣减服务金和消费金异常,订单ID: {}, 用户ID: {}, 使用服务金: {}, 使用消费金: {}", orderId, user.getId(), usedServiceAmount, usedConsumptionAmount, e); return new BenefitPointsResult(false, "扣减服务金和消费金异常: " + e.getMessage(), null); } @@ -903,169 +912,169 @@ public class BenefitPointsUtil { */ public static BenefitPointsResult refundServiceAndConsumption(Long orderId, Users user, BigDecimal usedServiceAmount, BigDecimal usedConsumptionAmount) { try { - log.info("【退回服务金和消费金】订单ID: {}, 用户ID: {}, 使用服务金抵扣: {}, 使用消费金抵扣: {}", + log.info("【退回服务金和消费金】订单ID: {}, 用户ID: {}, 使用服务金抵扣: {}, 使用消费金抵扣: {}", orderId, user.getId(), usedServiceAmount, usedConsumptionAmount); - + // 获取系统配置 Map config = getSystemConfig(); if (config == null) { log.error("【错误】获取系统配置失败"); return new BenefitPointsResult(false, "获取系统配置失败", null); } - + // 获取服务金扣减比例 BigDecimal serviceDeductionRatio = getConfigValue(config, SERVICE_DEDUCTION_KEY, new BigDecimal("100")); log.info("【服务金扣减比例】用户ID: {}, 服务金扣减比例: {}%", user.getId(), serviceDeductionRatio); - + // 获取消费金扣减比例 BigDecimal consumptionDeductionRatio = getConfigValue(config, CONSUMPTION_DEDUCTION_KEY, new BigDecimal("100")); log.info("【消费金扣减比例】用户ID: {}, 消费金扣减比例: {}%", user.getId(), consumptionDeductionRatio); - + // 获取用户最新信息 Users userDb = usersService.selectUsersById(user.getId()); if (userDb == null) { log.error("【错误】获取用户最新信息失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "获取用户最新信息失败", null); } - - BigDecimal beforeServicefee = userDb.getServicefee() != null ? userDb.getServicefee() : BigDecimal.ZERO; - BigDecimal beforeConsumption = userDb.getConsumption() != null ? userDb.getConsumption() : BigDecimal.ZERO; - - log.info("【退回前余额】用户ID: {}, 服务金余额: {}, 消费金余额: {}", + + BigDecimal beforeServicefee = userDb.getServicefee() != null ? userDb.getServicefee() : ZERO; + BigDecimal beforeConsumption = userDb.getConsumption() != null ? userDb.getConsumption() : ZERO; + + log.info("【退回前余额】用户ID: {}, 服务金余额: {}, 消费金余额: {}", user.getId(), beforeServicefee, beforeConsumption); - + BigDecimal afterServicefee = beforeServicefee; BigDecimal afterConsumption = beforeConsumption; - + // 服务金退回时,退回金额 = usedServiceAmount / (service_deduction 百分比) - if (usedServiceAmount != null && usedServiceAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedServiceAmount != null && usedServiceAmount.compareTo(ZERO) > 0) { // 计算实际退回金额:usedServiceAmount / (service_deduction 百分比) // 例如:service_deduction=80,usedServiceAmount=100,则实际退回=100/80=1.25倍 BigDecimal actualServiceRefund = usedServiceAmount.divide(serviceDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - log.info("【服务金退回计算】用户ID: {}, 服务金抵扣金额: {}, 服务金扣减比例: {}%, 实际退回金额: {}", + log.info("【服务金退回计算】用户ID: {}, 服务金抵扣金额: {}, 服务金扣减比例: {}%, 实际退回金额: {}", user.getId(), usedServiceAmount, serviceDeductionRatio, actualServiceRefund); - + // 退回服务金 afterServicefee = beforeServicefee.add(actualServiceRefund); userDb.setServicefee(afterServicefee); - log.info("【服务金退回成功】用户ID: {}, 退回前: {}, 实际退回金额: {}, 退回后: {}", + log.info("【服务金退回成功】用户ID: {}, 退回前: {}, 实际退回金额: {}, 退回后: {}", user.getId(), beforeServicefee, actualServiceRefund, afterServicefee); } - + // 消费金退回时,退回金额 = usedConsumptionAmount / (consumption_deduction 百分比) - if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(ZERO) > 0) { // 计算实际退回金额:usedConsumptionAmount / (consumption_deduction 百分比) // 例如:consumption_deduction=80,usedConsumptionAmount=100,则实际退回=100/80=1.25倍 BigDecimal actualConsumptionRefund = usedConsumptionAmount.divide(consumptionDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - log.info("【消费金退回计算】用户ID: {}, 消费金抵扣金额: {}, 消费金扣减比例: {}%, 实际退回金额: {}", + log.info("【消费金退回计算】用户ID: {}, 消费金抵扣金额: {}, 消费金扣减比例: {}%, 实际退回金额: {}", user.getId(), usedConsumptionAmount, consumptionDeductionRatio, actualConsumptionRefund); - + // 退回消费金 afterConsumption = beforeConsumption.add(actualConsumptionRefund); userDb.setConsumption(afterConsumption); - log.info("【消费金退回成功】用户ID: {}, 退回前: {}, 实际退回金额: {}, 退回后: {}", + log.info("【消费金退回成功】用户ID: {}, 退回前: {}, 实际退回金额: {}, 退回后: {}", user.getId(), beforeConsumption, actualConsumptionRefund, afterConsumption); } - + // 更新用户信息 int updateResult = usersService.updateUsers(userDb); if (updateResult <= 0) { log.error("【错误】用户福利金更新失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "用户福利金更新失败", null); } - + // 记录服务金退回日志 UserBenefitPoints serviceLog = null; - if (usedServiceAmount != null && usedServiceAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedServiceAmount != null && usedServiceAmount.compareTo(ZERO) > 0) { // 计算实际退回金额用于日志记录 BigDecimal actualServiceRefund = usedServiceAmount.divide(serviceDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - + serviceLog = createBenefitPointsLog( - orderId, - user.getId(), - TYPE_SERVICE_FEE, + orderId, + user.getId(), + TYPE_SERVICE_FEE, ORDER_TYPE_INCOME, // 退回为收入类型 usedServiceAmount, // 记录服务金抵扣金额 actualServiceRefund, // 记录实际退回的服务金金额 - beforeServicefee, - afterServicefee, + beforeServicefee, + afterServicefee, "订单取消,退回服务金" ); - + int serviceLogResult = userBenefitPointsService.insertUserBenefitPoints(serviceLog); if (serviceLogResult <= 0) { log.error("【错误】服务金退回日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "服务金退回日志记录失败", null); } } - + // 记录消费金退回日志 UserBenefitPoints consumptionLog = null; - if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(BigDecimal.ZERO) > 0) { + if (usedConsumptionAmount != null && usedConsumptionAmount.compareTo(ZERO) > 0) { // 计算实际退回金额用于日志记录 BigDecimal actualConsumptionRefund = usedConsumptionAmount.divide(consumptionDeductionRatio, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")); - + consumptionLog = createBenefitPointsLog( - orderId, - user.getId(), - TYPE_CONSUMPTION, + orderId, + user.getId(), + TYPE_CONSUMPTION, ORDER_TYPE_INCOME, // 退回为收入类型 usedConsumptionAmount, // 记录消费金抵扣金额 actualConsumptionRefund, // 记录实际退回的消费金金额 - beforeConsumption, - afterConsumption, + beforeConsumption, + afterConsumption, "订单取消,退回消费金" ); - + int consumptionLogResult = userBenefitPointsService.insertUserBenefitPoints(consumptionLog); if (consumptionLogResult <= 0) { log.error("【错误】消费金退回日志记录失败,用户ID: {}", user.getId()); return new BenefitPointsResult(false, "消费金退回日志记录失败", null); } } - + log.info("【退回完成】用户ID: {}, 服务金抵扣金额: {}, 消费金抵扣金额: {}, 服务金余额: {}, 消费金余额: {}", user.getId(), usedServiceAmount, usedConsumptionAmount, afterServicefee, afterConsumption); - + // 返回服务金日志作为主要结果 return new BenefitPointsResult(true, "退回服务金和消费金成功", serviceLog != null ? serviceLog : consumptionLog); - + } catch (Exception e) { - log.error("【错误】退回服务金和消费金异常,订单ID: {}, 用户ID: {}, 使用服务金: {}, 使用消费金: {}", + log.error("【错误】退回服务金和消费金异常,订单ID: {}, 用户ID: {}, 使用服务金: {}, 使用消费金: {}", orderId, user.getId(), usedServiceAmount, usedConsumptionAmount, e); return new BenefitPointsResult(false, "退回服务金和消费金异常: " + e.getMessage(), null); } } - + /** * 获取用户服务金消费金余额 - * + * * @param userId 用户ID * @return 余额信息 */ public static BenefitBalanceResult getBenefitBalance(Long userId) { try { log.info("【获取余额信息】用户ID: {}", userId); - + Users user = usersService.selectUsersById(userId); if (user == null) { log.error("【错误】用户不存在,用户ID: {}", userId); - return new BenefitBalanceResult(false, "用户不存在", BigDecimal.ZERO, BigDecimal.ZERO); + return new BenefitBalanceResult(false, "用户不存在", ZERO, ZERO); } - - BigDecimal servicefee = user.getServicefee() != null ? user.getServicefee() : BigDecimal.ZERO; - BigDecimal consumption = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; - + + BigDecimal servicefee = user.getServicefee() != null ? user.getServicefee() : ZERO; + BigDecimal consumption = user.getConsumption() != null ? user.getConsumption() : ZERO; + log.info("【余额信息】用户ID: {}, 服务金: {}, 消费金: {}", userId, servicefee, consumption); - + return new BenefitBalanceResult(true, "获取余额成功", servicefee, consumption); - + } catch (Exception e) { log.error("【错误】获取余额信息异常,用户ID: {}", userId, e); - return new BenefitBalanceResult(false, "获取余额异常: " + e.getMessage(), BigDecimal.ZERO, BigDecimal.ZERO); + return new BenefitBalanceResult(false, "获取余额异常: " + e.getMessage(), ZERO, ZERO); } } - + /** * 抵扣结果类 */ @@ -1075,7 +1084,7 @@ public class BenefitPointsUtil { private BigDecimal serviceMoney; // 服务金抵扣金额 private BigDecimal shopMoney; // 消费金抵扣金额 private BigDecimal finalAmount; // 最终支付金额 - + public BenefitDeductionResult(boolean success, String message, BigDecimal serviceMoney, BigDecimal shopMoney, BigDecimal finalAmount) { this.success = success; this.message = message; @@ -1083,23 +1092,23 @@ public class BenefitPointsUtil { this.shopMoney = shopMoney; this.finalAmount = finalAmount; } - + public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } - + public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } - + public BigDecimal getServiceMoney() { return serviceMoney; } public void setServiceMoney(BigDecimal serviceMoney) { this.serviceMoney = serviceMoney; } - + public BigDecimal getShopMoney() { return shopMoney; } public void setShopMoney(BigDecimal shopMoney) { this.shopMoney = shopMoney; } - + public BigDecimal getFinalAmount() { return finalAmount; } public void setFinalAmount(BigDecimal finalAmount) { this.finalAmount = finalAmount; } } - + /** * 余额结果类 */ @@ -1108,27 +1117,32 @@ public class BenefitPointsUtil { private String message; private BigDecimal servicefee; // 服务金余额 private BigDecimal consumption; // 消费金余额 - + public BenefitBalanceResult(boolean success, String message, BigDecimal servicefee, BigDecimal consumption) { this.success = success; this.message = message; this.servicefee = servicefee; this.consumption = consumption; } - + public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } - + public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } - + public BigDecimal getServicefee() { return servicefee; } public void setServicefee(BigDecimal servicefee) { this.servicefee = servicefee; } - + public BigDecimal getConsumption() { return consumption; } public void setConsumption(BigDecimal consumption) { this.consumption = consumption; } } - + + + + + + /** * 处理结果类 */ @@ -1136,20 +1150,260 @@ public class BenefitPointsUtil { private boolean success; private String message; private UserBenefitPoints log; - + public BenefitPointsResult(boolean success, String message, UserBenefitPoints log) { this.success = success; this.message = message; this.log = log; } - + public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } - + public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } - + public UserBenefitPoints getLog() { return log; } public void setLog(UserBenefitPoints log) { this.log = log; } } -} \ No newline at end of file + //------------------------------------------------------------------------------------------------------------------------------ + //服务金变更 + // private IUsersPayBeforService usersPayBeforService; + public static Map theBenefitPointsServiceMoneyChangeResult(UsersPayBefor usersPayBefor,BigDecimal servicemoney,int type,String reamk) { + //第一步获取系统维护的服务金比例 + Map remap=new HashMap<>(); + Map config = getSystemConfig(); + BigDecimal servicefeeRatio = getConfigValue(config, SERVICEFEE_KEY, new BigDecimal("0")); + BigDecimal amountService = servicemoney.multiply(servicefeeRatio).divide(new BigDecimal("100")); + Users user = usersService.selectUsersById(usersPayBefor.getUid()); + UsersPayBefor usersPayBeforData=usersPayBeforService.selectUsersPayBeforById(usersPayBefor.getId()); + if (user==null){ + remap.put("code",2); + return remap; + } + //type 1增加 0减少 + if(type==1){ + user.setServicefee(user.getServicefee().add(amountService)); + UserBenefitPoints benefitLog = new UserBenefitPoints(); + benefitLog.setOrderid(usersPayBefor.getId()); + benefitLog.setUid(usersPayBefor.getUid()); + benefitLog.setType(1L);//类别 1服务金 2消费金 + benefitLog.setOrdertype(1L); //数据类别 1收入 2支出 + benefitLog.setOrdermoney(usersPayBefor.getAllmoney()); + benefitLog.setMoney(amountService); + benefitLog.setBeformoney(user.getServicefee().subtract(amountService)); + benefitLog.setAftremoney(user.getServicefee()); + benefitLog.setReamk(reamk); + benefitLog.setDotime(new Date()); + userBenefitPointsService.insertUserBenefitPoints(benefitLog); + usersService.updateUsers(user); + //增加用户服务金金就是减少预支付的服务金 + usersPayBeforData.setServicemoney(usersPayBeforData.getServicemoney().subtract(servicemoney)); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + return remap; + + }else{ + user.setServicefee(user.getServicefee().subtract(amountService)); + UserBenefitPoints benefitLog = new UserBenefitPoints(); + benefitLog.setOrderid(usersPayBefor.getId()); + benefitLog.setUid(usersPayBefor.getUid()); + benefitLog.setType(1L);//类别 1服务金 2消费金 + benefitLog.setOrdertype(2L); //数据类别 1收入 2支出 + benefitLog.setOrdermoney(usersPayBefor.getAllmoney()); + benefitLog.setMoney(amountService); + benefitLog.setBeformoney(user.getServicefee().add(amountService)); + benefitLog.setAftremoney(user.getServicefee()); + benefitLog.setReamk(reamk); + benefitLog.setDotime(new Date()); + userBenefitPointsService.insertUserBenefitPoints(benefitLog); + usersService.updateUsers(user); + //减少用户服务金金就是增加预支付的服务金 + usersPayBeforData.setServicemoney(usersPayBeforData.getServicemoney().add(servicemoney)); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + return remap; + + } + } + + //余额变动 + public static Map theYueChangeResult(UsersPayBefor usersPayBefor,BigDecimal money,int type,String reamk) { + Map remap=new HashMap<>(); + //余额变动 + Users user = usersService.selectUsersById(usersPayBefor.getUid()); + UsersPayBefor usersPayBeforData=usersPayBeforService.selectUsersPayBeforById(usersPayBefor.getId()); + if(type==1){ + user.setBalance(user.getBalance().add(money)); + usersPayBeforData.setYemoney(usersPayBeforData.getYemoney().subtract(money)); + UserMemnerConsumptionLog userMemnerConsumptionLog=new UserMemnerConsumptionLog(); + userMemnerConsumptionLog.setUid(Math.toIntExact(user.getId())); + userMemnerConsumptionLog.setConsumptiontype(2); + userMemnerConsumptionLog.setConsumptiontime(new Date()); + userMemnerConsumptionLog.setConsumptionmoney(money); + userMemnerConsumptionLog.setReamk(reamk); + userMemnerConsumptionLog.setBeformoney(user.getBalance().subtract(money)); + userMemnerConsumptionLog.setAftermoney(user.getBalance()); + userMemnerConsumptionLog.setNowmoney(user.getBalance()); + userMemnerConsumptionLog.setType(1); + userMemnerConsumptionLog.setOrderid(usersPayBefor.getOrderid()); + userMemnerConsumptionLogService.insertUserMemnerConsumptionLog(userMemnerConsumptionLog); + usersService.updateUsers(user); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + }else{ + user.setBalance(user.getBalance().subtract(money)); + usersPayBeforData.setYemoney(usersPayBeforData.getYemoney().add(money)); + UserMemnerConsumptionLog userMemnerConsumptionLog=new UserMemnerConsumptionLog(); + userMemnerConsumptionLog.setUid(Math.toIntExact(user.getId())); + userMemnerConsumptionLog.setConsumptiontype(2); + userMemnerConsumptionLog.setConsumptiontime(new Date()); + userMemnerConsumptionLog.setConsumptionmoney(money); + userMemnerConsumptionLog.setReamk(reamk); + userMemnerConsumptionLog.setBeformoney(user.getBalance().add(money)); + userMemnerConsumptionLog.setAftermoney(user.getBalance()); + userMemnerConsumptionLog.setNowmoney(user.getBalance()); + userMemnerConsumptionLog.setType(2); + userMemnerConsumptionLog.setOrderid(usersPayBefor.getOrderid()); + userMemnerConsumptionLogService.insertUserMemnerConsumptionLog(userMemnerConsumptionLog); + usersService.updateUsers(user); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + } + + return remap; + } + + //购物金变更 + public static Map theBenefitPointsGoodsMoneyChangeResult(UsersPayBefor usersPayBefor,BigDecimal Goodsmoney,int type,String reamk) { + //第一步获取系统维护的服务金比例 + Map remap=new HashMap<>(); + Map config = getSystemConfig(); + BigDecimal goodsfeeRatio = getConfigValue(config, CONSUMPTION_KEY, new BigDecimal("0")); + BigDecimal amountService = Goodsmoney.multiply(goodsfeeRatio).divide(new BigDecimal("100")); + UsersPayBefor usersPayBeforData=usersPayBeforService.selectUsersPayBeforById(usersPayBefor.getId()); + Users user = usersService.selectUsersById(usersPayBefor.getUid()); + if (user==null){ + remap.put("code",2); + return remap; + } + //type 1增加 0减少 + if(type==1){ + user.setConsumption(user.getConsumption().add(amountService)); + UserBenefitPoints benefitLog = new UserBenefitPoints(); + benefitLog.setOrderid(usersPayBefor.getId()); + benefitLog.setUid(usersPayBefor.getUid()); + benefitLog.setType(2L);//类别 1服务金 2消费金 + benefitLog.setOrdertype(1L); //数据类别 1收入 2支出 + benefitLog.setOrdermoney(usersPayBefor.getAllmoney()); + benefitLog.setMoney(amountService); + benefitLog.setBeformoney(user.getConsumption().subtract(amountService)); + benefitLog.setAftremoney(user.getConsumption()); + benefitLog.setReamk(reamk); + benefitLog.setDotime(new Date()); + userBenefitPointsService.insertUserBenefitPoints(benefitLog); + usersService.updateUsers(user); + usersPayBeforData.setShopmoney(usersPayBeforData.getShopmoney().subtract(Goodsmoney)); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + return remap; + + }else{ + user.setConsumption(user.getConsumption().subtract(amountService)); + UserBenefitPoints benefitLog = new UserBenefitPoints(); + benefitLog.setOrderid(usersPayBefor.getId()); + benefitLog.setUid(usersPayBefor.getUid()); + benefitLog.setType(2L);//类别 1服务金 2消费金 + benefitLog.setOrdertype(2L); //数据类别 1收入 2支出 + benefitLog.setOrdermoney(usersPayBefor.getAllmoney()); + benefitLog.setMoney(amountService); + benefitLog.setBeformoney(user.getConsumption().add(amountService)); + benefitLog.setAftremoney(user.getConsumption()); + benefitLog.setReamk(reamk); + benefitLog.setDotime(new Date()); + userBenefitPointsService.insertUserBenefitPoints(benefitLog); + usersService.updateUsers(user); + usersPayBeforData.setShopmoney(usersPayBeforData.getShopmoney().add(Goodsmoney)); + usersPayBeforService.updateUsersPayBefor(usersPayBeforData); + remap.put("code",1); + return remap; + + } + } + + + //微信支付变动 + public static Map theWXmoneyChangeResult(UsersPayBefor usersPayBefor,BigDecimal money,int type,String reamk) { + Map remap=new HashMap<>(); + //余额变动 + Users user = usersService.selectUsersById(usersPayBefor.getUid()); + UsersPayBefor usersPayBeforData=usersPayBeforService.selectUsersPayBeforById(usersPayBefor.getId()); + int allmoney=usersPayBefor.getWxmoney().multiply(new BigDecimal(100)).intValue(); + int tkmoney=money.multiply(new BigDecimal(100)).intValue(); + WechatPayV3Util WW=new WechatPayV3Util(); + // 7. 调用微信退款接口 + Map refundResult = WW.refund(usersPayBeforData.getOrderid(), + usersPayBeforData.getOrderid(), + allmoney, + tkmoney, + "订单退款", + WechatPayUtil.PAY_FH + "/api/secondary/card/refund/notify" + ); + + PayMoneyLog payMoneyLog = new PayMoneyLog(); + if (usersPayBefor != null) { + payMoneyLog.setOid(usersPayBefor.getId()); + } + payMoneyLog.setOrderId(usersPayBefor.getOrderid()); + payMoneyLog.setUid(usersPayBefor.getUid()); + if (user != null) { + payMoneyLog.setUname(user.getName()); + } + payMoneyLog.setPrice(money); + payMoneyLog.setMark("订单退款"); + payMoneyLog.setPayTime(new Date()); + payMoneyLogService.insertPayMoneyLog(payMoneyLog); + +// if(type==1){ +// user.setBalance(user.getBalance().add(money)); +// usersPayBeforData.setYemoney(usersPayBeforData.getYemoney().subtract(money)); +// UserMemnerConsumptionLog userMemnerConsumptionLog=new UserMemnerConsumptionLog(); +// userMemnerConsumptionLog.setUid(Math.toIntExact(user.getId())); +// userMemnerConsumptionLog.setConsumptiontype(2); +// userMemnerConsumptionLog.setConsumptiontime(new Date()); +// userMemnerConsumptionLog.setConsumptionmoney(money); +// userMemnerConsumptionLog.setReamk(reamk); +// userMemnerConsumptionLog.setBeformoney(user.getBalance().subtract(money)); +// userMemnerConsumptionLog.setAftermoney(user.getBalance()); +// userMemnerConsumptionLog.setNowmoney(user.getBalance()); +// userMemnerConsumptionLog.setType(1); +// userMemnerConsumptionLog.setOrderid(usersPayBefor.getOrderid()); +// userMemnerConsumptionLogService.insertUserMemnerConsumptionLog(userMemnerConsumptionLog); +// usersService.updateUsers(user); +// usersPayBeforService.updateUsersPayBefor(usersPayBeforData); +// remap.put("code",1); +// }else{ +// user.setBalance(user.getBalance().subtract(money)); +// usersPayBeforData.setYemoney(usersPayBeforData.getYemoney().add(money)); +// UserMemnerConsumptionLog userMemnerConsumptionLog=new UserMemnerConsumptionLog(); +// userMemnerConsumptionLog.setUid(Math.toIntExact(user.getId())); +// userMemnerConsumptionLog.setConsumptiontype(2); +// userMemnerConsumptionLog.setConsumptiontime(new Date()); +// userMemnerConsumptionLog.setConsumptionmoney(money); +// userMemnerConsumptionLog.setReamk(reamk); +// userMemnerConsumptionLog.setBeformoney(user.getBalance().add(money)); +// userMemnerConsumptionLog.setAftermoney(user.getBalance()); +// userMemnerConsumptionLog.setNowmoney(user.getBalance()); +// userMemnerConsumptionLog.setType(2); +// userMemnerConsumptionLog.setOrderid(usersPayBefor.getOrderid()); +// userMemnerConsumptionLogService.insertUserMemnerConsumptionLog(userMemnerConsumptionLog); +// usersService.updateUsers(user); +// usersPayBeforService.updateUsersPayBefor(usersPayBeforData); +// remap.put("code",1); +// } + + return remap; + } + + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/RefundUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/RefundUtil.java index 9b5fa92..c54347e 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/RefundUtil.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/RefundUtil.java @@ -23,7 +23,7 @@ public class RefundUtil { /** * 处理统一退款业务方法 - 实现真实的业务退款与金额变动 * 配合 UsersPayBeforController.java 里面的 unifiedRefund 方法 - * + * * @param orderId 订单ID * @param wechatRefund 微信退款金额 * @param balanceRefund 余额退款金额 @@ -34,19 +34,19 @@ public class RefundUtil { * @param refundRemark 退款备注 * @return 退款处理结果 */ - public Map processUnifiedRefund(String orderId, BigDecimal wechatRefund, + public Map processUnifiedRefund(String orderId, BigDecimal wechatRefund, BigDecimal balanceRefund, BigDecimal shoppingGoldRefund, BigDecimal serviceGoldRefund, BigDecimal memberDiscountRefund, BigDecimal couponRefund, String refundRemark) { - + Map result = new HashMap<>(); result.put("success", false); result.put("message", ""); result.put("details", new HashMap<>()); - + try { System.out.println("=== 开始处理统一退款业务,订单号: " + orderId + " ==="); - + // 添加参数接收的调试日志 System.out.println("RefundUtil接收到的参数:"); System.out.println(" orderId: " + orderId); @@ -57,7 +57,7 @@ public class RefundUtil { System.out.println(" memberDiscountRefund: " + memberDiscountRefund + " (类型: " + (memberDiscountRefund != null ? memberDiscountRefund.getClass().getSimpleName() : "null") + ")"); System.out.println(" couponRefund: " + couponRefund + " (类型: " + (couponRefund != null ? couponRefund.getClass().getSimpleName() : "null") + ")"); System.out.println(" refundRemark: " + refundRemark); - + // 检查参数是否大于0 System.out.println("参数检查:"); System.out.println(" wechatRefund > 0: " + (wechatRefund != null && wechatRefund.compareTo(BigDecimal.ZERO) > 0)); @@ -66,7 +66,7 @@ public class RefundUtil { System.out.println(" serviceGoldRefund > 0: " + (serviceGoldRefund != null && serviceGoldRefund.compareTo(BigDecimal.ZERO) > 0)); System.out.println(" memberDiscountRefund > 0: " + (memberDiscountRefund != null && memberDiscountRefund.compareTo(BigDecimal.ZERO) > 0)); System.out.println(" couponRefund > 0: " + (couponRefund != null && couponRefund.compareTo(BigDecimal.ZERO) > 0)); - + // 1. 查询支付信息 System.out.println("步骤1: 查询支付信息..."); UsersPayBefor paymentInfo = usersPayBeforService.selectUsersPayBeforByOrderId(orderId); @@ -77,10 +77,10 @@ public class RefundUtil { return result; } System.out.println("✅ 支付信息查询成功"); - System.out.println(" 用户ID: " + paymentInfo.getUid() + - ", 订单金额: " + paymentInfo.getAllmoney() + + System.out.println(" 用户ID: " + paymentInfo.getUid() + + ", 订单金额: " + paymentInfo.getAllmoney() + ", 服务类型: " + paymentInfo.getServicetype()); - + // 2. 查询用户信息 System.out.println("步骤2: 查询用户信息..."); Users user = usersService.selectUsersById(paymentInfo.getUid()); @@ -91,11 +91,11 @@ public class RefundUtil { return result; } System.out.println("✅ 用户信息查询成功,用户: " + user.getName()); - + // 3. 处理各种退款类型 Map refundDetails = new HashMap<>(); boolean hasRefund = false; - + // 3.1 处理微信退款 if (wechatRefund != null && wechatRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.1: 处理微信退款..."); @@ -110,7 +110,7 @@ public class RefundUtil { return result; } } - + // 3.2 处理余额退款 if (balanceRefund != null && balanceRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.2: 处理余额退款..."); @@ -125,7 +125,7 @@ public class RefundUtil { return result; } } - + // 3.3 处理购物金退款(消费金) if (shoppingGoldRefund != null && shoppingGoldRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.3: 处理购物金退款(消费金)..."); @@ -140,7 +140,7 @@ public class RefundUtil { return result; } } - + // 3.4 处理服务金退款 if (serviceGoldRefund != null && serviceGoldRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.4: 处理服务金退款..."); @@ -155,7 +155,7 @@ public class RefundUtil { return result; } } - + // 3.5 会员优惠不参与退款(平台承担的折扣) if (memberDiscountRefund != null && memberDiscountRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.5: 会员优惠不参与退款(平台承担折扣)..."); @@ -163,7 +163,7 @@ public class RefundUtil { // 会员优惠不参与退款,但记录在退款详情中用于说明 refundDetails.put("memberDiscountNotRefunded", memberDiscountRefund); } - + // 3.6 处理优惠券退款 if (couponRefund != null && couponRefund.compareTo(BigDecimal.ZERO) > 0) { System.out.println("步骤3.6: 处理优惠券退款..."); @@ -178,18 +178,18 @@ public class RefundUtil { return result; } } - + if (!hasRefund) { String errorMsg = "没有有效的退款金额"; System.err.println("❌ " + errorMsg); result.put("message", errorMsg); return result; } - + // 4. 更新支付记录状态和扣减预支付金额 System.out.println("步骤4: 更新支付记录状态和扣减预支付金额..."); - boolean updateSuccess = updatePaymentStatus(paymentInfo, orderId, wechatRefund, balanceRefund, - shoppingGoldRefund, serviceGoldRefund, + boolean updateSuccess = updatePaymentStatus(paymentInfo, orderId, wechatRefund, balanceRefund, + shoppingGoldRefund, serviceGoldRefund, memberDiscountRefund, couponRefund); if (!updateSuccess) { System.err.println("❌ 支付记录状态更新失败"); @@ -197,11 +197,11 @@ public class RefundUtil { return result; } System.out.println("✅ 支付记录状态更新成功"); - + // 5. 处理积分和服务金消费金的扣除(退款时需要扣除之前增加的) System.out.println("步骤5: 处理积分和服务金消费金扣除..."); processRefundDeductions(paymentInfo, user, orderId); - + // 6. 记录退款流水日志 System.out.println("步骤6: 记录退款流水日志..."); boolean logSuccess = recordRefundLog(paymentInfo, refundDetails, refundRemark, orderId); @@ -210,24 +210,24 @@ public class RefundUtil { } else { System.out.println("✅ 退款流水日志记录成功"); } - + // 7. 设置成功结果 result.put("success", true); result.put("message", "退款处理成功"); result.put("details", refundDetails); - + System.out.println("=== 统一退款业务处理完成,结果: 成功 ==="); - + } catch (Exception e) { String errorMsg = "退款处理异常: " + e.getMessage(); System.err.println("❌ " + errorMsg); e.printStackTrace(); result.put("message", errorMsg); } - + return result; } - + /** * 退款方法 - 用于AppletController中的订单取消退款 * @param orderid 订单ID @@ -235,7 +235,7 @@ public class RefundUtil { */ public boolean refundOrder(String orderid) { System.out.println("=== 开始退款处理,订单号: " + orderid + " ==="); - + try { // 查询预支付记录 UsersPayBefor payBefor = usersPayBeforService.selectUsersPayBeforByOrderId(orderid); @@ -243,16 +243,16 @@ public class RefundUtil { System.err.println("❌ 预支付记录不存在,订单号: " + orderid); return false; } - + // 查询用户信息 Users user = usersService.selectUsersById(payBefor.getUid()); if (user == null) { System.err.println("❌ 用户不存在,用户ID: " + payBefor.getUid()); return false; } - + boolean refundSuccess = false; - + // 处理微信退款 if (payBefor.getWxmoney() != null && payBefor.getWxmoney().compareTo(BigDecimal.ZERO) > 0) { System.out.println(" 微信退款金额: " + payBefor.getWxmoney() + "元"); @@ -264,7 +264,7 @@ public class RefundUtil { System.err.println("❌ 微信退款处理失败"); } } - + // 处理余额退款 if (payBefor.getYemoney() != null && payBefor.getYemoney().compareTo(BigDecimal.ZERO) > 0) { System.out.println(" 余额退款金额: " + payBefor.getYemoney() + "元"); @@ -276,7 +276,7 @@ public class RefundUtil { System.err.println("❌ 余额退款处理失败"); } } - + // 处理购物金退款 if (payBefor.getShopmoney() != null && payBefor.getShopmoney().compareTo(BigDecimal.ZERO) > 0) { System.out.println(" 购物金退款金额: " + payBefor.getShopmoney() + "元"); @@ -288,7 +288,7 @@ public class RefundUtil { System.err.println("❌ 购物金退款处理失败"); } } - + // 处理服务金退款 if (payBefor.getServicemoney() != null && payBefor.getServicemoney().compareTo(BigDecimal.ZERO) > 0) { System.out.println(" 服务金退款金额: " + payBefor.getServicemoney() + "元"); @@ -300,7 +300,7 @@ public class RefundUtil { System.err.println("❌ 服务金退款处理失败"); } } - + // 处理优惠券退款 if (payBefor.getCouponmoney() != null && payBefor.getCouponmoney().compareTo(BigDecimal.ZERO) > 0) { System.out.println(" 优惠券退款金额: " + payBefor.getCouponmoney() + "元"); @@ -312,24 +312,24 @@ public class RefundUtil { System.err.println("❌ 优惠券退款处理失败"); } } - + // 如果退款成功,更新订单状态 if (refundSuccess) { payBefor.setStatus(3L); // 设置为已退款状态 usersPayBeforService.updateUsersPayBefor(payBefor); System.out.println("✅ 订单状态更新完成"); } - + System.out.println("=== 退款处理完成,最终结果: " + (refundSuccess ? "成功" : "失败") + " ==="); return refundSuccess; - + } catch (Exception e) { System.err.println("❌ 退款处理异常:" + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 处理微信退款 */ @@ -339,17 +339,17 @@ public class RefundUtil { System.err.println("❌ 微信支付单号为空,无法处理微信退款"); return false; } - + // 调用微信退款API Map refundResult = wechatPayV3Util.refund( - orderId, - paymentInfo.getPaycode(), - refundAmount.intValue(), - refundAmount.intValue(), - "统一退款", + orderId, + paymentInfo.getPaycode(), + refundAmount.intValue(), + refundAmount.intValue(), + "统一退款", "" ); - + // 根据微信退款结果判断是否成功 if (refundResult != null && "SUCCESS".equals(refundResult.get("return_code"))) { return true; @@ -357,14 +357,14 @@ public class RefundUtil { System.err.println("❌ 微信退款API调用失败: " + refundResult); return false; } - + } catch (Exception e) { System.err.println("❌ 微信退款处理异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 处理余额退款 */ @@ -374,21 +374,21 @@ public class RefundUtil { System.out.println("用户ID: " + user.getId() + ", 用户名: " + user.getName()); System.out.println("退款金额: " + refundAmount + " (类型: " + (refundAmount != null ? refundAmount.getClass().getSimpleName() : "null") + ")"); System.out.println("订单ID: " + orderId); - + BigDecimal beforeBalance = user.getBalance() != null ? user.getBalance() : BigDecimal.ZERO; BigDecimal afterBalance = beforeBalance.add(refundAmount); - + System.out.println("余额计算:"); System.out.println(" 退款前余额: " + beforeBalance); System.out.println(" 退款金额: " + refundAmount); System.out.println(" 退款后余额: " + afterBalance); - + // 更新用户余额 user.setBalance(afterBalance); System.out.println("尝试更新用户余额..."); int updateResult = usersService.updateUsers(user); System.out.println("用户余额更新结果: " + updateResult); - + if (updateResult > 0) { System.out.println("用户余额更新成功,开始记录流水..."); // 记录余额退款流水 @@ -402,26 +402,26 @@ public class RefundUtil { log.setAftermoney(afterBalance); log.setNowmoney(afterBalance); log.setType(1); // 1=收入 - + System.out.println("流水记录对象创建完成,尝试插入..."); int logResult = userMemnerConsumptionLogService.insertUserMemnerConsumptionLog(log); System.out.println("流水记录插入结果: " + logResult); - - System.out.println("✅ 余额退款成功,用户: " + user.getName() + + + System.out.println("✅ 余额退款成功,用户: " + user.getName() + ", 退款前: " + beforeBalance + "元, 退款后: " + afterBalance + "元"); return true; } else { System.err.println("❌ 用户余额更新失败"); return false; } - + } catch (Exception e) { System.err.println("❌ 余额退款处理异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 处理购物金退款(消费金) */ @@ -429,11 +429,11 @@ public class RefundUtil { try { BigDecimal beforeConsumption = user.getConsumption() != null ? user.getConsumption() : BigDecimal.ZERO; BigDecimal afterConsumption = beforeConsumption.add(refundAmount); - + // 更新用户消费金 user.setConsumption(afterConsumption); int updateResult = usersService.updateUsers(user); - + if (updateResult > 0) { // 记录消费金退款流水 UserBenefitPoints benefitPoints = new UserBenefitPoints(); @@ -448,24 +448,24 @@ public class RefundUtil { benefitPoints.setCreatedAt(new Date()); benefitPoints.setUpdatedAt(new Date()); benefitPoints.setReamk("统一退款-消费金退款,订单号:" + orderId); - + userBenefitPointsService.insertUserBenefitPoints(benefitPoints); - - System.out.println("✅ 消费金退款成功,用户: " + user.getName() + + + System.out.println("✅ 消费金退款成功,用户: " + user.getName() + ", 退款前: " + beforeConsumption + "元, 退款后: " + afterConsumption + "元"); return true; } else { System.err.println("❌ 用户消费金更新失败"); return false; } - + } catch (Exception e) { System.err.println("❌ 消费金退款处理异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 处理服务金退款 */ @@ -473,11 +473,11 @@ public class RefundUtil { try { BigDecimal beforeServiceGold = user.getServicefee() != null ? user.getServicefee() : BigDecimal.ZERO; BigDecimal afterServiceGold = beforeServiceGold.add(refundAmount); - + // 更新用户服务金 user.setServicefee(afterServiceGold); int updateResult = usersService.updateUsers(user); - + if (updateResult > 0) { // 记录服务金退款流水 UserBenefitPoints benefitPoints = new UserBenefitPoints(); @@ -492,24 +492,24 @@ public class RefundUtil { benefitPoints.setCreatedAt(new Date()); benefitPoints.setUpdatedAt(new Date()); benefitPoints.setReamk("统一退款-服务金退款,订单号:" + orderId); - + userBenefitPointsService.insertUserBenefitPoints(benefitPoints); - - System.out.println("✅ 服务金退款成功,用户: " + user.getName() + + + System.out.println("✅ 服务金退款成功,用户: " + user.getName() + ", 退款前: " + beforeServiceGold + "元, 退款后: " + afterServiceGold + "元"); return true; } else { System.err.println("❌ 用户服务金更新失败"); return false; } - + } catch (Exception e) { System.err.println("❌ 服务金退款处理异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 处理优惠券退款 */ @@ -519,20 +519,20 @@ public class RefundUtil { // 可以根据实际业务需求调整 System.out.println("✅ 优惠券退款处理成功,金额: " + refundAmount + "元"); return true; - + } catch (Exception e) { System.err.println("❌ 优惠券退款处理异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 更新支付记录状态和退款金额 * 注意:金额更新已经在Controller中完成,这里只处理业务逻辑 */ - private boolean updatePaymentStatus(UsersPayBefor paymentInfo, String orderId, - BigDecimal wechatRefund, BigDecimal balanceRefund, + private boolean updatePaymentStatus(UsersPayBefor paymentInfo, String orderId, + BigDecimal wechatRefund, BigDecimal balanceRefund, BigDecimal shoppingGoldRefund, BigDecimal serviceGoldRefund, BigDecimal memberDiscountRefund, BigDecimal couponRefund) { try { @@ -544,7 +544,7 @@ public class RefundUtil { System.out.println("服务金退款: " + serviceGoldRefund + "元"); System.out.println("会员优惠: " + memberDiscountRefund + "元(不参与退款)"); System.out.println("优惠券退款: " + couponRefund + "元"); - + // 重要:金额更新已经在Controller中完成,这里不需要重复处理 // 只需要验证数据一致性 System.out.println("=== 验证数据一致性 ==="); @@ -556,7 +556,7 @@ public class RefundUtil { System.out.println(" 优惠券: " + paymentInfo.getCouponmoney() + "元"); System.out.println(" 总金额: " + paymentInfo.getAllmoney() + "元"); System.out.println(" 退款金额: " + paymentInfo.getReturnmoney() + "元"); - + // 验证退款金额不能为负数 if (paymentInfo.getWxmoney() != null && paymentInfo.getWxmoney().compareTo(BigDecimal.ZERO) < 0) { System.err.println("❌ 微信支付金额不能为负数: " + paymentInfo.getWxmoney()); @@ -578,23 +578,23 @@ public class RefundUtil { System.err.println("❌ 优惠券金额不能为负数: " + paymentInfo.getCouponmoney()); return false; } - + System.out.println("✅ 数据一致性验证通过"); System.out.println("✅ 支付记录状态更新完成"); return true; - + } catch (Exception e) { System.err.println("❌ 更新支付记录状态异常: " + e.getMessage()); e.printStackTrace(); return false; } } - + /** * 扣减预支付数据中的对应金额 * 简化版:不进行复杂的金额扣减,因为退款金额已经在Controller中正确计算 */ - private void updatePaymentAmounts(UsersPayBefor record, BigDecimal wechatRefund, + private void updatePaymentAmounts(UsersPayBefor record, BigDecimal wechatRefund, BigDecimal balanceRefund, BigDecimal shoppingGoldRefund, BigDecimal serviceGoldRefund, BigDecimal memberDiscountRefund, BigDecimal couponRefund) { @@ -606,22 +606,22 @@ public class RefundUtil { System.out.println("服务金退款: " + serviceGoldRefund + "元"); System.out.println("会员优惠: " + memberDiscountRefund + "元(不参与退款)"); System.out.println("优惠券退款: " + couponRefund + "元"); - + // 简化逻辑:不进行复杂的金额扣减 // 因为退款金额已经在Controller中正确计算和更新了 // 这里只记录日志,不修改数据 - + System.out.println("ℹ️ 预支付金额保持不变(简化版)"); System.out.println("ℹ️ 订单总金额: " + record.getAllmoney() + "元"); System.out.println("ℹ️ 退款金额: " + record.getReturnmoney() + "元"); System.out.println("=== 预支付金额扣减完成(简化版) ==="); - + } catch (Exception e) { System.err.println("❌ 预支付金额扣减异常: " + e.getMessage()); e.printStackTrace(); } } - + /** * 处理退款扣除(积分、服务金、消费金等) */ @@ -630,28 +630,34 @@ public class RefundUtil { // 这里可以添加退款时需要扣除的积分、服务金、消费金等逻辑 // 根据实际业务需求调整 System.out.println("✅ 退款扣除处理完成"); - + } catch (Exception e) { System.err.println("⚠️ 退款扣除处理异常: " + e.getMessage()); e.printStackTrace(); } } - + /** * 记录退款流水日志 */ - private boolean recordRefundLog(UsersPayBefor paymentInfo, Map refundDetails, + private boolean recordRefundLog(UsersPayBefor paymentInfo, Map refundDetails, String refundRemark, String orderId) { try { // 这里可以添加记录退款流水日志的逻辑 // 根据实际业务需求调整 System.out.println("✅ 退款流水日志记录完成"); return true; - + } catch (Exception e) { System.err.println("❌ 退款流水日志记录异常: " + e.getMessage()); e.printStackTrace(); return false; } } + + + + + + }