From 57c69219eb101c5ecc577a77882febf0cded1aed Mon Sep 17 00:00:00 2001
From: huliguo <2023611923@qq.com>
Date: 星期一, 30 六月 2025 11:02:18 +0800
Subject: [PATCH] bug修改
---
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/miniapp/AppLotteryEventController.java | 200 +++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 188 insertions(+), 12 deletions(-)
diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/miniapp/AppLotteryEventController.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/miniapp/AppLotteryEventController.java
index 00834ab..06010bc 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/miniapp/AppLotteryEventController.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/miniapp/AppLotteryEventController.java
@@ -3,16 +3,16 @@
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.LotteryEvent;
-import com.ruoyi.goods.api.domain.UserLotteryEvent;
-import com.ruoyi.goods.api.domain.UserLotteryEventQuestions;
+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.ILotteryEventService;
-import com.ruoyi.goods.service.lottery.IUserLotteryEventQuestionsService;
-import com.ruoyi.goods.service.lottery.IUserLotteryEventService;
+import com.ruoyi.goods.service.lottery.*;
import com.ruoyi.system.api.domain.dto.LotteryEventListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -23,8 +23,14 @@
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
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
@@ -46,7 +52,14 @@
@Resource
private IUserLotteryEventQuestionsService userLotteryEventQuestionsService;
+ @Resource
+ private IUserLotteryEventQuestionsAnswersService userLotteryEventQuestionsAnswersService;
+ @Resource
+ private ILotteryEventQuestionsService lotteryEventServiceQuestionsService;
+
+ @Resource
+ private ILotteryEventQuestionsAnswersService lotteryEventQuestionsAnswersService;
@ResponseBody
@@ -57,6 +70,16 @@
})
public R<LotteryEventVo> getLotteryEvent(@PathVariable("id") String id) {
LotteryEventVo lotteryEvent = lotteryEventService.getLotteryEvent(id);
+ TLotteryEvent event = lotteryEventService.getById(id);
+ if (LocalDateTime.now().isBefore(event.getStartTime())) {
+ lotteryEvent.setStatus(1);
+ }
+ if (LocalDateTime.now().isAfter(event.getEndTime())) {
+ lotteryEvent.setStatus(3);
+ }
+ if (LocalDateTime.now().isAfter(event.getStartTime()) && LocalDateTime.now().isBefore(event.getEndTime())) {
+ lotteryEvent.setStatus(2);
+ }
return R.ok(lotteryEvent);
}
@@ -67,7 +90,7 @@
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "抽奖活动id", required = true, dataType = "String", paramType = "path")
})
- public R lotteryDraw(@PathVariable("id") String id) {
+ public R<TLotteryEventPrize> lotteryDraw(@PathVariable("id") String id) {
return lotteryEventService.lotteryDraw(id);
}
@@ -80,7 +103,20 @@
Page<AppLotteryEventPageVo> page = new Page<>();
page.setSize(dto.getPageSize());
page.setCurrent(dto.getPageNum());
- List<AppLotteryEventPageVo> activityPageVoList = lotteryEventService.pageAppLotteryEvent(page,userId);
+ List<AppLotteryEventPageVo> activityPageVoList = lotteryEventService.pageAppLotteryEvent(page, userId);
+ activityPageVoList.forEach(vo -> {
+ String startTime = vo.getStartTime();
+ String endTime = vo.getEndTime();
+ if (LocalDateTime.now().isBefore(LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))) {
+ vo.setStatus(1);
+ }
+ if (LocalDateTime.now().isAfter(LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))) {
+ vo.setStatus(3);
+ }
+ if (LocalDateTime.now().isAfter(LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) && LocalDateTime.now().isBefore(LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))) {
+ vo.setStatus(2);
+ }
+ });
return R.ok(page.setRecords(activityPageVoList));
}
@@ -93,21 +129,161 @@
})
public R<LotteryEventInfoVo> getLotteryEventInfo(@PathVariable("id") String id){
Long userId = SecurityUtils.getUserId();
- LotteryEvent lotteryEvent = lotteryEventService.getById(id);
+ TLotteryEvent 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.setEndTime(lotteryEvent.getEndTime().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));
+ int count = userLotteryEventService.count(new QueryWrapper<TUserLotteryEvent>().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));
+ if (5 == lotteryEvent.getActivityType()) {
+ TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().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());
+ if (vo.getContinueAnswer() == 1) {
+ long remainingTime = (lotteryEvent.getAnsweringTime() * 60) - (LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) - questionsServiceOne.getCreateTime().toEpochSecond(ZoneOffset.of("+8")));
+ int count1 = userLotteryEventQuestionsAnswersService.count(new QueryWrapper<TUserLotteryEventQuestionsAnswers>().eq("lottery_event_id", id).eq("user_id", userId));
+ vo.setRemainingTime(remainingTime);
+ vo.setCurrent(count1 - 1);
+ }
}
return R.ok(vo);
}
+ @ResponseBody
+ @PostMapping("/getLotteryEventQuestions")
+ @ApiOperation(value = "获取答题题目【2.0】", tags = "抽奖活动")
+ public R<LotteryEventQuestionsVo> getLotteryEventQuestions(@RequestBody LotteryEventQuestionsDto dto) {
+ Long userId = SecurityUtils.getUserId();
+ TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).eq("user_id", userId));
+ LotteryEventQuestionsVo vo = new LotteryEventQuestionsVo();
+ int count = lotteryEventServiceQuestionsService.count(new QueryWrapper<TLotteryEventQuestions>().eq("lottery_event_id", dto.getId()));
+ vo.setTotal(count);
+ //没有答题,直接从第一题开始
+ if (null == questionsServiceOne) {
+ if (-1 == dto.getStepOrDown()) {
+ return R.fail("操作失败");
+ }
+ TLotteryEventQuestions questions = lotteryEventServiceQuestionsService.getOne(new QueryWrapper<TLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort").last(" limit 1"));
+ vo.setCurrent(0);
+ vo.setName(questions.getName());
+ List<TLotteryEventQuestionsAnswers> list = lotteryEventQuestionsAnswersService.list(new QueryWrapper<TLotteryEventQuestionsAnswers>().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.getAnswers());
+ 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<TLotteryEventQuestions> list = lotteryEventServiceQuestionsService.list(new QueryWrapper<TLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort"));
+ TLotteryEventQuestions lotteryEventQuestions = list.get(current);
+ vo.setCurrent(current);
+ vo.setName(lotteryEventQuestions.getName());
+ List<TLotteryEventQuestionsAnswers> list1 = lotteryEventQuestionsAnswersService.list(new QueryWrapper<TLotteryEventQuestionsAnswers>().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.getAnswers());
+ return map;
+ }).collect(Collectors.toList());
+ if (-1 == dto.getStepOrDown()) {
+ TUserLotteryEventQuestionsAnswers one = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<TUserLotteryEventQuestionsAnswers>().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()).getAnswers());
+ }
+ }
+ vo.setOptions(options);
+ }
+ return R.ok(vo);
+ }
+
+ @ResponseBody
+ @PostMapping("/answerQuestion")
+ @ApiOperation(value = "答题操作【2.0】", tags = "抽奖活动")
+ public R answerQuestion(@RequestBody AnswerQuestionDto dto) {
+ List<TLotteryEventQuestions> list = lotteryEventServiceQuestionsService.list(new QueryWrapper<TLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).orderByAsc("sort"));
+ TLotteryEventQuestions lotteryEventQuestions = list.get(dto.getCurrent());
+ List<TLotteryEventQuestionsAnswers> list1 = lotteryEventQuestionsAnswersService.list(new QueryWrapper<TLotteryEventQuestionsAnswers>().eq("lottery_event_questions_id", lotteryEventQuestions.getId()));
+ TLotteryEventQuestionsAnswers lotteryEventQuestionsAnswers = list1.stream().filter(item -> item.getAnswers().equals(dto.getAnswer())).findAny().orElse(null);
+ //构建答题数据
+ Long userId = SecurityUtils.getUserId();
+ TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", dto.getId()).eq("user_id", userId));
+ if (null == questionsServiceOne) {
+ questionsServiceOne = new TUserLotteryEventQuestions();
+ questionsServiceOne.setId(IdUtils.simpleUUID());
+ questionsServiceOne.setUserId(userId);
+ questionsServiceOne.setLotteryEventId(dto.getId());
+ questionsServiceOne.setStatus(1);
+ questionsServiceOne.setCreateTime(LocalDateTime.now());
+ userLotteryEventQuestionsService.save(questionsServiceOne);
+ }
+ //构建或者修改答题数据
+ TUserLotteryEventQuestionsAnswers one = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<TUserLotteryEventQuestionsAnswers>().eq("lottery_event_question_id", lotteryEventQuestions.getId()).eq("user_id", userId));
+ if (null == one) {
+ one = new TUserLotteryEventQuestionsAnswers();
+ 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();
+ TLotteryEvent lotteryEvent = lotteryEventService.getById(id);
+ TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId));
+ if (null == questionsServiceOne) {
+ return R.fail("请先进行答题操作");
+ }
+ questionsServiceOne.setStatus(2);
+ questionsServiceOne.setEndTime(LocalDateTime.now());
+ //计算正确率
+ List<TUserLotteryEventQuestionsAnswers> list = userLotteryEventQuestionsAnswersService.list(new QueryWrapper<TUserLotteryEventQuestionsAnswers>().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);
+ }
}
--
Gitblit v1.7.1