This commit is contained in:
邱贞招 2025-01-10 16:23:47 +08:00
parent 22b5ec5067
commit 3fa3458f7b
2 changed files with 63 additions and 34 deletions

View File

@ -0,0 +1,30 @@
package com.ruoyi.ss.changeBalance.domain.enums;
import lombok.Getter;
@Getter
public enum BusinessType {
ORDER_INCOME("1", "订单收入:"),
REORDER_INCOME("2", "续单订单收入:"),
ORDER_REFUND("4", "订单退款:"),
WITHDRAW("5", "提现"),
WITHDRAW_FAIL("6", "提现失败"),
SMS_CHARGE("8", "短信扣费");
private final String code;
private final String description;
BusinessType(String code, String description) {
this.code = code;
this.description = description;
}
public static BusinessType fromCode(String code) {
for (BusinessType type : values()) {
if (type.getCode().equals(code)) {
return type;
}
}
return null; // 或者抛出异常
}
}

View File

@ -6,6 +6,7 @@ import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.changeBalance.domain.ChangeBalance;
import com.ruoyi.ss.changeBalance.domain.ChangeBalanceQuery;
import com.ruoyi.ss.changeBalance.domain.enums.BusinessType;
import com.ruoyi.ss.changeBalance.mapper.ChangeBalanceMapper;
import com.ruoyi.ss.changeBalance.service.IChangeBalanceService;
import com.ruoyi.ss.user.service.IUserService;
@ -17,6 +18,8 @@ import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import static com.ruoyi.common.constant.ServiceConstants.FLOW_TYPE_INCOME;
/**
* 余额变动Service业务层处理
*
@ -138,63 +141,59 @@ public class ChangeBalanceServiceImpl implements IChangeBalanceService
*/
@Override
public int generateChangeBalance(String orderNo,String outTradeNo,String type,String busType,BigDecimal payFee,Long userId,String userName,String phone,String userType) {
// 获取当前余额
BigDecimal balance = userService.selectUserBalanceById(userId);
// 计算变更后的余额
BigDecimal afterBalance;
if (type.equals(FLOW_TYPE_INCOME)) {
afterBalance = balance.add(payFee);
} else {
afterBalance = balance.subtract(payFee);
// 校验余额是否会变成负数
if (afterBalance.compareTo(BigDecimal.ZERO) < 0) {
throw new ServiceException("余额不足");
}
}
ChangeBalance rlChangeBalance = new ChangeBalance();
rlChangeBalance.setOrderNo(orderNo);
rlChangeBalance.setOutTradeNo(outTradeNo);
rlChangeBalance.setType(type);
rlChangeBalance.setBusType(busType);
BigDecimal balance = userService.selectUserBalanceById(userId);
rlChangeBalance.setBeforeBalance(balance);
if(type.equals("1")){
rlChangeBalance.setAfterBalance(balance.add(payFee));
}else{
rlChangeBalance.setAfterBalance(balance.subtract(payFee));
}
rlChangeBalance.setAfterBalance(afterBalance);
rlChangeBalance.setAmount(payFee);
rlChangeBalance.setOwnerId(userId);
rlChangeBalance.setOwnerName(userName);
rlChangeBalance.setOwnerType(userType);
rlChangeBalance.setOwnerPhone(phone);
rlChangeBalance.setCreateTime(DateUtils.getNowDate());
// 根据 busType 生成不同的 reason 描述
BigDecimal changeAmount = rlChangeBalance.getAmount();
String reason = "";
switch (busType) {
case "1":
reason = "订单收入:" + orderNo;
break;
case "2":
reason = "续单订单收入:" + orderNo;
break;
case "4":
reason = "订单退款:" + orderNo;
BigDecimal changeAmount = rlChangeBalance.getAmount();
String reason = "未知业务类型"; // 默认值
BusinessType businessType = BusinessType.fromCode(busType);
if (businessType != null) {
reason = businessType.getDescription() + orderNo;
if (businessType == BusinessType.ORDER_REFUND || businessType == BusinessType.WITHDRAW || businessType == BusinessType.SMS_CHARGE) {
changeAmount = changeAmount.negate();
break;
case "5":
reason = "提现";
changeAmount = changeAmount.negate();
break;
case "6":
reason = "提现失败";
break;
case "8":
reason = "短信扣费";
changeAmount = changeAmount.negate();
break;
default:
reason = "未知业务类型";
break;
}
}
rlChangeBalance.setReason(reason);
BigDecimal finalChangeAmount = changeAmount;
Boolean execute = transactionTemplate.execute(e -> {
int i = insertRlChangeBalance(rlChangeBalance);
ServiceUtil.assertion(i == 0, "账变记录插入失败");
int i2 = userService.changeBalance(finalChangeAmount,userId);
int i2 = userService.changeBalance(finalChangeAmount, userId);
ServiceUtil.assertion(i2 == 0, "用户余额改变失败");
return Boolean.TRUE;
});
if(Boolean.FALSE.equals(execute))throw new ServiceException("账变记录插入失败");
if (Boolean.FALSE.equals(execute)) throw new ServiceException("账变记录插入失败");
return 1;
}