|
|
@@ -0,0 +1,405 @@
|
|
|
+package com.zhongzheng.modules.user.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.zhongzheng.common.utils.DateUtils;
|
|
|
+import com.zhongzheng.modules.exam.vo.ExamArrangementTimeVo;
|
|
|
+import com.zhongzheng.modules.user.vo.CalendarStudyVo;
|
|
|
+import com.zhongzheng.modules.user.vo.CoursePlanVo;
|
|
|
+import com.zhongzheng.modules.user.vo.DayStudyVo;
|
|
|
+import io.micrometer.core.instrument.util.TimeUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.github.pagehelper.Page;
|
|
|
+import com.zhongzheng.modules.user.bo.UserPlanAddBo;
|
|
|
+import com.zhongzheng.modules.user.bo.UserPlanQueryBo;
|
|
|
+import com.zhongzheng.modules.user.bo.UserPlanEditBo;
|
|
|
+import com.zhongzheng.modules.user.domain.UserPlan;
|
|
|
+import com.zhongzheng.modules.user.mapper.UserPlanMapper;
|
|
|
+import com.zhongzheng.modules.user.vo.UserPlanVo;
|
|
|
+import com.zhongzheng.modules.user.service.IUserPlanService;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 【请填写功能名称】Service业务层处理
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ * @date 2021-06-24
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> implements IUserPlanService {
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserPlanVo queryById(Long userId){
|
|
|
+ UserPlan db = this.baseMapper.selectById(userId);
|
|
|
+ return BeanUtil.toBean(db, UserPlanVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<UserPlanVo> queryList(UserPlanQueryBo bo) {
|
|
|
+ LambdaQueryWrapper<UserPlan> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(StrUtil.isNotBlank(bo.getSchedule()), UserPlan::getSchedule, bo.getSchedule());
|
|
|
+ lqw.eq(bo.getExamDate() != null, UserPlan::getExamDate, bo.getExamDate());
|
|
|
+ lqw.eq(StrUtil.isNotBlank(bo.getReminderTime()), UserPlan::getReminderTime, bo.getReminderTime());
|
|
|
+ lqw.eq(bo.getStudyCount() != null, UserPlan::getStudyCount, bo.getStudyCount());
|
|
|
+ lqw.eq(bo.getStudyDay() != null, UserPlan::getStudyDay, bo.getStudyDay());
|
|
|
+ lqw.eq(bo.getEndTime() != null, UserPlan::getEndTime, bo.getEndTime());
|
|
|
+ lqw.eq(bo.getStartTime() != null, UserPlan::getStartTime, bo.getStartTime());
|
|
|
+ lqw.eq(bo.getStatus() != null, UserPlan::getStatus, bo.getStatus());
|
|
|
+ lqw.eq(bo.getPitchNum() != null, UserPlan::getPitchNum, bo.getPitchNum());
|
|
|
+ lqw.eq(bo.getStudyNum() != null, UserPlan::getStudyNum, bo.getStudyNum());
|
|
|
+ return entity2Vo(this.list(lqw));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实体类转化成视图对象
|
|
|
+ *
|
|
|
+ * @param collection 实体类集合
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<UserPlanVo> entity2Vo(Collection<UserPlan> collection) {
|
|
|
+ List<UserPlanVo> voList = collection.stream()
|
|
|
+ .map(any -> BeanUtil.toBean(any, UserPlanVo.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (collection instanceof Page) {
|
|
|
+ Page<UserPlan> page = (Page<UserPlan>)collection;
|
|
|
+ Page<UserPlanVo> pageVo = new Page<>();
|
|
|
+ BeanUtil.copyProperties(page,pageVo);
|
|
|
+ pageVo.addAll(voList);
|
|
|
+ voList = pageVo;
|
|
|
+ }
|
|
|
+ return voList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean insertByAddBo(UserPlanAddBo bo) {
|
|
|
+ UserPlan add = BeanUtil.toBean(bo, UserPlan.class);
|
|
|
+ validEntityBeforeSave(add);
|
|
|
+ add.setCreateTime(DateUtils.getNowTime());
|
|
|
+ add.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ return this.save(add);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateByEditBo(UserPlanEditBo bo) {
|
|
|
+ UserPlan update = BeanUtil.toBean(bo, UserPlan.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ update.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ return this.updateById(update);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ *
|
|
|
+ * @param entity 实体类数据
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(UserPlan entity){
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if(isValid){
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return this.removeByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserPlanVo generate(UserPlanQueryBo bo) throws ParseException {
|
|
|
+ //自定义学习计划,自动生成计划
|
|
|
+ if (bo.getCourseId() != null && bo.getStudyCount() == null){
|
|
|
+ UserPlanVo userPlanVo = generateSelf(bo);
|
|
|
+ return userPlanVo;
|
|
|
+ }else{
|
|
|
+ UserPlanVo userPlanVo = generateSelfBo(bo);
|
|
|
+ return userPlanVo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserPlanVo generateSelfBo(UserPlanQueryBo bo) throws ParseException {
|
|
|
+ //获得当前课程最近一个考试计划 考生考生计划或者最近考试计划
|
|
|
+ CoursePlanVo coursePlanVo = baseMapper.selectByCourse(bo);
|
|
|
+
|
|
|
+ //最近考试时间七天前到现在的天数,学习视频时长
|
|
|
+ Date date1 = new Date(bo.getExamDate()*1000L);
|
|
|
+ //获得相隔天数
|
|
|
+ Long[] longs =bo.getStudyCount();
|
|
|
+ int dutyDays = getDutyDays(new Date(), date1,longs,bo.getStudyDay());
|
|
|
+
|
|
|
+ //计算每天需要学习多少节课
|
|
|
+ int Sec = (int)Math.ceil((double)coursePlanVo.getSectionNum()/dutyDays);
|
|
|
+
|
|
|
+ //总节数
|
|
|
+ Integer sectionNum = coursePlanVo.getSectionNum();
|
|
|
+
|
|
|
+ //获得今天日期,将每天节数写入
|
|
|
+ List<CalendarStudyVo> calendarStudyVos = new ArrayList<>();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ //课程节数不足结束循环
|
|
|
+ while (sectionNum != 0 ) {
|
|
|
+ int month=0;
|
|
|
+ //获得当月
|
|
|
+ if (!CollectionUtils.isEmpty(calendarStudyVos) && calendarStudyVos.size() > 0){
|
|
|
+ for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
|
|
|
+ if (month < calendarStudyVo.getMonth()){
|
|
|
+ month = Convert.toInt(calendarStudyVo.getMonth()+1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ month = cal.get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long firstDayOfMonth = getFirstDayOfMonth(month);
|
|
|
+ Long lastDayOfMonth = getLastDayOfMonth(month);
|
|
|
+
|
|
|
+ List<DayStudyVo> dayStudyVos = getDays(firstDayOfMonth, lastDayOfMonth);
|
|
|
+ for (DayStudyVo dayStudyVo : dayStudyVos) {
|
|
|
+ if (dayStudyVo.getDate() > Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && month == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
|
|
|
+ if (Sec > sectionNum) {
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
|
|
|
+ sectionNum = sectionNum-Sec;
|
|
|
+ }else if (Sec <= sectionNum){
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
|
|
|
+ sectionNum = 0;
|
|
|
+ }
|
|
|
+ }else if(month != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
|
|
|
+ if (Sec > sectionNum) {
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
|
|
|
+ sectionNum = sectionNum-Sec;
|
|
|
+ }else if (Sec <= sectionNum){
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
|
|
|
+ sectionNum = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
|
|
|
+ calendarStudyVo.setYear(Convert.toLong(year));
|
|
|
+ calendarStudyVo.setMonth(Convert.toLong(month));
|
|
|
+ calendarStudyVo.setDayStudyList(dayStudyVos);
|
|
|
+ calendarStudyVos.add(calendarStudyVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询是否解锁题库
|
|
|
+
|
|
|
+ UserPlanVo userPlanVo = new UserPlanVo();
|
|
|
+ userPlanVo.setCalendarStudyVo(calendarStudyVos);
|
|
|
+ List<CoursePlanVo> coursePlanVos = new ArrayList<>();
|
|
|
+ coursePlanVos.add(coursePlanVo);
|
|
|
+ userPlanVo.setCoursePlanVo(coursePlanVos);
|
|
|
+ return userPlanVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserPlanVo generateSelf(UserPlanQueryBo bo) throws ParseException {
|
|
|
+ //获得当前课程最近一个考试计划 考生考生计划或者最近考试计划
|
|
|
+ CoursePlanVo coursePlanVo = baseMapper.selectByCourse(bo);
|
|
|
+ ExamArrangementTimeVo examArrangementTimeVo = baseMapper.selectByExam(coursePlanVo.getCategoryId());
|
|
|
+
|
|
|
+ //最近考试时间七天前到现在的天数,学习视频时长
|
|
|
+ Date date1 = new Date(examArrangementTimeVo.getStartTime()*1000L);
|
|
|
+ //获得相隔天数
|
|
|
+ Long[] longs ={1L,2L,3L,4L,5L,6L};
|
|
|
+ int dutyDays = getDutyDays(new Date(), date1,longs,7L);
|
|
|
+
|
|
|
+ //计算每天需要学习多少节课
|
|
|
+ int Sec = (int)Math.ceil((double)coursePlanVo.getSectionNum()/dutyDays);
|
|
|
+
|
|
|
+ //总节数
|
|
|
+ Integer sectionNum = coursePlanVo.getSectionNum();
|
|
|
+
|
|
|
+ //获得今天日期,将每天节数写入
|
|
|
+ List<CalendarStudyVo> calendarStudyVos = new ArrayList<>();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ //课程节数不足结束循环
|
|
|
+ while (sectionNum != 0 ) {
|
|
|
+ int month=0;
|
|
|
+ //获得当月
|
|
|
+ if (!CollectionUtils.isEmpty(calendarStudyVos) && calendarStudyVos.size() > 0){
|
|
|
+ for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
|
|
|
+ if (month < calendarStudyVo.getMonth()){
|
|
|
+ month = Convert.toInt(calendarStudyVo.getMonth()+1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ month = cal.get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long firstDayOfMonth = getFirstDayOfMonth(month);
|
|
|
+ Long lastDayOfMonth = getLastDayOfMonth(month);
|
|
|
+
|
|
|
+ List<DayStudyVo> dayStudyVos = getDays(firstDayOfMonth, lastDayOfMonth);
|
|
|
+ for (DayStudyVo dayStudyVo : dayStudyVos) {
|
|
|
+ if (dayStudyVo.getDate() > Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && month == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
|
|
|
+ if (Sec > sectionNum) {
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
|
|
|
+ sectionNum = sectionNum-Sec;
|
|
|
+ }else if (Sec <= sectionNum){
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
|
|
|
+ sectionNum = 0;
|
|
|
+ }
|
|
|
+ }else if(month != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
|
|
|
+ if (Sec > sectionNum) {
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
|
|
|
+ sectionNum = sectionNum-Sec;
|
|
|
+ }else if (Sec <= sectionNum){
|
|
|
+ dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
|
|
|
+ sectionNum = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
|
|
|
+ calendarStudyVo.setYear(Convert.toLong(year));
|
|
|
+ calendarStudyVo.setMonth(Convert.toLong(month));
|
|
|
+ calendarStudyVo.setDayStudyList(dayStudyVos);
|
|
|
+ calendarStudyVos.add(calendarStudyVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询是否解锁题库
|
|
|
+
|
|
|
+ UserPlanVo userPlanVo = new UserPlanVo();
|
|
|
+ userPlanVo.setCalendarStudyVo(calendarStudyVos);
|
|
|
+ List<CoursePlanVo> coursePlanVos = new ArrayList<>();
|
|
|
+ coursePlanVos.add(coursePlanVo);
|
|
|
+ userPlanVo.setCoursePlanVo(coursePlanVos);
|
|
|
+ return userPlanVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean ifTrue(Long studyDay,Long[] studyDays) {
|
|
|
+ for (Long day : studyDays) {
|
|
|
+ if (day.equals(studyDay)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个日期之间相差的天数
|
|
|
+ * @param bdate 较大的时间
|
|
|
+ * @return 相差天数
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static int daysBetween(Date bdate) throws ParseException
|
|
|
+ {
|
|
|
+ Date smdate=new Date();
|
|
|
+ //获得七天前的日期
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(bdate);
|
|
|
+ c.add(Calendar.DATE, - 7);
|
|
|
+ bdate = c.getTime();
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ smdate=sdf.parse(sdf.format(smdate));
|
|
|
+ bdate=sdf.parse(sdf.format(bdate));
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(smdate);
|
|
|
+ long time1 = cal.getTimeInMillis();
|
|
|
+ cal.setTime(bdate);
|
|
|
+ long time2 = cal.getTimeInMillis();
|
|
|
+ long between_days=(time2-time1)/(1000*3600*24);
|
|
|
+
|
|
|
+ return Integer.parseInt(String.valueOf(between_days))-1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static List<DayStudyVo> getDays(long time_start, long time_end) {
|
|
|
+ try {
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date c_start = dateFormat.parse(dateFormat.format(new Date(time_start * 1000).getTime()));
|
|
|
+ Date c_end = dateFormat.parse(dateFormat.format(new Date(time_end * 1000).getTime()));
|
|
|
+ Calendar tempStart = Calendar.getInstance();
|
|
|
+ tempStart.setTime(c_start);
|
|
|
+ Calendar tempEnd = Calendar.getInstance();
|
|
|
+ tempEnd.setTime(c_end);
|
|
|
+ tempEnd.add(Calendar.DATE, +1);
|
|
|
+ List<DayStudyVo> dayStudyVos = new ArrayList<>();
|
|
|
+ while (tempStart.before(tempEnd)) {
|
|
|
+ String format = dateFormat.format(tempStart.getTime());
|
|
|
+ String strh = format.substring(format.length() -2,format.length());
|
|
|
+ int week = tempStart.get(Calendar.DAY_OF_WEEK)- 1;
|
|
|
+ DayStudyVo dayStudyVo = new DayStudyVo();
|
|
|
+ dayStudyVo.setStudyDay(Convert.toLong(week));
|
|
|
+ dayStudyVo.setDate(Convert.toLong(strh));
|
|
|
+ dayStudyVos.add(dayStudyVo);
|
|
|
+ tempStart.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ }
|
|
|
+ return dayStudyVos;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前月第一天
|
|
|
+ * @param month
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getFirstDayOfMonth(int month) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ // 设置月份
|
|
|
+ calendar.set(Calendar.MONTH, month - 1);
|
|
|
+ // 获取某月最小天数
|
|
|
+ int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
|
|
|
+ // 设置日历中月份的最小天数
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, firstDay);
|
|
|
+
|
|
|
+ Date time = calendar.getTime();
|
|
|
+ return time.getTime() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getLastDayOfMonth(int month) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ // 设置月份
|
|
|
+ calendar.set(Calendar.MONTH, month - 1);
|
|
|
+ // 获取某月最大天数
|
|
|
+ int lastDay=0;
|
|
|
+ //2月的平年瑞年天数
|
|
|
+ if(month==2) {
|
|
|
+ lastDay = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ }else {
|
|
|
+ lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+ // 设置日历中月份的最大天数
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, lastDay);
|
|
|
+ Date time = calendar.getTime();
|
|
|
+ return time.getTime() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static int getDutyDays(Date startDateStr, Date endDateStr,Long[] longs,Long studyDay) throws ParseException {
|
|
|
+ int result = 0;
|
|
|
+ Date startDate = startDateStr;
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(startDate);
|
|
|
+ c.add(Calendar.DATE, - Convert.toInt(studyDay));
|
|
|
+ startDate = c.getTime();
|
|
|
+ Date endDate = endDateStr;
|
|
|
+
|
|
|
+ while (startDate.compareTo(endDate) <= 0) {
|
|
|
+ for (Long integer : longs) {
|
|
|
+ if (startDate.getDay() != integer) {
|
|
|
+ result++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ startDate.setDate(startDate.getDate() + 1);
|
|
|
+ }
|
|
|
+ return result-1;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|