puhanshu
2022-01-05 eb49b39db4de118a2161fd2a57b96f8f1b84eade
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
package com.panzhihua.service_community.service.impl;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
 
import javax.annotation.Resource;
 
import com.panzhihua.common.model.vos.community.microCommercialStreet.TopStatisticsVO;
import com.panzhihua.common.utlis.DateUtils;
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.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 java.util.Date;
 
/**
 * (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;
 
    /**
     * 分页查询戳戳游戏
     * @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
    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)) {
            //设为热门
            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("未设置戳戳币总额");
            }
            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);
    }
}