admin
2021-11-19 4daec329f36cc9d86911c08b8ec8e00270792189
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
package com.ks.app.service.manager;
 
import com.ks.goldcorn.exception.GoldAppException;
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.pojo.DO.GoldCornGetSource;
import com.ks.goldcorn.pojo.DO.GoldCornRecord;
import com.ks.goldcorn.pojo.DO.GoldCornRecordCountMap;
import com.ks.goldcorn.pojo.Query.GoldCornRecordQuery;
import com.ks.goldcorn.service.remote.GoldCornBalanceService;
import com.ks.goldcorn.service.remote.GoldCornGetSourceService;
import com.ks.goldcorn.service.remote.GoldCornRecordService;
import com.ks.goldcorn.service.remote.GoldCornTradeService;
import com.ks.app.entity.vip.OrderRecord;
import com.ks.app.entity.vip.OrderType;
import com.ks.app.exception.goldcorn.GoldCornException;
import com.ks.app.service.inter.config.SystemConfigService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author hxh
 * @title: GoldCornManager
 * @description: TODO
 * @date 2021/11/17 14:54
 */
@Component
public class GoldCornManager {
 
 
    @Resource
    private SystemConfigService systemConfigService;
 
//    @Reference(version = "1.0", check = false)
    private GoldCornTradeService goldCornTradeService;
 
//    @Reference(version = "1.0", check = false)
    private GoldCornRecordService goldCornRecordService;
 
//    @Reference(version = "1.0", check = false)
    private GoldCornBalanceService goldCornBalanceService;
 
//    @Reference(version = "1.0", check = false)
    private GoldCornGetSourceService goldCornGetSourceService;
 
 
    public void init(Long uid) throws Exception {
        goldCornBalanceService.init(getAppCode(), getUid(uid));
    }
 
 
    /**
     * 获取金币系统的用户ID
     *
     * @param uid
     * @return
     * @throws Exception
     */
    public String getUid(Long uid) throws Exception {
        String prefix = "";//systemConfigService.getValueCache(SystemEnum.location,"thirdUidPrefix");
        if (StringUtil.isNullOrEmpty(prefix)) {
            throw new Exception("用户ID前缀获取出错");
        }
        return prefix + uid;
    }
 
    public String getAppCode() {
        String appCode = "XXXX";
        //TODO 获取appcode
        return appCode;
    }
 
    public long getBalance(Long uid) {
        if (uid == null) {
            return 0;
        }
        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";
        }
        consumeGoldCorn(appCode, tuid, sourceCode, goldCornCount, title, desc);
    }
 
 
    /**
     * 添加金币
     *
     * @param uid
     * @param source
     * @param goldCorn
     * @param title
     * @param desc
     * @throws GoldUserException
     * @throws GoldAppException
     * @throws GoldTradeException
     * @throws Exception
     */
//    public void addGoldCorn(String uid, CodeCornGetSourceType source, Integer goldCorn, String title, String desc) throws GoldUserException, GoldAppException, GoldTradeException, Exception {
//        addGoldCorn(getAppCode(), getUid(uid), source.name(), goldCorn, title, desc);
//    }
//
//    public GoldCornGetSource getGoldCornGetSource(CodeCornGetSourceType source) throws GoldAppException {
//        return goldCornGetSourceService.selectByAppAndCode(getAppCode(), source.name());
//    }
 
    /**
     * 返回影视豆
     *
     * @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退款";
        }
        addGoldCorn(appCode, tuid, sourceCode, record.getGoldCorn(), title, desc);
    }
 
    public List<GoldCornRecord> getRecordList(GoldCornRecordQuery query, Long uid) throws Exception {
        query.setAppCode(getAppCode());
        query.setUid(getUid(uid));
        return goldCornRecordService.listUserRecord(query);
    }
 
 
    public long getRecordCount(GoldCornRecordQuery query, Long uid) throws Exception {
        query.setAppCode(getAppCode());
        query.setUid(getUid(uid));
        return goldCornRecordService.countUserRecord(query);
    }
 
    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);
    }
 
 
    public List<GoldCornGetSource> listGetCornSource(int page, int pageSize) throws GoldAppException {
        return goldCornGetSourceService.listShow(getAppCode(), page, pageSize);
    }
 
    public List<GoldCornRecordCountMap> countRecordByGetSource(Long uid, List<String> sourceCodes, Date minCreateTime, Date maxCreateTime) throws Exception {
        if (uid == null) {
            return new ArrayList<>();
        }
        GoldCornRecordQuery query = new GoldCornRecordQuery();
        query.setUid(getUid(uid));
        query.setAppCode(getAppCode());
        query.setSourceCodes(sourceCodes);
        query.setMinCreateTime(minCreateTime);
        query.setMaxCreateTime(maxCreateTime);
        query.setType(GoldCornRecord.TYPE_GET);
        return goldCornRecordService.countUserRecordBySource(query);
    }
 
}