挂账接口

This commit is contained in:
2026-04-28 15:03:16 +08:00
parent 1d3fce4523
commit ab7598d706
9 changed files with 123 additions and 224 deletions

View File

@@ -2,6 +2,7 @@ package com.czg.controller.admin;
import com.czg.log.annotation.OperationLog; import com.czg.log.annotation.OperationLog;
import com.czg.order.dto.CreditBuyerDTO; import com.czg.order.dto.CreditBuyerDTO;
import com.czg.order.entity.CreditBuyer;
import com.czg.order.param.CreditBuyerQueryParam; import com.czg.order.param.CreditBuyerQueryParam;
import com.czg.order.param.CreditBuyerRepaymentParam; import com.czg.order.param.CreditBuyerRepaymentParam;
import com.czg.order.service.CreditBuyerService; import com.czg.order.service.CreditBuyerService;
@@ -43,10 +44,9 @@ public class CreditBuyerController {
@GetMapping("{id}") @GetMapping("{id}")
@OperationLog("挂账人-详情") @OperationLog("挂账人-详情")
//@SaAdminCheckPermission("creditBuyer:info") //@SaAdminCheckPermission("creditBuyer:info")
public CzgResult<CreditBuyerDTO> getCreditBuyerById(@PathVariable("id") String id) { public CzgResult<CreditBuyer> getCreditBuyerById(@PathVariable("id") String id) {
AssertUtil.isNull(id, "{}不能为空", "id"); AssertUtil.isNull(id, "{}不能为空", "id");
CreditBuyerDTO data = creditBuyerService.getCreditBuyerById(id); return CzgResult.success(creditBuyerService.getById(id));
return CzgResult.success(data);
} }
/** /**
@@ -55,7 +55,7 @@ public class CreditBuyerController {
@PostMapping @PostMapping
@OperationLog("挂账人-新增") @OperationLog("挂账人-新增")
//@SaAdminCheckPermission("creditBuyer:add") //@SaAdminCheckPermission("creditBuyer:add")
public CzgResult<Void> addCreditBuyer(@RequestBody CreditBuyerDTO dto) { public CzgResult<Void> addCreditBuyer(@RequestBody CreditBuyer dto) {
creditBuyerService.addCreditBuyer(dto); creditBuyerService.addCreditBuyer(dto);
return CzgResult.success(); return CzgResult.success();
} }
@@ -66,7 +66,7 @@ public class CreditBuyerController {
@PutMapping @PutMapping
@OperationLog("挂账人-修改") @OperationLog("挂账人-修改")
//@SaAdminCheckPermission("creditBuyer:update") //@SaAdminCheckPermission("creditBuyer:update")
public CzgResult<Void> updateCreditBuyer(@RequestBody CreditBuyerDTO dto) { public CzgResult<Void> updateCreditBuyer(@RequestBody CreditBuyer dto) {
creditBuyerService.updateCreditBuyer(dto); creditBuyerService.updateCreditBuyer(dto);
return CzgResult.success(); return CzgResult.success();
} }

View File

@@ -76,4 +76,14 @@ public class CreditBuyer implements Serializable {
* 删除标志 0-正常 1-删除 * 删除标志 0-正常 1-删除
*/ */
private Integer isDel; private Integer isDel;
/**
* 还款提醒日 最大28
*/
private Integer expireRemindDay;
/**
* 还款提醒 提醒多久以前的数据 暂定单位 月
*/
private Integer expireRemind;
} }

View File

@@ -36,10 +36,18 @@ public class CreditBuyerOrder implements Serializable {
* 挂账人编码 * 挂账人编码
*/ */
private String creditBuyerId; private String creditBuyerId;
/**
* 订单金额
*/
private BigDecimal orderAmount;
/** /**
* 已付金额 * 已付金额
*/ */
private BigDecimal paidAmount; private BigDecimal paidAmount;
/**
* 退款金额
*/
private BigDecimal refundAmount;
/** /**
* 状态 unpaid-未付款 partial-部分支付 paid-已付款 * 状态 unpaid-未付款 partial-部分支付 paid-已付款
*/ */

View File

@@ -3,6 +3,7 @@ package com.czg.order.service;
import com.czg.order.dto.CreditBuyerOrderDTO; import com.czg.order.dto.CreditBuyerOrderDTO;
import com.czg.order.entity.CreditBuyerOrder; import com.czg.order.entity.CreditBuyerOrder;
import com.czg.order.entity.CreditPaymentRecord; import com.czg.order.entity.CreditPaymentRecord;
import com.czg.order.entity.OrderInfo;
import com.czg.order.param.CreditBuyerOrderQueryParam; import com.czg.order.param.CreditBuyerOrderQueryParam;
import com.czg.order.vo.CreditBuyerOrderSummaryVo; import com.czg.order.vo.CreditBuyerOrderSummaryVo;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
@@ -46,17 +47,7 @@ public interface CreditBuyerOrderService extends IService<CreditBuyerOrder> {
* @param creditBuyerId 挂账人id * @param creditBuyerId 挂账人id
* @param orderId 订单id * @param orderId 订单id
*/ */
boolean save(String creditBuyerId, Long orderId); boolean save(OrderInfo orderInfo, String creditBuyerId, Long orderId);
/**
* 挂账人退款(整单退款)
*
* @param creditBuyerId 挂账人id
* @param orderId 订单id
* @return
*/
@Deprecated
boolean refund(String creditBuyerId, Long orderId);
/** /**
@@ -67,6 +58,6 @@ public interface CreditBuyerOrderService extends IService<CreditBuyerOrder> {
* @param refundAmount 退款金额 * @param refundAmount 退款金额
* @return * @return
*/ */
boolean partRefund(String creditBuyerId, Long orderId, BigDecimal refundAmount); boolean partRefund(String creditBuyerId, Long orderId, String orderNo, BigDecimal refundAmount);
} }

View File

@@ -17,11 +17,10 @@ import com.mybatisflex.core.service.IService;
public interface CreditBuyerService extends IService<CreditBuyer> { public interface CreditBuyerService extends IService<CreditBuyer> {
Page<CreditBuyerDTO> getCreditBuyerPage(CreditBuyerQueryParam param); Page<CreditBuyerDTO> getCreditBuyerPage(CreditBuyerQueryParam param);
CreditBuyerDTO getCreditBuyerById(String id);
void addCreditBuyer(CreditBuyerDTO dto); void addCreditBuyer(CreditBuyer dto);
void updateCreditBuyer(CreditBuyerDTO dto); void updateCreditBuyer(CreditBuyer dto);
void deleteCreditBuyer(String id); void deleteCreditBuyer(String id);

View File

@@ -25,14 +25,14 @@ public class CreditBuyerRepaymentVo implements Serializable {
* 还款金额 * 还款金额
*/ */
private BigDecimal repaymentAmount; private BigDecimal repaymentAmount;
/** // /**
* 支付欠款 // * 支付欠款
*/ // */
private BigDecimal payAmount; // private BigDecimal payAmount;
/** // /**
* 转存金额 // * 转存金额
*/ // */
private BigDecimal rechargeAmount; // private BigDecimal rechargeAmount;
/** /**
* 还款反馈消息 * 还款反馈消息
*/ */

View File

@@ -7,7 +7,6 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.czg.constants.SystemConstants; import com.czg.constants.SystemConstants;
import com.czg.exception.CzgException; import com.czg.exception.CzgException;
import com.czg.order.dto.CreditBuyerDTO;
import com.czg.order.dto.CreditBuyerOrderDTO; import com.czg.order.dto.CreditBuyerOrderDTO;
import com.czg.order.entity.CreditBuyer; import com.czg.order.entity.CreditBuyer;
import com.czg.order.entity.CreditBuyerOrder; import com.czg.order.entity.CreditBuyerOrder;
@@ -20,7 +19,6 @@ import com.czg.order.vo.CreditBuyerOrderSummaryVo;
import com.czg.service.order.mapper.CreditBuyerMapper; import com.czg.service.order.mapper.CreditBuyerMapper;
import com.czg.service.order.mapper.CreditBuyerOrderMapper; import com.czg.service.order.mapper.CreditBuyerOrderMapper;
import com.czg.service.order.mapper.CreditPaymentRecordMapper; import com.czg.service.order.mapper.CreditPaymentRecordMapper;
import com.czg.service.market.mapper.OrderInfoMapper;
import com.czg.utils.PageUtil; import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
@@ -53,8 +51,6 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
@Resource @Resource
@Lazy @Lazy
private CreditBuyerService creditBuyerService; private CreditBuyerService creditBuyerService;
@Resource
private OrderInfoMapper orderInfoMapper;
@Override @Override
public Page<CreditBuyerOrderDTO> getCreditBuyerOrderPage(CreditBuyerOrderQueryParam param) { public Page<CreditBuyerOrderDTO> getCreditBuyerOrderPage(CreditBuyerOrderQueryParam param) {
@@ -140,11 +136,11 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean save(String creditBuyerId, Long orderId) { public boolean save(OrderInfo orderInfo, String creditBuyerId, Long orderId) {
if (StrUtil.isBlank(creditBuyerId)) { if (StrUtil.isBlank(creditBuyerId)) {
throw new CzgException("挂账人id不能为空"); throw new CzgException("挂账人id不能为空");
} }
CreditBuyerDTO creditBuyer = creditBuyerService.getCreditBuyerById(creditBuyerId); CreditBuyer creditBuyer = creditBuyerService.getById(creditBuyerId);
if (creditBuyer == null) { if (creditBuyer == null) {
throw new CzgException("挂账人不存在"); throw new CzgException("挂账人不存在");
} }
@@ -156,84 +152,47 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
if (status != null && status == SystemConstants.OneZero.ZERO) { if (status != null && status == SystemConstants.OneZero.ZERO) {
throw new CzgException("挂账人已被停用"); throw new CzgException("挂账人已被停用");
} }
OrderInfo orderInfo = orderInfoMapper.selectOneById(orderId);
if (orderInfo == null) { if (orderInfo == null) {
throw new CzgException("订单不存在"); throw new CzgException("订单不存在");
} }
// 账户余额 // 账户余额
BigDecimal accountBalance = creditBuyer.getAccountBalance(); BigDecimal accountBalance = creditBuyer.getAccountBalance();
// 如果有余额的话从余额里面扣除没有余额的话从信用额度里面扣除余额和信用额度都为0则不允许挂账余额+信用额度刚好够支付这笔订单的话需要同时减余额减信用额度 boolean greater = NumberUtil.isGreater(orderInfo.getOrderAmount(), accountBalance);
if (NumberUtil.isGreaterOrEqual(accountBalance, orderInfo.getOrderAmount())) { if (greater) {
// 减余额 throw new CzgException(StrUtil.format("剩余可挂账金额{} ,不足{}元", accountBalance, orderInfo.getOrderAmount()));
creditBuyer.setAccountBalance(NumberUtil.sub(accountBalance, orderInfo.getOrderAmount())); }
CreditBuyer dbRecord = BeanUtil.copyProperties(creditBuyer, CreditBuyer.class); // 减余额
creditBuyerMapper.update(dbRecord); creditBuyer.setAccountBalance(NumberUtil.sub(accountBalance, orderInfo.getOrderAmount()));
// 记录还款记录 CreditBuyer upCreditBuyer = new CreditBuyer();
CreditPaymentRecord record = new CreditPaymentRecord(); upCreditBuyer.setId(creditBuyerId);
record.setCreditBuyerId(creditBuyerId); upCreditBuyer.setAccountBalance(creditBuyer.getAccountBalance());
record.setOrderId(orderId); creditBuyerMapper.update(upCreditBuyer);
record.setRepaymentAmount(orderInfo.getOrderAmount());
record.setPaymentMethod("余额支付"); CreditBuyerOrder entity = new CreditBuyerOrder();
record.setPaymentTime(LocalDateTime.now()); entity.setCreditBuyerId(creditBuyerId);
record.setRemark("挂账时余额充足,直接从余额扣除"); entity.setOrderId(orderId);
creditPaymentRecordMapper.insert(record); entity.setOrderAmount(orderInfo.getOrderAmount());
CreditBuyerOrder entity = new CreditBuyerOrder(); //减去订单金额 余额仍大于挂账额度 则全额付款
entity.setCreditBuyerId(creditBuyerId); if (NumberUtil.isGreaterOrEqual(creditBuyer.getAccountBalance(), creditBuyer.getCreditAmount())) {
entity.setOrderId(orderId);
entity.setPaidAmount(orderInfo.getOrderAmount()); entity.setPaidAmount(orderInfo.getOrderAmount());
entity.setStatus("paid"); entity.setStatus("paid");
entity.setLastPaymentTime(LocalDateTime.now()); } else {
entity.setLastPaymentMethod(record.getPaymentMethod()); if(NumberUtil.isGreater(accountBalance, creditBuyer.getCreditAmount())){
entity.setRemark(record.getRemark()); entity.setStatus("partial");
return super.save(entity); entity.setPaidAmount(NumberUtil.sub(accountBalance, creditBuyer.getCreditAmount()));
}else {
entity.setStatus("unpaid");
entity.setPaidAmount(BigDecimal.ZERO);
}
} }
CreditBuyerOrder entity = null; entity.setRefundAmount(BigDecimal.ZERO);
if (NumberUtil.isGreater(accountBalance, BigDecimal.ZERO)) { entity.setLastPaymentTime(LocalDateTime.now());
// 减余额 return save(entity);
creditBuyer.setAccountBalance(BigDecimal.ZERO);
CreditBuyer dbRecord = BeanUtil.copyProperties(creditBuyer, CreditBuyer.class);
creditBuyerMapper.update(dbRecord);
// 记录还款记录
CreditPaymentRecord record = new CreditPaymentRecord();
record.setCreditBuyerId(creditBuyerId);
record.setOrderId(orderId);
record.setRepaymentAmount(accountBalance);
record.setPaymentMethod("余额支付");
record.setPaymentTime(LocalDateTime.now());
record.setRemark("挂账时余额不足,先扣除现有余额,其他的从挂账额度中扣除");
creditPaymentRecordMapper.insert(record);
entity = new CreditBuyerOrder();
entity.setCreditBuyerId(creditBuyerId);
entity.setOrderId(orderId);
entity.setPaidAmount(accountBalance);
entity.setStatus("partial");
entity.setLastPaymentTime(LocalDateTime.now());
entity.setLastPaymentMethod(record.getPaymentMethod());
entity.setRemark(record.getRemark());
//super.save(entity);
orderInfo.setPayAmount(NumberUtil.sub(orderInfo.getOrderAmount(), accountBalance));
}
// 剩余挂账额度
BigDecimal remainingAmount = creditBuyer.getRemainingAmount();
// 验证挂账金额是否大于剩余额度
boolean greater = NumberUtil.isGreater(orderInfo.getOrderAmount(), remainingAmount);
if (greater) {
throw new CzgException(StrUtil.format("{}:¥{}不能大于剩余挂账额度({})", "挂账金额", orderInfo.getOrderAmount(), remainingAmount));
}
if (entity == null) {
entity = new CreditBuyerOrder();
entity.setStatus("unpaid");
entity.setPaidAmount(BigDecimal.ZERO);
entity.setCreditBuyerId(creditBuyerId);
entity.setOrderId(orderId);
}
return super.saveOrUpdate(entity);
} }
@Override @Override
@Deprecated
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean refund(String creditBuyerId, Long orderId) { public boolean partRefund(String creditBuyerId, Long orderId, String orderNo, BigDecimal refundAmount) {
if (StrUtil.isBlank(creditBuyerId)) { if (StrUtil.isBlank(creditBuyerId)) {
throw new CzgException("挂账人id不能为空"); throw new CzgException("挂账人id不能为空");
} }
@@ -241,66 +200,12 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
if (creditBuyer == null) { if (creditBuyer == null) {
throw new CzgException("挂账人不存在"); throw new CzgException("挂账人不存在");
} }
OrderInfo orderInfo = orderInfoMapper.selectOneById(orderId);
if (orderInfo == null) {
throw new CzgException("订单不存在");
}
CreditBuyerOrderQueryParam param = new CreditBuyerOrderQueryParam(); CreditBuyerOrderQueryParam param = new CreditBuyerOrderQueryParam();
param.setCreditBuyerId(creditBuyerId); param.setCreditBuyerId(creditBuyerId);
param.setOrderId(Convert.toStr(orderId)); param.setOrderId(Convert.toStr(orderId));
CreditBuyerOrderDTO dto = super.mapper.getOne(param); CreditBuyerOrder creditBuyerOrder = getOne(query().eq(CreditBuyerOrder::getCreditBuyerId, creditBuyerId).eq(CreditBuyerOrder::getOrderId, orderId));
if (dto == null) { if (creditBuyerOrder == null) {
throw new CzgException("挂账订单不存在");
}
// 1.只挂账未还款的情况,直接删除挂账订单
if ("unpaid".equals(dto.getStatus())) {
super.mapper.deleteById(dto.getId());
return true;
}
// 2.部分还款/已还款,删除挂账订单+红冲还款记录,并把已还款金额退回余额或挂账额度
if ("partial".equals(dto.getStatus()) || "paid".equals(dto.getStatus())) {
// 已还款金额
BigDecimal paidAmount = dto.getPaidAmount();
// 已还款金额进行红冲
CreditPaymentRecord record = new CreditPaymentRecord();
record.setCreditBuyerId(creditBuyerId);
record.setOrderId(orderId);
record.setRepaymentAmount(NumberUtil.sub(BigDecimal.ZERO, paidAmount));
record.setPaymentMethod("挂账退款");
record.setPaymentTime(LocalDateTime.now());
record.setRemark(StrUtil.format("挂账订单:{}申请退款,已归还挂账额度或账户余额", orderInfo.getOrderNo()));
creditPaymentRecordMapper.insert(record);
// 删除挂账订单,恢复挂账额度
super.mapper.deleteById(dto.getId());
// 退回余额
creditBuyer.setAccountBalance(NumberUtil.add(creditBuyer.getAccountBalance(), paidAmount));
creditBuyerService.updateById(creditBuyer);
return true;
}
return false;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean partRefund(String creditBuyerId, Long orderId, BigDecimal refundAmount) {
if (StrUtil.isBlank(creditBuyerId)) {
throw new CzgException("挂账人id不能为空");
}
CreditBuyerDTO creditBuyer = creditBuyerService.getCreditBuyerById(creditBuyerId);
if (creditBuyer == null) {
throw new CzgException("挂账人不存在");
}
OrderInfo orderInfo = orderInfoMapper.selectOneById(orderId);
if (orderInfo == null) {
throw new CzgException("订单不存在");
}
CreditBuyerOrderQueryParam param = new CreditBuyerOrderQueryParam();
param.setCreditBuyerId(creditBuyerId);
param.setOrderId(Convert.toStr(orderId));
CreditBuyerOrderDTO dto = mapper.getOne(param);
if (dto == null) {
throw new CzgException("挂账订单不存在"); throw new CzgException("挂账订单不存在");
} }
// 已还款金额进行红冲 // 已还款金额进行红冲
@@ -310,10 +215,10 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
record.setRepaymentAmount(NumberUtil.sub(BigDecimal.ZERO, refundAmount)); record.setRepaymentAmount(NumberUtil.sub(BigDecimal.ZERO, refundAmount));
record.setPaymentMethod("挂账退款"); record.setPaymentMethod("挂账退款");
record.setPaymentTime(LocalDateTime.now()); record.setPaymentTime(LocalDateTime.now());
record.setRemark(StrUtil.format("挂账订单:{},申请退款¥{}元,已恢复挂账额度。", orderInfo.getOrderNo(), refundAmount)); record.setRemark(StrUtil.format("挂账订单:{},申请退款¥{}元,已恢复挂账额度。", orderNo, refundAmount));
creditPaymentRecordMapper.insert(record); creditPaymentRecordMapper.insert(record);
dto = mapper.getOne(param); //退款金额减去 待支付金额
BigDecimal sub = NumberUtil.sub(refundAmount, dto.getUnpaidAmount()); BigDecimal sub = NumberUtil.sub(refundAmount, NumberUtil.sub(creditBuyerOrder.getOrderAmount(), creditBuyerOrder.getPaidAmount()));
if (NumberUtil.isGreater(sub, BigDecimal.ZERO)) { if (NumberUtil.isGreater(sub, BigDecimal.ZERO)) {
CreditPaymentRecord flow = new CreditPaymentRecord(); CreditPaymentRecord flow = new CreditPaymentRecord();
flow.setCreditBuyerId(creditBuyerId); flow.setCreditBuyerId(creditBuyerId);
@@ -321,16 +226,24 @@ public class CreditBuyerOrderServiceImpl extends ServiceImpl<CreditBuyerOrderMap
flow.setRepaymentAmount(sub); flow.setRepaymentAmount(sub);
flow.setPaymentMethod("转储余额"); flow.setPaymentMethod("转储余额");
flow.setPaymentTime(LocalDateTime.now()); flow.setPaymentTime(LocalDateTime.now());
flow.setRemark(StrUtil.format("挂账订单:{},申请退款¥{}元,由于此挂账订单已提前还款,溢出部分¥{}元将转储至账户余额。", orderInfo.getOrderNo(), refundAmount, sub)); flow.setRemark(StrUtil.format("挂账订单:{},申请退款¥{}元,由于此挂账订单已提前还款,溢出部分¥{}元将转储至账户余额。", orderNo, refundAmount, sub));
creditPaymentRecordMapper.insert(flow); creditPaymentRecordMapper.insert(flow);
UpdateChain.of(CreditBuyerOrder.class) UpdateChain.of(CreditBuyerOrder.class)
.set(CreditBuyerOrder::getPaidAmount, NumberUtil.sub(dto.getPaidAmount(), sub)) .set(CreditBuyerOrder::getPaidAmount, NumberUtil.sub(creditBuyerOrder.getPaidAmount(), sub))
.eq(CreditBuyerOrder::getId, dto.getId()) .set(CreditBuyerOrder::getRefundAmount, NumberUtil.add(creditBuyerOrder.getRefundAmount(), sub))
.eq(CreditBuyerOrder::getId, creditBuyerOrder.getId())
.update(); .update();
// 退回余额 // 退回余额
creditBuyer.setAccountBalance(NumberUtil.add(creditBuyer.getAccountBalance(), sub)); creditBuyer.setAccountBalance(NumberUtil.add(creditBuyer.getAccountBalance(), sub));
CreditBuyer entity = BeanUtil.copyProperties(dto, CreditBuyer.class); CreditBuyer entity = new CreditBuyer();
entity.setId(creditBuyer.getId());
entity.setAccountBalance(creditBuyer.getAccountBalance());
creditBuyerService.updateById(entity); creditBuyerService.updateById(entity);
} else {
UpdateChain.of(CreditBuyerOrder.class)
.set(CreditBuyerOrder::getRefundAmount, NumberUtil.add(creditBuyerOrder.getRefundAmount(), sub))
.eq(CreditBuyerOrder::getId, creditBuyerOrder.getId())
.update();
} }
return true; return true;
} }

View File

@@ -65,7 +65,7 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
return PageUtil.convert(pageInfo); return PageUtil.convert(pageInfo);
} }
private void commonVerify(CreditBuyerDTO dto) { private void commonVerify(CreditBuyer dto) {
try { try {
Assert.notNull(dto.getShopId(), "{}({})不能为空", "店铺id", "shopId"); Assert.notNull(dto.getShopId(), "{}({})不能为空", "店铺id", "shopId");
Assert.notNull(dto.getStatus(), "{}({})不能为空", "状态", "status"); Assert.notNull(dto.getStatus(), "{}({})不能为空", "状态", "status");
@@ -83,21 +83,9 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
} }
} }
@Override
public CreditBuyerDTO getCreditBuyerById(String id) {
Long shopId = StpKit.USER.getShopId(0L);
CreditBuyerQueryParam param = new CreditBuyerQueryParam();
param.setId(id);
param.setShopId(shopId);
List<CreditBuyerDTO> list = super.mapper.findCreditBuyerList(param);
if (CollUtil.isEmpty(list)) {
return null;
}
return list.getFirst();
}
@Override @Override
public void addCreditBuyer(CreditBuyerDTO dto) { public void addCreditBuyer(CreditBuyer dto) {
Long shopId = StpKit.USER.getShopId(0L); Long shopId = StpKit.USER.getShopId(0L);
dto.setShopId(shopId); dto.setShopId(shopId);
commonVerify(dto); commonVerify(dto);
@@ -109,13 +97,13 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
if (!ArrayUtil.contains(new String[]{"total", "order"}, dto.getRepaymentMethod())) { if (!ArrayUtil.contains(new String[]{"total", "order"}, dto.getRepaymentMethod())) {
throw new CzgException(StrUtil.format("{}({})不合法", "还款方式", "repaymentMethod")); throw new CzgException(StrUtil.format("{}({})不合法", "还款方式", "repaymentMethod"));
} }
dto.setAccountBalance(BigDecimal.ZERO); dto.setAccountBalance(dto.getCreditAmount());
CreditBuyer entity = BeanUtil.copyProperties(dto, CreditBuyer.class); CreditBuyer entity = BeanUtil.copyProperties(dto, CreditBuyer.class);
super.save(entity); super.save(entity);
} }
@Override @Override
public void updateCreditBuyer(CreditBuyerDTO dto) { public void updateCreditBuyer(CreditBuyer dto) {
try { try {
Assert.notEmpty(dto.getId(), "{}不能为空", "id"); Assert.notEmpty(dto.getId(), "{}不能为空", "id");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@@ -128,18 +116,27 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
if (entity == null) { if (entity == null) {
throw new CzgException("挂账人不存在"); throw new CzgException("挂账人不存在");
} }
CreditBuyerDTO record = getCreditBuyerById(dto.getId()); //余额减去挂账金额 100 60 挂账40 修改后 如果是 80
// 验证挂账额度是否小于已挂账金额 BigDecimal sub = NumberUtil.sub(dto.getCreditAmount(), entity.getCreditAmount());
boolean less = NumberUtil.isLess(dto.getCreditAmount(), NumberUtil.nullToZero(record.getOwedAmount())); //修改后的余额
if (less) { BigDecimal balance = NumberUtil.add(entity.getAccountBalance(), sub);
throw new CzgException(StrUtil.format("{}({})不能小于已挂账金额({})", "挂账额度", "creditAmount", record.getOwedAmount())); if (NumberUtil.isLess(balance, BigDecimal.ZERO)) {
throw new CzgException("挂账额度不能小于已挂账金额");
} }
BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(false).setIgnoreProperties("repaymentMethod", "accountBalance")); BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(false).setIgnoreProperties("repaymentMethod", "accountBalance"));
super.updateById(entity); entity.setAccountBalance(balance);
updateById(entity);
} }
@Override @Override
public void deleteCreditBuyer(String id) { public void deleteCreditBuyer(String id) {
CreditBuyer entity = getById(id);
if (entity == null) {
throw new CzgException("挂账人不存在");
}
if (NumberUtil.isLess(entity.getAccountBalance(), entity.getCreditAmount())) {
throw new CzgException("删除失败,有未还款挂账金额。");
}
Long shopId = StpKit.USER.getShopId(0L); Long shopId = StpKit.USER.getShopId(0L);
UpdateChain.of(CreditBuyer.class) UpdateChain.of(CreditBuyer.class)
.set(CreditBuyer::getIsDel, SystemConstants.OneZero.ONE) .set(CreditBuyer::getIsDel, SystemConstants.OneZero.ONE)
@@ -160,7 +157,7 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new CzgException(e.getMessage()); throw new CzgException(e.getMessage());
} }
CreditBuyerDTO dto = getCreditBuyerById(param.getId()); CreditBuyer dto = getById(param.getId());
if (dto == null) { if (dto == null) {
throw new CzgException("挂账人不存在"); throw new CzgException("挂账人不存在");
} }
@@ -174,44 +171,28 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
if (NumberUtil.isLess(repaymentAmount, BigDecimal.ZERO)) { if (NumberUtil.isLess(repaymentAmount, BigDecimal.ZERO)) {
throw new CzgException("还款金额不能小于0"); throw new CzgException("还款金额不能小于0");
} }
BigDecimal initRepaymentAmount = NumberUtil.add(repaymentAmount, BigDecimal.ZERO);
// 已挂账金额 CreditBuyer upEntity = new CreditBuyer();
BigDecimal owedAmount = dto.getOwedAmount(); upEntity.setId(param.getId());
if (NumberUtil.equals(owedAmount, BigDecimal.ZERO)) { upEntity.setAccountBalance(NumberUtil.add(dto.getAccountBalance(), repaymentAmount));
dto.setAccountBalance(NumberUtil.add(dto.getAccountBalance(), repaymentAmount)); super.updateById(upEntity);
CreditBuyer entity = BeanUtil.copyProperties(dto, CreditBuyer.class);
super.updateById(entity); CreditPaymentRecord record2 = new CreditPaymentRecord();
CreditPaymentRecord record = new CreditPaymentRecord(); record2.setCreditBuyerId(param.getId());
record.setCreditBuyerId(param.getId()); record2.setRepaymentAmount(repaymentAmount);
record.setRepaymentAmount(repaymentAmount); record2.setPaymentMethod(param.getPaymentMethod());
record.setPaymentMethod(param.getPaymentMethod()); record2.setPaymentTime(LocalDateTime.now());
record.setPaymentTime(LocalDateTime.now()); record2.setRemark(param.getRemark());
record.setRemark(param.getRemark()); creditPaymentRecordMapper.insert(record2);
creditPaymentRecordMapper.insert(record); if (NumberUtil.isGreaterOrEqual(dto.getAccountBalance(), dto.getCreditAmount())) {
CreditBuyerRepaymentVo result = new CreditBuyerRepaymentVo(); CreditBuyerRepaymentVo result = new CreditBuyerRepaymentVo();
result.setRepaymentCount(0); result.setRepaymentCount(0);
result.setRepaymentAmount(repaymentAmount); result.setRepaymentAmount(repaymentAmount);
result.setRepaymentMsg(StrUtil.format("账单无需还款,{}元已转储至余额。", repaymentAmount)); result.setRepaymentMsg("账单无需还款,已转储至余额。");
return result; return result;
} }
// 转存余额
BigDecimal rechargeAmount = BigDecimal.ZERO;
if (NumberUtil.isGreater(repaymentAmount, owedAmount)) {
rechargeAmount = NumberUtil.sub(repaymentAmount, owedAmount);
dto.setAccountBalance(NumberUtil.add(dto.getAccountBalance(), rechargeAmount));
CreditBuyer entity = BeanUtil.copyProperties(dto, CreditBuyer.class);
super.updateById(entity);
CreditPaymentRecord record = new CreditPaymentRecord();
record.setCreditBuyerId(param.getId());
record.setRepaymentAmount(rechargeAmount);
record.setPaymentMethod(param.getPaymentMethod());
record.setPaymentTime(LocalDateTime.now());
record.setRemark(param.getRemark());
creditPaymentRecordMapper.insert(record);
}
// 校验完毕,可以批量还款 // 校验完毕,可以批量还款
CreditBuyerOrderQueryParam where = new CreditBuyerOrderQueryParam(); CreditBuyerOrderQueryParam where = new CreditBuyerOrderQueryParam();
where.setCreditBuyerId(param.getId()); where.setCreditBuyerId(param.getId());
where.setStatusList(List.of("unpaid", "partial")); where.setStatusList(List.of("unpaid", "partial"));
@@ -271,12 +252,9 @@ public class CreditBuyerServiceImpl extends ServiceImpl<CreditBuyerMapper, Credi
} }
} }
CreditBuyerRepaymentVo result = new CreditBuyerRepaymentVo(); CreditBuyerRepaymentVo result = new CreditBuyerRepaymentVo();
BigDecimal payAmount = NumberUtil.sub(initRepaymentAmount, repaymentAmount);
result.setRepaymentCount(repaymentCount); result.setRepaymentCount(repaymentCount);
result.setRepaymentAmount(initRepaymentAmount); result.setRepaymentMsg(StrUtil.format("共计还款{}笔,还款金额:{}元,当前余额:{}元。",
result.setPayAmount(payAmount); repaymentCount, NumberUtil.sub(param.getRepaymentAmount(), repaymentAmount), upEntity.getAccountBalance()));
result.setRechargeAmount(rechargeAmount);
result.setRepaymentMsg(StrUtil.format("共计还款{}笔,还款金额:{}元,支付欠款:{}元,转存余额:{}元,当前余额:{}元。", repaymentCount, initRepaymentAmount, payAmount, rechargeAmount, dto.getAccountBalance()));
return result; return result;
} }

View File

@@ -130,7 +130,7 @@ public class OrderPayServiceImpl implements OrderPayService {
orderInfoCustomService.upOrderInfo(orderInfo, orderInfo.getOrderAmount(), orderInfoCustomService.upOrderInfo(orderInfo, orderInfo.getOrderAmount(),
LocalDateTime.now(), null, PayEnums.CREDIT_PAY); LocalDateTime.now(), null, PayEnums.CREDIT_PAY);
//挂账后续逻辑 //挂账后续逻辑
buyerOrderService.save(payParam.getCreditBuyerId().toString(), orderInfo.getId()); buyerOrderService.save(orderInfo, payParam.getCreditBuyerId().toString(), orderInfo.getId());
return CzgResult.success(); return CzgResult.success();
} }
@@ -498,7 +498,7 @@ public class OrderPayServiceImpl implements OrderPayService {
shopUserService.updateMoney(shopUserMoneyEditDTO); shopUserService.updateMoney(shopUserMoneyEditDTO);
} else if (orderInfo.getPayType().equals(PayEnums.CREDIT_PAY.getValue())) { } else if (orderInfo.getPayType().equals(PayEnums.CREDIT_PAY.getValue())) {
AssertUtil.isNull(orderInfo.getCreditBuyerId(), "挂账单退款失败,未查询到挂账人"); AssertUtil.isNull(orderInfo.getCreditBuyerId(), "挂账单退款失败,未查询到挂账人");
buyerOrderService.partRefund(orderInfo.getCreditBuyerId().toString(), orderInfo.getId(), param.getRefundAmount()); buyerOrderService.partRefund(orderInfo.getCreditBuyerId().toString(), orderInfo.getId(), orderInfo.getOrderNo(), param.getRefundAmount());
} else if (!orderInfo.getPayType().equals(PayEnums.CASH_PAY.getValue())) { } else if (!orderInfo.getPayType().equals(PayEnums.CASH_PAY.getValue())) {
//退款 param.getRefundAmount() //退款 param.getRefundAmount()
if (orderInfo.getPayOrderId() == null) { if (orderInfo.getPayOrderId() == null) {