admin
2020-04-15 3d92fcd729376c584a07d5f23b8e06f81fbcbcde
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
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.InviteOrderSubsidyDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.InviteOrderSubsidyDebtRepayHistoryMapper;
import com.yeshi.fanli.entity.money.InviteOrderSubsidyDebt;
import com.yeshi.fanli.entity.money.InviteOrderSubsidyDebtRepayHistory;
import com.yeshi.fanli.exception.money.InviteOrderSubsidyDebtException;
import com.yeshi.fanli.service.inter.money.InviteOrderSubsidyDebtService;
 
@Service
public class InviteOrderSubsidyDebtServiceImpl implements InviteOrderSubsidyDebtService {
    @Resource
    private InviteOrderSubsidyDebtMapper inviteOrderSubsidyDebtMapper;
 
    @Resource
    private InviteOrderSubsidyDebtRepayHistoryMapper inviteOrderSubsidyDebtRepayHistoryMapper;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDebt(InviteOrderSubsidyDebt debt) throws InviteOrderSubsidyDebtException {
        if (debt == null || debt.getOriginMoney() == null || debt.getOriginMoney() == null || debt.getUid() == null) {
            throw new InviteOrderSubsidyDebtException(1, "数据不完整");
        }
        if (debt.getCreateTime() == null)
            debt.setCreateTime(new Date());
        debt.setLeftMoney(debt.getOriginMoney());
        inviteOrderSubsidyDebtMapper.insertSelective(debt);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void repayDebt(Long debtId, BigDecimal money) throws InviteOrderSubsidyDebtException {
        InviteOrderSubsidyDebt debt = inviteOrderSubsidyDebtMapper.selectByPrimaryKeyForUpdate(debtId);
        if (debt == null)
            throw new InviteOrderSubsidyDebtException(1, "债务ID不存在");
        if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            throw new InviteOrderSubsidyDebtException(3, "还钱必须大于0");
        if (debt.getLeftMoney().compareTo(money) < 0)
            throw new InviteOrderSubsidyDebtException(2, "还钱过多");
        // 还钱
        InviteOrderSubsidyDebt update = new InviteOrderSubsidyDebt();
        update.setId(debt.getId());
        update.setLeftMoney(debt.getLeftMoney().subtract(money));
        update.setUpdateTime(new Date());
        inviteOrderSubsidyDebtMapper.updateByPrimaryKeySelective(update);
        // 加入还钱记录
        InviteOrderSubsidyDebtRepayHistory record = new InviteOrderSubsidyDebtRepayHistory();
        record.setCreateTime(new Date());
        record.setDebt(debt);
        record.setMoney(money);
        record.setUid(debt.getUid());
        inviteOrderSubsidyDebtRepayHistoryMapper.insertSelective(record);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BigDecimal repayDebtByUid(Long uid, Date maxEstimatePayTime, BigDecimal money)
            throws InviteOrderSubsidyDebtException {
        // 还钱
        long count = countNeedRepayDebt(uid, maxEstimatePayTime);
        int page = (int) (count % 200 == 0 ? count / 200 : count / 200 + 1);
        List<InviteOrderSubsidyDebt> totalList = new ArrayList<>();
        for (int i = 0; i < page; i++) {
            List<InviteOrderSubsidyDebt> tempList = listNeedRepayDebt(uid, maxEstimatePayTime, i + 1, 200);
            if (tempList != null && tempList.size() > 0) {
                totalList.addAll(tempList);
            }
        }
 
        // 剩余资金
        BigDecimal leftMoney = new BigDecimal(money.toString());
 
        for (InviteOrderSubsidyDebt 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 (InviteOrderSubsidyDebtException e) {
                // 上笔还款未成功,继续下一笔还款
            }
        }
 
        return leftMoney;// 返回剩余的资金
    }
 
    @Override
    public List<InviteOrderSubsidyDebt> listNeedRepayDebt(Long uid, Date maxEstimatePayTime, int page, int count) {
        return inviteOrderSubsidyDebtMapper.listByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null,
                uid, maxEstimatePayTime, (page - 1) * count, count);
    }
 
    @Override
    public long countNeedRepayDebt(Long uid, Date maxEstimatePayTime) {
        return inviteOrderSubsidyDebtMapper.countByLeftMoneyAndUidAndMaxEstimatePayTime(new BigDecimal("0.01"), null,
                uid, maxEstimatePayTime);
    }
 
    @Override
    public BigDecimal getTotalDebtMoney(Long uid, Date maxEstimatePayTime) {
        BigDecimal money = inviteOrderSubsidyDebtMapper.sumLeftMoneyByUid(uid, maxEstimatePayTime);
        if (money == null)
            money = new BigDecimal(0);
        return money;
    }
    
    
    @Override
    public InviteOrderSubsidyDebt getBySourceId(Long sourceId) {
        return inviteOrderSubsidyDebtMapper.getBySourceId(sourceId);
    }
 
}