admin
2021-04-14 759f8df85ddb840682f91bad31e874fa0b58c075
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
package com.yeshi.buwan.service.manager;
 
import com.ks.goldcorn.exception.GoldAppException;
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.service.remote.GoldCornBalanceService;
import com.ks.goldcorn.service.remote.GoldCornTradeService;
import com.yeshi.buwan.domain.vip.OrderType;
import com.yeshi.buwan.domain.vip.OrderRecord;
import com.yeshi.buwan.exception.goldcorn.GoldCornException;
import com.yeshi.buwan.service.inter.system.SystemConfigService;
import com.yeshi.buwan.util.StringUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
@Component
public class GoldCornManager {
 
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Reference(version = "1.0", check = false)
    private GoldCornTradeService goldCornTradeService;
 
    @Reference(version = "1.0", check = false)
    private GoldCornBalanceService goldCornBalanceService;
 
    public void init(String uid) throws Exception {
        goldCornBalanceService.init(getAppCode(), getUid(uid));
    }
 
 
    /**
     * 获取金币系统的用户ID
     *
     * @param uid
     * @return
     * @throws Exception
     */
    public String getUid(String uid) throws Exception {
        String prefix = systemConfigService.getConfigValueByKeyCache("thirdUidPrefix");
        if (StringUtil.isNullOrEmpty(prefix)) {
            throw new Exception("用户ID前缀获取出错");
        }
        return prefix + uid;
    }
 
    public String getAppCode() throws GoldAppException {
        String appCode = systemConfigService.getConfigValueByKeyCache("goldCornAppCode");
        if (StringUtil.isNullOrEmpty(appCode)) {
            throw new GoldAppException(GoldAppException.CODE_NOT_EXIST, "应用不存在");
        }
        return appCode;
    }
 
    public long getBalance(String uid) {
        try {
            Long count = goldCornBalanceService.getBalance(getAppCode(), getUid(uid));
            if (count == null) {
                init(uid);
            }
            return count == null ? 0 : count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 
 
    /**
     * 金币消耗
     *
     * @param record
     * @param goldCornCount 金币数量
     * @throws GoldCornException
     * @throws Exception
     */
    public void consumeGoldCorn(OrderRecord record, int goldCornCount) throws GoldCornException, Exception {
        String tuid = getUid(record.getUid());
        String appCode = getAppCode();
        String sourceCode = "";
        String title = "";
        String desc = "";
        if (record.getOrderType() == OrderType.vip) {
            sourceCode = "buyVIP";
            title = "购买VIP";
        } else if (record.getOrderType() == OrderType.video) {
            sourceCode = "buyVideo";
            title = "购买单片";
        }
        consumeGoldCorn(appCode, tuid, sourceCode, goldCornCount, title, desc);
    }
 
    /**
     * 返回影视豆
     *
     * @param record
     * @throws GoldCornException
     * @throws Exception
     */
    public void drawbackGoldCorn(OrderRecord record) throws GoldCornException, Exception {
        String tuid = getUid(record.getUid());
        String appCode = getAppCode();
        String sourceCode = "";
        String title = "";
        String desc = "";
        if (record.getOrderType() == OrderType.vip) {
            sourceCode = "buyVIP";
            title = "购买VIP退款";
        } else if (record.getOrderType() == OrderType.video) {
            sourceCode = "buyVideo";
            title = "购买单片退款";
        }
        addGoldCorn(appCode, tuid, sourceCode, record.getGoldCorn(), title, desc);
    }
 
    private void addGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException {
        goldCornTradeService.addGoldCorn(appCode, uid, sourceCode, goldCornCount, title, desc);
    }
 
 
    private void consumeGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException {
        goldCornTradeService.consumeGoldCorn(appCode, uid, sourceCode, goldCornCount, title, desc);
    }
 
 
}