package com.ruoyi.cwgl.service.impl;
|
|
import java.util.List;
|
|
import com.ruoyi.common.utils.DateUtils;
|
import javax.annotation.Resource;
|
|
import lombok.NonNull;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.stereotype.Service;
|
import org.springframework.scheduling.annotation.Async;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.ruoyi.common.utils.PageUtils;
|
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.annotation.DataSource;
|
import com.ruoyi.common.enums.DataSourceType;
|
import com.ruoyi.common.core.service.BaseService;
|
|
import com.ruoyi.cwgl.mapper.FundFlowClaimDetailMapper;
|
import com.ruoyi.cwgl.mapper.FundFlowMapper;
|
import com.ruoyi.cwgl.domain.FundFlowClaimDetail;
|
import com.ruoyi.cwgl.domain.FundFlow;
|
import com.ruoyi.cwgl.service.IFundFlowClaimDetailService;
|
import com.ruoyi.common.core.text.Convert;
|
import java.math.BigDecimal;
|
|
/**
|
* 账单认领明细Service业务层处理
|
*
|
* @author ruoyi
|
* @date 2026-01-12
|
*/
|
@Service
|
@Transactional(rollbackFor = Exception.class)
|
public class FundFlowClaimDetailServiceImpl extends BaseService<FundFlowClaimDetailMapper, FundFlowClaimDetail> implements IFundFlowClaimDetailService
|
{
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
@Resource
|
private FundFlowClaimDetailMapper fundFlowClaimDetailMapper;
|
|
@Resource
|
private FundFlowMapper fundFlowMapper;
|
|
|
/**
|
* 查询账单认领明细
|
*
|
* @param id 账单认领明细ID
|
* @return 账单认领明细
|
*/
|
@DataSource(DataSourceType.SLAVE)
|
@Override
|
public FundFlowClaimDetail selectFundFlowClaimDetailById(Integer id)
|
{
|
return fundFlowClaimDetailMapper.selectFundFlowClaimDetailById(id);
|
}
|
|
/**
|
* 查询账单认领明细 记录数
|
*
|
* @param fundFlowClaimDetail 账单认领明细
|
* @return 账单认领明细集合
|
*/
|
@DataSource(DataSourceType.SLAVE)
|
@Override
|
public int selectFundFlowClaimDetailCount(FundFlowClaimDetail fundFlowClaimDetail)
|
{
|
return fundFlowClaimDetailMapper.selectFundFlowClaimDetailCount(fundFlowClaimDetail);
|
}
|
|
/**
|
* 查询账单认领明细列表
|
*
|
* @param fundFlowClaimDetail 账单认领明细
|
* @return 账单认领明细
|
*/
|
@DataSource(DataSourceType.SLAVE)
|
@Override
|
public List<FundFlowClaimDetail> selectFundFlowClaimDetailList(FundFlowClaimDetail fundFlowClaimDetail)
|
{
|
return fundFlowClaimDetailMapper.selectFundFlowClaimDetailList(fundFlowClaimDetail);
|
}
|
|
/**
|
* 查询账单认领明细列表 异步 导出
|
*
|
* @param fundFlowClaimDetail 账单认领明细
|
* @param exportKey 导出功能的唯一标识
|
* @return 账单认领明细集合
|
*/
|
@DataSource(DataSourceType.SLAVE)
|
@Async
|
@Override
|
public void export(FundFlowClaimDetail fundFlowClaimDetail,String exportKey) {
|
|
super.export(FundFlowClaimDetail.class,exportKey,"fundFlowClaimDetailData",(pageNum)->{
|
PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE);
|
return selectFundFlowClaimDetailList(fundFlowClaimDetail);
|
});
|
}
|
|
|
/**
|
* 新增账单认领明细
|
*
|
* @param fundFlowClaimDetail 账单认领明细
|
* @return 结果
|
*/
|
@Override
|
public int insertFundFlowClaimDetail(FundFlowClaimDetail fundFlowClaimDetail)
|
{
|
fundFlowClaimDetail.setCreateTime(DateUtils.getNowDate());
|
return fundFlowClaimDetailMapper.insertFundFlowClaimDetail(fundFlowClaimDetail);
|
}
|
|
/**
|
* 新增账单认领明细[批量]
|
*
|
* @param fundFlowClaimDetails 账单认领明细
|
* @return 结果
|
*/
|
@Override
|
public int insertFundFlowClaimDetailBatch(List<FundFlowClaimDetail> fundFlowClaimDetails)
|
{
|
int rows = fundFlowClaimDetailMapper.insertFundFlowClaimDetailBatch(fundFlowClaimDetails);
|
return rows;
|
}
|
|
/**
|
* 修改账单认领明细
|
*
|
* @param fundFlowClaimDetail 账单认领明细
|
* @return 结果
|
*/
|
@Override
|
public int updateFundFlowClaimDetail(FundFlowClaimDetail fundFlowClaimDetail)
|
{
|
fundFlowClaimDetail.setUpdateTime(DateUtils.getNowDate());
|
return fundFlowClaimDetailMapper.updateFundFlowClaimDetail(fundFlowClaimDetail);
|
}
|
|
/**
|
* 修改账单认领明细[批量]
|
*
|
* @param fundFlowClaimDetails 账单认领明细
|
* @return 结果
|
*/
|
@Override
|
public int updateFundFlowClaimDetailBatch(List<FundFlowClaimDetail> fundFlowClaimDetails){
|
return fundFlowClaimDetailMapper.updateFundFlowClaimDetailBatch(fundFlowClaimDetails);
|
}
|
|
/**
|
* 删除账单认领明细对象
|
*
|
* @param ids 需要删除的数据ID
|
* @return 结果
|
*/
|
@Override
|
public int deleteFundFlowClaimDetailByIds(String ids)
|
{
|
return deleteFundFlowClaimDetailByIds(Convert.toIntArray(ids));
|
}
|
|
/**
|
* 删除账单认领明细对象
|
*
|
*
|
* @param ids 需要删除的数据ID
|
* @return 结果
|
*/
|
@Override
|
public int deleteFundFlowClaimDetailByIds(Integer[] ids)
|
{
|
return fundFlowClaimDetailMapper.deleteFundFlowClaimDetailByIds(ids);
|
}
|
|
/**
|
* 删除账单认领明细信息
|
*
|
* @param id 账单认领明细ID
|
* @return 结果
|
*/
|
@Override
|
public int deleteFundFlowClaimDetailById(Integer id)
|
{
|
return fundFlowClaimDetailMapper.deleteFundFlowClaimDetailById(id);
|
}
|
|
/**
|
* 账单认领
|
*
|
* @param fundFlowId 资金流水ID
|
* @param claimDetails 账单认领明细列表
|
* @return 结果
|
*/
|
@Override
|
public int claimBill(Integer fundFlowId, List<FundFlowClaimDetail> claimDetails)
|
{
|
if (fundFlowId == null) {
|
throw new RuntimeException("资金流水ID不能为空");
|
}
|
|
if (claimDetails == null || claimDetails.isEmpty()) {
|
throw new RuntimeException("认领明细列表不能为空");
|
}
|
// 计算总认领金额
|
BigDecimal totalClaimAmount = BigDecimal.ZERO;
|
// 验证认领明细的必填字段
|
for (FundFlowClaimDetail claimDetail : claimDetails) {
|
if (claimDetail.getClaimAmount() == null || claimDetail.getClaimAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
throw new RuntimeException("认领金额必须大于0");
|
}
|
totalClaimAmount = totalClaimAmount.add(claimDetail.getClaimAmount());
|
}
|
|
// 查询资金流水信息
|
FundFlow fundFlow = fundFlowMapper.selectFundFlowById(fundFlowId);
|
|
// 根据总认领金额与交易金额的比较设置状态
|
String newStatus = getString(fundFlowId, claimDetails, fundFlow);
|
|
// 更新资金流水状态和已认领金额
|
fundFlow.setStatus(newStatus);
|
fundFlow.setClaimedAmount(totalClaimAmount); // 设置已认领金额
|
fundFlow.setUpdateTime(DateUtils.getNowDate());
|
int updateResult = fundFlowMapper.updateFundFlow(fundFlow);
|
if (updateResult <= 0) {
|
throw new RuntimeException("更新资金流水状态失败,ID: " + fundFlowId);
|
}
|
|
// 先删除该资金流水ID下的所有认领明细(避免重复认领)
|
fundFlowClaimDetailMapper.deleteFundFlowClaimDetailByFundFlowId(fundFlowId);
|
|
// 批量保存所有账单认领明细
|
for (FundFlowClaimDetail claimDetail : claimDetails) {
|
// 设置资金流水ID
|
claimDetail.setFundFlowId(fundFlowId);
|
// 设置认领日期
|
claimDetail.setClaimDate(DateUtils.getNowDate());
|
// 设置创建时间
|
claimDetail.setCreateTime(DateUtils.getNowDate());
|
}
|
|
int insertResult = fundFlowClaimDetailMapper.insertFundFlowClaimDetailBatch(claimDetails);
|
return insertResult;
|
}
|
|
/**
|
* 根据资金流水ID、认领详情列表和资金流水对象获取状态字符串
|
* @param fundFlowId 资金流水ID,用于标识特定的资金流水记录
|
* @param claimDetails 认领详情列表,包含所有认领金额信息
|
* @param fundFlow 资金流水对象,包含交易金额等关键信息
|
* @return 返回处理后的状态字符串
|
* @throws RuntimeException 当资金流水对象为null时抛出异常
|
*/
|
private static @NonNull String getString(Integer fundFlowId, List<FundFlowClaimDetail> claimDetails, FundFlow fundFlow) {
|
// 检查资金流水对象是否为空,如果为空则抛出异常
|
if (fundFlow == null) {
|
throw new RuntimeException("资金流水不存在,ID: " + fundFlowId);
|
}
|
|
// 计算总认领金额
|
BigDecimal totalClaimAmount = BigDecimal.ZERO;
|
for (FundFlowClaimDetail claimDetail : claimDetails) {
|
totalClaimAmount = totalClaimAmount.add(claimDetail.getClaimAmount());
|
}
|
|
// 验证总认领金额不能超过交易金额
|
String newStatus = getString(fundFlow, totalClaimAmount);
|
return newStatus;
|
}
|
|
private static @NonNull String getString(FundFlow fundFlow, BigDecimal totalClaimAmount) {
|
BigDecimal transactionAmount = fundFlow.getTransactionAmount();
|
if (totalClaimAmount.compareTo(transactionAmount) > 0) {
|
throw new RuntimeException("总认领金额不能超过交易金额。交易金额: " + transactionAmount + ", 总认领金额: " + totalClaimAmount);
|
}
|
|
// 根据总认领金额与交易金额的比较设置状态
|
String newStatus;
|
if (totalClaimAmount.compareTo(transactionAmount) < 0) {
|
// 总认领金额小于交易金额,状态改为2(部分认领)
|
newStatus = "2";
|
} else {
|
// 总认领金额等于交易金额,状态改为3(完全认领)
|
newStatus = "3";
|
}
|
return newStatus;
|
}
|
}
|