PrizeService.java
11.2 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.w1hd.zzhnc.service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.RandomUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.testng.collections.Lists;
import org.testng.util.Strings;
import com.w1hd.zzhnc.dao.LotteryLogDao;
import com.w1hd.zzhnc.dao.PrizeDao;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Lotterylog;
import com.w1hd.zzhnc.model.Prize;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.JsonMapper;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.RedisUtil;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
public class PrizeService {
private static final int MAX_CHANGES = 4; // 无条件最大可抽奖次数
final String LotteryLogID_ = RedisUtil.PROJECTNAME.concat("_").concat("lotteryLog_");// 中奖记录
final String PRIZE_KILL_FANSID = RedisUtil.PROJECTNAME.concat("_").concat("PRIZE_KILL_FANSID_");// 保存粉丝中奖的日志
@Autowired
PrizeDao prizeDao;
@Autowired
LotteryLogDao lotteryLogDao;
@Autowired
ActivityService activityService;
public boolean add(Prize prize) {
return prizeDao.insert(prize) > 0;
}
public boolean update(Prize p) {
return prizeDao.updateByPrimaryKeySelective(p) > 0;
}
public PageResults<Prize> search(String key, Integer page, Integer size) {
Example ex = new Example(Prize.class);
Criteria c = ex.createCriteria();
if (!Strings.isNullOrEmpty(key)) {
c.andCondition(" (name like \"%" + key + "%\" )");
}
c.andEqualTo("deleted", false);
RowBounds row = new RowBounds((page - 1) * size, size);
List<Prize> list = prizeDao.selectByExampleAndRowBounds(ex, row);
int count = prizeDao.selectCountByExample(ex);
PageResults<Prize> pageResults = new PageResults<>();
pageResults.setTotal(count);
pageResults.setPage(page);
pageResults.setPageSize(size);
pageResults.setRows(list);
return pageResults;
}
public boolean delete(String id) {
return prizeDao.deleteByPrimaryKey(id) > 0;
}
/**
* 增加一条奖项记录
*
* @return
*/
@SuppressWarnings("unchecked")
public Integer insert(Integer fansId, Integer pType, BigDecimal mny) {
Activity setting = activityService.getActivitySetting();
Prize p = null;
switch (pType) {
case 1:
p = setting.getP1();
p.setNum(p.getNum() - 1);
setting.setP1(p);
break;
case 2:
p = setting.getP2();
p.setNum(p.getNum() - 1);
setting.setP2(p);
break;
case 3:
p = setting.getP3();
p.setNum(p.getNum() - 1);
setting.setP4(p);
break;
case 4:
p = setting.getP4();
p.setNum(p.getNum() - 1);
setting.setP4(p);
break;
case 5:
p = setting.getP5();
p.setNum(p.getNum() - 1);
setting.setP5(p);
break;
case 6:
p = setting.getP6();
p.setNum(p.getNum() - 1);
setting.setP6(p);
break;
case 7:
p = setting.getP7();
p.setNum(p.getNum() - 1);
setting.setP7(p);
break;
case 8:
p = setting.getP8();
p.setNum(p.getNum() - 1);
setting.setP8(p);
break;
default:
break;
}
setting.setCurrentMny(setting.getCurrentMny().add(mny));
activityService.updateActivity(setting);
Lotterylog lotterylog = new Lotterylog();
lotterylog.setCreatedtime(new Date());
lotterylog.setFansid(fansId);
lotterylog.setMny(mny);
lotterylog.setPrizeName(p.getName());
lotterylog.setPrizeId(pType);
if ("谢谢".equals(p.getName())) {
lotterylog.setStatus(3);
} else {
lotterylog.setStatus(1);
if (mny.doubleValue() > 1.0) {
lotterylog.setStatus(2);
String order = creatOrder(fansId, (int) (mny.doubleValue() * 100));
System.out.println("抽中一个红包 >" + order);
lotterylog.setPrizeUrl(order);
}
}
lotterylog.setTurn(setting.getTurn());
lotteryLogDao.insert(lotterylog);
redisTemplate.opsForValue().set(LotteryLogID_ + lotterylog.getId(), lotterylog);
return lotterylog.getId();
}
/** 获取我的奖品 */
public List<Lotterylog> getMyLotteryLog(Integer fansId) {
Example ex = new Example(Lotterylog.class);
Criteria c = ex.createCriteria();
c.andEqualTo("fansid", fansId);
List<Integer> status = Lists.newArrayList();
status.add(1);
status.add(2);
c.andIn("status", status);
List<Lotterylog> list = lotteryLogDao.selectByExample(ex);
return list;
}
/** 核销一个奖品记录 */
public Lotterylog updateLotteryLog(Integer id, Integer fansId) {
Lotterylog lotterylog = lotteryLogDao.selectByPrimaryKey(id);
lotterylog.setStatus(2);
lotterylog.setUpdatetime(new Date());
lotteryLogDao.updateByPrimaryKey(lotterylog);
return lotterylog;
}
@Autowired
FansService fansService;
@Autowired
RedisTemplate redisTemplate;
public Integer changes(Integer fansId) {
Integer lotteryLog = (Integer) redisTemplate.opsForValue().get(PRIZE_KILL_FANSID + fansId);
if (lotteryLog == null) {
return 1;
} else {
Activity activitySetting = activityService.getActivitySetting();
int fansCount = fansService.getCountParentFansId(fansId);
Example ex = new Example(Lotterylog.class);
ex.createCriteria().andEqualTo("fansid", fansId);
int lotteryCount = lotteryLogDao.selectCountByExample(ex);
if (lotteryCount > MAX_CHANGES) {
return 0;
}
int shareCount = activitySetting.getShareCount();
if (shareCount == 0)
shareCount = 1;
int changes = fansCount / shareCount;
changes = changes - lotteryCount;
return changes + 1;
}
}
@SuppressWarnings("unchecked")
public Integer randomKill(Integer fansId, Integer count) {
if (count == 8) {
return 0;
}
redisTemplate.delete(PRIZE_KILL_FANSID + fansId);
Activity activity = activityService.getActivitySetting();
int nextInt = RandomUtils.nextInt(0, 100);
System.out.println("幸运数字 ---》》》" + nextInt);
int sumNum = 0;
int sumPro = 0;
Prize tmp = null;
List<Prize> prizeList = activityService.getPrizeList();
for (Prize p : prizeList) {
if (p != null) {
sumNum += p.getNum();
sumPro += p.getProbability();
if (p.getName().contains("谢谢")) {
tmp = p;
}
}
}
int mny = 0;
Integer logId = 0;
if (nextInt > sumPro) {
System.out.println("随机数大于总的中奖概率 随机数:" + nextInt + ",中奖总概率" + sumPro);
if (tmp != null) {
if (tmp.getIsMoney()) {
mny = RandomUtils.nextInt(tmp.getMixMoney(), tmp.getMaxMoney());
}
logId = insert(fansId, tmp.getId(), new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
}
} else {
Prize p8 = activity.getP8();
Prize p7 = activity.getP7();
Prize p6 = activity.getP6();
Prize p5 = activity.getP5();
Prize p4 = activity.getP4();
Prize p3 = activity.getP3();
Prize p2 = activity.getP2();
Prize p1 = activity.getP1();
if (p1 != null && nextInt < p1.getProbability() && p1.getNum() > 1) {
if (p1.getIsMoney()) {
mny = RandomUtils.nextInt(p1.getMixMoney(), p1.getMaxMoney());
}
logId = insert(fansId, 1, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p2 != null && nextInt < (p2.getProbability() + p1.getProbability()) && p2.getNum() > 1) {
if (p2.getIsMoney()) {
mny = RandomUtils.nextInt(p2.getMixMoney(), p2.getMaxMoney());
}
logId = insert(fansId, 2, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p3 != null && nextInt < (p3.getProbability() + p2.getProbability() + p1.getProbability())
&& p3.getNum() > 1) {
if (p3.getIsMoney()) {
mny = RandomUtils.nextInt(p3.getMixMoney(), p3.getMaxMoney());
}
logId = insert(fansId, 3, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p4 != null
&& nextInt < (p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
&& p4.getNum() > 1) {
if (p4.getIsMoney()) {
mny = RandomUtils.nextInt(p4.getMixMoney(), p4.getMaxMoney());
}
logId = insert(fansId, 4, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p5 != null && nextInt < (p5.getProbability() + p4.getProbability() + p3.getProbability()
+ p2.getProbability() + p1.getProbability()) && p5.getNum() > 1) {
if (p5.getIsMoney()) {
mny = RandomUtils.nextInt(p5.getMixMoney(), p5.getMaxMoney());
}
logId = insert(fansId, 5, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p6 != null && nextInt < (p6.getProbability() + p5.getProbability() + p4.getProbability()
+ p3.getProbability() + p2.getProbability() + p1.getProbability()) && p6.getNum() > 1) {
if (p6.getIsMoney()) {
mny = RandomUtils.nextInt(p6.getMixMoney(), p6.getMaxMoney());
}
logId = insert(fansId, 6, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p7 != null && nextInt < (p7.getProbability() + p6.getProbability() + p5.getProbability()
+ p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
&& p7.getNum() > 1) {
if (p7.getIsMoney()) {
mny = RandomUtils.nextInt(p7.getMixMoney(), p7.getMaxMoney());
}
logId = insert(fansId, 7, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else if (p8 != null
&& nextInt < (p8.getProbability() + p7.getProbability() + p6.getProbability() + p5.getProbability()
+ p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
&& p8.getNum() > 1) {
if (p8.getIsMoney()) {
mny = RandomUtils.nextInt(p7.getMixMoney(), p7.getMaxMoney());
}
logId = insert(fansId, 8, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
} else {
if (sumNum > 0) {
return randomKill(fansId, count++);
}
}
}
redisTemplate.opsForValue().set(PRIZE_KILL_FANSID + fansId, logId);
return logId;
}
public Lotterylog getLog(Integer logId) {
return (Lotterylog) redisTemplate.opsForValue().get(LotteryLogID_ + logId);
}
public static String getAuthorizeUrl(String orderId, String activityId, int fansId) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd06aec668fe359d1&redirect_uri=http://www.w1hd.com/api/wx/wxd06aec668fe359d1/payWxMpUser2/"
+ orderId + "/" + activityId + "/nmamtf&response_type=code&scope=snsapi_userinfo&state=" + fansId
+ "&component_appid=wx79ad5a35526f26fb&connect_redirect=1#wechat_redirect";
return url;
}
public static String creatOrder(Integer fansId, Integer mny) {
// /** 测试的 */
// String merchatId = "161";
// String activityId = "1290";
/** 正式的 */
String merchatId = "228";
String activityId = "1444";
String sendGet = CommonUtil.sendGet("http://www.w1hd.com/api/wx/createOrder",
"merchantid=" + merchatId + "&wxactivityId=" + activityId + "&mny=" + mny);
Object fromJsonString = JsonMapper.fromJsonString(sendGet, Map.class);
Map<String, Object> map = (Map<String, Object>) JsonMapper.fromJsonString(sendGet, Map.class);
System.out.println("订单生成请求结果:" + sendGet);
if (!map.get("code").equals(0) || !map.containsKey("data")) {
return "fail";
}
return getAuthorizeUrl(map.get("data").toString(), activityId, fansId);
}
}