admin
2022-10-28 0e9b6603d4ae9d11c1fbc90257ce816c5807b8ff
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
package com.yeshi.makemoney.app.service.impl.money;
 
import com.ks.lib.common.exception.ParamsException;
import com.yeshi.makemoney.app.dao.money.UserMoneyBalanceMapper;
import com.yeshi.makemoney.app.entity.money.UserMoneyBalance;
import com.yeshi.makemoney.app.entity.money.UserMoneyRecord;
import com.yeshi.makemoney.app.exception.money.ExtractException;
import com.yeshi.makemoney.app.exception.money.UserMoneyBalanceException;
import com.yeshi.makemoney.app.service.inter.money.UserMoneyRecordService;
import com.yeshi.makemoney.app.service.inter.money.UserMoneyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
 
@Service
public class UserMoneyServiceImpl implements UserMoneyService {
 
    @Resource
    private UserMoneyBalanceMapper userMoneyBalanceMapper;
 
    @Resource
    private UserMoneyRecordService userMoneyRecordService;
 
 
    @Override
    public void init(Long uid) {
        UserMoneyBalance balance = userMoneyBalanceMapper.selectByPrimaryKeyForUpdate(uid);
        if (balance != null) {
            return;
        }
        balance = new UserMoneyBalance();
        balance.setCreateTime(new Date());
        balance.setMoney(new BigDecimal(0));
        balance.setId(uid);
        userMoneyBalanceMapper.insertSelective(balance);
    }
 
    @Override
    public BigDecimal getBalance(Long uid) {
        UserMoneyBalance balance = userMoneyBalanceMapper.selectByPrimaryKey(uid);
        if (balance == null) {
            return new BigDecimal(0);
        }
        return balance.getMoney();
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void subUserMoney(UserMoneyRecord record) throws ParamsException, UserMoneyBalanceException {
        userMoneyRecordService.add(record);
        UserMoneyBalance balance = userMoneyBalanceMapper.selectByPrimaryKeyForUpdate(record.getUser().getId());
        if (balance == null || balance.getMoney() == null || balance.getMoney().compareTo(record.getMoney().abs()) < 0) {
            throw new UserMoneyBalanceException(UserMoneyBalanceException.CODE_BALANCE_NOT_ENOUGH, "账户余额不足");
        }
        //添加到提现记录,并扣除账户余额
        userMoneyBalanceMapper.subMoney(record.getUser().getId(), record.getMoney().abs());
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addUserMoney(UserMoneyRecord record) throws ParamsException {
        userMoneyRecordService.add(record);
        //用户余额上锁
        UserMoneyBalance balance = userMoneyBalanceMapper.selectByPrimaryKeyForUpdate(record.getUser().getId());
        if (balance == null) {
            init(record.getUser().getId());
            balance = userMoneyBalanceMapper.selectByPrimaryKeyForUpdate(record.getUser().getId());
        }
        userMoneyBalanceMapper.addMoney(record.getUser().getId(), record.getMoney().abs());
    }
}