feat:充值扣款

This commit is contained in:
dzc committed 2025-04-16 12:49:10 +08:00
1 parent 823b49d4a1
commit 2fd5cd6ee1
2 files changed
+82 -46

No files matched your search

@@ -14,7 +14,7 @@ import java.util.Arrays;
public enum WalletTypeEnum {
EAT("1", "饭卡钱包"),
PAY("2", "虚拟钱包"),
PAY("2", "消费钱包"),
;
private final String code;
@@ -43,6 +43,7 @@ import com.bsd.pjcloud.wallet.api.entity.WalletTypeEntity;
import com.bsd.pjcloud.wallet.api.entity.recharge.WalletRechargeApplyEntity;
import com.bsd.pjcloud.wallet.api.entity.recharge.WalletRechargeImportEntity;
import com.bsd.pjcloud.wallet.api.utils.WalletDataUtil;
import com.bsd.pjcloud.wallet.api.vo.WalletManageVo;
import com.bsd.pjcloud.wallet.api.vo.recharge.WalletRechargeApplyVo;
import com.bsd.pjcloud.wallet.api.vo.recharge.WalletRechargeImportVo;
import com.bsd.pjcloud.wallet.api.vo.recharge.excel.WalletRechargeByApplyExcelVo;
@@ -85,11 +86,11 @@ public class WalletRechargeApplyServiceImpl extends ServiceImpl<WalletRechargeAp
private final WalletManageService walletManageService;
private final WalletTransactionService walletTransactionService;
private final WalletTypeService walletTypeService;
private final WalletThirdZyService walletThirdZyService;
private final WalletTransactionMapper walletTransactionMapper;
private final WalletManageMapper walletManageMapper;
private final WalletTypeMapper walletTypeMapper;
private final WalletThirdZyService walletThirdZyService;
private final RemoteUserService userService;
private final RemoteDeptService deptService;
@@ -181,41 +182,7 @@ public class WalletRechargeApplyServiceImpl extends ServiceImpl<WalletRechargeAp
throw new RuntimeException("饭卡类型不存在");
}
// 判断申请下如果类型为记账扣款,余额是否够扣款
if (ObjectUtil.equals(detail.getBizType(), WalletBizRechargeEnum.REDUCTION.getCode())) {
List<WalletRechargeImportEntity> importList = rechargeImportService.list(new LambdaQueryWrapper<WalletRechargeImportEntity>()
.eq(WalletRechargeImportEntity::getApplyId, detail.getId())
);
List<Long> importUserIds = importList.stream().map(WalletRechargeImportEntity::getUserId).distinct().toList();
// 查询用户
List<UserVO> userList = userService.getUserDeptByIds(importUserIds).getData();
// 错误信息
StringBuilder errorMsg = new StringBuilder();
// 查询用户余额
WalletManageDto userWalletDto = new WalletManageDto();
userWalletDto.setUserIds(importUserIds);
userWalletDto.setWalletTypeId(detail.getWalletTypeId());
List<WalletManageEntity> userWalletList = walletManageMapper.list(userWalletDto);
for (WalletRechargeImportEntity i : importList) {
userWalletList.stream().filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId())).findFirst().ifPresent(wallet -> {
int discountPrice = wallet.getBalance() - i.getPrice();
if (discountPrice < 0) {
// 查询用户名字
UserVO userTarget = userList.stream().filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId())).findFirst().get();
if (ObjectUtil.isEmpty(userTarget)) {
errorMsg.append("用户id<").append(wallet.getUserId()).append(">");
} else {
errorMsg.append("用户名<").append(userTarget.getName()).append(">");
}
errorMsg.append("记账扣款失败,");
errorMsg.append("操作前的金额<").append(wallet.getBalance() / 100).append(">,");
errorMsg.append("记账扣款的金额<").append(i.getPrice() / 100).append(">,");
errorMsg.append("记账扣款后的金额<").append(discountPrice / 100).append(">\n");
}
});
}
String errorMsgString = errorMsg.toString();
if (ObjectUtil.isNotEmpty(errorMsgString)) throw new RuntimeException(errorMsgString);
}
validateWalletBalance(detail);
// 发起审批
WalletRechargeApplyFlowDto flowDto = new WalletRechargeApplyFlowDto();
flowDto.setApplyUsername(user.getSysUser().getUsername());
@@ -347,16 +314,42 @@ public class WalletRechargeApplyServiceImpl extends ServiceImpl<WalletRechargeAp
return;
}
getSelf().updateBeforePrice(detail);
if (ObjectUtil.equals(detail.getBizType(), WalletBizRechargeEnum.REDUCTION.getCode())) {
importList.forEach(v -> v.setPrice(-1 * Math.abs(v.getPrice())));
}
for (WalletRechargeImportEntity v : importList) {
// 查询每个人的钱包余额
WalletManageEntity wallet = walletManageService.getWallet(v.getUserId(), detail.getWalletTypeId());
if (ObjectUtil.isEmpty(wallet)) {
continue;
// 如果是充值就充值到指定钱包
if (ObjectUtil.equals(detail.getBizType(), WalletBizRechargeEnum.ADD.getCode())) {
for (WalletRechargeImportEntity v : importList) {
// 查询每个人的钱包余额
WalletManageEntity wallet = walletManageService.getWallet(v.getUserId(), detail.getWalletTypeId());
if (ObjectUtil.isEmpty(wallet)) {
continue;
}
walletTransactionService.addWalletBalance(wallet.getId(), v.getPrice(), String.valueOf(v.getId()), WalletBizEnum.BATCH);
}
}
// 如果是记账扣款就先扣消费钱包,再扣饭卡钱包
if (ObjectUtil.equals(detail.getBizType(), WalletBizRechargeEnum.REDUCTION.getCode())) {
List<Long> importUserIds = importList.stream().map(WalletRechargeImportEntity::getUserId).distinct().toList();
// 查询用户钱包
WalletManageDto userWalletDto = new WalletManageDto();
userWalletDto.setUserIds(importUserIds);
List<WalletManageVo> userWalletList = walletManageMapper.joinList(userWalletDto);
for (WalletRechargeImportEntity i : importList) {
// 消费钱包
WalletManageVo payWalletVo = userWalletList.stream()
.filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId()) && ObjectUtil.equals(j.getWalletTypeUniqueCode(), WalletTypeEnum.PAY.getCode()))
.findFirst().get();
if (ObjectUtil.isEmpty(payWalletVo)) continue;
// 饭卡钱包
WalletManageVo cardWalletVo = userWalletList.stream()
.filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId()) && ObjectUtil.equals(j.getWalletTypeUniqueCode(), WalletTypeEnum.EAT.getCode()))
.findFirst().get();
if (ObjectUtil.isEmpty(cardWalletVo)) continue;
// 先判断消费钱包是不是够用
int discountPrice = payWalletVo.getBalance() - i.getPrice();
walletTransactionService.addWalletBalance(payWalletVo.getId(), payWalletVo.getBalance() * -1, String.valueOf(i.getId()), WalletBizEnum.BATCH);
if (discountPrice < 0) {
walletTransactionService.addWalletBalance(cardWalletVo.getId(), discountPrice, String.valueOf(i.getId()), WalletBizEnum.BATCH);
}
}
walletTransactionService.addWalletBalance(wallet.getId(), v.getPrice(), String.valueOf(v.getId()), WalletBizEnum.BATCH);
}
// 同步到正元钱包状态
List<Long> userIds = importList.stream().map(WalletRechargeImportEntity::getUserId).distinct().toList();
@@ -484,6 +477,49 @@ public class WalletRechargeApplyServiceImpl extends ServiceImpl<WalletRechargeAp
return count >= 3;
}
/**
* 判断申请下如果类型为记账扣款,余额是否够扣款
*/
private void validateWalletBalance(WalletRechargeApplyEntity applyDto) {
if (ObjectUtil.notEqual(applyDto.getBizType(), WalletBizRechargeEnum.REDUCTION.getCode())) return;
List<WalletRechargeImportEntity> importList = rechargeImportService.list(new LambdaQueryWrapper<WalletRechargeImportEntity>()
.eq(WalletRechargeImportEntity::getApplyId, applyDto.getId())
);
List<Long> importUserIds = importList.stream().map(WalletRechargeImportEntity::getUserId).distinct().toList();
// 查询用户
List<UserVO> userList = userService.getUserDeptByIds(importUserIds).getData();
// 错误信息
StringBuilder errorMsg = new StringBuilder();
// 查询用户钱包
WalletManageDto userWalletDto = new WalletManageDto();
userWalletDto.setUserIds(importUserIds);
List<WalletManageVo> userWalletList = walletManageMapper.joinList(userWalletDto);
for (WalletRechargeImportEntity i : importList) {
List<WalletManageVo> walletList = userWalletList.stream().filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId())).toList();
int walletBalance = walletList.stream().map(WalletManageEntity::getBalance).map(BigDecimal::valueOf).reduce(BigDecimal::add).get().intValue();
int discountPrice = walletBalance - i.getPrice();
if (discountPrice < 0) {
// 查询用户名字
UserVO userTarget = userList.stream().filter(j -> ObjectUtil.equals(i.getUserId(), j.getUserId())).findFirst().get();
if (ObjectUtil.isEmpty(userTarget)) {
errorMsg.append("用户id<").append(i.getUserId()).append(">");
} else {
errorMsg.append("用户名<").append(userTarget.getName()).append(">");
}
errorMsg.append("记账扣款失败,");
for (WalletManageVo j : walletList) {
WalletTypeEnum walletTypeEnum = WalletTypeEnum.valueOfByCode(j.getWalletTypeUniqueCode());
errorMsg.append(walletTypeEnum.getDesc()).append("余额为<").append(j.getBalance() / 100).append(">,");
}
errorMsg.append("总余额为<").append(walletBalance / 100).append(">");
errorMsg.append("记账扣款的金额<").append(i.getPrice() / 100).append(">,");
errorMsg.append("记账扣款后的金额<").append(discountPrice / 100).append(">\n");
}
}
String errorMsgString = errorMsg.toString();
if (ObjectUtil.isNotEmpty(errorMsgString)) throw new RuntimeException(errorMsgString);
}
private WalletRechargeApplyServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}