|
|
@@ -1,7 +1,13 @@
|
|
|
package com.zsElectric.boot.business.service.impl;
|
|
|
|
|
|
+import com.zsElectric.boot.business.mapper.FirmAccountLogMapper;
|
|
|
+import com.zsElectric.boot.business.model.entity.FirmAccountLog;
|
|
|
+import com.zsElectric.boot.business.model.form.FirmBalanceChangeForm;
|
|
|
+import com.zsElectric.boot.core.exception.BusinessException;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
@@ -13,9 +19,12 @@ import com.zsElectric.boot.business.model.query.FirmInfoQuery;
|
|
|
import com.zsElectric.boot.business.model.vo.FirmInfoVO;
|
|
|
import com.zsElectric.boot.business.converter.FirmInfoConverter;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
@@ -26,11 +35,13 @@ import cn.hutool.core.util.StrUtil;
|
|
|
* @author zsElectric
|
|
|
* @since 2025-12-11 10:10
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
public class FirmInfoServiceImpl extends ServiceImpl<FirmInfoMapper, FirmInfo> implements FirmInfoService {
|
|
|
|
|
|
private final FirmInfoConverter firmInfoConverter;
|
|
|
+ private final FirmAccountLogMapper firmAccountLogMapper;
|
|
|
|
|
|
/**
|
|
|
* 获取企业信息分页列表
|
|
|
@@ -100,4 +111,100 @@ public class FirmInfoServiceImpl extends ServiceImpl<FirmInfoMapper, FirmInfo> i
|
|
|
return this.removeByIds(idList);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上账(充值)- 增加企业余额
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean recharge(Long firmId, BigDecimal amount, String remark) {
|
|
|
+ FirmBalanceChangeForm form = new FirmBalanceChangeForm();
|
|
|
+ form.setFirmId(firmId);
|
|
|
+ form.setAmount(amount);
|
|
|
+ form.setIncomeType(1); // 转入
|
|
|
+ form.setChangeType(1); // 充值
|
|
|
+ form.setRemark(remark);
|
|
|
+ return changeBalance(form);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下账(扣款)- 减少企业余额
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean deduct(Long firmId, BigDecimal amount, String remark) {
|
|
|
+ FirmBalanceChangeForm form = new FirmBalanceChangeForm();
|
|
|
+ form.setFirmId(firmId);
|
|
|
+ form.setAmount(amount);
|
|
|
+ form.setIncomeType(2); // 转出
|
|
|
+ form.setChangeType(2); // 提现/扣款
|
|
|
+ form.setRemark(remark);
|
|
|
+ return changeBalance(form);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企业账户余额变更
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean changeBalance(FirmBalanceChangeForm form) {
|
|
|
+ // 1. 查询企业信息
|
|
|
+ FirmInfo firmInfo = this.getById(form.getFirmId());
|
|
|
+ if (firmInfo == null) {
|
|
|
+ throw new BusinessException("企业不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 计算变更前余额
|
|
|
+ BigDecimal beforeBalance = firmInfo.getBalance() != null ? firmInfo.getBalance() : BigDecimal.ZERO;
|
|
|
+ BigDecimal changeAmount = form.getAmount();
|
|
|
+ BigDecimal afterBalance;
|
|
|
+
|
|
|
+ // 3. 根据收支类型计算变更后余额
|
|
|
+ if (form.getIncomeType() == 1) {
|
|
|
+ // 上账/转入
|
|
|
+ afterBalance = beforeBalance.add(changeAmount);
|
|
|
+ // 更新累计充值
|
|
|
+ BigDecimal totalRecharge = firmInfo.getTotalRecharge() != null ? firmInfo.getTotalRecharge() : BigDecimal.ZERO;
|
|
|
+ firmInfo.setTotalRecharge(totalRecharge.add(changeAmount));
|
|
|
+ } else {
|
|
|
+ // 下账/转出
|
|
|
+ if (beforeBalance.compareTo(changeAmount) < 0) {
|
|
|
+ throw new BusinessException("企业余额不足");
|
|
|
+ }
|
|
|
+ afterBalance = beforeBalance.subtract(changeAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 更新企业余额
|
|
|
+ firmInfo.setBalance(afterBalance);
|
|
|
+ this.updateById(firmInfo);
|
|
|
+
|
|
|
+ // 5. 记录资金流水
|
|
|
+ FirmAccountLog accountLog = new FirmAccountLog();
|
|
|
+ accountLog.setFirmId(form.getFirmId());
|
|
|
+ accountLog.setProjectName("企业账户");
|
|
|
+ accountLog.setName(form.getIncomeType() == 1 ? "上账" : "下账");
|
|
|
+ accountLog.setSerialNo(generateSerialNo());
|
|
|
+ accountLog.setIncomeType(form.getIncomeType());
|
|
|
+ accountLog.setBeforeChange(beforeBalance);
|
|
|
+ accountLog.setAfterChange(afterBalance);
|
|
|
+ accountLog.setMoneyChange(changeAmount);
|
|
|
+ accountLog.setChangeType(form.getChangeType() != null ? form.getChangeType() : (form.getIncomeType() == 1 ? 1 : 2));
|
|
|
+ accountLog.setRemark(form.getRemark());
|
|
|
+ accountLog.setCreateTime(LocalDateTime.now());
|
|
|
+ firmAccountLogMapper.insert(accountLog);
|
|
|
+
|
|
|
+ log.info("企业账户余额变更成功 - firmId: {}, incomeType: {}, amount: {}, before: {}, after: {}",
|
|
|
+ form.getFirmId(), form.getIncomeType(), changeAmount, beforeBalance, afterBalance);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成流水号
|
|
|
+ */
|
|
|
+ private String generateSerialNo() {
|
|
|
+ String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
|
|
|
+ String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 8).toUpperCase();
|
|
|
+ return "FIRM" + timestamp + uuid;
|
|
|
+ }
|
|
|
+
|
|
|
}
|