admin
2020-11-13 ae08b37317103344b9be1b9f91b6bdf7abbc839b
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
package com.ks.goldcorn.service;
 
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.mapper.GoldCornBalanceMapper;
import com.ks.goldcorn.pojo.DO.GoldCornBalance;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
 
@Component
public class GoldCornBalanceManager {
 
    @Resource
    private GoldCornBalanceMapper goldCornBalanceMapper;
 
    @Transactional(rollbackFor = Exception.class)
    public void addMoney(Long appId, String uid, int money) throws GoldUserException {
        GoldCornBalance balance = goldCornBalanceMapper.selectByAppIdAndUidForUpdate(appId, uid);
        if (balance == null) {
            throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
        }
        goldCornBalanceMapper.addMoney(balance.getId(), money);
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    public void subMoney(Long appId, String uid, int money) throws GoldUserException, GoldTradeException {
        GoldCornBalance balance = goldCornBalanceMapper.selectByAppIdAndUidForUpdate(appId, uid);
        if (balance == null) {
            throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
        }
 
        if (balance.getBalance() < money) {
            throw new GoldUserException(GoldTradeException.CODE_BALANCE_NOT_ENOUGH, "余额不足");
        }
 
        goldCornBalanceMapper.subMoney(balance.getId(), money);
    }
}