admin
2020-11-09 2147d023563a7c9d05d97547c00d6b0162c0644c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
    }
 
}