package com.zhongzheng.modules.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.gson.JsonArray;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.exam.vo.ExamArrangementTimeVo;
import com.zhongzheng.modules.user.domain.UserPlanCource;
import com.zhongzheng.modules.user.domain.UserStudyRecord;
import com.zhongzheng.modules.user.service.IOrderPossessUserService;
import com.zhongzheng.modules.user.service.IUserPlanCourceService;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.vo.*;
import io.micrometer.core.instrument.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.service.IUserPlanService;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author change
 * @date 2021-06-24
 */
@Service
public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> implements IUserPlanService {


    @Autowired
    private IUserPlanCourceService userPlanCourceService;

    @Autowired
    private IUserStudyRecordService userStudyRecordService;

    @Autowired
    private IOrderPossessUserService iOrderPossessUserService;

    @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
    @Transactional(rollbackFor = Exception.class)
    public UserPlanVo generate(UserPlanQueryBo bo) throws ParseException {
        //判断是否课程已被生成过课程
        Integer rank = baseMapper.selectCountCourse(bo);
        if (rank > 0){
            throw new IllegalArgumentException("您当前有课程的计划，请到计划修改上修改计划");
        }
        //查询是否拥有该课程
        Arrays.stream(bo.getCourseId()).forEach(a ->{
            Integer i = Validator.isNotNull(iOrderPossessUserService.queryByTypeAndId(Convert.toLong(a),1,bo.getUserId()))?1:0;
            if (i < 1){
                throw new IllegalArgumentException("您没有拥有该课程");
            }
        });


        //自定义学习计划，自动生成计划
        if (bo.getCourseId() != null && bo.getStudyCount() == null){
            UserPlanVo userPlanVo = generateSelf(bo);
            if (bo.getStatus() != null && bo.getStatus() == 1){
                UserPlan userPlan = new UserPlan();


                String s = JSON.toJSONString(userPlanVo.getCalendarStudyVo());
                String s1 = JSON.toJSONString(userPlanVo.getStudyCount());
                userPlan.setStudyDay(bo.getStudyDay());
                userPlan.setStudyCount(s1);
                userPlan.setUserId(bo.getUserId());
                userPlan.setSchedule(s);
                userPlan.setPitchNum(userPlanVo.getPitchNum());
                userPlan.setStudyNum(0L);
                userPlan.setCreateTime(DateUtils.getNowTime());
                userPlan.setUpdateTime(DateUtils.getNowTime());
                userPlan.setExamDate(userPlanVo.getExamDate());
                if (bo.getStatus() != null){
                    userPlan.setStatus(bo.getStatus());
                }
                if (bo.getStatus() == 1) {
                    boolean save = this.save(userPlan);
                    for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                        coursePlanVo.setPlanId(userPlan.getPlanId());
                        baseMapper.insertSource(coursePlanVo);
                    }
                }
                if (bo.getStatus() == 2){
                    userPlan.setPlanId(bo.getPlanId());
                    boolean save = this.updateById(userPlan);
                    for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                        coursePlanVo.setPlanId(userPlan.getPlanId());
                        baseMapper.insertSource(coursePlanVo);
                    }
                }
            }
            return userPlanVo;
        }else{
            validEntitySave(bo);
            UserPlanVo userPlanVo = generateSelfBo(bo);
            if (bo.getStatus() != null && bo.getStatus() == 1) {
                UserPlan userPlan = new UserPlan();
                String s = JSON.toJSONString(userPlanVo.getCalendarStudyVo());
                String s1 = JSON.toJSONString(userPlanVo.getStudyCount());

                userPlan.setStudyDay(bo.getStudyDay());
                userPlan.setStudyCount(s1);
                userPlan.setUserId(bo.getUserId());
                userPlan.setSchedule(s);
                userPlan.setExamDate(bo.getExamDate());
                userPlan.setReminderTime(bo.getReminderTime());
                userPlan.setPitchNum(userPlanVo.getPitchNum());
                userPlan.setStudyNum(0L);
                userPlan.setCreateTime(DateUtils.getNowTime());
                userPlan.setUpdateTime(DateUtils.getNowTime());
                userPlan.setStartTime(bo.getStartTime());
                userPlan.setEndTime(bo.getEndTime());
                userPlan.setStudyDay(bo.getStudyDay());
                if (bo.getStatus() != null){
                    userPlan.setStatus(bo.getStatus());
                }
                if (bo.getStatus() == 1) {
                    boolean save = this.save(userPlan);
                    for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                        coursePlanVo.setPlanId(userPlan.getPlanId());
                        baseMapper.insertSource(coursePlanVo);
                    }
                }
                if (bo.getStatus() == 2){
                    userPlan.setPlanId(bo.getPlanId());
                    boolean save = this.updateById(userPlan);
                    for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                        coursePlanVo.setPlanId(userPlan.getPlanId());
                        baseMapper.insertSource(coursePlanVo);
                    }
                }
            }
            return userPlanVo;
        }
    }

    private void validEntitySave(UserPlanQueryBo bo) {
        if (bo.getStudyDay() == null){
            throw new IllegalArgumentException("复习天数为空");
        }
        if (bo.getCourseId() == null){
            throw new IllegalArgumentException("课程为空");
        }
        if (bo.getStudyCount() == null){
            throw new IllegalArgumentException("周复习为空");
        }
        if (bo.getExamDate() == null){
            throw new IllegalArgumentException("考试时间为空");
        }

        if (bo.getStartTime() == null || bo.getEndTime() == null){
            throw new IllegalArgumentException("起止天数为空");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserPlanVo> userPlan(UserPlanQueryBo bo) {
        List<UserPlan> userPlans = baseMapper.selectByUserPlan(bo);
        List<UserPlanVo> userPlanVos = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        for (UserPlan userPlan : userPlans) {
            UserPlanVo userPlanVo = new UserPlanVo();
            userPlanVo.setUserId(userPlan.getUserId());
            userPlanVo.setExamDate(userPlan.getExamDate());
            userPlanVo.setReminderTime(userPlan.getReminderTime());
            userPlanVo.setStudyDay(userPlan.getStudyDay());
            userPlanVo.setStartTime(userPlan.getStartTime());
            userPlanVo.setEndTime(userPlan.getEndTime());
            userPlanVo.setPitchNum(userPlan.getPitchNum());
            userPlanVo.setStudyNum(userPlan.getStudyNum());
            userPlanVo.setPlanId(userPlan.getPlanId());

            //得到日历日期
            List<CalendarStudyVo> calendarStudyVos = JSONObject.parseArray(userPlan.getSchedule(), CalendarStudyVo.class);
            //得到学习的周
            List<Long> cont = JSONObject.parseArray(userPlan.getStudyCount(), Long.class);
            Long[] strings = new Long[cont.size()];
            cont.toArray(strings);

            userPlanVo.setCalendarStudyVo(calendarStudyVos);
            userPlanVo.setStudyCount(strings);
            userPlanVo.setCoursePlanVo(null);

            //获得计划的课程
            List<UserPlanCource> userPlanClsList = baseMapper.selectCourse(userPlan.getPlanId());
            //初始化需要得到的数组
            Integer[] array = new Integer[userPlanClsList.size()];
            //使用for循环得到数组
            for (int i = 0; i < userPlanClsList.size(); i++) {
                array[i] = Convert.toInt(userPlanClsList.get(i).getSourceId());
            }
            bo.setCourseId(array);
            List<CoursePlanVo> coursePlanVo = baseMapper.selectByListCourse(bo);


            coursePlanVo.stream().forEach(s -> {
                //获得当天学习课程节数
                calendarStudyVos.stream().filter(c -> c.getMonth() == cal.get(Calendar.MONTH) + 1).forEach(c -> {
                    c.getDayStudyList().stream()
                            .filter(g -> g.getDate() == cal.get(Calendar.DAY_OF_MONTH))
                            .forEach(d -> {
                                if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0) {
                                    Map<Long, Long> courseDayMap = d.getCourseDayMap();
                                    Long aLong = courseDayMap.get(s.getCourseId());
                                    s.setStudyGoal(aLong);
                                }
                            });
                });
            });

            //更新当天学习节数是否已经完成
            //代表前面是否有未完成课程
            Long studyNum = 0L;
            boolean goal = true;
            for (CoursePlanVo b : coursePlanVo) {
                if (goal == false) {
                    break;
                }
                List<UserStudyRecord> studyCount = userStudyRecordService.selectUserStudy(bo.getUserId(), Convert.toInt(b.getCourseId()));
                List<UserStudyRecord> myList = studyCount.stream().distinct().collect(Collectors.toList());
                studyNum= studyNum+Convert.toLong(myList.size());
                //更新当天学习节数是否已经完成
                if (b.getStudyGoal() != null && studyCount.size() >= b.getStudyGoal() ) {
                    for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                        if (calendarStudyVo.getMonth() == cal.get(Calendar.MONTH) + 1) {
                            for (DayStudyVo d : calendarStudyVo.getDayStudyList()) {
                                if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() == cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                    if (d.getPerform() == 1){
                                        break;
                                    }
                                    Map<Long, Long> sectionIdMap = d.getSectionIdMap();
                                    for (UserStudyRecord userStudyRecord : studyCount) {
                                        sectionIdMap.remove(userStudyRecord.getSectionId());
                                    }
                                    if (sectionIdMap.isEmpty()) {
                                        d.setPerform(1);
                                    } else {
                                        d.setPerform(0);
                                        goal = false;
                                    }
                                }
                                if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() < cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                    d.setPerform(2);
                                }
                            }
                        }

                    }

                } else {
                    for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                        for (DayStudyVo d : calendarStudyVo.getDayStudyList()) {
                            if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() < cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                d.setPerform(2);
                            }
                            if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() == cal.get(Calendar.DAY_OF_MONTH)) {
                                d.setPerform(0);
                            }
                        }
                    }
                    goal = false;
                }

            }
            UserPlan userPlan1 = new UserPlan();
            userPlan1.setPlanId(userPlan.getPlanId());
            String calendar = JSON.toJSONString(calendarStudyVos);
            userPlan1.setSchedule(calendar);
            userPlan1.setUpdateTime(DateUtils.getNowTime());
            userPlan1.setStudyNum(studyNum);
            int i = baseMapper.updateById(userPlan1);
            userPlanVo.setCoursePlanVo(coursePlanVo);
            out:if (!CollectionUtils.isEmpty(userPlanVo.getCalendarStudyVo())){
                for (CalendarStudyVo calendarStudyVo : userPlanVo.getCalendarStudyVo()) {
                    if (calendarStudyVo.getMonth() == (cal.get(Calendar.MONTH)+1)){
                        break out;
                    }
                }
                //得到日历日期
                Long firstDayOfMonth = getFirstDayOfMonth(cal.get(Calendar.MONTH)+1);
                Long lastDayOfMonth = getLastDayOfMonth(cal.get(Calendar.MONTH)+1);
                List<DayStudyVo> dayStudyVosOne = getDays(firstDayOfMonth,lastDayOfMonth);
                CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
                calendarStudyVo.setDayStudyList(dayStudyVosOne);
                calendarStudyVo.setYear(Convert.toLong(cal.get(Calendar.YEAR)));
                calendarStudyVo.setMonth(Convert.toLong(cal.get(Calendar.MONTH)+1));
                calendarStudyVos.add(calendarStudyVo);
                userPlanVo.setCalendarStudyVo(calendarStudyVos);
            }
            userPlanVos.add(userPlanVo);

        }

        return userPlanVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGenerate(UserPlanAddBo bo) throws ParseException {
        UserPlanQueryBo userPlanQueryBo = BeanUtil.toBean(bo, UserPlanQueryBo.class);
        if (bo.getStatus() == 0) {
            UserPlan userPlan = new UserPlan();
            userPlan.setPlanId(bo.getPlanId());
            userPlan.setStatus(0);
            userPlan.setUpdateTime(DateUtils.getNowTime());
            int i = baseMapper.updateById(userPlan);
        }else {
            //自定义学习计划，自动生成计划
                UserPlanVo userPlanVo = generateSelfBo(userPlanQueryBo);
                if (bo.getStatus() != null) {
                    UserPlan userPlan = new UserPlan();
                    String s = JSON.toJSONString(userPlanVo.getCalendarStudyVo());
                    String s1 = JSON.toJSONString(userPlanVo.getStudyCount());
                    userPlan.setUpdateTime(DateUtils.getNowTime());
                    userPlan.setStudyDay(bo.getStudyDay());
                    userPlan.setStudyCount(s1);
                    userPlan.setUserId(bo.getUserId());
                    userPlan.setSchedule(s);
                    userPlan.setExamDate(bo.getExamDate());
                    userPlan.setReminderTime(bo.getReminderTime());
                    userPlan.setPitchNum(userPlanVo.getPitchNum());
                    userPlan.setStudyNum(0L);
                    userPlan.setCreateTime(DateUtils.getNowTime());
                    userPlan.setUpdateTime(DateUtils.getNowTime());
                    userPlan.setStartTime(bo.getStartTime());
                    userPlan.setEndTime(bo.getEndTime());
                    userPlan.setStudyDay(bo.getStudyDay());
                    if (bo.getStatus() != null) {
                        userPlan.setStatus(bo.getStatus());
                    }
                    if (bo.getStatus() == 1) {
                        boolean save = this.save(userPlan);
                        for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                            coursePlanVo.setPlanId(userPlan.getPlanId());
                            baseMapper.insertSource(coursePlanVo);
                        }
                    }
                    if (bo.getStatus() == 2) {
                        userPlan.setPlanId(bo.getPlanId());
                        userPlan.setStatus(1);
                        boolean save = this.updateById(userPlan);
                        LambdaQueryWrapper<UserPlanCource> lqw = Wrappers.lambdaQuery();
                        lqw.eq(bo.getPlanId() != null,UserPlanCource::getPlanId,bo.getPlanId());
                        userPlanCourceService.remove(lqw);
                        for (CoursePlanVo coursePlanVo : userPlanVo.getCoursePlanVo()) {
                            coursePlanVo.setPlanId(userPlan.getPlanId());
                            baseMapper.insertSource(coursePlanVo);
                        }
                    }
            }

        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserPlanVo userPlanSeven(UserPlanQueryBo bo) {
        List<UserPlan> userPlans = baseMapper.selectByUserPlan(bo);
        UserPlanVo userPlanVos = new UserPlanVo();
        Calendar cal = Calendar.getInstance();
        if (!CollectionUtils.isEmpty(userPlans)) {
            UserPlan userPlan = userPlans.get(0);

            //得到日历日期
            List<CalendarStudyVo> calendarStudyVos = JSONObject.parseArray(userPlan.getSchedule(), CalendarStudyVo.class);


            //获得计划的课程
            List<UserPlanCource> userPlanClsList = baseMapper.selectCourse(userPlan.getPlanId());
            //初始化需要得到的数组
            Integer[] array = new Integer[userPlanClsList.size()];
            //使用for循环得到数组
            for(int i = 0; i < userPlanClsList.size();i++){
                array[i] = Convert.toInt(userPlanClsList.get(i).getSourceId());
            }
            bo.setCourseId(array);
            List<CoursePlanVo> coursePlanVo = baseMapper.selectByListCourse(bo);

            userPlanVos.setCoursePlanVo(coursePlanVo);
            List<DayStudyVo> dayStudyVos = new ArrayList<>();
            List<DayStudyVo> finalDayStudyVos = dayStudyVos;

            //更新当天学习节数是否已经完成
            //代表前面是否有未完成课程
            Long studyNum = 0L;
            boolean goal = true;
            for (CoursePlanVo b : coursePlanVo) {
                if (goal == false) {
                    break;
                }
                List<UserStudyRecord> studyCount = userStudyRecordService.selectUserStudy(bo.getUserId(), Convert.toInt(b.getCourseId()));
                List<UserStudyRecord> myList = studyCount.stream().distinct().collect(Collectors.toList());
                studyNum= studyNum+Convert.toLong(myList.size());
                //更新当天学习节数是否已经完成
                if (b.getStudyGoal() != null && studyCount.size() >= b.getStudyGoal() ) {
                    for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                        if (calendarStudyVo.getMonth() == cal.get(Calendar.MONTH) + 1) {
                            for (DayStudyVo d : calendarStudyVo.getDayStudyList()) {
                                if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() == cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                    if (d.getPerform() == 1){
                                        break;
                                    }
                                    Map<Long, Long> sectionIdMap = d.getSectionIdMap();
                                    for (UserStudyRecord userStudyRecord : studyCount) {
                                        sectionIdMap.remove(userStudyRecord.getSectionId());
                                    }
                                    if (sectionIdMap.isEmpty()) {
                                        d.setPerform(1);
                                    } else {
                                        d.setPerform(0);
                                        goal = false;
                                    }
                                }
                                if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() < cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                    d.setPerform(2);
                                }
                            }
                        }

                    }

                } else {
                    for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                        for (DayStudyVo d : calendarStudyVo.getDayStudyList()) {
                            if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() < cal.get(Calendar.DAY_OF_MONTH) && d.getPerform() == 0) {
                                d.setPerform(2);
                            }
                            if (d.getStudyCourseKnob() != null && d.getStudyCourseKnob() != 0 && d.getDate() == cal.get(Calendar.DAY_OF_MONTH)) {
                                d.setPerform(0);
                            }
                        }
                    }
                    goal = false;
                }

            }

            UserPlan userPlan1 = new UserPlan();
            userPlan1.setPlanId(userPlan.getPlanId());
            String calendar = JSON.toJSONString(calendarStudyVos);
            userPlan1.setSchedule(calendar);
            userPlan1.setUpdateTime(DateUtils.getNowTime());
            userPlan1.setStudyNum(studyNum);
            int i = baseMapper.updateById(userPlan1);


            calendarStudyVos.stream().filter(c -> c.getMonth() == (cal.get(Calendar.MONTH)+1))
                    .forEach(c -> {
                        finalDayStudyVos.addAll(c.getDayStudyList().stream()
                                .filter(v -> v.getDate() < 7 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() > cal.get(Calendar.DAY_OF_MONTH) - 7)
                                .collect(Collectors.toList()));
                    });
            switch (cal.get(Calendar.DAY_OF_WEEK)- 1){
                case 0:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v -> v.getDate() <= cal.get(Calendar.DAY_OF_MONTH) + 7)
                            .collect(Collectors.toList());
                    break;
                case 1:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 6 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) -1)
                            .collect(Collectors.toList());
                    break;
                case 2:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 5 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 2)
                            .collect(Collectors.toList());
                    break;
                case 3:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 4 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 3)
                            .collect(Collectors.toList());
                    break;
                case 4:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 3 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 4)
                            .collect(Collectors.toList());
                    break;
                case 5:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 2 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 5)
                            .collect(Collectors.toList());
                    break;
                case 6:
                    dayStudyVos=finalDayStudyVos.stream()
                            .filter(v ->  v.getDate() < 1 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 6)
                            .collect(Collectors.toList());
                    break;
            }
            List<DayStudyVo> finalDayStudyVos1 = dayStudyVos;
            calendarStudyVos.stream().forEach(c -> {
                c.setDayStudyList(finalDayStudyVos1);
            });
            userPlanVos.setCalendarStudyVo(calendarStudyVos);
            out:if (!CollectionUtils.isEmpty(userPlanVos.getCoursePlanVo())){
                for (CalendarStudyVo calendarStudyVo : userPlanVos.getCalendarStudyVo()) {
                    if (calendarStudyVo.getMonth() == (cal.get(Calendar.MONTH)+1)){
                        break out;
                    }
                }
                //得到日历日期
                Long firstDayOfMonth = getFirstDayOfMonth(cal.get(Calendar.MONTH)+1);
                Long lastDayOfMonth = getLastDayOfMonth(cal.get(Calendar.MONTH)+1);
                List<DayStudyVo> dayStudyVosOne = getDays(firstDayOfMonth,lastDayOfMonth);
                switch (cal.get(Calendar.DAY_OF_WEEK)- 1){
                    case 0:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v -> v.getDate() <= cal.get(Calendar.DAY_OF_MONTH) + 7)
                                .collect(Collectors.toList());
                        break;
                    case 1:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 6 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) -1)
                                .collect(Collectors.toList());
                        break;
                    case 2:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 5 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 2)
                                .collect(Collectors.toList());
                        break;
                    case 3:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 4 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 3)
                                .collect(Collectors.toList());
                        break;
                    case 4:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 3 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 4)
                                .collect(Collectors.toList());
                        break;
                    case 5:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 2 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 5)
                                .collect(Collectors.toList());
                        break;
                    case 6:
                        dayStudyVosOne=dayStudyVosOne.stream()
                                .filter(v ->  v.getDate() < 1 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 6)
                                .collect(Collectors.toList());
                        break;
                }
                CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
                calendarStudyVo.setDayStudyList(dayStudyVosOne);
                calendarStudyVo.setYear(Convert.toLong(cal.get(Calendar.YEAR)));
                calendarStudyVo.setMonth(Convert.toLong(cal.get(Calendar.MONTH)+1));
                calendarStudyVos.add(calendarStudyVo);
                userPlanVos.setCalendarStudyVo(calendarStudyVos);
            }
        }else  {
            //得到日历日期
            List<CalendarStudyVo> calendarStudyVos = new ArrayList<>();
            Long firstDayOfMonth = getFirstDayOfMonth(cal.get(Calendar.MONTH)+1);
            Long lastDayOfMonth = getLastDayOfMonth(cal.get(Calendar.MONTH)+1);
            List<DayStudyVo> dayStudyVos = getDays(firstDayOfMonth,lastDayOfMonth);
            switch (cal.get(Calendar.DAY_OF_WEEK)- 1){
                case 0:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v -> v.getDate() <= cal.get(Calendar.DAY_OF_MONTH) + 7)
                            .collect(Collectors.toList());
                    break;
                case 1:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 6 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) -1)
                            .collect(Collectors.toList());
                    break;
                case 2:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 5 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 2)
                            .collect(Collectors.toList());
                    break;
                case 3:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 4 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 3)
                            .collect(Collectors.toList());
                    break;
                case 4:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 3 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 4)
                            .collect(Collectors.toList());
                    break;
                case 5:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 2 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 5)
                            .collect(Collectors.toList());
                    break;
                case 6:
                    dayStudyVos=dayStudyVos.stream()
                            .filter(v ->  v.getDate() < 1 + cal.get(Calendar.DAY_OF_MONTH) && v.getDate() >= cal.get(Calendar.DAY_OF_MONTH) - 6)
                            .collect(Collectors.toList());
                    break;
            }
            CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
            calendarStudyVo.setDayStudyList(dayStudyVos);
            calendarStudyVo.setYear(Convert.toLong(cal.get(Calendar.YEAR)));
            calendarStudyVo.setMonth(Convert.toLong(cal.get(Calendar.MONTH)+1));
            calendarStudyVos.add(calendarStudyVo);
            userPlanVos.setCalendarStudyVo(calendarStudyVos);
        }


        return userPlanVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editPlan(UserPlanQueryBo bo) throws ParseException {
        List<UserPlan> userPlans = baseMapper.selectByUserPlan(bo);
        if (CollectionUtils.isEmpty(userPlans)){
            throw new IllegalArgumentException("没有该计划");
        }
        UserPlan userPlan = userPlans.get(0);

        //得到日历日期
        List<CalendarStudyVo> calendarStudyVos = JSONObject.parseArray(userPlan.getSchedule(), CalendarStudyVo.class);

        //获得计划的课程
        List<UserPlanCource> userPlanClsList = baseMapper.selectCourse(userPlan.getPlanId());
        //初始化需要得到的数组
        Integer[] array = new Integer[userPlanClsList.size()];
        //使用for循环得到数组
        for (int i = 0; i < userPlanClsList.size(); i++) {
            array[i] = Convert.toInt(userPlanClsList.get(i).getSourceId());
        }
        bo.setCourseId(array);
        List<CoursePlanVo> coursePlanVo = baseMapper.selectByListCourse(bo);

        for (CoursePlanVo planVo : coursePlanVo) {
            List<UserStudyRecord> studyCount = userStudyRecordService.selectUserStudy(bo.getUserId(), Convert.toInt(planVo.getCourseId()));
            List<UserStudyRecord> myList = studyCount.stream().distinct().collect(Collectors.toList());

            List<Long> listId= baseMapper.selectByCourseId(planVo.getCourseId());
            Map<Long,Long> listMap = new HashMap<>();
            for (Long aLong : listId) {
                listMap.put(aLong,planVo.getCourseId());
            }
            for (UserStudyRecord userStudyRecord : myList) {
                listMap.remove(userStudyRecord);
            }


            //最近考试时间七天前到现在的天数，学习视频时长
            Date date1 = new Date(userPlan.getExamDate()*1000L);
            //获得相隔天数
            //得到学习的周
            List<Long> cont = JSONObject.parseArray(userPlan.getStudyCount(), Long.class);
            Long[] longs = new Long[cont.size()];
            cont.toArray(longs);

            int dutyDays = getDutyDays(new Date(), date1,longs,7L);

            //计算每天需要学习多少节课
            int size = listMap.size();

            int Sec = (int)Math.ceil((double)listMap.size()/dutyDays);

            //总节数
            Integer sectionNum = listMap.size();
            if(sectionNum <= 0){
                throw new IllegalArgumentException("该课程没有节数，无法生成计划");
            }

            //key和value都循环
            listId = new ArrayList<>();
            for(Map.Entry<Long,Long> entry : listMap.entrySet()) {
                listId.add(entry.getKey());
            }

            //获得今天日期，将每天节数写入
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            Integer month = 0;

            for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                if (month < calendarStudyVo.getMonth()){
                    month = Convert.toInt(calendarStudyVo.getMonth());
                }
                for (DayStudyVo dayStudyVo : calendarStudyVo.getDayStudyList()) {
                    if (dayStudyVo.getDate() > Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && calendarStudyVo.getMonth() == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
                        dayStudyVo.setStudyCourseKnob(0L);
                        dayStudyVo.setCourseDayMap(new HashMap<>());
                        dayStudyVo.setSectionIdMap(new HashMap<>());
                    }else if(calendarStudyVo.getMonth() > (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                        dayStudyVo.setStudyCourseKnob(0L);
                        dayStudyVo.setCourseDayMap(new HashMap<>());
                        dayStudyVo.setSectionIdMap(new HashMap<>());
                    }
                }
            }

            //将原有的月份添加课程的节数
            for (CalendarStudyVo calendarStudyVo : calendarStudyVos) {
                if (month < calendarStudyVo.getMonth()){
                    month = Convert.toInt(calendarStudyVo.getMonth());
                }
                for (DayStudyVo dayStudyVo : calendarStudyVo.getDayStudyList()) {
                    if (dayStudyVo.getDate() > Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && calendarStudyVo.getMonth() == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
                        if (sectionNum == 0){
                            break;
                        }
                        if (Sec < sectionNum) {
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec)+dayStudyVo.getStudyCourseKnob());
                            sectionNum = sectionNum-Sec;
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);
                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = new HashMap<>();
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            dayStudyVo.setPerform(0);
                        }else if (Sec >= sectionNum){
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum)+dayStudyVo.getStudyCourseKnob());
                            sectionNum = 0;
                            dayStudyVo.setPerform(0);
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = new HashMap<>();
                            for (Long aLong : listId) {
                                sectionIdMap.put(aLong,planVo.getCourseId());
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            dayStudyVo.setCourseDayMap(courseDayMap);
                        }
                    }else if(calendarStudyVo.getMonth() > (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                        if (Sec < sectionNum) {
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec)+dayStudyVo.getStudyCourseKnob());

                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);
                            dayStudyVo.setPerform(0);
                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = new HashMap<>();
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            sectionNum = sectionNum-Sec;
                        }else if (Sec >= sectionNum){
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum)+dayStudyVo.getStudyCourseKnob());
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);
                            dayStudyVo.setPerform(0);
                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = new HashMap<>();
                            for (Long aLong : listId) {
                                sectionIdMap.put(aLong,planVo.getCourseId());
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            sectionNum = 0;

                        }
                    }
                }
            }

            //原有课程不满足课程节数则再创建课程
            if (sectionNum != 0){
                while (sectionNum != 0 ) {
                    if (CollectionUtils.isEmpty(calendarStudyVos)) {
                        month = cal.get(Calendar.MONTH)+1;
                    }else {
                        ++month;
                    }
                    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 (sectionNum == 0){
                                break;
                            }
                            if (Sec < sectionNum) {
                                dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                                sectionNum = sectionNum-Sec;
                                Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                                if (courseDayMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                                dayStudyVo.setCourseDayMap(courseDayMap);
                                //添加课程学习节ID
                                Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                                if (sectionIdMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                for (int i = 0; i < Sec; i++) {
                                    if (listId.size() >= Sec) {
                                        sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                        listId.remove(0);
                                    }
                                }
                                dayStudyVo.setSectionIdMap(sectionIdMap);
                                dayStudyVo.setPerform(0);
                            }else if (Sec >= sectionNum){
                                dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                                Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                                if (courseDayMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                                dayStudyVo.setCourseDayMap(courseDayMap);

                                //添加课程学习节ID
                                Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                                if (sectionIdMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                for (int i = 0; i < Sec; i++) {
                                    if (listId.size() >= Sec) {
                                        sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                        listId.remove(0);
                                    }
                                }
                                dayStudyVo.setSectionIdMap(sectionIdMap);
                                sectionNum = 0;
                                dayStudyVo.setPerform(0);
                            }
                        }else if(month > (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                            if (Sec < sectionNum) {
                                dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                                sectionNum = sectionNum-Sec;
                                Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                                if (courseDayMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                                dayStudyVo.setCourseDayMap(courseDayMap);

                                //添加课程学习节ID
                                Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                                if (sectionIdMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                for (int i = 0; i < Sec; i++) {
                                    if (listId.size() >= Sec) {
                                        sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                        listId.remove(0);
                                    }
                                }
                                dayStudyVo.setSectionIdMap(sectionIdMap);
                                dayStudyVo.setPerform(0);
                            }else if (Sec >= sectionNum){
                                dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                                Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                                if (courseDayMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                                dayStudyVo.setCourseDayMap(courseDayMap);
                                sectionNum = 0;

                                //添加课程学习节ID
                                Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                                if (sectionIdMap == null) {
                                    courseDayMap = new HashMap<>();
                                }
                                for (int i = 0; i < Sec; i++) {
                                    if (listId.size() >= Sec) {
                                        sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                        listId.remove(0);
                                    }
                                }
                                dayStudyVo.setSectionIdMap(sectionIdMap);
                                dayStudyVo.setPerform(0);
                            }
                        }
                    }
                    CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
                    calendarStudyVo.setYear(Convert.toLong(year));
                    calendarStudyVo.setMonth(Convert.toLong(month));
                    calendarStudyVo.setDayStudyList(dayStudyVos);
                    List<CalendarStudyVo> calendarStudyVo1 = calendarStudyVos;
                    calendarStudyVo1.add(calendarStudyVo);
                }
            }

        }
        UserPlan userPlan1 = new UserPlan();
        userPlan1.setPlanId(userPlan.getPlanId());
        String calendar = JSON.toJSONString(calendarStudyVos);
        userPlan1.setSchedule(calendar);
        userPlan1.setUpdateTime(DateUtils.getNowTime());
        int i = baseMapper.updateById(userPlan1);


        return true;
    }


    private UserPlanVo generateSelfBo(UserPlanQueryBo bo) throws ParseException {


        //获得当前课程最近一个考试计划  考生考生计划或者最近考试计划
        List<CoursePlanVo> coursePlanVo = baseMapper.selectByListCourse(bo);

        UserPlanVo userPlanVo1 = null;
        Long category=coursePlanVo.get(0).getCategoryId();
        for (CoursePlanVo planVo : coursePlanVo) {
            if (!category.equals(planVo.getCategoryId())){
                throw new IllegalArgumentException("课程类型不符合同一类型");
            }
            if (userPlanVo1 == null) {
                userPlanVo1 = generateList(bo, planVo);
            }else {
                userPlanVo1 = generateOr(bo,planVo,userPlanVo1);
            }
        }
        return userPlanVo1;
    }

    private UserPlanVo generateOr(UserPlanQueryBo bo, CoursePlanVo planVo, UserPlanVo userPlanVo1) throws ParseException {
        //最近考试时间七天前到现在的天数，学习视频时长
        Date date1 = new Date(bo.getExamDate()*1000L);
        //获得相隔天数
        Long[] longs =bo.getStudyCount();
        int dutyDays = getDutyDays(new Date(), date1,longs,7L);

        //计算每天需要学习多少节课
        int Sec = (int)Math.ceil((double)planVo.getSectionNum()/dutyDays);

        //总节数
        Integer sectionNum = planVo.getSectionNum();
        if(sectionNum <= 0){
            throw new IllegalArgumentException("课程错误");
        }
        //查出节的ID
        List<Long> listId= baseMapper.selectByCourseId(planVo.getCourseId());
        //获得今天日期，将每天节数写入
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        Integer month = 0;
        //将原有的月份添加课程的节数
        for (CalendarStudyVo calendarStudyVo : userPlanVo1.getCalendarStudyVo()) {
            if (month < calendarStudyVo.getMonth()){
                month = Convert.toInt(calendarStudyVo.getMonth());
            }
            for (DayStudyVo dayStudyVo : calendarStudyVo.getDayStudyList()) {
                if (dayStudyVo.getDate() > Calendar.getInstance().get(Calendar.DAY_OF_MONTH) && calendarStudyVo.getMonth() == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
                    if (sectionNum == 0){
                        break;
                    }
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec)+dayStudyVo.getStudyCourseKnob());
                        sectionNum = sectionNum-Sec;
                        Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                        courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(courseDayMap);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setPerform(0);
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum)+dayStudyVo.getStudyCourseKnob());
                        sectionNum = 0;
                        dayStudyVo.setPerform(0);
                        Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                        courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,planVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setCourseDayMap(courseDayMap);
                    }
                }else if(calendarStudyVo.getMonth() != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec)+dayStudyVo.getStudyCourseKnob());

                        Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                        courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(courseDayMap);
                        dayStudyVo.setPerform(0);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = sectionNum-Sec;
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum)+dayStudyVo.getStudyCourseKnob());
                        Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                        courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(courseDayMap);
                        dayStudyVo.setPerform(0);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,planVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = 0;

                    }
                }
            }
        }

        //原有课程不满足课程节数则再创建课程
        if (sectionNum != 0){
            while (sectionNum != 0 ) {
                if (CollectionUtils.isEmpty(userPlanVo1.getCalendarStudyVo())) {
                    month = cal.get(Calendar.MONTH)+1;
                }else {
                    ++month;
                }
                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 (sectionNum == 0){
                            break;
                        }
                        if (Sec < sectionNum) {
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                            sectionNum = sectionNum-Sec;
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            if (courseDayMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);
                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                            if (sectionIdMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            dayStudyVo.setPerform(0);
                        }else if (Sec >= sectionNum){
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            if (courseDayMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);

                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                            if (sectionIdMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            sectionNum = 0;
                            dayStudyVo.setPerform(0);
                        }
                    }else if(month != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                        if (Sec < sectionNum) {
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                            sectionNum = sectionNum-Sec;
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            if (courseDayMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);

                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                            if (sectionIdMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            dayStudyVo.setPerform(0);
                        }else if (Sec >= sectionNum){
                            dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                            Map<Long, Long> courseDayMap = dayStudyVo.getCourseDayMap();
                            if (courseDayMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            courseDayMap.put(planVo.getCourseId(),Convert.toLong(Sec));
                            dayStudyVo.setCourseDayMap(courseDayMap);
                            sectionNum = 0;

                            //添加课程学习节ID
                            Map<Long,Long> sectionIdMap = dayStudyVo.getSectionIdMap();
                            if (sectionIdMap == null) {
                                courseDayMap = new HashMap<>();
                            }
                            for (int i = 0; i < Sec; i++) {
                                if (listId.size() >= Sec) {
                                    sectionIdMap.put(listId.get(0),planVo.getCourseId());
                                    listId.remove(0);
                                }
                            }
                            dayStudyVo.setSectionIdMap(sectionIdMap);
                            dayStudyVo.setPerform(0);
                        }
                    }
                }
                CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
                calendarStudyVo.setYear(Convert.toLong(year));
                calendarStudyVo.setMonth(Convert.toLong(month));
                calendarStudyVo.setDayStudyList(dayStudyVos);
                List<CalendarStudyVo> calendarStudyVo1 = userPlanVo1.getCalendarStudyVo();
                calendarStudyVo1.add(calendarStudyVo);
                userPlanVo1.setCalendarStudyVo(calendarStudyVo1);
            }
        }
        //查询是否解锁题库
        userPlanVo1.setStudyCount(longs);
        userPlanVo1.setUserId(bo.getUserId());
        userPlanVo1.setStudyDay(0L);
        userPlanVo1.setPitchNum(Convert.toLong(sectionNum)+userPlanVo1.getPitchNum());
        List<CoursePlanVo> coursePlanVo = userPlanVo1.getCoursePlanVo();
        coursePlanVo.add(planVo);
        userPlanVo1.setCoursePlanVo(coursePlanVo);
        return userPlanVo1;
    }

    private UserPlanVo generateList(UserPlanQueryBo bo, CoursePlanVo coursePlanVo) throws ParseException {
        UserPlanVo userPlanVo = new UserPlanVo();
        //最近考试时间七天前到现在的天数，学习视频时长
        Date date1 = new Date(bo.getStartTime()*1000L);
        Date date = new Date(bo.getEndTime()*1000L);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date1);

        //获得相隔天数
        Long[] longs =bo.getStudyCount();
        int dutyDays = getDutyDays(date1, date,longs,0L);

        //计算每天需要学习多少节课
        int Sec = (int)Math.ceil((double)coursePlanVo.getSectionNum()/dutyDays);

        //总节数
        Integer sectionNum = coursePlanVo.getSectionNum();
        if(sectionNum <= 0){
            throw new IllegalArgumentException("该课程没有节数，无法生成计划");
        }
        userPlanVo.setPitchNum(Convert.toLong(sectionNum));
        //查出节的ID
        List<Long> listId= baseMapper.selectByCourseId(coursePlanVo.getCourseId());

        //获得今天日期，将每天节数写入
        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() > ca.get(Calendar.DAY_OF_MONTH) && month == (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)) {
                    if (sectionNum == 0){
                        break;
                    }
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                        sectionNum = sectionNum-Sec;
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),coursePlanVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setPerform(0);
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                        sectionNum = 0;
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,coursePlanVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setPerform(0);
                    }
                }else if(month != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                        sectionNum = sectionNum-Sec;
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),coursePlanVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setPerform(0);
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                        sectionNum = 0;
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,coursePlanVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        dayStudyVo.setPerform(0);
                    }
                }
            }
            CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
            calendarStudyVo.setYear(Convert.toLong(year));
            calendarStudyVo.setMonth(Convert.toLong(month));
            calendarStudyVo.setDayStudyList(dayStudyVos);
            calendarStudyVos.add(calendarStudyVo);
        }

        //查询是否解锁题库


        userPlanVo.setCalendarStudyVo(calendarStudyVos);
        List<CoursePlanVo> coursePlanVos = new ArrayList<>();
        userPlanVo.setStudyCount(longs);
        userPlanVo.setUserId(bo.getUserId());

        userPlanVo.setStudyDay(0L);
        coursePlanVos.add(coursePlanVo);
        userPlanVo.setCoursePlanVo(coursePlanVos);
        return userPlanVo;
    }

    private UserPlanVo generateSelf(UserPlanQueryBo bo) throws ParseException {
        UserPlanVo userPlanVo = new UserPlanVo();

        //获得当前课程最近一个考试计划  考生考生计划或者最近考试计划
        CoursePlanVo coursePlanVo = baseMapper.selectByCourse(bo);
        ExamArrangementTimeVo examArrangementTimeVo = baseMapper.selectByExam(coursePlanVo.getCategoryId());
        if (examArrangementTimeVo == null){
            throw new IllegalArgumentException("当前课程无考试时间");
        }
        userPlanVo.setExamDate(examArrangementTimeVo.getStartTime());
        //最近考试时间七天前到现在的天数，学习视频时长
        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();
        if(sectionNum <= 0){
            throw new IllegalArgumentException("该课程没有节数，无法生成计划");
        }
        userPlanVo.setPitchNum(Convert.toLong(sectionNum));

        //查出节的ID
        List<Long> listId= baseMapper.selectByCourseId(coursePlanVo.getCourseId());

        //获得今天日期，将每天节数写入
        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 (sectionNum == 0){
                        break;
                    }
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                        dayStudyVo.setPerform(0);
                        //添加课程节数
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),coursePlanVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = sectionNum-Sec;
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                        dayStudyVo.setPerform(0);
                        //添加课程节数
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,coursePlanVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = 0;
                    }
                }else if(month != (cal.get(Calendar.MONTH) + 1) && ifTrue(dayStudyVo.getStudyDay(),longs)){
                    if (Sec < sectionNum) {
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(Sec));
                        dayStudyVo.setPerform(0);
                        //添加课程节数
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (int i = 0; i < Sec; i++) {
                            if (listId.size() >= Sec) {
                                sectionIdMap.put(listId.get(0),coursePlanVo.getCourseId());
                                listId.remove(0);
                            }
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = sectionNum-Sec;
                    }else if (Sec >= sectionNum){
                        dayStudyVo.setStudyCourseKnob(Convert.toLong(sectionNum));
                        dayStudyVo.setPerform(0);
                        //添加课程节数
                        Map<Long,Long> studyDstudyVo = new HashMap<Long, Long>();
                        studyDstudyVo.put(coursePlanVo.getCourseId(),Convert.toLong(Sec));
                        dayStudyVo.setCourseDayMap(studyDstudyVo);
                        //添加课程学习节ID
                        Map<Long,Long> sectionIdMap = new HashMap<>();
                        for (Long aLong : listId) {
                            sectionIdMap.put(aLong,coursePlanVo.getCourseId());
                        }
                        dayStudyVo.setSectionIdMap(sectionIdMap);
                        sectionNum = 0;
                    }
                }
                dayStudyVo.setPerform(0);
            }
            CalendarStudyVo calendarStudyVo = new CalendarStudyVo();
            calendarStudyVo.setYear(Convert.toLong(year));
            calendarStudyVo.setMonth(Convert.toLong(month));
            calendarStudyVo.setDayStudyList(dayStudyVos);
            calendarStudyVos.add(calendarStudyVo);
        }

        //查询是否解锁题库


        userPlanVo.setCalendarStudyVo(calendarStudyVos);
        List<CoursePlanVo> coursePlanVos = new ArrayList<>();
        userPlanVo.setStudyCount(longs);
        userPlanVo.setUserId(bo.getUserId());

        userPlanVo.setStudyDay(0L);
        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 endDate = endDateStr;
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DATE, - Convert.toInt(studyDay));
        endDate = c.getTime();

        Date startDate = startDateStr;

        while (startDate.compareTo(endDate) <= 0) {
            for (Long integer : longs) {
                if (startDate.getDay() != integer) {
                    result++;
                    break;
                }
            }
            startDate.setDate(startDate.getDate() + 1);
        }
        return result-1;
    }

    //获得今天时间的0点
    public static int getTimesmorning(){
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, 0);

        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.MINUTE, 0);

        cal.set(Calendar.MILLISECOND, 0);

        return (int) (cal.getTimeInMillis()/1000);

    }

    //获得当天24点时间
    public static int getTimesnight(){
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, 24);

        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.MINUTE, 0);

        cal.set(Calendar.MILLISECOND, 0);

        return (int) (cal.getTimeInMillis()/1000);

    }

}
