Преглед изворни кода

submit:每日一练周记录

yangdamao пре 3 година
родитељ
комит
7ff9dd8ffc

+ 54 - 0
zhongzheng-common/src/main/java/com/zhongzheng/common/utils/DateUtils.java

@@ -7,6 +7,8 @@ import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import cn.hutool.core.lang.Validator;
 import org.apache.commons.lang3.RandomStringUtils;
@@ -245,6 +247,58 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
         return cal.getTime().getTime()/1000;
     }
 
+    /**
+     * 根据当前日期获得所在周的日期区间(周一和周日日期)
+     */
+    public static Map<String, Long> getTimeInterval(Date date){
+        Map<String, Long> map = new HashMap<>();
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(date);
+        // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
+        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
+        if(1 == dayWeek){
+            cal.add(Calendar.DAY_OF_MONTH,-1);
+        }
+        // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
+        cal.setFirstDayOfWeek(Calendar.MONDAY);
+        // 获得当前日期是一个星期的第几天
+        int day = cal.get(Calendar.DAY_OF_WEEK);
+        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
+        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
+        Long imptimeBegin = cal.getTime().getTime();
+        cal.add(Calendar.DATE,6);
+        Long imptimeEnd = cal.getTime().getTime();
+        map.put("start", imptimeBegin/1000);
+        map.put("end", imptimeEnd/1000);
+        return map;
+    }
+
+
+    /**
+     * 根据当前日期获得上周的日期区间(上周周一和周日日期)
+     */
+    public static Map<String, Long> getLastTimeInterval(Date date){
+        Map<String, Long> map = new HashMap<>();
+        Calendar calendar1 = Calendar.getInstance();
+        Calendar calendar2 = Calendar.getInstance();
+        calendar1.setTime(date);
+        calendar2.setTime(date);
+        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
+        if(dayOfWeek <= 0){
+            dayOfWeek = 7;
+        }
+        int offset1 = 1 - dayOfWeek;
+        int offset2 = 7 - dayOfWeek;
+        calendar1.add(Calendar.DATE, offset1 - 7);
+        calendar2.add(Calendar.DATE, offset2 - 7);
+        // last Monday
+        Long lastBeginDate = calendar1.getTime().getTime();
+        // last Sunday
+        Long lastEndDate = calendar2.getTime().getTime();
+        map.put("laststart", lastBeginDate/1000);
+        map.put("lastend", lastEndDate/1000);
+        return map;
+    }
 
     /**
      * 获取日期格式订单号

+ 88 - 7
zhongzheng-system/src/main/java/com/zhongzheng/modules/bank/service/impl/QuestionServiceImpl.java

@@ -33,10 +33,7 @@ import com.zhongzheng.modules.exam.bo.ExamKnowledgeAddBo;
 import com.zhongzheng.modules.exam.bo.ExamKnowledgeBusinessAddBo;
 import com.zhongzheng.modules.exam.domain.ExamKnowledge;
 import com.zhongzheng.modules.exam.service.IExamKnowledgeService;
-import com.zhongzheng.modules.goods.bo.BankGoodsExamAddBo;
-import com.zhongzheng.modules.goods.bo.GoodsQueryBo;
-import com.zhongzheng.modules.goods.bo.TodayExamTobVo;
-import com.zhongzheng.modules.goods.bo.TodayExamWeekRecordDetailVo;
+import com.zhongzheng.modules.goods.bo.*;
 import com.zhongzheng.modules.goods.domain.Goods;
 import com.zhongzheng.modules.goods.domain.GoodsExamTime;
 import com.zhongzheng.modules.goods.service.IGoodsExamTimeService;
@@ -1893,6 +1890,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
             vo.setRecordCount(v.size());
             User user = iUserService.getById(k);
             vo.setUserName(user.getRealname());
+            vo.setAvatar(user.getAvatar());
             voList.add(vo);
         });
         return voList.stream().sorted(Comparator.comparing(TodayExamTobVo::getRecordCount).reversed()).collect(Collectors.toList());
@@ -1900,15 +1898,98 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
 
     @Override
     public TodayExamWeekRecordDetailVo getTodayExamWeekRecord(Long goodsId, Long userId) {
-       //获取每日一练的打卡记录
+        //获取每日一练的打卡记录
         List<UserSpecialExamRecord> recordList = userSpecialExamRecordService
                 .list(new LambdaQueryWrapper<UserSpecialExamRecord>()
                 .eq(UserSpecialExamRecord::getUserId, userId)
                 .eq(UserSpecialExamRecord::getGoodsId, goodsId));
-
-        return null;
+        if (CollectionUtils.isEmpty(recordList)){
+            return null;
+        }
+        List<UserExamRecord> userExamRecordList = new ArrayList<>();
+        //做题记录
+        recordList.forEach(item -> {
+            List<UserExamRecord> list = iUserExamRecordService.list(new LambdaQueryWrapper<UserExamRecord>()
+                    .eq(UserExamRecord::getGoodsId, goodsId)
+                    .eq(UserExamRecord::getUserId, userId)
+                    .eq(ObjectUtils.isNotNull(item.getModuleExamId()) && item.getModuleExamId() != 0, UserExamRecord::getModuleExamId, item.getModuleExamId())
+                    .eq(ObjectUtils.isNotNull(item.getChapterExamId()) && item.getChapterExamId() != 0, UserExamRecord::getChapterExamId, item.getChapterExamId())
+                    .eq(UserExamRecord::getExamId, item.getExamId())
+                    .ge(UserExamRecord::getCreateTime,item.getRecordTime())
+                    .le(UserExamRecord::getChapterExamId,item.getRecordTime() + (24*60*60))
+                    .eq(UserExamRecord::getStatus,1));
+            userExamRecordList.addAll(list);
+        });
+        //做题数和正确率
+        Long doNum = 0L;
+        Long rightNum = 0L;
+        List<UserExamRecord> weekRecord = new ArrayList<>();
+        List<UserExamRecord> lastWeekRecord = new ArrayList<>();
+        //本周和上周的时间
+        Calendar cal = Calendar.getInstance();
+        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
+        Long time = 24*60*60L;
+        Map<String, Long> timeInterval = DateUtils.getTimeInterval(cal.getTime());
+        Long start = timeInterval.get("start");
+        Long end = timeInterval.get("end") + time;
+        Map<String, Long> lastTimeInterval = DateUtils.getLastTimeInterval(cal.getTime());
+        Long laststart = lastTimeInterval.get("laststart");
+        Long lastEnd = lastTimeInterval.get("lastend")+ time;
+        for (UserExamRecord record : userExamRecordList) {
+            doNum += record.getAllQuestionNum();
+            rightNum += record.getRightQuestionNum();
+            //本周记录
+            if (record.getCreateTime() > start && record.getCreateTime() < end){
+                weekRecord.add(record);
+            }else if (record.getCreateTime() > laststart && record.getCreateTime() < lastEnd){
+                //上周记录
+                lastWeekRecord.add(record);
+            }
+        }
+        TodayExamWeekRecordDetailVo result = new TodayExamWeekRecordDetailVo();
+        result.setDoNum(doNum);
+        result.setAccuracyAvg(getPercent(rightNum.intValue(),doNum.intValue()));
+        //本周
+        if (CollectionUtils.isNotEmpty(weekRecord)){
+            List<TodayExamWeekRecordVo> voList = new ArrayList<>();
+            Long todayTime = DateUtils.getTodayZeroTime() + time;
+            Long startTime = start;
+            while ( startTime < todayTime){
+                Long endTime = startTime + time;
+                List<UserExamRecord> records = new ArrayList<>();
+                for (UserExamRecord record : weekRecord) {
+                    if (record.getCreateTime() >= startTime && record.getCreateTime() <= endTime){
+                        records.add(record);
+                    }
+                }
+                if (CollectionUtils.isNotEmpty(records)){
+                    TodayExamWeekRecordVo vo = new TodayExamWeekRecordVo();
+                    vo.setExamCount(records.size());
+                    Exam exam = iExamService.getById(records.get(0).getExamId());
+                    vo.setExamId(exam.getExamId());
+                    vo.setExamName(exam.getExamName());
+                    vo.setExamTime(startTime);
+                    voList.add(vo);
+                }
+                startTime += time;
+            }
+            result.setResultList(voList);
+            Long doSum = weekRecord.stream().mapToLong(UserExamRecord::getAllQuestionNum).sum();
+            Long rightSum = weekRecord.stream().mapToLong(UserExamRecord::getRightQuestionNum).sum();
+            result.setWeekAccuracyDoNum(doSum);
+            result.setWeekAccuracyAvg(getPercent(rightSum.intValue(),doSum.intValue()));
+        }
+        //上周
+        if (CollectionUtils.isNotEmpty(lastWeekRecord)){
+            Long lastSum = lastWeekRecord.stream().mapToLong(UserExamRecord::getAllQuestionNum).sum();
+            Long lastRigheSum = lastWeekRecord.stream().mapToLong(UserExamRecord::getRightQuestionNum).sum();
+            result.setLastWeekAccuracyDoNum(lastSum);
+            result.setLastWeekAccuracyAvg(getPercent(lastRigheSum.intValue(),lastSum.intValue()));
+        }
+        return result;
     }
 
+
     private String getPercent(Integer x,Integer y){
         Double d1 = x * 1.0;
         Double d2 = y * 1.0;

+ 3 - 0
zhongzheng-system/src/main/java/com/zhongzheng/modules/goods/bo/TodayExamTobVo.java

@@ -18,6 +18,9 @@ public class TodayExamTobVo implements Serializable {
     @ApiModelProperty("用户昵称")
     private String userName;
 
+    @ApiModelProperty("用户头像")
+    private String avatar;
+
     @ApiModelProperty("打卡次数")
     private Integer recordCount;
 

+ 3 - 3
zhongzheng-system/src/main/java/com/zhongzheng/modules/goods/bo/TodayExamWeekRecordDetailVo.java

@@ -17,7 +17,7 @@ public class TodayExamWeekRecordDetailVo implements Serializable {
     private String accuracyAvg;
 
     @ApiModelProperty("累计做题数")
-    private Integer doNum;
+    private Long doNum;
 
     @ApiModelProperty("本周做题记录")
     private List<TodayExamWeekRecordVo> resultList;
@@ -29,8 +29,8 @@ public class TodayExamWeekRecordDetailVo implements Serializable {
     private String weekAccuracyAvg;
 
     @ApiModelProperty("上周的做题数")
-    private Integer lastWeekAccuracyDoNum;
+    private Long lastWeekAccuracyDoNum;
 
     @ApiModelProperty("本周的做题数")
-    private Integer weekAccuracyDoNum;
+    private Long weekAccuracyDoNum;
 }