| | |
| | | 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 com.yeshi.fanli.entity.order.UserOrderWeiQuanRecord;
|
| | | import com.yeshi.fanli.service.inter.order.UserOrderWeiQuanRecordService;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import com.yeshi.fanli.dao.mybatis.money.TeamRewardDebtMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.money.TeamRewardDebtRepayHistoryMapper;
|
| | | import com.yeshi.fanli.entity.money.TeamRewardDebt;
|
| | | import com.yeshi.fanli.entity.money.TeamRewardDebtRepayHistory;
|
| | | import com.yeshi.fanli.exception.money.TeamRewardDebtException;
|
| | | import com.yeshi.fanli.service.inter.money.TeamRewardDebtService;
|
| | |
|
| | | @Service
|
| | | public class TeamRewardDebtServiceImpl implements TeamRewardDebtService {
|
| | | @Resource
|
| | | private TeamRewardDebtMapper teamRewardDebtMapper;
|
| | |
|
| | | @Resource
|
| | | private TeamRewardDebtRepayHistoryMapper teamRewardDebtRepayHistoryMapper;
|
| | |
|
| | | @Resource
|
| | | private UserOrderWeiQuanRecordService userOrderWeiQuanRecordService;
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void addDebt(TeamRewardDebt debt) throws TeamRewardDebtException {
|
| | | if (debt == null || debt.getOriginMoney() == null || debt.getOriginMoney() == null || debt.getUid() == null || StringUtil.isNullOrEmpty(debt.getTradeId()) || debt.getSourceType() == null) {
|
| | | throw new TeamRewardDebtException(1, "数据不完整");
|
| | | }
|
| | | if (debt.getCreateTime() == null)
|
| | | debt.setCreateTime(new Date());
|
| | | debt.setLeftMoney(debt.getOriginMoney());
|
| | | teamRewardDebtMapper.insertSelective(debt);
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void repayDebt(Long debtId, BigDecimal money) throws TeamRewardDebtException {
|
| | | TeamRewardDebt debt = teamRewardDebtMapper.selectByPrimaryKeyForUpdate(debtId);
|
| | | if (debt == null)
|
| | | throw new TeamRewardDebtException(1, "债务ID不存在");
|
| | | if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
|
| | | throw new TeamRewardDebtException(3, "还钱必须大于0");
|
| | | if (debt.getLeftMoney().compareTo(money) < 0)
|
| | | throw new TeamRewardDebtException(2, "还钱过多");
|
| | | // 还钱
|
| | | TeamRewardDebt update = new TeamRewardDebt();
|
| | | update.setId(debt.getId());
|
| | | update.setLeftMoney(debt.getLeftMoney().subtract(money));
|
| | | update.setUpdateTime(new Date());
|
| | | teamRewardDebtMapper.updateByPrimaryKeySelective(update);
|
| | | // 加入还钱记录
|
| | | TeamRewardDebtRepayHistory record = new TeamRewardDebtRepayHistory();
|
| | | record.setCreateTime(new Date());
|
| | | record.setDebt(debt);
|
| | | record.setMoney(money);
|
| | | record.setUid(debt.getUid());
|
| | | teamRewardDebtRepayHistoryMapper.insertSelective(record);
|
| | | //还钱是否还完
|
| | | if (update.getLeftMoney().compareTo(new BigDecimal(0)) <= 0) {
|
| | | //已经还完钱
|
| | | userOrderWeiQuanRecordService.weiQuanMoneyReturn(debt.getUid(), debt.getTradeId(), debt.getSourceType());
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public BigDecimal repayDebtByUid(Long uid, Date maxEstimatePayTime, BigDecimal money) throws TeamRewardDebtException {
|
| | | // 还钱
|
| | | long count = countNeedRepayDebt(uid, maxEstimatePayTime);
|
| | | int page = (int) (count % 200 == 0 ? count / 200 : count / 200 + 1);
|
| | | List<TeamRewardDebt> totalList = new ArrayList<>();
|
| | | for (int i = 0; i < page; i++) {
|
| | | List<TeamRewardDebt> tempList = listNeedRepayDebt(uid, maxEstimatePayTime, i + 1, 200);
|
| | | if (tempList != null && tempList.size() > 0) {
|
| | | totalList.addAll(tempList);
|
| | | }
|
| | | }
|
| | |
|
| | | // 剩余资金
|
| | | BigDecimal leftMoney = new BigDecimal(money.toString());
|
| | |
|
| | | for (TeamRewardDebt 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 (TeamRewardDebtException e) {
|
| | | // 上笔还款未成功,继续下一笔还款
|
| | | }
|
| | | }
|
| | |
|
| | | return leftMoney;// 返回剩余的资金
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<TeamRewardDebt> listNeedRepayDebt(Long uid, Date maxEstimatePayTime, int page, int count) {
|
| | | return teamRewardDebtMapper.listByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid, maxEstimatePayTime, (page - 1) * count, count);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public long countNeedRepayDebt(Long uid, Date maxEstimatePayTime) {
|
| | | return teamRewardDebtMapper.countByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid, maxEstimatePayTime);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public BigDecimal getTotalDebtMoney(Long uid, Date maxEstimatePayTime) {
|
| | | BigDecimal money = teamRewardDebtMapper.sumLeftMoneyByUid(uid, maxEstimatePayTime);
|
| | | if (money == null)
|
| | | money = new BigDecimal(0);
|
| | | return money;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public TeamRewardDebt selectBySourceId(Long sourceId) {
|
| | | return teamRewardDebtMapper.selectBySourceId(sourceId);
|
| | | }
|
| | |
|
| | | }
|
| | | 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 com.yeshi.fanli.entity.order.UserOrderWeiQuanRecord; |
| | | import com.yeshi.fanli.service.inter.order.UserOrderWeiQuanRecordService; |
| | | import com.yeshi.fanli.util.StringUtil; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import com.yeshi.fanli.dao.mybatis.money.TeamRewardDebtMapper; |
| | | import com.yeshi.fanli.dao.mybatis.money.TeamRewardDebtRepayHistoryMapper; |
| | | import com.yeshi.fanli.entity.money.TeamRewardDebt; |
| | | import com.yeshi.fanli.entity.money.TeamRewardDebtRepayHistory; |
| | | import com.yeshi.fanli.exception.money.TeamRewardDebtException; |
| | | import com.yeshi.fanli.service.inter.money.TeamRewardDebtService; |
| | | |
| | | @Service |
| | | public class TeamRewardDebtServiceImpl implements TeamRewardDebtService { |
| | | @Resource |
| | | private TeamRewardDebtMapper teamRewardDebtMapper; |
| | | |
| | | @Resource |
| | | private TeamRewardDebtRepayHistoryMapper teamRewardDebtRepayHistoryMapper; |
| | | |
| | | @Resource |
| | | private UserOrderWeiQuanRecordService userOrderWeiQuanRecordService; |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void addDebt(TeamRewardDebt debt) throws TeamRewardDebtException { |
| | | if (debt == null || debt.getOriginMoney() == null || debt.getOriginMoney() == null || debt.getUid() == null || StringUtil.isNullOrEmpty(debt.getTradeId()) || debt.getSourceType() == null) { |
| | | throw new TeamRewardDebtException(1, "数据不完整"); |
| | | } |
| | | if (debt.getCreateTime() == null) |
| | | debt.setCreateTime(new Date()); |
| | | debt.setLeftMoney(debt.getOriginMoney()); |
| | | teamRewardDebtMapper.insertSelective(debt); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void repayDebt(Long debtId, BigDecimal money, String beizhu) throws TeamRewardDebtException { |
| | | TeamRewardDebt debt = teamRewardDebtMapper.selectByPrimaryKeyForUpdate(debtId); |
| | | if (debt == null) |
| | | throw new TeamRewardDebtException(1, "债务ID不存在"); |
| | | if (money == null || money.compareTo(new BigDecimal(0)) <= 0) |
| | | throw new TeamRewardDebtException(3, "还钱必须大于0"); |
| | | if (debt.getLeftMoney().compareTo(money) < 0) |
| | | throw new TeamRewardDebtException(2, "还钱过多"); |
| | | // 还钱 |
| | | TeamRewardDebt update = new TeamRewardDebt(); |
| | | update.setId(debt.getId()); |
| | | update.setLeftMoney(debt.getLeftMoney().subtract(money)); |
| | | update.setUpdateTime(new Date()); |
| | | teamRewardDebtMapper.updateByPrimaryKeySelective(update); |
| | | // 加入还钱记录 |
| | | TeamRewardDebtRepayHistory record = new TeamRewardDebtRepayHistory(); |
| | | record.setCreateTime(new Date()); |
| | | record.setDebt(debt); |
| | | record.setMoney(money); |
| | | record.setUid(debt.getUid()); |
| | | record.setBeiZhu(beizhu); |
| | | teamRewardDebtRepayHistoryMapper.insertSelective(record); |
| | | //还钱是否还完 |
| | | if (update.getLeftMoney().compareTo(new BigDecimal(0)) <= 0) { |
| | | //已经还完钱 |
| | | userOrderWeiQuanRecordService.weiQuanMoneyReturn(debt.getUid(), debt.getTradeId(), debt.getSourceType()); |
| | | } |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public BigDecimal repayDebtByUid(Long uid, Date maxEstimatePayTime, BigDecimal money) throws TeamRewardDebtException { |
| | | // 还钱 |
| | | long count = countNeedRepayDebt(uid, maxEstimatePayTime); |
| | | int page = (int) (count % 200 == 0 ? count / 200 : count / 200 + 1); |
| | | List<TeamRewardDebt> totalList = new ArrayList<>(); |
| | | for (int i = 0; i < page; i++) { |
| | | List<TeamRewardDebt> tempList = listNeedRepayDebt(uid, maxEstimatePayTime, i + 1, 200); |
| | | if (tempList != null && tempList.size() > 0) { |
| | | totalList.addAll(tempList); |
| | | } |
| | | } |
| | | |
| | | // 剩余资金 |
| | | BigDecimal leftMoney = new BigDecimal(money.toString()); |
| | | |
| | | for (TeamRewardDebt 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,null); |
| | | leftMoney = leftMoney.subtract(repayMoney); |
| | | } catch (TeamRewardDebtException e) { |
| | | // 上笔还款未成功,继续下一笔还款 |
| | | } |
| | | } |
| | | |
| | | return leftMoney;// 返回剩余的资金 |
| | | } |
| | | |
| | | @Override |
| | | public List<TeamRewardDebt> listNeedRepayDebt(Long uid, Date maxEstimatePayTime, int page, int count) { |
| | | return teamRewardDebtMapper.listByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid, maxEstimatePayTime, (page - 1) * count, count); |
| | | } |
| | | |
| | | @Override |
| | | public long countNeedRepayDebt(Long uid, Date maxEstimatePayTime) { |
| | | return teamRewardDebtMapper.countByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null, uid, maxEstimatePayTime); |
| | | } |
| | | |
| | | @Override |
| | | public BigDecimal getTotalDebtMoney(Long uid, Date maxEstimatePayTime) { |
| | | BigDecimal money = teamRewardDebtMapper.sumLeftMoneyByUid(uid, maxEstimatePayTime); |
| | | if (money == null) |
| | | money = new BigDecimal(0); |
| | | return money; |
| | | } |
| | | |
| | | @Override |
| | | public TeamRewardDebt selectBySourceId(Long sourceId) { |
| | | return teamRewardDebtMapper.selectBySourceId(sourceId); |
| | | } |
| | | |
| | | } |