额度审批

This commit is contained in:
luojiaquan
2026-09-17 09:00:26 +08:00
parent 6487146117
commit 324da1baa1
14 changed files with 974 additions and 439 deletions
@@ -0,0 +1,102 @@
package com.ruoyi.customer.customermoney.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 客户额度调整申请审批 customer_credit_apply
* 信用额度/额外额度(临时额度)调整先申请,审批通过后才真正调整账户额度
*
* @author ruoyi
* @date 2026-09-16
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class CustomerCreditApply extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 待审批 */
public static final String STATUS_PENDING = "PENDING";
/** 已通过 */
public static final String STATUS_APPROVED = "APPROVED";
/** 已驳回 */
public static final String STATUS_REJECTED = "REJECTED";
/** 已撤销 */
public static final String STATUS_CANCELLED = "CANCELLED";
/** 申请单ID */
private Long applyId;
/** 申请单号(EDSQ前缀) */
private String applyNo;
/** 客户ID */
private Long customerId;
/** 客户账户ID */
private Long accountId;
/** 额度类型:CREDIT信用额度 TEMP临时额度(额外额度) */
private String creditType;
/** 调整方式:SET设置为 ADD增加 SUB减少 */
private String adjustMode;
/** 调整金额(正数) */
private java.math.BigDecimal changeAmount;
/** 临时额度生效日期 */
@JsonFormat(pattern = "yyyy-MM-dd")
private Date effectiveDate;
/** 临时额度失效日期(空=长期) */
@JsonFormat(pattern = "yyyy-MM-dd")
private Date expireDate;
/** 调整原因 */
private String reason;
/** 状态:PENDING/APPROVED/REJECTED/CANCELLED */
private String status;
/** 申请人 */
private String applyBy;
/** 申请人ID */
private Long applyUserId;
/** 申请时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date applyTime;
/** 审批人 */
private String approveBy;
/** 审批人ID */
private Long approveUserId;
/** 审批时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date approveTime;
/** 审批意见/驳回原因 */
private String approveRemark;
/** 审批通过后生成的额度流水ID */
private Long flowId;
/** 删除标志 */
private String delFlag;
/* -------- 展示字段 -------- */
/** 客户编码 */
private String customerCode;
/** 客户名称 */
private String customerName;
}
@@ -0,0 +1,35 @@
package com.ruoyi.customer.customermoney.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.customer.customermoney.domain.CustomerCreditApply;
/**
* 客户额度调整申请审批 Mapper
*
* @author ruoyi
* @date 2026-09-16
*/
public interface CustomerCreditApplyMapper
{
/** 新增申请 */
int insertApply(CustomerCreditApply apply);
/** 按ID查申请(行锁,审批事务使用) */
CustomerCreditApply selectApplyByIdForUpdate(@Param("applyId") Long applyId);
/** 按ID查申请 */
CustomerCreditApply selectApplyById(@Param("applyId") Long applyId);
/** 查询申请列表(可按客户/状态/类型过滤) */
List<CustomerCreditApply> selectApplyList(CustomerCreditApply query);
/** 审批通过:PENDING -> APPROVED,回写账户ID、流水ID、审批人/时间/意见 */
int updateApproved(CustomerCreditApply apply);
/** 审批驳回:PENDING -> REJECTED */
int updateRejected(CustomerCreditApply apply);
/** 申请人撤销:PENDING -> CANCELLED */
int updateCancelled(@Param("applyId") Long applyId, @Param("updateBy") String updateBy);
}
@@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.customer.customermoney.domain.CustomerAccount;
import com.ruoyi.customer.customermoney.domain.CustomerBalanceFlow;
import com.ruoyi.customer.customermoney.domain.CustomerCreditApply;
import com.ruoyi.customer.customermoney.domain.CustomerCreditFlow;
import com.ruoyi.customer.customermoney.domain.CustomerReceipt;
@@ -52,11 +53,28 @@ public interface ICustomerMoneyService
List<CustomerCreditFlow> selectCreditFlowList(CustomerCreditFlow query);
/**
* 调整客户额度(信用额度/临时额度)
* 事务内:锁定CNY账户(不存在则初始化)-> 校验(不能为负、不能低于已用额度)
* -> 乐观锁更新额度 -> 回写客户档案额度字段 -> 写额度流水
* 提交额度调整申请(信用额度/临时额度)。申请仅落库为 PENDING,不立即变动账户额度;
* 申请时会按账户当前额度做一次合法性校验(不能为负、不能低于已用额度),审批通过时会再次校验。
*/
int adjustCredit(CustomerCreditFlow flow);
int applyCreditAdjust(CustomerCreditApply apply);
/**
* 审批通过额度调整申请:PENDING -> APPROVED。
* 事务内重新锁定CNY账户并按审批时的实际额度计算/校验,通过后调整额度、回写客户档案、写额度流水。
*/
int approveCreditApply(Long applyId, String approveRemark);
/** 审批驳回:PENDING -> REJECTED,驳回原因必填 */
int rejectCreditApply(Long applyId, String rejectReason);
/** 申请人撤销:PENDING -> CANCELLED */
int cancelCreditApply(Long applyId);
/** 查询额度调整申请列表 */
List<CustomerCreditApply> selectCreditApplyList(CustomerCreditApply query);
/** 查询额度调整申请详情 */
CustomerCreditApply selectCreditApplyById(Long applyId);
/**
* 账单确认时占用授信额度(used_credit += amount)。该币种账户不存在时跳过。
@@ -20,8 +20,10 @@ import com.ruoyi.customer.customerinfo.domain.BmsCustomerAttachment;
import com.ruoyi.customer.customerinfo.mapper.BmsCustomerMapper;
import com.ruoyi.customer.customermoney.domain.CustomerAccount;
import com.ruoyi.customer.customermoney.domain.CustomerBalanceFlow;
import com.ruoyi.customer.customermoney.domain.CustomerCreditApply;
import com.ruoyi.customer.customermoney.domain.CustomerCreditFlow;
import com.ruoyi.customer.customermoney.domain.CustomerReceipt;
import com.ruoyi.customer.customermoney.mapper.CustomerCreditApplyMapper;
import com.ruoyi.customer.customermoney.mapper.CustomerMoneyMapper;
import com.ruoyi.customer.customermoney.service.ICustomerMoneyService;
import com.ruoyi.system.domain.BmsExchangeRate;
@@ -48,6 +50,9 @@ public class CustomerMoneyServiceImpl implements ICustomerMoneyService
@Autowired
private CustomerMoneyMapper customerMoneyMapper;
@Autowired
private CustomerCreditApplyMapper creditApplyMapper;
@Autowired
private BmsCustomerMapper bmsCustomerMapper;
@@ -268,53 +273,27 @@ public class CustomerMoneyServiceImpl implements ICustomerMoneyService
return customerMoneyMapper.selectCreditFlowList(query);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int adjustCredit(CustomerCreditFlow cmd)
/**
* 执行额度调整(额度申请审批通过后调用)。
* 锁CNY账户 -> 按审批时实际额度计算并校验 -> 乐观锁更新 -> 回写客户档案 -> 写额度流水。
* 返回已落库的额度流水(含ID)。
*/
private CustomerCreditFlow executeAdjustCredit(CustomerCreditFlow cmd)
{
if (cmd == null || cmd.getCustomerId() == null)
{
throw new ServiceException("客户ID不能为空");
}
Long customerId = cmd.getCustomerId();
String creditType = cmd.getCreditType();
if (!CustomerCreditFlow.CREDIT_TYPE_CREDIT.equals(creditType)
&& !CustomerCreditFlow.CREDIT_TYPE_TEMP.equals(creditType))
{
throw new ServiceException("额度类型不正确");
}
String adjustMode = cmd.getAdjustMode();
if (!CustomerCreditFlow.MODE_SET.equals(adjustMode)
&& !CustomerCreditFlow.MODE_ADD.equals(adjustMode)
&& !CustomerCreditFlow.MODE_SUB.equals(adjustMode))
{
throw new ServiceException("调整方式不正确");
}
BigDecimal input = nz(cmd.getChangeAmount());
if (input.compareTo(BigDecimal.ZERO) <= 0)
{
throw new ServiceException("调整金额必须大于0");
}
if (!StringUtils.hasText(cmd.getReason()))
{
throw new ServiceException("请填写调整原因");
}
String reason = cmd.getReason().trim();
if (reason.length() > 255)
{
throw new ServiceException("调整原因不能超过255个字");
}
String username = SecurityUtils.getUsername();
Long userId = SecurityUtils.getUserId();
Long customerId = cmd.getCustomerId();
String creditType = cmd.getCreditType();
String adjustMode = cmd.getAdjustMode();
String reason = cmd.getReason();
boolean isTemp = CustomerCreditFlow.CREDIT_TYPE_TEMP.equals(creditType);
// 1. 行锁锁定CNY账户(不存在则按客户档案额度初始化创建);先处理临时额度过期
CustomerAccount account = getOrInitCnyAccount(customerId, username);
expireTempCreditIfNeeded(account);
// 1. 参数/日期/结果合法性校验并锁定CNY账户(不存在则按客户档案额度初始化);先处理临时额度过期
CustomerAccount account = validateAdjustCommand(cmd, username);
BigDecimal input = nz(cmd.getChangeAmount());
// 2. 计算调整结果
BigDecimal before = isTemp ? nz(account.getExtraCredit()) : nz(account.getTotalCredit());
BigDecimal otherCredit = isTemp ? nz(account.getTotalCredit()) : nz(account.getExtraCredit());
BigDecimal after;
switch (adjustMode)
{
@@ -329,40 +308,18 @@ public class CustomerMoneyServiceImpl implements ICustomerMoneyService
break;
}
after = after.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
if (after.compareTo(BigDecimal.ZERO) < 0)
{
throw new ServiceException("调整后额度不能为负数");
}
if (otherCredit.add(after).compareTo(nz(account.getUsedCredit())) < 0)
{
throw new ServiceException("调整后授信总额度不能小于已用额度:"
+ nz(account.getUsedCredit()).toPlainString());
}
if (after.compareTo(before) == 0)
{
throw new ServiceException("额度未发生变化,无需调整");
}
// 3. 临时额度有效期校验(不允许预约未来生效,失效日必须晚于生效日)
// 3. 临时额度有效期处理(不允许预约未来生效,失效日必须晚于生效日)
Date effectiveDate = null;
Date expireDate = null;
if (isTemp)
{
LocalDate today = LocalDate.now();
LocalDate eff = cmd.getEffectiveDate() != null ? toLocalDate(cmd.getEffectiveDate()) : today;
if (eff.isAfter(today))
{
throw new ServiceException("生效日期不能晚于今天,需要未来生效请在生效当天再设置");
}
effectiveDate = toSqlDate(eff);
if (cmd.getExpireDate() != null)
{
LocalDate exp = toLocalDate(cmd.getExpireDate());
if (!exp.isAfter(eff))
{
throw new ServiceException("失效日期必须晚于生效日期");
}
expireDate = toSqlDate(exp);
expireDate = toSqlDate(toLocalDate(cmd.getExpireDate()));
}
account.setExtraCredit(after);
account.setExtraEffectiveDate(effectiveDate);
@@ -408,7 +365,263 @@ public class CustomerMoneyServiceImpl implements ICustomerMoneyService
flow.setRemark(cmd.getRemark());
flow.setDelFlag("0");
customerMoneyMapper.insertCreditFlow(flow);
return 1;
return flow;
}
/* ================= 额度调整:申请 / 审批 ================= */
@Override
public List<CustomerCreditApply> selectCreditApplyList(CustomerCreditApply query)
{
return creditApplyMapper.selectApplyList(query);
}
@Override
public CustomerCreditApply selectCreditApplyById(Long applyId)
{
return creditApplyMapper.selectApplyById(applyId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int applyCreditAdjust(CustomerCreditApply apply)
{
if (apply == null || apply.getCustomerId() == null)
{
throw new ServiceException("客户ID不能为空");
}
// 构造内部调整命令,复用统一的参数/日期/额度合法性校验(只校验,不写入)
CustomerCreditFlow cmd = new CustomerCreditFlow();
cmd.setCustomerId(apply.getCustomerId());
cmd.setCreditType(apply.getCreditType());
cmd.setAdjustMode(apply.getAdjustMode());
cmd.setChangeAmount(nz(apply.getChangeAmount()));
cmd.setReason(apply.getReason());
cmd.setRemark(apply.getRemark());
cmd.setEffectiveDate(apply.getEffectiveDate());
cmd.setExpireDate(apply.getExpireDate());
String username = SecurityUtils.getUsername();
Long userId = SecurityUtils.getUserId();
// 1. 参数校验 + 日期校验 + 按当前账户额度预演结果,提前拦截不可能通过的申请(不写入账户)
validateAdjustCommand(cmd, username);
apply.setChangeAmount(cmd.getChangeAmount());
apply.setReason(cmd.getReason());
// 2. 待审批申请落库,不动账户额度
apply.setApplyNo(generateNo("EDSQ"));
apply.setStatus(CustomerCreditApply.STATUS_PENDING);
apply.setApplyBy(username);
apply.setApplyUserId(userId);
apply.setApplyTime(new Date());
apply.setCreateBy(username);
return creditApplyMapper.insertApply(apply);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int approveCreditApply(Long applyId, String approveRemark)
{
if (applyId == null)
{
throw new ServiceException("申请单ID不能为空");
}
CustomerCreditApply apply = creditApplyMapper.selectApplyByIdForUpdate(applyId);
if (apply == null)
{
throw new ServiceException("额度调整申请不存在");
}
if (!CustomerCreditApply.STATUS_PENDING.equals(apply.getStatus()))
{
throw new ServiceException("该申请已处理,不能重复审批");
}
String username = SecurityUtils.getUsername();
Long userId = SecurityUtils.getUserId();
// 以审批时账户的实际额度重新计算并调整(申请期间额度可能已变动)
CustomerCreditFlow cmd = new CustomerCreditFlow();
cmd.setCustomerId(apply.getCustomerId());
cmd.setCreditType(apply.getCreditType());
cmd.setAdjustMode(apply.getAdjustMode());
cmd.setChangeAmount(nz(apply.getChangeAmount()));
cmd.setReason(apply.getReason());
cmd.setRemark(apply.getRemark());
cmd.setEffectiveDate(apply.getEffectiveDate());
cmd.setExpireDate(apply.getExpireDate());
CustomerCreditFlow flow = executeAdjustCredit(cmd);
CustomerCreditApply update = new CustomerCreditApply();
update.setApplyId(applyId);
update.setAccountId(flow.getAccountId());
update.setFlowId(flow.getId());
update.setApproveBy(username);
update.setApproveUserId(userId);
update.setApproveRemark(approveRemark);
int rows = creditApplyMapper.updateApproved(update);
if (rows == 0)
{
throw new ServiceException("申请状态已变化,审批失败");
}
return rows;
}
@Override
@Transactional(rollbackFor = Exception.class)
public int rejectCreditApply(Long applyId, String rejectReason)
{
if (applyId == null)
{
throw new ServiceException("申请单ID不能为空");
}
if (!StringUtils.hasText(rejectReason))
{
throw new ServiceException("请填写驳回原因");
}
String reason = rejectReason.trim();
if (reason.length() > 255)
{
throw new ServiceException("驳回原因不能超过255个字");
}
CustomerCreditApply apply = creditApplyMapper.selectApplyByIdForUpdate(applyId);
if (apply == null)
{
throw new ServiceException("额度调整申请不存在");
}
if (!CustomerCreditApply.STATUS_PENDING.equals(apply.getStatus()))
{
throw new ServiceException("该申请已处理,不能重复审批");
}
CustomerCreditApply update = new CustomerCreditApply();
update.setApplyId(applyId);
update.setApproveBy(SecurityUtils.getUsername());
update.setApproveUserId(SecurityUtils.getUserId());
update.setApproveRemark(reason);
int rows = creditApplyMapper.updateRejected(update);
if (rows == 0)
{
throw new ServiceException("申请状态已变化,驳回失败");
}
return rows;
}
@Override
@Transactional(rollbackFor = Exception.class)
public int cancelCreditApply(Long applyId)
{
if (applyId == null)
{
throw new ServiceException("申请单ID不能为空");
}
CustomerCreditApply apply = creditApplyMapper.selectApplyByIdForUpdate(applyId);
if (apply == null)
{
throw new ServiceException("额度调整申请不存在");
}
if (!CustomerCreditApply.STATUS_PENDING.equals(apply.getStatus()))
{
throw new ServiceException("仅待审批的申请可以撤销");
}
int rows = creditApplyMapper.updateCancelled(applyId, SecurityUtils.getUsername());
if (rows == 0)
{
throw new ServiceException("申请状态已变化,撤销失败");
}
return rows;
}
/**
* 额度调整参数/日期/结果合法性校验(申请与审批共用)。
* 锁定CNY账户做预演但不写入,返回当前账户;规整后的金额/原因/日期回写到 cmd。
*/
private CustomerAccount validateAdjustCommand(CustomerCreditFlow cmd, String username)
{
if (cmd == null || cmd.getCustomerId() == null)
{
throw new ServiceException("客户ID不能为空");
}
String creditType = cmd.getCreditType();
if (!CustomerCreditFlow.CREDIT_TYPE_CREDIT.equals(creditType)
&& !CustomerCreditFlow.CREDIT_TYPE_TEMP.equals(creditType))
{
throw new ServiceException("额度类型不正确");
}
String adjustMode = cmd.getAdjustMode();
if (!CustomerCreditFlow.MODE_SET.equals(adjustMode)
&& !CustomerCreditFlow.MODE_ADD.equals(adjustMode)
&& !CustomerCreditFlow.MODE_SUB.equals(adjustMode))
{
throw new ServiceException("调整方式不正确");
}
BigDecimal input = nz(cmd.getChangeAmount());
if (input.compareTo(BigDecimal.ZERO) <= 0)
{
throw new ServiceException("调整金额必须大于0");
}
if (!StringUtils.hasText(cmd.getReason()))
{
throw new ServiceException("请填写调整原因");
}
String reason = cmd.getReason().trim();
if (reason.length() > 255)
{
throw new ServiceException("调整原因不能超过255个字");
}
cmd.setChangeAmount(input);
cmd.setReason(reason);
boolean isTemp = CustomerCreditFlow.CREDIT_TYPE_TEMP.equals(creditType);
CustomerAccount account = getOrInitCnyAccount(cmd.getCustomerId(), username);
expireTempCreditIfNeeded(account);
BigDecimal before = isTemp ? nz(account.getExtraCredit()) : nz(account.getTotalCredit());
BigDecimal otherCredit = isTemp ? nz(account.getTotalCredit()) : nz(account.getExtraCredit());
BigDecimal after;
switch (adjustMode)
{
case CustomerCreditFlow.MODE_SET:
after = input;
break;
case CustomerCreditFlow.MODE_ADD:
after = before.add(input);
break;
default:
after = before.subtract(input);
break;
}
after = after.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
if (after.compareTo(BigDecimal.ZERO) < 0)
{
throw new ServiceException("调整后额度不能为负数");
}
if (otherCredit.add(after).compareTo(nz(account.getUsedCredit())) < 0)
{
throw new ServiceException("调整后授信总额度不能小于已用额度:"
+ nz(account.getUsedCredit()).toPlainString());
}
if (after.compareTo(before) == 0)
{
throw new ServiceException("额度未发生变化,无需调整");
}
if (isTemp)
{
LocalDate today = LocalDate.now();
LocalDate eff = cmd.getEffectiveDate() != null ? toLocalDate(cmd.getEffectiveDate()) : today;
if (eff.isAfter(today))
{
throw new ServiceException("生效日期不能晚于今天,需要未来生效请在生效当天再提交");
}
if (cmd.getExpireDate() != null)
{
LocalDate exp = toLocalDate(cmd.getExpireDate());
if (!exp.isAfter(eff))
{
throw new ServiceException("失效日期必须晚于生效日期");
}
}
}
return account;
}
/* ================= 账单联动:额度占用 / 余额核销 ================= */