admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.yeshi.fanli.service.impl.money;
 
import java.math.BigDecimal;
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.AlipayAccountValidNormalHistoryMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtReturnHistoryMapper;
import com.yeshi.fanli.entity.bus.user.AlipayAccountValidNormalHistory;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.money.UserMoneyDebt;
import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
import com.yeshi.fanli.entity.money.UserMoneyDebtReturnHistory;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
import com.yeshi.fanli.exception.money.UserMoneyDebtException;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.money.tb.TaoBaoWeiQuanDrawBackService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
 
@Service
public class UserMoneyDebtServiceImpl implements UserMoneyDebtService {
 
    @Resource
    private UserMoneyDebtMapper userMoneyDebtMapper;
 
    @Resource
    private UserMoneyDebtReturnHistoryMapper userMoneyDebtReturnHistoryMapper;
 
    @Resource
    private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService;
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private HongBaoOrderService hongBaoOrderService;
 
    @Resource
    private UserMoneyService userMoneyService;
 
    @Resource
    private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Override
    public void addUserMoneyDebt(UserMoneyDebt debt) throws UserMoneyDebtException {
        if (debt == null)
            return;
 
        if (debt.getType() == UserMoneyDebtTypeEnum.order) {
            if (debt.getSourceId() == null)
                throw new UserMoneyDebtException(1, "sourceId为空");
            if (debt.getUid() == null || debt.getOriginMoney() == null)
                throw new UserMoneyDebtException(2, "信息不完整");
 
            if (debt.getLeftMoney() == null)
                debt.setLeftMoney(debt.getOriginMoney());
 
            if (debt.getCreateTime() == null)
                debt.setCreateTime(new Date());
 
            UserMoneyDebt old = userMoneyDebtMapper.selectByUidAndTypeAndSourceId(debt.getUid(), debt.getType(),
                    debt.getSourceId());
            if (old != null) {
                throw new UserMoneyDebtException(3, "对应售后已经存在");
            }
            userMoneyDebtMapper.insertSelective(debt);
        }
    }
 
    @Override
    public UserMoneyDebt selectByTypeAndSourceId(UserMoneyDebtTypeEnum type, Long sourceId) {
 
        return userMoneyDebtMapper.selectByTypeAndSourceId(type, sourceId);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void repayDebt(UserMoneyDebt debt, BigDecimal money) throws UserMoneyDebtException {
        if (debt == null || debt.getId() == null)
            throw new UserMoneyDebtException(1, "参数不完整");
 
        if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            throw new UserMoneyDebtException(2, "偿还资金需大于0");
 
        UserMoneyDebt old = userMoneyDebtMapper.selectByPrimaryKey(debt.getId());
        if (old == null)
            throw new UserMoneyDebtException(3, "借贷关系不存在");
 
        if (old.getUid().longValue() != debt.getUid())
            throw new UserMoneyDebtException(4, "只能本人还钱");
 
        if (money.compareTo(old.getLeftMoney()) < 0)
            throw new UserMoneyDebtException(5, "不够还");
 
        UserMoneyDebt update = new UserMoneyDebt();
        update.setId(old.getId());
        update.setUpdateTime(new Date());
        update.setLeftMoney(old.getLeftMoney().subtract(money));
        userMoneyDebtMapper.updateByPrimaryKeySelective(update);
 
        UserMoneyDebtReturnHistory history = new UserMoneyDebtReturnHistory();
        history.setBeiZhu(null);
        history.setCreateTime(new Date());
        history.setDebt(old);
        history.setMoney(money);
        history.setUid(old.getUid());
        userMoneyDebtReturnHistoryMapper.insertSelective(history);
 
        if (debt.getType() == UserMoneyDebtTypeEnum.order) {
            HongBaoV2 hb = hongBaoV2Service.selectByPrimaryKey(debt.getSourceId());
            if (hb == null) {
                throw new UserMoneyDebtException(6, "偿还的订单ID不存在");
            }
 
            TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackService.selectByHongBaoId(hb.getId());
            if (weiQuanDrawBack == null)
                throw new UserMoneyDebtException(7, "尚未找到退款信息");
 
            // 资金变化,添加用户资金记录,添加相关通知
            Long uid = debt.getUid();
            switch (hb.getType()) {
            case HongBaoV2.TYPE_ZIGOU:
                // 新版资金记录
                try {
                    UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack,
                            money);
                    userMoneyService.subUserMoney(uid, money, userMoneyDetail);
                } catch (UserMoneyDetailException e) {
                    throw new UserMoneyDebtException(12, "插入资金详情出错");
                }
 
                // 新版通知
                List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
                        weiQuanDrawBack.getOrderId());
                int goodsCount = 0;
                for (CommonOrder co : orderList) {
                    if (co.getState() != CommonOrder.STATE_SX)
                        goodsCount += co.getCount();
                }
 
                BigDecimal fanliMoney = new BigDecimal(0);
                List<HongBaoOrder> hongBaoOrderList = hongBaoOrderService
                        .listDetailByOrderIdAndSourceType(weiQuanDrawBack.getOrderId(), Constant.SOURCE_TYPE_TAOBAO);
 
                for (HongBaoOrder hongBaoOrder : hongBaoOrderList) {
                    fanliMoney = fanliMoney.add(hongBaoOrder.getHongBaoV2().getMoney());
                }
 
                userMoneyMsgNotificationService.fanliOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(),
                        Constant.SOURCE_TYPE_TAOBAO, money, fanliMoney, goodsCount,
                        orderList.get(0).getThirdCreateTime());
 
                break;
            case HongBaoV2.TYPE_SHARE_GOODS:
 
                // 新版资金记录
                try {
                    UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(debt.getUid(),
                            weiQuanDrawBack, money);
                    userMoneyService.subUserMoney(uid, money, userMoneyDetail);
 
                    // 新版通知
                    List<CommonOrder> orderList1 = commonOrderService
                            .listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, weiQuanDrawBack.getOrderId());
                    int goodsCount1 = 0;
                    for (CommonOrder co : orderList1) {
                        if (co.getState() != CommonOrder.STATE_SX)
                            goodsCount1 += co.getCount();
                    }
 
                    BigDecimal fanliMoney1 = new BigDecimal(0);
                    List<HongBaoOrder> hongBaoOrderList1 = hongBaoOrderService.listDetailByOrderIdAndSourceType(
                            weiQuanDrawBack.getOrderId(), Constant.SOURCE_TYPE_TAOBAO);
 
                    for (HongBaoOrder hongBaoOrder : hongBaoOrderList1) {
                        fanliMoney1 = fanliMoney1.add(hongBaoOrder.getHongBaoV2().getMoney());
                    }
 
                    userMoneyMsgNotificationService.shareOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(),
                            Constant.SOURCE_TYPE_TAOBAO, money, fanliMoney1, goodsCount1,
                            orderList1.get(0).getThirdCreateTime());
 
                } catch (UserMoneyDetailException e) {
                    throw new UserMoneyDebtException(12, "插入资金详情出错");
                }
                break;
            case HongBaoV2.TYPE_SHARE_ERJI:
            case HongBaoV2.TYPE_SHARE_YIJI:
            case HongBaoV2.TYPE_ERJI:
            case HongBaoV2.TYPE_YIJI:
 
                // 新版资金记录
                try {
                    UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInviteWeiQuan(uid, weiQuanDrawBack,
                            money);
                    userMoneyService.subUserMoney(uid, money, userMoneyDetail);
                } catch (UserMoneyDetailException e) {
                    throw new UserMoneyDebtException(12, "插入资金详情出错");
                }
 
                break;
            }
        } else if (debt.getType() == UserMoneyDebtTypeEnum.extractVerify) {// 提现验证
            // 新版资金记录
            AlipayAccountValidNormalHistory aliPayAccountHistory = alipayAccountValidNormalHistoryMapper
                    .selectByPrimaryKey(debt.getSourceId());
 
            AlipayAccountValidNormalHistory first = alipayAccountValidNormalHistoryMapper
                    .selectLatestByUid(aliPayAccountHistory.getUid());
 
            if (aliPayAccountHistory == null)
                return;
            try {
                UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(aliPayAccountHistory,
                        money);
                userMoneyService.subUserMoney(aliPayAccountHistory.getUid(), money, userMoneyDetail);
            } catch (UserMoneyDetailException e) {
                throw new UserMoneyDebtException(12, "插入资金详情出错");
            }
            userMoneyMsgNotificationService.alipayAccountValid(aliPayAccountHistory, "1个月",
                    first.getId().longValue() != aliPayAccountHistory.getId());
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void repayDebt(Long uid) {
        // 查询是否有欠债
        List<UserMoneyDebt> list = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 50);
        if (list != null && list.size() > 0) {// 有欠债
            for (UserMoneyDebt debt : list) {
                BigDecimal leftMoney = userInfoService.getBalance(uid);
                if (leftMoney.compareTo(debt.getLeftMoney()) >= 0) {// 有足够的资金偿还债务
                    try {
                        repayDebt(debt, debt.getLeftMoney());
                    } catch (UserMoneyDebtException e) {
                        try {
                            LogHelper.errorDetailInfo(e, "uid:" + uid + " debtId:" + debt.getId(), null);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        }
    }
 
    @Override
    public boolean isHaveDebtToRepay(Long uid) {
        List<UserMoneyDebt> debtList = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 1);
        if (debtList != null && debtList.size() > 0)
            return true;
        return false;
    }
 
}