puhanshu
2022-01-08 262cdc113a44bbd51f98a56ce91966dc7b2ea938
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package com.panzhihua.service_community.service.impl;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsCouponVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsEvaluateVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsMerchantVO;
import com.panzhihua.common.utlis.QRCodeUtils;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.service_community.dao.McsEvaluateDAO;
import com.panzhihua.service_community.entity.McsCoupon;
import com.panzhihua.service_community.entity.McsEvaluate;
import com.panzhihua.service_community.util.NearbyUtil;
import com.panzhihua.service_community.util.QRCodeUtil;
import com.spatial4j.core.shape.Rectangle;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetPopularForGameDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetShelfForGameDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsGameVO;
import com.panzhihua.common.model.vos.community.microCommercialStreet.TopStatisticsVO;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.service_community.dao.McsCouponDAO;
import com.panzhihua.service_community.dao.McsGameDAO;
import com.panzhihua.service_community.dao.McsMerchantDAO;
import com.panzhihua.service_community.entity.McsGame;
import com.panzhihua.service_community.entity.McsMerchant;
import com.panzhihua.service_community.service.McsGameService;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * (McsGame)表服务实现类
 *
 * @author makejava
 * @since 2021-12-28 14:37:09
 */
@Service("mcsGameService")
public class McsGameServiceImpl extends ServiceImpl<McsGameDAO, McsGame> implements McsGameService {
 
    @Resource
    private McsMerchantDAO mcsMerchantDAO;
    @Resource
    private McsCouponDAO mcsCouponDAO;
    @Resource
    private McsEvaluateDAO mcsEvaluateDAO;
 
    /**
     * 分页查询戳戳游戏
     * @param pageMcsGameDTO
     * @return
     */
    @Override
    public R pageMcsGame(PageMcsGameDTO pageMcsGameDTO) {
        Page page = new Page<>();
        page.setSize(pageMcsGameDTO.getPageSize());
        page.setCurrent(pageMcsGameDTO.getPageNum());
        IPage<McsGameVO> mcsGames = this.baseMapper.pageMcsGame(page, pageMcsGameDTO);
        return R.ok(mcsGames);
    }
 
    /**
     * 设为/取消游戏热门
     * @param setPopularForGameDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R setPopularForGame(SetPopularForGameDTO setPopularForGameDTO) {
        McsGame mcsGame = this.baseMapper.selectById(setPopularForGameDTO.getGameId());
        if (isNull(mcsGame)) {
            return R.fail("修改数据不存在");
        }
        Integer type = setPopularForGameDTO.getType();
        if (type.equals(1)) {
            //设为热门
            List<McsGame> popularGames = this.baseMapper.selectList(new QueryWrapper<McsGame>().lambda().eq(McsGame::getIsPopular, true));
            if (nonNull(popularGames) && !popularGames.isEmpty()) {
                popularGames.forEach(e -> e.setIsPopular(false));
                this.updateBatchById(popularGames);
            }
            mcsGame.setIsPopular(true);
        } else if (type.equals(2)) {
            //取消热门
            mcsGame.setIsPopular(false);
        } else {
            return R.fail("未知错误");
        }
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("修改失败,请重新尝试");
    }
 
    /**
     * 上架/下架戳戳游戏
     * @param setShelfForGameDTO
     * @return
     */
    @Override
    public R setShelfForGame(SetShelfForGameDTO setShelfForGameDTO) {
        McsGame mcsGame = this.baseMapper.selectById(setShelfForGameDTO.getGameId());
        if (isNull(mcsGame)) {
            return R.fail("修改数据不存在");
        }
        Integer type = setShelfForGameDTO.getType();
        if (type.equals(1)) {
            //上架
            McsMerchant mcsMerchant = mcsMerchantDAO.selectById(mcsGame.getMerchantId());
            Integer publishLimit = mcsMerchant.getPublishLimit();
            Integer publishCount = this.baseMapper.selectPublishCount(mcsGame.getMerchantId());
            if (publishCount >= publishLimit) {
                return R.fail("空闲戳戳点不足");
            }
            mcsGame.setStatus(2);
        } else if (type.equals(2)) {
            //下架
            mcsGame.setStatus(3);
        } else {
            return R.fail("未知错误");
        }
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("修改失败,请重新尝试");
    }
 
    /**
     * 删除戳戳游戏
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    public R deleteMcsGame(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame)) {
            return R.fail("修改数据不存在");
        }
        mcsGame.setIsDel(true);
        mcsGame.setStatus(McsGame.Status.yxj);
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("删除失败,请重新尝试");
    }
 
    /**
     * 新增戳戳游戏
     * @param mcsGameDTO
     * @return
     */
    @Override
    public R addMcsGame(McsGameDTO mcsGameDTO) {
        Long userId = mcsGameDTO.getCreatedBy();
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("未查询到商家信息");
        }
        Integer type = mcsGameDTO.getType();
        McsGame mcsGame = new McsGame();
        if (type.equals(1)) {
            //1.戳戳币游戏
            Integer allocation = mcsGameDTO.getAllocation();
            Integer coins = mcsGameDTO.getCoins();
            if (isNull(allocation)) {
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币额度");
            }
            if (allocation.equals(1)) {
                mcsGame.setSurplusCoins(mcsGameDTO.getCoins());
            }
        } else if (type.equals(2)) {
            //2.体验游戏
            Integer awardType = mcsGameDTO.getAwardType();
            if (isNull(awardType)) {
                return R.fail("缺少奖励类型");
            }
        } else {
            return R.fail("未知错误");
        }
        BeanUtils.copyProperties(mcsGameDTO, mcsGame);
        mcsGame.setSurplusCoupons(mcsGameDTO.getCoupons());
        mcsGame.setStatus(McsGame.Status.wfb);
        mcsGame.setMerchantId(mcsMerchant.getId());
        int num = this.baseMapper.insert(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("新增失败,请重新尝试");
    }
 
    /**
     * 编辑戳戳游戏
     * @param mcsGameDTO
     * @return
     */
    @Override
    public R putMcsGame(McsGameDTO mcsGameDTO) {
        McsGame mcsGame = this.baseMapper.selectById(mcsGameDTO.getId());
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        Integer type = mcsGameDTO.getType();
        if (type.equals(1)) {
            //1.戳戳币游戏
            Integer allocation = mcsGameDTO.getAllocation();
            Integer coins = mcsGameDTO.getCoins();
            if (isNull(allocation)) {
                return R.fail("缺少分配方式");
            }
            if (isNull(coins)) {
                return R.fail("未设置戳戳币额度");
            }
            Integer beforeCoins = mcsGame.getCoins();
            Integer beforeSurplusCoins = mcsGame.getSurplusCoins();
            Integer beforeUsedCoins = beforeCoins - beforeSurplusCoins;
            Integer surplusCoins = coins - beforeUsedCoins;
            if (surplusCoins < 0) {
                return R.fail("戳戳币总额少于已发出的数量");
            }
            mcsGame.setSurplusCoins(surplusCoins);
        } else if (type.equals(2)) {
            //2.体验游戏
            Integer awardType = mcsGameDTO.getAwardType();
            if (isNull(awardType)) {
                return R.fail("缺少奖励类型");
            }
        } else {
            return R.fail("未知错误");
        }
        Integer beforeCoupons = mcsGame.getCoupons();
        Integer beforeSurplusCoupons = mcsGame.getSurplusCoupons();
        Integer beforeUsedCoupons = beforeCoupons - beforeSurplusCoupons;
        BeanUtils.copyProperties(mcsGameDTO, mcsGame);
        Integer coupons = mcsGame.getCoupons();
        Integer surplusCoupons = coupons - beforeUsedCoupons;
        if (surplusCoupons < 0) {
            return R.fail("卷总数少于已发出的数量");
        }
        mcsGame.setSurplusCoupons(surplusCoupons);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("编辑失败,请重新尝试");
    }
 
    /**
     * 发布戳戳游戏
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    public R publishMcsGame(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        McsMerchant mcsMerchant = mcsMerchantDAO.selectById(mcsGame.getMerchantId());
        Integer publishLimit = mcsMerchant.getPublishLimit();
        Integer publishCount = this.baseMapper.selectPublishCount(mcsGame.getMerchantId());
        if (publishCount >= publishLimit) {
            return R.fail("空闲戳戳点不足");
        }
        mcsGame.setStatus(McsGame.Status.jxz);
        mcsGame.setPublishAt(new Date());
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("发布失败,请重新尝试");
    }
 
    /**
     * 结束戳戳游戏
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    public R finishMcsGame(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame)) {
            return R.fail("资源不存在");
        }
        mcsGame.setStatus(McsGame.Status.yjs);
        mcsGame.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsGame);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("结束失败,请重新尝试");
    }
 
    /**
     * 戳戳游戏顶部统计数据
     * @param userId
     * @return
     */
    @Override
    public R getTopStatistics(Long userId) {
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("未查询到商家信息");
        }
        TopStatisticsVO topStatisticsVO = new TopStatisticsVO();
        topStatisticsVO.setExpireAt(mcsMerchant.getExpireAt());
        if (nonNull(mcsMerchant.getExpireAt())) {
            int surplusLitDays = DateUtils.differentDays(new Date(), mcsMerchant.getExpireAt());
            topStatisticsVO.setSurplusLitDays(surplusLitDays > 0 ? surplusLitDays : 0);
        }
        Integer appliedCount = this.baseMapper.selectAppliedCount(mcsMerchant.getId());
        topStatisticsVO.setAppliedTotal(appliedCount);
        Integer verifiedCount = this.baseMapper.selectVerifiedCount(mcsMerchant.getId());
        topStatisticsVO.setVerifiedTotal(verifiedCount);
 
        Integer publishLimit = mcsMerchant.getPublishLimit();
        Integer publishCount = this.baseMapper.selectPublishCount(mcsMerchant.getId());
        Integer idleTotal = publishLimit - publishCount;
        topStatisticsVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0);
        return R.ok(topStatisticsVO);
    }
 
    /**
     * 戳戳卷领取
     * @param gameId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R applyMcsCoupon(Long gameId, Long userId) {
        McsGame mcsGame = this.baseMapper.selectById(gameId);
        if (isNull(mcsGame) || mcsGame.getIsDel()) {
            return R.fail("游戏已下架");
        }
        Integer userAppliedCount = mcsCouponDAO.selectCount(new QueryWrapper<McsCoupon>().lambda().eq(McsCoupon::getGameId, gameId).eq(McsCoupon::getUserId, userId));
        if (userAppliedCount > 0) {
            return R.fail("请勿重复领取");
        }
        Integer surplusCoupons = mcsGame.getSurplusCoupons();
        if (surplusCoupons <= 0) {
            return R.fail("抢光了");
        }
        McsCoupon mcsCoupon = new McsCoupon();
        Long snowflakeId = Snowflake.getId();
        mcsCoupon.setId(snowflakeId);
        mcsCoupon.setGameId(gameId);
        mcsCoupon.setUserId(userId);
        mcsCoupon.setQrCode(QRCodeUtil.getBase64QRCode(snowflakeId.toString()));
        Integer type = mcsGame.getType();
        if (type.equals(1)) {
            //戳戳币游戏
            Integer allocation = mcsGame.getAllocation();
            if (allocation.equals(1)) {
                //随机分配
            } else {
                //固定值
                mcsCoupon.setCoin(mcsGame.getCoins());
                mcsCoupon.setAward("戳戳币:" + mcsGame.getCoins());
            }
        } else {
            //体验游戏
            Integer awardType = mcsGame.getAwardType();
            if (awardType.equals(1)) {
                mcsCoupon.setAward("免费产品");
            } else {
                mcsCoupon.setAward("免费服务");
            }
        }
        int num = mcsCouponDAO.insert(mcsCoupon);
        if (num > 0) {
            mcsGame.setSurplusCoupons(surplusCoupons - 1);
            this.baseMapper.updateById(mcsGame);
            McsCouponVO mcsCouponVO = new McsCouponVO();
            mcsCouponVO.setId(snowflakeId);
            mcsCouponVO.setIsVerified(false);
            mcsCouponVO.setQrCode(mcsCoupon.getQrCode());
            mcsCouponVO.setAward(mcsCoupon.getAward());
            return R.ok(mcsCouponVO);
        }
        return R.fail("领取失败,请重试");
    }
 
    /**
     * h5分页查询戳戳游戏
     * @param pageMcsGameDTO
     * @return
     */
    @Override
    public R pageH5McsGame(PageMcsGameDTO pageMcsGameDTO) {
        Page page = new Page<>();
        page.setSize(pageMcsGameDTO.getPageSize());
        page.setCurrent(pageMcsGameDTO.getPageNum());
        String lat = pageMcsGameDTO.getLat();
        String lon = pageMcsGameDTO.getLon();
        Integer distance = pageMcsGameDTO.getDistance();
        IPage<McsGameVO> mcsGames;
        if (isNotEmpty(lat) && isNotEmpty(lon) && nonNull(distance)) {
            Rectangle rectangle = new NearbyUtil().getRectangle(pageMcsGameDTO.getDistance(),
                    Double.parseDouble(pageMcsGameDTO.getLon()), Double.parseDouble(pageMcsGameDTO.getLat()));
            mcsGames = this.baseMapper.pageH5McsGame(page, rectangle.getMinX(), rectangle.getMaxX(),
                    rectangle.getMinY(), rectangle.getMaxY(), pageMcsGameDTO);
        } else {
            mcsGames = this.baseMapper.pageH5McsGame(page, null, null, null, null, pageMcsGameDTO);
        }
 
        return R.ok(mcsGames);
    }
 
    /**
     * 游戏详情
     * @param gameId
     * @return
     */
    @Override
    public R getMcsGame(Long gameId) {
        McsGameVO mcsGameVO = this.baseMapper.selectDetailById(gameId);
        Integer gameCount = this.baseMapper.selectCount(new QueryWrapper<McsGame>().lambda()
                .eq(McsGame::getMerchantId, mcsGameVO.getMerchantId()).eq(McsGame::getIsDel, false));
        mcsGameVO.setGameCount(gameCount);
        return R.ok(mcsGameVO);
    }
 
    public static void main(String[] args) {
        int total = 300;
        int people = 10;
        dispath(total, people, 30);
 
    }
 
    public static void dispath(int total, int people, int min) {
        for (int i = 0; i < people - 1; i++) {
            int leftPeople = people - i;
 
            double avg = Double.valueOf(total) / leftPeople;
 
            double ratio1 = RandomUtils.nextInt(0, 3);
 
            double ratio2 = RandomUtils.nextInt(0, 1);
 
            double ratio = ratio1 + ratio2;
 
            int cur = (int) Math.floor(ratio * avg) > min ? (int) Math.floor(ratio * avg) : min;
 
            // 扣减总额阅点数目
 
            total = total - cur;
 
            System.out.format("第 %d 个红包: %d 阅点,剩下: %d 阅点\n", i + 1, cur,total);
        }
        // 剩余的就是最后一个用户的阅点额度
 
        System.out.format("第 %d 个红包: %d 阅点,剩下: 0 阅点\n", people, total);
    }
 
}