余额变动记录

This commit is contained in:
墨大叔 2024-08-06 13:32:16 +08:00
parent 995a89b0dc
commit 3ae0c70b87
12 changed files with 166 additions and 31 deletions

View File

@ -65,6 +65,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<insert id="insertReceiveBill" parameterType="ReceiveBill" useGeneratedKeys="true" keyProperty="billId">
<selectKey keyProperty="billId" resultType="Long" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into ss_receive_bill
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">user_id,</if>

View File

@ -11,6 +11,7 @@ import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.receiveBill.domain.enums.ReceiveBillStatus;
import com.ruoyi.ss.receiveBill.domain.enums.ReceiveBillType;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import com.ruoyi.ss.transactionBill.domain.TransactionBill;
import com.ruoyi.ss.transactionBill.domain.bo.RechargeBO;
import com.ruoyi.ss.user.domain.SmUserVo;
@ -158,14 +159,14 @@ public class ReceiveBillServiceImpl implements ReceiveBillService
bill.setDescription(String.format("%s年%s月-设备%s月费", billTime.getYear(), billTime.getMonthValue(), device.getDeviceNo()));
bill.setReceivedAmount(amount);
Integer result = transactionTemplate.execute(status -> {
// 用户余额扣减
int subtract = userService.subtractBalance(mch.getUserId(), amount, false, bill.getDescription());
ServiceUtil.assertion(subtract != 1, "扣减商户余额失败");
// 插入账单
int insert = this.insertReceiveBill(bill);
ServiceUtil.assertion(insert != 1, "新增账单失败");
// 用户余额扣减
int subtract = userService.subtractBalance(mch.getUserId(), amount, false, bill.getDescription(), RecordBalanceBstType.RECEIVABLE, bill.getBillId());
ServiceUtil.assertion(subtract != 1, "扣减商户余额失败");
// 设备续费30天
int renewal = deviceService.renewalRentTime(device.getDeviceId(), 30, TimeUnit.DAYS);
ServiceUtil.assertion(renewal != 1, "设备续费失败");

View File

@ -41,4 +41,10 @@ public class RecordBalance extends BaseEntity
@ApiModelProperty("变化金额")
private BigDecimal amount;
@ApiModelProperty("业务类型")
private String bstType;
@ApiModelProperty("业务ID")
private Long bstId;
}

View File

@ -0,0 +1,22 @@
package com.ruoyi.ss.recordBalance.domain.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author wjh
* 2024/8/6
*/
@Getter
@AllArgsConstructor
public enum RecordBalanceBstType {
RECHARGE("1", "充值订单"),
REFUND("2", "退款订单"),
WITHDRAW("3", "提现申请"),
RECEIVABLE("4", "应收账");
private final String type;
private final String msg;
}

View File

@ -61,4 +61,9 @@ public interface RecordBalanceMapper
* @return 结果
*/
public int deleteRecordBalanceByIds(Long[] ids);
/**
* 查询一个
*/
RecordBalanceVO selectOne(@Param("query") RecordBalanceQuery query);
}

View File

@ -15,6 +15,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
srb.reason,
srb.create_time,
srb.amount,
srb.bst_type,
srb.bst_id,
su.user_name as user_name
from ss_record_balance srb
left join sm_user su on su.user_id = srb.user_id
@ -25,6 +27,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="query.userId != null "> and srb.user_id = #{query.userId}</if>
<if test="query.reason != null and query.reason != ''"> and srb.reason like concat('%', #{query.reason}, '%')</if>
<if test="query.userName != null and query.userName != ''"> and su.user_name like concat('%', #{query.userName}, '%')</if>
<if test="query.bstType != null and query.bstType != ''"> and srb.bst_type = #{query.bstType}</if>
<if test="query.bstId != null "> and srb.bst_id = #{query.bstId}</if>
</sql>
<select id="selectRecordBalanceList" parameterType="RecordBalanceQuery" resultMap="RecordBalanceResult">
@ -39,6 +43,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where srb.id = #{id}
</select>
<select id="selectOne" resultMap="RecordBalanceResult">
<include refid="selectRecordBalanceVo"/>
<where>
<include refid="searchCondition"/>
</where>
limit 1
</select>
<insert id="insertRecordBalance" parameterType="RecordBalance" useGeneratedKeys="true" keyProperty="id">
insert into ss_record_balance
<trim prefix="(" suffix=")" suffixOverrides=",">
@ -48,6 +60,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="reason != null and reason != ''">reason,</if>
<if test="createTime != null">create_time,</if>
<if test="amount != null">amount,</if>
<if test="bstId != null">bst_id,</if>
<if test="bstType != null">bst_type,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userId != null">#{userId},</if>
@ -56,6 +70,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="reason != null and reason != ''">#{reason},</if>
<if test="createTime != null">#{createTime},</if>
<if test="amount != null">#{amount},</if>
<if test="bstId != null">#{bstId},</if>
<if test="bstType != null">#{bstType},</if>
</trim>
</insert>
@ -68,6 +84,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="data.reason != null and data.reason != ''">reason = #{data.reason},</if>
<if test="data.createTime != null">create_time = #{data.createTime},</if>
<if test="data.amount != null">amount = #{data.amount},</if>
<if test="data.bstType != null">bst_type = #{data.bstType},</if>
<if test="data.bstId != null">bst_id = #{data.bstId},</if>
</trim>
where id = #{id}
</update>

View File

@ -5,6 +5,7 @@ import java.util.List;
import com.ruoyi.ss.recordBalance.domain.RecordBalance;
import com.ruoyi.ss.recordBalance.domain.RecordBalanceVO;
import com.ruoyi.ss.recordBalance.domain.RecordBalanceQuery;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
/**
* 余额变动记录Service接口
@ -68,8 +69,19 @@ public interface RecordBalanceService
* @param userId 用户ID
* @param beforeBalance 变动前余额
* @param amount 变动的金额
* @param reason
* @param reason 变动原因
* @param bstType
* @param bstId
*/
int record(Long userId, BigDecimal beforeBalance, BigDecimal amount, String reason);
int record(Long userId, BigDecimal beforeBalance, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId);
/**
* 查询用户余额变动记录
*/
RecordBalanceVO selectRecordBalanceById(Long id, Long userId);
/**
* 查询一个
*/
RecordBalanceVO selectOne(RecordBalanceQuery query);
}

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -99,7 +100,7 @@ public class RecordBalanceServiceImpl implements RecordBalanceService
}
@Override
public int record(Long userId, BigDecimal beforeBalance, BigDecimal amount, String reason) {
public int record(Long userId, BigDecimal beforeBalance, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId) {
if (userId == null || beforeBalance == null || amount == null || StringUtils.isBlank(reason)) {
return 0;
}
@ -109,7 +110,25 @@ public class RecordBalanceServiceImpl implements RecordBalanceService
record.setAmount(amount);
record.setAfterBalance(beforeBalance.add(amount));
record.setReason(reason);
record.setBstType(bstType.getType());
record.setBstId(bstId);
return this.insertRecordBalance(record);
}
@Override
public RecordBalanceVO selectRecordBalanceById(Long id, Long userId) {
if (id == null || userId == null) {
return null;
}
RecordBalanceQuery query = new RecordBalanceQuery();
query.setUserId(userId);
query.setId(id);
return this.selectOne(query);
}
@Override
public RecordBalanceVO selectOne(RecordBalanceQuery query) {
return recordBalanceMapper.selectOne(query);
}
}

View File

@ -17,6 +17,7 @@ import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.receiveBill.service.ReceiveBillService;
import com.ruoyi.ss.record.time.service.IRecordTimeService;
import com.ruoyi.ss.record.time.service.RecordTimeConverter;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import com.ruoyi.ss.refund.domain.Refund;
import com.ruoyi.ss.refund.domain.RefundVO;
import com.ruoyi.ss.refund.service.RefundConverter;
@ -392,17 +393,17 @@ public class TransactionBillServiceImpl implements TransactionBillService {
bill.setChannelCost(channel.getCostRate().multiply(bill.getMoney()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); // 渠道成本
transactionTemplate.execute(status -> {
// 提现申请
int insert = this.insertSmTransactionBill(bill);
ServiceUtil.assertion(insert != 1, "提现申请失败");
// 减少余额并判断提现金额是否超额减少的是交易金额
userService.subtractBalance(userId, bill.getMoney(), "提现");
userService.subtractBalance(userId, bill.getMoney(), String.format("提现申请:%s", bill.getBillNo()), RecordBalanceBstType.WITHDRAW, bill.getBillId());
// 减少余额后的用户信息
SmUserVo afterUser = smUserMapper.selectSimpleById(userId);
bill.setAfterBalance(afterUser.getBalance());
// 提现申请
int insert = this.insertSmTransactionBill(bill);
ServiceUtil.assertion(insert != 1, "提现申请失败");
return insert;
});
@ -531,7 +532,7 @@ public class TransactionBillServiceImpl implements TransactionBillService {
// 返还客户余额
TransactionBill bill = transactionBillMapper.selectSmTransactionBillByBillId(dto.getBillId());
ServiceUtil.assertion(bill == null || bill.getUserId() == null, "数据不存在");
userService.addBalance(bill.getUserId(), bill.getMoney(), "提现驳回");
userService.addBalance(bill.getUserId(), bill.getMoney(), String.format("提现驳回: %s", bill.getBillNo()), RecordBalanceBstType.WITHDRAW, bill.getBillId());
return updateCount;
});
@ -711,7 +712,7 @@ public class TransactionBillServiceImpl implements TransactionBillService {
ServiceUtil.assertion(updateCount != 1, "修改订单状态失败,请刷新后重试");
// 商户余额增加
userService.addBalance(bill.getMchId(), bill.getArrivalAmount(), String.format("订单充值:%s", billNo));
userService.addBalance(bill.getMchId(), bill.getArrivalAmount(), String.format("订单充值:%s", billNo), RecordBalanceBstType.RECHARGE, bill.getBillId());
// 记录下充值后的余额
SmUserVo user = userService.selectSmUserByUserId(bill.getMchId());
@ -974,7 +975,7 @@ public class TransactionBillServiceImpl implements TransactionBillService {
RefundVO refundVO = refundService.selectRefundByRefundNo(refund.getRefundNo());
// 商户余额按照比例扣减
userService.subtractBalance(bill.getMchId(), refund.getMchAmount(), "退款");
userService.subtractBalance(bill.getMchId(), refund.getMchAmount(), refund.getReason(), RecordBalanceBstType.REFUND, refund.getBillId());
// 修改原订单的退款金额和退款手续费
int updateRefundAmount = this.addRefundAmount(bill.getBillId(), refund.getAmount(), refund.getMchAmount(), refund.getServiceAmount());

View File

@ -2,6 +2,7 @@ package com.ruoyi.ss.user.service;
import com.ruoyi.common.core.domain.entity.SmUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import com.ruoyi.ss.user.domain.SmUserQuery;
import com.ruoyi.ss.user.domain.SmUserVo;
@ -101,33 +102,39 @@ public interface ISmUserService
/**
* 增加余额
*
* @param userId 用户id
* @param amount 金额
* @param userId 用户id
* @param amount 金额
* @param reason
* @param bstType
* @param bstId
*/
void addBalance(Long userId, BigDecimal amount, String reason);
void addBalance(Long userId, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId);
/**
* 减少余额
*
* @param userId 用户id
* @param amount 金额
* @param userId 用户id
* @param amount 金额
* @param reason
* @param bstType
* @param bstId
*/
default void subtractBalance(Long userId, BigDecimal amount, String reason) {
subtractBalance(userId, amount, true, reason);
default void subtractBalance(Long userId, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId) {
subtractBalance(userId, amount, true, reason, bstType, bstId);
}
/**
* 减少余额
*
* @param userId 用户id
* @param amount 金额
* @param check 是否校验余额是否充足
* @param reason 原因
* @param userId 用户id
* @param amount 金额
* @param check 是否校验余额是否充足
* @param reason 原因
* @param bstType
* @param bstId
* @return
*/
int subtractBalance(Long userId, BigDecimal amount, boolean check, String reason);
int subtractBalance(Long userId, BigDecimal amount, boolean check, String reason, RecordBalanceBstType bstType, Long bstId);
/**
* 修改微信OpenId

View File

@ -9,6 +9,7 @@ import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.domain.DeviceQuery;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import com.ruoyi.ss.recordBalance.service.RecordBalanceService;
import com.ruoyi.ss.store.domain.StoreQuery;
import com.ruoyi.ss.store.domain.StoreVo;
@ -125,7 +126,7 @@ public class SmUserServiceImpl implements ISmUserService
@Override
@Transactional
public void addBalance(Long userId, BigDecimal amount, String reason) {
public void addBalance(Long userId, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId) {
ServiceUtil.assertion(BigDecimal.ZERO.compareTo(amount) > 0, "增加的金额需要大于0");
// 查询用户
@ -136,13 +137,13 @@ public class SmUserServiceImpl implements ISmUserService
ServiceUtil.assertion(updateCount != 1, "增加用户余额失败");
// 余额变动记录
int record = recordBalanceService.record(userId, user.getBalance(), amount, reason);
int record = recordBalanceService.record(userId, user.getBalance(), amount, reason, bstType, bstId);
ServiceUtil.assertion(record != 1, "用户余额变动记录失败");
}
@Override
@Transactional
public int subtractBalance(Long userId, BigDecimal amount, boolean check, String reason) {
public int subtractBalance(Long userId, BigDecimal amount, boolean check, String reason, RecordBalanceBstType bstType, Long bstId) {
ServiceUtil.assertion(BigDecimal.ZERO.compareTo(amount) > 0, "减少的金额需要大于0");
// 查询用户
@ -153,7 +154,7 @@ public class SmUserServiceImpl implements ISmUserService
ServiceUtil.assertion(updateCount != 1, "减少用户余额失败:用户不存在或余额不足");
// 余额变动记录
int record = recordBalanceService.record(userId, user.getBalance(), amount, reason);
int record = recordBalanceService.record(userId, user.getBalance(), amount.negate(), reason, bstType, bstId);
ServiceUtil.assertion(record != 1, "用户余额变动记录失败");
return updateCount;

View File

@ -0,0 +1,40 @@
package com.ruoyi.web.controller.app;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ss.recordBalance.domain.RecordBalanceQuery;
import com.ruoyi.ss.recordBalance.service.RecordBalanceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wjh
* 2024/8/6
*/
@RestController
@RequestMapping("/app/recordBalance")
public class AppRecordBalanceController extends BaseController {
@Autowired
private RecordBalanceService recordBalanceService;
@ApiOperation("查询本人余额变动列表")
@GetMapping("/list")
public TableDataInfo list(RecordBalanceQuery query) {
startPage();
query.setUserId(getUserId());
return getDataTable(recordBalanceService.selectRecordBalanceList(query));
}
@ApiOperation("查询本人余额变动详情")
@GetMapping("/{id}")
public AjaxResult getDetail(@PathVariable Long id) {
return success(recordBalanceService.selectRecordBalanceById(id, getUserId()));
}
}