Pu Zhibing
4 天以前 d6a0c57043e8cc20694a3c678bf8e3a8f28f6499
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
package com.ruoyi.goods.controller.miniapp;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.goods.api.domain.*;
import com.ruoyi.goods.domain.dto.AnswerQuestionDto;
import com.ruoyi.goods.domain.dto.LotteryEventQuestionsDto;
import com.ruoyi.goods.domain.vo.AppLotteryEventPageVo;
import com.ruoyi.goods.domain.vo.LotteryEventInfoVo;
import com.ruoyi.goods.domain.vo.LotteryEventQuestionsVo;
import com.ruoyi.goods.domain.vo.LotteryEventVo;
import com.ruoyi.goods.service.lottery.*;
import com.ruoyi.system.api.domain.dto.LotteryEventListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @author zhibing.pu
 * @Date 2025/5/26 15:26
 */
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "小程序抽奖相关接口")
@RequestMapping("/app/lotteryEvent")
public class AppLotteryEventController {
    
    @Resource
    private ILotteryEventService lotteryEventService;
    
    @Resource
    private IUserLotteryEventService userLotteryEventService;
    
    @Resource
    private IUserLotteryEventQuestionsService userLotteryEventQuestionsService;
    
    @Resource
    private IUserLotteryEventQuestionsAnswersService userLotteryEventQuestionsAnswersService;
    
    @Resource
    private ILotteryEventQuestionsService lotteryEventServiceQuestionsService;
    
    @Resource
    private ILotteryEventQuestionsAnswersService lotteryEventQuestionsAnswersService;
    
    
    @ResponseBody
    @PostMapping("/getLotteryEvent/{id}")
    @ApiOperation(value = "获取抽奖页详情【2.0】", tags = "抽奖活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖活动id", required = true, dataType = "String", paramType = "path")
    })
    public R<LotteryEventVo> getLotteryEvent(@PathVariable("id") String id) {
        LotteryEventVo lotteryEvent = lotteryEventService.getLotteryEvent(id);
        return R.ok(lotteryEvent);
    }
    
    
    @ResponseBody
    @PostMapping("/lotteryDraw/{id}")
    @ApiOperation(value = "抽奖操作【2.0】", tags = "抽奖活动", notes = "返回奖品ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖活动id", required = true, dataType = "String", paramType = "path")
    })
    public R lotteryDraw(@PathVariable("id") String id) {
        return lotteryEventService.lotteryDraw(id);
    }
    
    
    @ResponseBody
    @PostMapping("/getLotteryEventList")
    @ApiOperation(value = "获取普通抽奖列表【2.0】", tags = "抽奖活动")
    public R<Page<AppLotteryEventPageVo>> getLotteryEventList(@RequestBody LotteryEventListDto dto){
        Long userId = SecurityUtils.getUserId();
        Page<AppLotteryEventPageVo> page = new Page<>();
        page.setSize(dto.getPageSize());
        page.setCurrent(dto.getPageNum());
        List<AppLotteryEventPageVo> activityPageVoList = lotteryEventService.pageAppLotteryEvent(page,userId);
        return R.ok(page.setRecords(activityPageVoList));
    }
    
    
    @ResponseBody
    @PostMapping("/getLotteryEventInfo/{id}")
    @ApiOperation(value = "获取抽奖活动详情【2.0】", tags = "抽奖活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖活动id", required = true, dataType = "String", paramType = "path")
    })
    public R<LotteryEventInfoVo> getLotteryEventInfo(@PathVariable("id") String id){
        Long userId = SecurityUtils.getUserId();
        LotteryEvent lotteryEvent = lotteryEventService.getById(id);
        LotteryEventInfoVo vo = new LotteryEventInfoVo();
        vo.setId(lotteryEvent.getId());
        vo.setName(lotteryEvent.getName());
        vo.setActivityProfile(lotteryEvent.getActivityProfile());
        vo.setStartTime(lotteryEvent.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        vo.setActivityContent(lotteryEvent.getActivityContent());
        int count = userLotteryEventService.count(new QueryWrapper<UserLotteryEvent>().eq("lottery_event_id", id).eq("user_id", userId));
        vo.setParticipation(count > 0);
        //答题抽奖需要判断是否答题完成
        if (5 == lotteryEvent.getActivityType()) {
            UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId));
            vo.setContinueAnswer(null != questionsServiceOne ? questionsServiceOne.getStatus() : 0);
            vo.setAnswerStartTime(null != questionsServiceOne ? questionsServiceOne.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : null);
            vo.setAnswerTimeLimit(lotteryEvent.getAnsweringTime());
        }
        return R.ok(vo);
    }
    
    @ResponseBody
    @PostMapping("/getLotteryEventQuestions")
    @ApiOperation(value = "获取答题题目【2.0】", tags = "抽奖活动")
    public R<LotteryEventQuestionsVo> getLotteryEventQuestions(@RequestBody LotteryEventQuestionsDto dto) {
        Long userId = SecurityUtils.getUserId();
        UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).eq("user_id", userId));
        LotteryEventQuestionsVo vo = new LotteryEventQuestionsVo();
        int count = lotteryEventServiceQuestionsService.count(new QueryWrapper<LotteryEventQuestions>().eq("lottery_event_id", dto.getId()));
        vo.setTotal(count);
        //没有答题,直接从第一题开始
        if (null == questionsServiceOne) {
            if (-1 == dto.getStepOrDown()) {
                return R.fail("操作失败");
            }
            LotteryEventQuestions questions = lotteryEventServiceQuestionsService.getOne(new QueryWrapper<LotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort"));
            vo.setCurrent(1);
            vo.setName(questions.getName());
            List<LotteryEventQuestionsAnswers> list = lotteryEventQuestionsAnswersService.list(new QueryWrapper<LotteryEventQuestionsAnswers>().eq("lottery_event_questions_id", questions.getId()));
            //封装答题
            List<Map<String, String>> options = list.stream().map(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("id", item.getId());
                map.put("name", item.getAnswer());
                return map;
            }).collect(Collectors.toList());
            vo.setOptions(options);
        } else {
            //上一题
            Integer current = dto.getCurrent();
            if (-1 == dto.getStepOrDown()) {
                if (current - 1 <= 0) {
                    return R.fail("操作失败");
                }
                current--;
            } else {
                //下一题
                if (current >= count) {
                    return R.fail("操作失败");
                }
                current++;
            }
            List<LotteryEventQuestions> list = lotteryEventServiceQuestionsService.list(new QueryWrapper<LotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort"));
            LotteryEventQuestions lotteryEventQuestions = list.get(current);
            vo.setCurrent(current);
            vo.setName(lotteryEventQuestions.getName());
            List<LotteryEventQuestionsAnswers> list1 = lotteryEventQuestionsAnswersService.list(new QueryWrapper<LotteryEventQuestionsAnswers>().eq("lottery_event_questions_id", lotteryEventQuestions.getId()));
            //封装答题
            List<Map<String, String>> options = list1.stream().map(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("id", item.getId());
                map.put("name", item.getAnswer());
                return map;
            }).collect(Collectors.toList());
            if (-1 == dto.getStepOrDown()) {
                UserLotteryEventQuestionsAnswers one = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<UserLotteryEventQuestionsAnswers>().eq("lottery_event_id", dto.getId()).eq("user_id", userId).orderByDesc("create_time").last(" limit 0, 1"));
                if (null != one) {
                    vo.setAnswer(lotteryEventQuestionsAnswersService.getById(one.getLotteryEventQuestionsAnswersId()).getAnswer());
                }
            }
            vo.setOptions(options);
        }
        return R.ok(vo);
    }
    
    @ResponseBody
    @PostMapping("/answerQuestion")
    @ApiOperation(value = "答题操作【2.0】", tags = "抽奖活动")
    public R answerQuestion(@RequestBody AnswerQuestionDto dto) {
        List<LotteryEventQuestions> list = lotteryEventServiceQuestionsService.list(new QueryWrapper<LotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort"));
        LotteryEventQuestions lotteryEventQuestions = list.get(dto.getCurrent());
        List<LotteryEventQuestionsAnswers> list1 = lotteryEventQuestionsAnswersService.list(new QueryWrapper<LotteryEventQuestionsAnswers>().eq("lottery_event_questions_id", lotteryEventQuestions.getId()));
        LotteryEventQuestionsAnswers lotteryEventQuestionsAnswers = list1.stream().filter(item -> item.getAnswer().equals(dto.getAnswer())).findAny().orElse(null);
        //构建答题数据
        Long userId = SecurityUtils.getUserId();
        UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).eq("user_id", userId));
        if (null == questionsServiceOne) {
            questionsServiceOne = new UserLotteryEventQuestions();
            questionsServiceOne.setId(IdUtils.simpleUUID());
            questionsServiceOne.setUserId(userId);
            questionsServiceOne.setLotteryEventId(dto.getId());
            questionsServiceOne.setStatus(1);
            questionsServiceOne.setCreateTime(LocalDateTime.now());
            userLotteryEventQuestionsService.save(questionsServiceOne);
        }
        //构建或者修改答题数据
        UserLotteryEventQuestionsAnswers one = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<UserLotteryEventQuestionsAnswers>().eq("lottery_event_question_id", lotteryEventQuestions.getId()).eq("user_id", userId));
        if (null == one) {
            one = new UserLotteryEventQuestionsAnswers();
            one.setId(IdUtils.simpleUUID());
            one.setLotteryEventId(dto.getId());
            one.setUserId(userId);
            one.setLotteryEventQuestionId(lotteryEventQuestions.getId());
            one.setLotteryEventQuestionsAnswersId(lotteryEventQuestionsAnswers.getId());
            one.setCreateTime(LocalDateTime.now());
        }
        one.setIsCorrect(0);
        //答案正确
        if (null != lotteryEventQuestionsAnswers && 1 == lotteryEventQuestionsAnswers.getIsRight()) {
            one.setIsCorrect(1);
        }
        userLotteryEventQuestionsAnswersService.saveOrUpdate(one);
        return R.ok();
    }
    
    @ResponseBody
    @PostMapping("/endAnswerQuestion/{id}")
    @ApiOperation(value = "结束答题【2.0】", tags = "抽奖活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖活动id", required = true, dataType = "String", paramType = "path")
    })
    public R<Boolean> endAnswerQuestion(@PathVariable("id") String id) {
        Long userId = SecurityUtils.getUserId();
        LotteryEvent lotteryEvent = lotteryEventService.getById(id);
        UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId));
        questionsServiceOne.setStatus(2);
        questionsServiceOne.setEndTime(LocalDateTime.now());
        //计算正确率
        List<UserLotteryEventQuestionsAnswers> list = userLotteryEventQuestionsAnswersService.list(new QueryWrapper<UserLotteryEventQuestionsAnswers>().eq("lottery_event_id", id).eq("user_id", userId));
        int count = list.size();
        int right = list.stream().filter(item -> 1 == item.getIsCorrect()).collect(Collectors.toList()).size();
        BigDecimal multiply = new BigDecimal(right).divide(new BigDecimal(count), 4, BigDecimal.ROUND_HALF_UP).setScale(4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        questionsServiceOne.setCorrectAnswerRate(multiply);
        userLotteryEventQuestionsService.updateById(questionsServiceOne);
        if (multiply.compareTo(lotteryEvent.getAccuracy()) >= 0) {
            return R.ok(true);
        }
        return R.ok(false);
    }
}