From 3ae0c70b8767f0dce625a86c5a74188948c47493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Tue, 6 Aug 2024 13:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=99=E9=A2=9D=E5=8F=98=E5=8A=A8=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../receiveBill/mapper/ReceiveBillMapper.xml | 3 ++ .../service/impl/ReceiveBillServiceImpl.java | 9 +++-- .../recordBalance/domain/RecordBalance.java | 6 +++ .../domain/enums/RecordBalanceBstType.java | 22 ++++++++++ .../mapper/RecordBalanceMapper.java | 5 +++ .../mapper/RecordBalanceMapper.xml | 18 +++++++++ .../service/RecordBalanceService.java | 16 +++++++- .../impl/RecordBalanceServiceImpl.java | 21 +++++++++- .../impl/TransactionBillServiceImpl.java | 17 ++++---- .../ruoyi/ss/user/service/ISmUserService.java | 31 ++++++++------ .../user/service/impl/SmUserServiceImpl.java | 9 +++-- .../app/AppRecordBalanceController.java | 40 +++++++++++++++++++ 12 files changed, 166 insertions(+), 31 deletions(-) create mode 100644 smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/enums/RecordBalanceBstType.java create mode 100644 smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppRecordBalanceController.java diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/mapper/ReceiveBillMapper.xml b/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/mapper/ReceiveBillMapper.xml index a6d9e5b3..7cc21995 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/mapper/ReceiveBillMapper.xml +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/mapper/ReceiveBillMapper.xml @@ -65,6 +65,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + SELECT LAST_INSERT_ID() + insert into ss_receive_bill user_id, diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/service/impl/ReceiveBillServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/service/impl/ReceiveBillServiceImpl.java index 411ace41..e2bb636c 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/service/impl/ReceiveBillServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/receiveBill/service/impl/ReceiveBillServiceImpl.java @@ -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, "设备续费失败"); diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/RecordBalance.java b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/RecordBalance.java index e1443c99..84489c5d 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/RecordBalance.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/RecordBalance.java @@ -41,4 +41,10 @@ public class RecordBalance extends BaseEntity @ApiModelProperty("变化金额") private BigDecimal amount; + + @ApiModelProperty("业务类型") + private String bstType; + + @ApiModelProperty("业务ID") + private Long bstId; } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/enums/RecordBalanceBstType.java b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/enums/RecordBalanceBstType.java new file mode 100644 index 00000000..2b964a67 --- /dev/null +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/domain/enums/RecordBalanceBstType.java @@ -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; + +} diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.java b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.java index 775e9e8e..a4555b15 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.java @@ -61,4 +61,9 @@ public interface RecordBalanceMapper * @return 结果 */ public int deleteRecordBalanceByIds(Long[] ids); + + /** + * 查询一个 + */ + RecordBalanceVO selectOne(@Param("query") RecordBalanceQuery query); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.xml b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.xml index 7dc522f6..f3268a3c 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.xml +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/mapper/RecordBalanceMapper.xml @@ -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" and srb.user_id = #{query.userId} and srb.reason like concat('%', #{query.reason}, '%') and su.user_name like concat('%', #{query.userName}, '%') + and srb.bst_type = #{query.bstType} + and srb.bst_id = #{query.bstId} + + insert into ss_record_balance @@ -48,6 +60,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" reason, create_time, amount, + bst_id, + bst_type, #{userId}, @@ -56,6 +70,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{reason}, #{createTime}, #{amount}, + #{bstId}, + #{bstType}, @@ -68,6 +84,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" reason = #{data.reason}, create_time = #{data.createTime}, amount = #{data.amount}, + bst_type = #{data.bstType}, + bst_id = #{data.bstId}, where id = #{id} diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/RecordBalanceService.java b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/RecordBalanceService.java index 61bfa763..700bf062 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/RecordBalanceService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/RecordBalanceService.java @@ -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); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/impl/RecordBalanceServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/impl/RecordBalanceServiceImpl.java index 32991bf3..dba207ac 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/impl/RecordBalanceServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/recordBalance/service/impl/RecordBalanceServiceImpl.java @@ -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); + } } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java index 9772230a..8dacd235 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java @@ -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()); diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java index a2b82e58..a65e1565 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java @@ -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 diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java index c65f56d4..b95ebd80 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java @@ -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; diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppRecordBalanceController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppRecordBalanceController.java new file mode 100644 index 00000000..6dbf3535 --- /dev/null +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppRecordBalanceController.java @@ -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())); + } + +}