admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/money/TeamDividentsDebtServiceImpl.java
@@ -1,122 +1,122 @@
package com.yeshi.fanli.service.impl.money;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.money.TeamDividentsDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.TeamDividentsDebtRepayHistoryMapper;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsDebt;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsDebtRepayHistory;
import com.yeshi.fanli.exception.money.TeamDividentsDebtException;
import com.yeshi.fanli.service.inter.money.TeamDividentsDebtService;
@Service
public class TeamDividentsDebtServiceImpl implements TeamDividentsDebtService {
   @Resource
   private TeamDividentsDebtMapper teamDividentsDebtMapper;
   @Resource
   private TeamDividentsDebtRepayHistoryMapper teamDividentsDebtRepayHistoryMapper;
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void addDebt(TeamDividentsDebt debt) throws TeamDividentsDebtException {
      if (debt == null || debt.getOriginMoney() == null || debt.getUid() == null) {
         throw new TeamDividentsDebtException(1, "数据不完整");
      }
      if (debt.getCreateTime() == null)
         debt.setCreateTime(new Date());
      debt.setLeftMoney(debt.getOriginMoney());
      teamDividentsDebtMapper.insertSelective(debt);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void repayDebt(Long debtId, BigDecimal money) throws TeamDividentsDebtException {
      TeamDividentsDebt debt = teamDividentsDebtMapper.selectByPrimaryKeyForUpdate(debtId);
      if (debt == null)
         throw new TeamDividentsDebtException(1, "债务ID不存在");
      if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
         throw new TeamDividentsDebtException(3, "还钱必须大于0");
      if (debt.getLeftMoney().compareTo(money) < 0)
         throw new TeamDividentsDebtException(2, "还钱过多");
      // 还钱
      TeamDividentsDebt update = new TeamDividentsDebt();
      update.setId(debt.getId());
      update.setLeftMoney(debt.getLeftMoney().subtract(money));
      update.setUpdateTime(new Date());
      teamDividentsDebtMapper.updateByPrimaryKeySelective(update);
      // 加入还钱记录
      TeamDividentsDebtRepayHistory record = new TeamDividentsDebtRepayHistory();
      record.setCreateTime(new Date());
      record.setDebt(debt);
      record.setMoney(money);
      record.setUid(debt.getUid());
      teamDividentsDebtRepayHistoryMapper.insertSelective(record);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public BigDecimal repayDebtByUid(Long uid, Date maxEstimatePayTime, BigDecimal money)
         throws TeamDividentsDebtException {
      // 还钱
      long count = countNeedRepayDebt(uid, maxEstimatePayTime);
      int page = (int) (count % 200 == 0 ? count / 200 : count / 200 + 1);
      List<TeamDividentsDebt> totalList = new ArrayList<>();
      for (int i = 0; i < page; i++) {
         List<TeamDividentsDebt> tempList = listNeedRepayDebt(uid, maxEstimatePayTime, i + 1, 200);
         if (tempList != null && tempList.size() > 0) {
            totalList.addAll(tempList);
         }
      }
      // 剩余资金
      BigDecimal leftMoney = new BigDecimal(money.toString());
      for (TeamDividentsDebt debt : totalList) {
         if (leftMoney.compareTo(new BigDecimal(0)) <= 0)// 余额不足扣款
            break;
         BigDecimal repayMoney = null;
         if (debt.getLeftMoney().compareTo(leftMoney) >= 0)
            repayMoney = new BigDecimal(leftMoney.toString());
         else
            repayMoney = debt.getLeftMoney();
         try {
            repayDebt(debt.getId(), repayMoney);
            leftMoney = leftMoney.subtract(repayMoney);
         } catch (TeamDividentsDebtException e) {
            // 上笔还款未成功,继续下一笔还款
         }
      }
      return leftMoney;// 返回剩余的资金
   }
   @Override
   public List<TeamDividentsDebt> listNeedRepayDebt(Long uid, Date maxEstimatePayTime, int page, int count) {
      return teamDividentsDebtMapper.listByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid,
            maxEstimatePayTime, (page - 1) * count, count);
   }
   @Override
   public long countNeedRepayDebt(Long uid, Date maxEstimatePayTime) {
      return teamDividentsDebtMapper.countByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid,
            maxEstimatePayTime);
   }
   @Override
   public BigDecimal getTotalDebtMoney(Long uid, Date maxEstimatePayTime) {
      BigDecimal money = teamDividentsDebtMapper.sumLeftMoneyByUid(uid, maxEstimatePayTime);
      if (money == null)
         money = new BigDecimal(0);
      return money;
   }
}
package com.yeshi.fanli.service.impl.money;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.money.TeamDividentsDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.TeamDividentsDebtRepayHistoryMapper;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsDebt;
import com.yeshi.fanli.entity.order.dividents.TeamDividentsDebtRepayHistory;
import com.yeshi.fanli.exception.money.TeamDividentsDebtException;
import com.yeshi.fanli.service.inter.money.TeamDividentsDebtService;
@Service
public class TeamDividentsDebtServiceImpl implements TeamDividentsDebtService {
   @Resource
   private TeamDividentsDebtMapper teamDividentsDebtMapper;
   @Resource
   private TeamDividentsDebtRepayHistoryMapper teamDividentsDebtRepayHistoryMapper;
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void addDebt(TeamDividentsDebt debt) throws TeamDividentsDebtException {
      if (debt == null || debt.getOriginMoney() == null || debt.getUid() == null) {
         throw new TeamDividentsDebtException(1, "数据不完整");
      }
      if (debt.getCreateTime() == null)
         debt.setCreateTime(new Date());
      debt.setLeftMoney(debt.getOriginMoney());
      teamDividentsDebtMapper.insertSelective(debt);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void repayDebt(Long debtId, BigDecimal money) throws TeamDividentsDebtException {
      TeamDividentsDebt debt = teamDividentsDebtMapper.selectByPrimaryKeyForUpdate(debtId);
      if (debt == null)
         throw new TeamDividentsDebtException(1, "债务ID不存在");
      if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
         throw new TeamDividentsDebtException(3, "还钱必须大于0");
      if (debt.getLeftMoney().compareTo(money) < 0)
         throw new TeamDividentsDebtException(2, "还钱过多");
      // 还钱
      TeamDividentsDebt update = new TeamDividentsDebt();
      update.setId(debt.getId());
      update.setLeftMoney(debt.getLeftMoney().subtract(money));
      update.setUpdateTime(new Date());
      teamDividentsDebtMapper.updateByPrimaryKeySelective(update);
      // 加入还钱记录
      TeamDividentsDebtRepayHistory record = new TeamDividentsDebtRepayHistory();
      record.setCreateTime(new Date());
      record.setDebt(debt);
      record.setMoney(money);
      record.setUid(debt.getUid());
      teamDividentsDebtRepayHistoryMapper.insertSelective(record);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public BigDecimal repayDebtByUid(Long uid, Date maxEstimatePayTime, BigDecimal money)
         throws TeamDividentsDebtException {
      // 还钱
      long count = countNeedRepayDebt(uid, maxEstimatePayTime);
      int page = (int) (count % 200 == 0 ? count / 200 : count / 200 + 1);
      List<TeamDividentsDebt> totalList = new ArrayList<>();
      for (int i = 0; i < page; i++) {
         List<TeamDividentsDebt> tempList = listNeedRepayDebt(uid, maxEstimatePayTime, i + 1, 200);
         if (tempList != null && tempList.size() > 0) {
            totalList.addAll(tempList);
         }
      }
      // 剩余资金
      BigDecimal leftMoney = new BigDecimal(money.toString());
      for (TeamDividentsDebt debt : totalList) {
         if (leftMoney.compareTo(new BigDecimal(0)) <= 0)// 余额不足扣款
            break;
         BigDecimal repayMoney = null;
         if (debt.getLeftMoney().compareTo(leftMoney) >= 0)
            repayMoney = new BigDecimal(leftMoney.toString());
         else
            repayMoney = debt.getLeftMoney();
         try {
            repayDebt(debt.getId(), repayMoney);
            leftMoney = leftMoney.subtract(repayMoney);
         } catch (TeamDividentsDebtException e) {
            // 上笔还款未成功,继续下一笔还款
         }
      }
      return leftMoney;// 返回剩余的资金
   }
   @Override
   public List<TeamDividentsDebt> listNeedRepayDebt(Long uid, Date maxEstimatePayTime, int page, int count) {
      return teamDividentsDebtMapper.listByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid,
            maxEstimatePayTime, (page - 1) * count, count);
   }
   @Override
   public long countNeedRepayDebt(Long uid, Date maxEstimatePayTime) {
      return teamDividentsDebtMapper.countByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid,
            maxEstimatePayTime);
   }
   @Override
   public BigDecimal getTotalDebtMoney(Long uid, Date maxEstimatePayTime) {
      BigDecimal money = teamDividentsDebtMapper.sumLeftMoneyByUid(uid, maxEstimatePayTime);
      if (money == null)
         money = new BigDecimal(0);
      return money;
   }
}