admin
2020-11-12 10229cb1635c70c82dbab3aa536ffaf4fa543b63
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
package com.ks.goldcorn.service.remote;
 
import com.ks.goldcorn.exception.GoldAppException;
import com.ks.goldcorn.exception.GoldRecordException;
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.mapper.GoldCornAppInfoMapper;
import com.ks.goldcorn.mapper.GoldCornBalanceMapper;
import com.ks.goldcorn.mapper.GoldCornConsumeSourceMapper;
import com.ks.goldcorn.mapper.GoldCornGetSourceMapper;
import com.ks.goldcorn.pojo.DO.*;
import com.ks.goldcorn.service.GoldCornRecordManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
 
@Service
public class GoldCornTradeServiceImpl implements GoldCornTradeService {
 
    @Resource
    private GoldCornAppInfoMapper goldCornAppInfoMapper;
 
    @Resource
    private GoldCornBalanceMapper goldCornBalanceMapper;
 
    @Resource
    private GoldCornConsumeSourceMapper goldCornConsumeSourceMapper;
 
    @Resource
    private GoldCornBalanceManager goldCornBalanceManager;
 
    @Resource
    private GoldCornGetSourceMapper goldCornGetSourceMapper;
 
    @Resource
    private GoldCornRecordManager goldCornRecordManager;
 
    /**
     * 验证系统与用户
     *
     * @param appCode
     * @param uid
     * @return
     * @throws GoldAppException
     * @throws GoldUserException
     */
    private GoldCornAppInfo validateAppAndUser(String appCode, String uid) throws GoldAppException, GoldUserException {
        GoldCornAppInfo appInfo = goldCornAppInfoMapper.selectByAppCode(appCode);
        if (appInfo == null) {
            throw new GoldAppException(GoldAppException.CODE_NOT_EXIST, "app is not exist");
        }
 
        GoldCornBalance balance = goldCornBalanceMapper.selectByAppIdAndUid(appInfo.getId(), uid);
        if (balance == null) {
            throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
        }
 
        return appInfo;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addGoldCorn(String appCode, String uid, GoldCornGetSource source) throws GoldAppException, GoldUserException, GoldTradeException {
        GoldCornAppInfo app = validateAppAndUser(appCode, uid);
        source = goldCornGetSourceMapper.selectByPrimaryKey(source.getId());
        if (source == null) {
            throw new GoldUserException(GoldTradeException.CODE_NOT_EXIST, "source is not exist");
        }
 
        //加记录
        GoldCornRecord record = new GoldCornRecord();
        record.setAppId(app.getId());
        record.setGoldCorn(source.getGoldCorn());
        record.setSourceId(source.getId());
        record.setType(GoldCornRecord.TYPE_GET);
        record.setUid(uid);
        try {
            goldCornRecordManager.addRecord(record);
        } catch (GoldRecordException e) {
            throw new GoldTradeException(GoldTradeException.CODE_ADD_RECORD_FAIL, "添加记录失败");
        }
        //加余额
        goldCornBalanceManager.addMoney(app.getId(), uid, source.getGoldCorn());
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void consumeGoldCorn(String appCode, String uid, GoldCornConsumeSource source) throws GoldAppException, GoldUserException, GoldTradeException {
        GoldCornAppInfo app = validateAppAndUser(appCode, uid);
        source = goldCornConsumeSourceMapper.selectByPrimaryKey(source.getId());
        if (source == null) {
            throw new GoldUserException(GoldTradeException.CODE_NOT_EXIST, "source is not exist");
        }
        //加记录
        GoldCornRecord record = new GoldCornRecord();
        record.setAppId(app.getId());
        record.setGoldCorn(source.getGoldCorn());
        record.setSourceId(source.getId());
        record.setType(GoldCornRecord.TYPE_CONSUME);
        record.setUid(uid);
        try {
            goldCornRecordManager.addRecord(record);
        } catch (GoldRecordException e) {
            throw new GoldTradeException(GoldTradeException.CODE_ADD_RECORD_FAIL, "添加记录失败");
        }
        //减余额
        goldCornBalanceManager.subMoney(app.getId(), uid, source.getGoldCorn());
    }
}