package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.AppUserTypeEnum;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.JWTUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.dto.*;
import com.ruoyi.entity.*;
import com.ruoyi.service.*;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: RuoYi-Vue-master
 * @description:
 * @author: niujie
 * @create: 2023-08-07 15:25
 */
@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainingPlanClassificationService trainingPlanClassificationService;
    @Autowired
    private QuestionBankService questionBankService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TrainingPlanService trainingPlanService;
    @Autowired
    private TrainingPlanPostService trainingPlanPostService;
    @Autowired
    private TrainingPlanDocumentsService trainingPlanDocumentsService;
    @Autowired
    private TrainingPlanCourseService trainingPlanCourseService;
    @Autowired
    private LessonLearningRecordService lessonLearningRecordService;
    @Autowired
    private EmployeeCourseExamServer employeeCourseExamServer;
    @Autowired
    private EmployeeCourseExamRecordsService employeeCourseExamRecordsService;
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private ParperQuestionBankService parperQuestionBankService;
    @Autowired
    private ISysPostService iSysPostService;

    /**
     * 查询培训计划分类列表
     *
     * @param categoriesName
     * @return
     */
    @Override
    public List<TrainingPlanClassificationDto> getTrainingPlanClassificationList(String categoriesName) {
        List<TrainingPlanClassification> trainingPlanClassificationList = trainingPlanClassificationService.list(Wrappers.<TrainingPlanClassification>lambdaQuery()
                .like(StringUtils.isNotBlank(categoriesName), TrainingPlanClassification::getCategoriesName, categoriesName)
                .eq(TrainingPlanClassification::getIsDel, DeletedEnum.VALID.getValue())
                .eq(TrainingPlanClassification::getParantId, "0")
                .orderByDesc(TrainingPlanClassification::getCreateDate));

        List<TrainingPlanClassificationDto> trainingPlanClassificationDtos = new ArrayList<>();

        for (TrainingPlanClassification trainingPlanClassification : trainingPlanClassificationList) {
            SysUser sysUser = sysUserService.selectUserById(trainingPlanClassification.getCreateUserId());
            String nickName = sysUser != null ? sysUser.getNickName() : "-";
//            String nickName = sysUserService.selectUserById(trainingPlanClassification.getCreateUserId()).getNickName();
            TrainingPlanClassificationDto trainingPlanClassificationDto = new TrainingPlanClassificationDto();
            BeanUtils.copyProperties(trainingPlanClassification, trainingPlanClassificationDto);
            trainingPlanClassificationDto.setUserName(nickName);

            //查询子
            getChildTrainingPlanClass(trainingPlanClassification, trainingPlanClassificationDto);


            trainingPlanClassificationDtos.add(trainingPlanClassificationDto);
        }

        return trainingPlanClassificationDtos;
    }

    /**
     * 新增/修改培训计划分类
     *
     * @param trainingPlanClassification
     * @return
     */
    @Override
    public boolean saveTrainingPlanClassificationList(TrainingPlanClassification trainingPlanClassification) {
        //新增
        if (StringUtils.isBlank(trainingPlanClassification.getId())) {
            if (StringUtils.isBlank(trainingPlanClassification.getParantId())) {
                trainingPlanClassification.setParantId("0");
            }
            trainingPlanClassification.setCreateDefault();
        }
        //修改
        else {
//            trainingPlanClassification.setCreateDefault();
            trainingPlanClassification.setUpdateDefault();
        }
        return trainingPlanClassificationService.saveOrUpdate(trainingPlanClassification);
    }

    /**
     * 查看培训计划分类详情
     *
     * @param id
     * @return
     */
    @Override
    public TrainingPlanClassification getTrainingPlanClassificationById(String id) {
        return trainingPlanClassificationService.getById(id);
    }

    /**
     * 删除培训计划分类
     *
     * @param id
     * @return
     */
    @Override
    public boolean delTrainingPlanClassificationById(String id) {
        TrainingPlanClassification trainingPlanClassification = new TrainingPlanClassification();
        trainingPlanClassification.setId(id);
        trainingPlanClassification.setIsDel(DeletedEnum.INVALID.getValue());
        return trainingPlanClassificationService.updateById(trainingPlanClassification);
    }

    /**
     * 查询末级培训计划分类
     *
     * @return
     */
    @Override
    public List<TrainingPlanClassification> queryLastLevelClassification() {
        List<TrainingPlanClassification> trainingPlanClassificationList = trainingPlanClassificationService.list(Wrappers.<TrainingPlanClassification>lambdaQuery()
                .eq(TrainingPlanClassification::getIsDel, DeletedEnum.VALID.getValue())
                .eq(TrainingPlanClassification::getParantId, "0")
                .orderByAsc(TrainingPlanClassification::getClassificationSort));
        List<TrainingPlanClassification> trainingPlanClassifications = new ArrayList<>();
        for (TrainingPlanClassification trainingPlanClassification : trainingPlanClassificationList) {
            TrainingPlanClassificationDto trainingPlanClassificationDto = new TrainingPlanClassificationDto();
            BeanUtils.copyProperties(trainingPlanClassification, trainingPlanClassificationDto);
            List<TrainingPlanClassification> childTrainingPlanClass = getChildTrainingPlanClass(trainingPlanClassification, trainingPlanClassificationDto);
            if (childTrainingPlanClass.size() > 0) {
                trainingPlanClassifications.addAll(childTrainingPlanClass);
            } else {
                trainingPlanClassifications.add(trainingPlanClassification);
            }
        }
        return trainingPlanClassifications;
    }

    private List<TrainingPlanClassification> getChildTrainingPlanClass(TrainingPlanClassification trainingPlanClassification, TrainingPlanClassificationDto trainingPlanClassificationDto) {
        List<TrainingPlanClassification> trainingPlanClassificationList = trainingPlanClassificationService.list(Wrappers.<TrainingPlanClassification>lambdaQuery()
                .eq(TrainingPlanClassification::getIsDel, DeletedEnum.VALID.getValue())
                .eq(TrainingPlanClassification::getParantId, trainingPlanClassification.getId())
                .orderByDesc(TrainingPlanClassification::getCreateDate));

        for (TrainingPlanClassification data : trainingPlanClassificationList) {
            SysUser sysUser = sysUserService.selectUserById(trainingPlanClassification.getCreateUserId());
            String nickName = sysUser != null ? sysUser.getNickName() : "-";
            data.setUserName(nickName);
        }

        trainingPlanClassificationDto.setTrainingPlanClassificationDtos(trainingPlanClassificationList);
        return trainingPlanClassificationList;
    }

    /**
     * 查询题库列表
     *
     * @return
     */
    @Override
    public PageInfo<QuestionBank> getQuestionBankList(String questionTitle, String questionTypes, String trainingPlanClassificationId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<QuestionBank> questionBankList = questionBankService.list(Wrappers.<QuestionBank>lambdaQuery()
                .like(StringUtils.isNotBlank(questionTitle), QuestionBank::getQuestionTitle, questionTitle)
                .eq(StringUtils.isNotBlank(questionTypes), QuestionBank::getQuestionTypes, questionTypes)
                .eq(StringUtils.isNotBlank(trainingPlanClassificationId), QuestionBank::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(QuestionBank::getIsDel, DeletedEnum.VALID.getValue())
                .orderByDesc(QuestionBank::getCreateDate));
        for (QuestionBank questionBank : questionBankList) {
            questionBank.setTrainingPlanClassificationId(trainingPlanClassificationService.getById(questionBank.getTrainingPlanClassificationId()).getCategoriesName());
            questionBank.setUserName(sysUserService.selectUserById(questionBank.getCreateUserId()).getNickName());
        }
        PageInfo<QuestionBank> pageInfo = new PageInfo<>(questionBankList);
        return pageInfo;
    }

    /**
     * 删除题库详情
     *
     * @param id
     * @return
     */
    @Override
    public boolean delQuestionBankById(String id) {
        QuestionBank questionBank = new QuestionBank();
        questionBank.setId(id);
        questionBank.setIsDel(DeletedEnum.INVALID.getValue());
        return questionBankService.updateById(questionBank);
    }

    /**
     * 查询题库详情
     *
     * @param id
     * @return
     */
    @Override
    public QuestionBank getQuestionBankById(String id) {
        return questionBankService.getById(id);
    }

    /**
     * 新增/修改题库
     *
     * @param questionBank
     * @return
     */
    @Override
    public boolean saveQuestionBank(QuestionBank questionBank) {
        if (StringUtils.isBlank(questionBank.getId())) {
            questionBank.setIsDel(DeletedEnum.VALID.getValue());
            questionBank.setCreateDefault();
        } else {
            questionBank.setUpdateDefault();
        }
        return questionBankService.saveOrUpdate(questionBank);
    }

    /**
     * 查询题库列表
     *
     * @param trainingPlanClassificationId
     * @return
     */
    @Override
    public List<QuestionBank> getQuestionBankSelect(String trainingPlanClassificationId) {
        List<QuestionBank> questionBankList = questionBankService.list(Wrappers.<QuestionBank>lambdaQuery()
                .eq(QuestionBank::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(QuestionBank::getIsDel, DeletedEnum.VALID.getValue())
                .orderByDesc(QuestionBank::getCreateDate));


        //设置选项数组
        for (QuestionBank questionBank : questionBankList) {
            questionBank.setQuestionOptions(Arrays.asList(questionBank.getQuestionOption().split("##")));
        }
        return questionBankList;
    }

    /**
     * 查询课程列表
     *
     * @return
     */
    @Override
    public PageInfo<Course> getCourseList(String courseTitle, String categoriesAuthor, String categoriesLecturer, String trainingPlanClassificationId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Course> courseList = courseService.list(Wrappers.<Course>lambdaQuery()
                .like(StringUtils.isNotBlank(courseTitle), Course::getCourseTitle, courseTitle)
                .like(StringUtils.isNotBlank(categoriesAuthor), Course::getCategoriesAuthor, categoriesAuthor)
                .like(StringUtils.isNotBlank(categoriesLecturer), Course::getCategoriesLecturer, categoriesLecturer)
                .eq(StringUtils.isNotBlank(trainingPlanClassificationId), Course::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(Course::getIsDel, DeletedEnum.VALID.getValue())
                .orderByDesc(Course::getCreateDate));
        for (Course course : courseList) {
            course.setTrainingPlanClassificationId(trainingPlanClassificationService.getById(course.getTrainingPlanClassificationId()).getCategoriesName());
            course.setUserName(sysUserService.selectUserById(course.getCreateUserId()).getNickName());
        }
        PageInfo<Course> pageInfo = new PageInfo(courseList);
        return pageInfo;
    }

    /**
     * 查询全部课程
     *
     * @return
     */
    @Override
    public List<Course> getCourseSelect(String trainingPlanClassificationId) {
        return courseService.list(Wrappers.<Course>lambdaQuery()
                .eq(Course::getIsDel, DeletedEnum.VALID.getValue())
                .eq(Course::getTrainingPlanClassificationId, trainingPlanClassificationId));
    }

    /**
     * 查询课程详情
     *
     * @param id
     * @return
     */
    @Override
    public Course getCourseById(String id) {
        return courseService.getById(id);
    }

    /**
     * 删除课程详情
     *
     * @param id
     * @return
     */
    @Override
    public boolean delCourseById(String id) {
        Course course = new Course();
        course.setId(id);
        course.setIsDel(DeletedEnum.INVALID.getValue());
        return courseService.updateById(course);
    }

    /**
     * 新增/修改课程
     *
     * @param course
     * @return
     */
    @Override
    public boolean saveCourse(Course course) {
        if (StringUtils.isBlank(course.getId())) {
            course.setIsDel(DeletedEnum.VALID.getValue());
            course.setCreateDefault();
        } else {
            course.setUpdateDefault();
        }
        return courseService.saveOrUpdate(course);
    }

    /**
     * 查询培训计划列表
     *
     * @param trainingPlanName
     * @param lessonScheduleState
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<TrainingPlan> getTrainingPlanList(String trainingPlanName, String lessonScheduleState, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<TrainingPlan> trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery()
                .like(StringUtils.isNotBlank(trainingPlanName), TrainingPlan::getTrainingPlanName, trainingPlanName)
                .eq(StringUtils.isNotBlank(lessonScheduleState), TrainingPlan::getLessonScheduleState, lessonScheduleState)
                .eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue())
                .orderByDesc(TrainingPlan::getCreateDate));

        for (TrainingPlan trainingPlan : trainingPlanList) {
//            String userName = sysUserService.selectUserById(trainingPlan.getApproveBy()).getUserName();
            SysUser sysUser = sysUserService.selectUserById(trainingPlan.getApproveBy());
            String userName = sysUser != null ? sysUser.getUserName() : "-";
            String nickName = sysUser != null ? sysUser.getNickName() : "-";
            trainingPlan.setApproveName(userName);
            trainingPlan.setApproveNickName(nickName);
        }
        PageInfo<TrainingPlan> pageInfo = new PageInfo<>(trainingPlanList);
        return pageInfo;
    }

    /**
     * 删除培训计划
     *
     * @param id
     * @return
     */
    @Override
    public boolean delTrainingPlanById(String id) {
        TrainingPlan trainingPlan = new TrainingPlan();
        trainingPlan.setId(id);
        trainingPlan.setIsDel(DeletedEnum.INVALID.getValue());
        return trainingPlanService.updateById(trainingPlan);
    }

    /**
     * 获取培训计划
     *
     * @param id
     * @return
     */
    @Override
    public TrainingPlanDto getTrainingPlanById(String id) {
        TrainingPlanDto trainingPlanDto = new TrainingPlanDto();
        TrainingPlan trainingPlan = trainingPlanService.getById(id);
        BeanUtils.copyProperties(trainingPlan, trainingPlanDto);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trainingPlanDto.setAllTime(new String[]{simpleDateFormat.format(trainingPlanDto.getLearningStartTime()), simpleDateFormat.format(trainingPlanDto.getLearningEndTime())});
        List<TrainingPlanPost> trainingPlanPostList = trainingPlanPostService.list(Wrappers.<TrainingPlanPost>lambdaQuery().eq(TrainingPlanPost::getTrainingPlanId, id));
        trainingPlanDto.setSysPostList(trainingPlanPostList);
        List<TrainingPlanDocuments> trainingPlanDocumentsList = trainingPlanDocumentsService.list(Wrappers.<TrainingPlanDocuments>lambdaQuery().eq(TrainingPlanDocuments::getTrainingPlanId, id));
        trainingPlanDto.setTrainingPlanDocumentsList(trainingPlanDocumentsList);
        List<TrainingPlanCourse> trainingPlanCourseList = trainingPlanCourseService.list(Wrappers.<TrainingPlanCourse>lambdaQuery().eq(TrainingPlanCourse::getTrainingPlanId, id));
        trainingPlanDto.setTrainingPlanCourseList(trainingPlanCourseList);
        return trainingPlanDto;
    }

    /**
     * 新增/修改培训计划
     *
     * @param trainingPlanDto
     * @return
     */
    @Override
    @Transactional
    public boolean saveTrainingPlan(TrainingPlanDto trainingPlanDto) {
        if (StringUtils.isBlank(trainingPlanDto.getId())) {
            trainingPlanDto.setCreateDefault();
        } else {
            trainingPlanDto.setUpdateDefault();
        }
        TrainingPlan trainingPlan = new TrainingPlan();
        BeanUtils.copyProperties(trainingPlanDto, trainingPlan);
        trainingPlan.setLessonScheduleState("1");
        trainingPlanService.saveOrUpdate(trainingPlan);
        String id = trainingPlan.getId();
        List<TrainingPlanPost> planDtoSysPostList = trainingPlanDto.getSysPostList();
        List<TrainingPlanDocuments> trainingPlanDocumentsList = trainingPlanDto.getTrainingPlanDocumentsList();
        List<TrainingPlanCourse> trainingPlanCourseList = trainingPlanDto.getTrainingPlanCourseList();

        trainingPlanPostService.remove(Wrappers.<TrainingPlanPost>lambdaQuery().eq(TrainingPlanPost::getTrainingPlanId, id));
        trainingPlanDocumentsService.remove(Wrappers.<TrainingPlanDocuments>lambdaQuery().eq(TrainingPlanDocuments::getTrainingPlanId, id));
        trainingPlanCourseService.remove(Wrappers.<TrainingPlanCourse>lambdaQuery().eq(TrainingPlanCourse::getTrainingPlanId, id));

        for (TrainingPlanPost trainingPlanPost : planDtoSysPostList) {
            trainingPlanPost.setCreateDefault();
            trainingPlanPost.setTrainingPlanId(id);
        }
        trainingPlanPostService.saveBatch(planDtoSysPostList);
        for (TrainingPlanDocuments trainingPlanDocuments : trainingPlanDocumentsList) {
            trainingPlanDocuments.setCreateDefault();
            trainingPlanDocuments.setTrainingPlanId(id);
        }
        trainingPlanDocumentsService.saveBatch(trainingPlanDocumentsList);
        for (TrainingPlanCourse trainingPlanCourse : trainingPlanCourseList) {
            trainingPlanCourse.setCreateDefault();
            trainingPlanCourse.setTrainingPlanId(id);
        }

        return trainingPlanCourseService.saveBatch(trainingPlanCourseList);
    }

    /**
     * APP培训列表:只有一处调用
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<TrainingPlan> queryTrainPlanPage(Integer pageNo, Integer pageSize) {
        //查询岗位与培训计划关联表，按岗位查询培训计划ID
        List<TrainingPlanPost> trainingPlanPostList = new ArrayList<>();

        //1、领导角色查看全部->进入考试的时候需要判断是否有考试资格
        //非领导角色：
        //2、考试人根据培训计划中配置的岗位匹配(注意：添加人/审核人不需要在APP上查询，在PC上可以查看即可，APP只让考试人查看)
        String requestToken = JWTUtil.getRequestToken();
        String appUserType = null;
        try {
            appUserType = JWTUtil.getAppUserType();
        } catch (Exception e) {
        }
        //承包商不让查看，直接返回空集合
        if (appUserType != null && AppUserTypeEnum.CONTRACTOR.getValue() == Integer.parseInt(appUserType)) {
            return new PageInfo<>();
        }
        // APP请求时
        else if (StringUtils.isNotBlank(requestToken)) {
            boolean leader = JWTUtil.isLeader();
            // 非领导角色不能查看全部，需要筛选
            if (!leader) {
                //APP登录人员的岗位集合
                List<Long> postIdList = JWTUtil.getPostIdList();

                //岗位为空时返回空
                if (CollectionUtils.isEmpty(postIdList)) {
                    return new PageInfo<>();
                }

                //2、查询岗位与培训计划关联表，按岗位查询培训计划ID
                trainingPlanPostList = trainingPlanPostService.list(Wrappers.<TrainingPlanPost>lambdaQuery()
                        .in(TrainingPlanPost::getPostId, postIdList)
                        .eq(TrainingPlanPost::getIsDel, DeletedEnum.VALID.getValue()).orderByDesc(TrainingPlanPost::getCreateDate));
                if (CollectionUtils.isEmpty(trainingPlanPostList)) {
                    return new PageInfo<>();
                }
            }

            //3、查询培训计划：按培训计划ID(岗位关联出来的)、开始时间、结束时间、审核通过、正常状态 条件查询 -> 创建时间倒叙
             /*   List<String> trainingPlanIds = trainingPlanPostList.stream().map(TrainingPlanPost::getTrainingPlanId).collect(Collectors.toList());
                PageHelper.startPage(pageNo, pageSize);
                List<TrainingPlan> trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery()
                        .eq(TrainingPlan::getLessonScheduleState, "2")
                        .eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue())
                        .le(TrainingPlan::getLearningStartTime, new Date())
                        .ge(TrainingPlan::getLearningEndTime, new Date())
                        .in(!CollectionUtils.isEmpty(trainingPlanIds), TrainingPlan::getId, trainingPlanIds)//领导时不筛选
                        .orderByDesc(TrainingPlan::getCreateDate));
                PageInfo<TrainingPlan> pageInfo = new PageInfo<>(trainingPlanList);
                return pageInfo;*/

        }


        //1、查询人员的岗位列表
//        String pcUserId = JWTUtil.getPCUserId();
//        List<Long> postIds = iSysPostService.selectPostListByUserId(Long.parseLong(pcUserId));
//        if (postIds == null || postIds.size() <= 0) {
//            return new PageInfo<>();
//        }

        //2、查询岗位与培训计划关联表，按岗位查询培训计划ID
//        List<TrainingPlanPost> trainingPlanPostList = trainingPlanPostService.list(Wrappers.<TrainingPlanPost>lambdaQuery()
//                .in(TrainingPlanPost::getPostId, postIds)
//                .eq(TrainingPlanPost::getIsDel, DeletedEnum.VALID.getValue()).orderByDesc(TrainingPlanPost::getCreateDate));
//        if (CollectionUtils.isEmpty(trainingPlanPostList)) {
//            return new PageInfo<>();
//        }

        //3、查询培训计划：按培训计划ID(岗位关联出来的)、开始时间、结束时间、审核通过、正常状态 条件查询 -> 创建时间倒叙
        List<String> trainingPlanIds = trainingPlanPostList.stream().map(TrainingPlanPost::getTrainingPlanId).collect(Collectors.toList());
        PageHelper.startPage(pageNo, pageSize);
        List<TrainingPlan> trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery()
                .eq(TrainingPlan::getLessonScheduleState, "2")
                .eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue())
                .le(TrainingPlan::getLearningStartTime, new Date())
                .ge(TrainingPlan::getLearningEndTime, new Date())
//                .in(TrainingPlan::getId, trainingPlanIds)
                .in(!CollectionUtils.isEmpty(trainingPlanIds), TrainingPlan::getId, trainingPlanIds)//领导时不筛选
                .orderByDesc(TrainingPlan::getCreateDate));
        PageInfo<TrainingPlan> pageInfo = new PageInfo<>(trainingPlanList);
        return pageInfo;
    }

    /**
     * 通过计划ID获取课程
     *
     * @param trainingPlanId
     * @return
     */
    @Override
    @Transactional
    public List<Course> queryCourseByTrainingPlanId(String trainingPlanId) {
        TrainingPlan trainingPlan = trainingPlanService.getById(trainingPlanId);
        List<Course> courseList = new ArrayList<>();
        // 计划类型：1、文件，2、课程
        if (trainingPlan.getPlanType().equals("2")) {
            List<TrainingPlanCourse> trainingPlanCourseList = trainingPlanCourseService.list(Wrappers.<TrainingPlanCourse>lambdaQuery()
                    .eq(TrainingPlanCourse::getTrainingPlanId, trainingPlanId));
            List<String> trainingPlanClassificationId = trainingPlanCourseList.stream().map(TrainingPlanCourse::getTrainingPlanClassificationId).collect(Collectors.toList());
            courseList.addAll(courseService.list(Wrappers.<Course>lambdaQuery().in(Course::getId, trainingPlanClassificationId).eq(Course::getIsDel, DeletedEnum.VALID.getValue())));
        } else {
            List<TrainingPlanDocuments> trainingPlanDocumentsList = trainingPlanDocumentsService.list(Wrappers.<TrainingPlanDocuments>lambdaQuery()
                    .eq(TrainingPlanDocuments::getTrainingPlanId, trainingPlanId)
                    .eq(TrainingPlanDocuments::getIsDel, DeletedEnum.VALID.getValue()));
            for (TrainingPlanDocuments trainingPlanDocuments : trainingPlanDocumentsList) {
                Course course = new Course();
                course.setId(trainingPlanDocuments.getId());
                course.setCoursewareUrl(trainingPlanDocuments.getFileUrl());
                course.setCoursewareName(trainingPlanDocuments.getName());
                course.setCourseTitle(trainingPlanDocuments.getName());
//                course.setCoverPhoto(trainingPlan.getCoverPhoto());
                courseList.add(course);
            }
        }

        String appUserId = JWTUtil.getUserId();
        List<LessonLearningRecord> lessonLearningRecordList = new ArrayList<>();
        for (Course course : courseList) {
            LessonLearningRecord lessonLearningRecord = new LessonLearningRecord();
            lessonLearningRecord.setCreateDefaultForApp();
            lessonLearningRecord.setCourseId(course.getId());
            lessonLearningRecord.setStaffId(appUserId);
            lessonLearningRecord.setTrainingPlanId(trainingPlanId);
            lessonLearningRecord.setLearningState("1");
            lessonLearningRecordList.add(lessonLearningRecord);
        }
        lessonLearningRecordService.saveOrUpdateBatch(lessonLearningRecordList);
        return courseList;
    }

    /**
     * 通过计划ID获取考试卷
     *
     * @param trainingPlanId
     * @return
     */
    @Override
    public AjaxResult queryQuestionBankByTrainingPlanId(String trainingPlanId) {
        TrainingPlan trainingPlan = trainingPlanService.getById(trainingPlanId);
        if (trainingPlan.getIsExamination().equals("2")) {
            return AjaxResult.error("该培训不需要考试。");
        }

        List<EmployeeCourseExamRecords> courseExamRecords = employeeCourseExamRecordsService.list(Wrappers.<EmployeeCourseExamRecords>lambdaQuery()
                .eq(EmployeeCourseExamRecords::getStaffId, JWTUtil.getUserId())
                .eq(EmployeeCourseExamRecords::getTrainingPlanId, trainingPlanId)
                .orderByDesc(EmployeeCourseExamRecords::getCreateDate));
//        if (courseExamRecords.size() > 0 && StringUtils.isNotBlank(courseExamRecords.get(0).getCertificateId())) {
        if (courseExamRecords.size() > 0 && courseExamRecords.get(0).getPass() != null && courseExamRecords.get(0).getPass() == 2) {
            return AjaxResult.error("您已经通过考试，不需要再次考试。");
        }

        //防止空指针，最大考试次数默认设置为1次
        String retakesNumber = trainingPlan.getRetakesNumber();
        if (StringUtils.isBlank(retakesNumber)) {
            retakesNumber = "1";
        }

//        if (trainingPlan.getRetakesNumber().equals(courseExamRecords.size() + "")) {
        if (courseExamRecords.size() >= Integer.parseInt(retakesNumber)) {
            return AjaxResult.error("考试已经到达最大次数，不允许再次考试。");
        }


        //培训计划分类ID
        String trainingPlanClassificationId = trainingPlan.getTrainingPlanClassificationId();
        //获取试卷
        List<Paper> paperList = paperService.list(Wrappers.<Paper>lambdaQuery()
                .eq(Paper::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(Paper::getIsDel, DeletedEnum.VALID.getValue())
                .eq(Paper::getPaperStatus, "0"));
        if (paperList.size() <= 0) {
            return AjaxResult.error("当前培训暂无试卷，请联系管理员处理。");
        }

//        int size = 0;
//        if (Integer.parseInt("1") == paperList.size()) {
//            size = Integer.parseInt("1");
//        } else if (Integer.parseInt("1") >= paperList.size()) {
//            size = paperList.size();
//        } else {
//            size = Integer.parseInt("1");
//        }
        //打乱试卷的排序，然后取第一个
        Collections.shuffle(paperList);
        List<Paper> papers = paperList.subList(0, 1);
        Paper paper = papers.get(0);
        List<ParperQuestionBank> parperQuestionBankList = parperQuestionBankService.list(Wrappers.<ParperQuestionBank>lambdaQuery().eq(ParperQuestionBank::getPaperId, paper.getId()));
        List<String> questionBankIds = parperQuestionBankList.stream().map(ParperQuestionBank::getQuestionBankId).collect(Collectors.toList());
        List<QuestionBank> questionBankList = questionBankService.list(Wrappers.<QuestionBank>lambdaQuery().in(QuestionBank::getId, questionBankIds));
        questionBankList.stream().forEach(a -> a.setPaperId(paper.getId()));//试卷ID
        //设置选项数组
        for (QuestionBank questionBank : questionBankList) {
            questionBank.setQuestionOptions(Arrays.asList(questionBank.getQuestionOption().split("##")));
            questionBank.setQuestionAnschas(Arrays.asList(questionBank.getQuestionAnscha().split("##")));
        }
        return AjaxResult.success(questionBankList);
    }

    /**
     * 通过培训计划ID获取试卷前期校验
     *
     * @param trainingPlanId
     * @return
     */
    @Override
    public AjaxResult queryQuestionBankByTrainingPlanIdValidate(String trainingPlanId) {
        //培训列表时，因为领导查看全部，去考试时判断是否有权限
        String requestToken = JWTUtil.getRequestToken();
        String appUserType = null;
        try {
            appUserType = JWTUtil.getAppUserType();
        } catch (Exception e) {
        }
        //承包商不让查看，直接返回空集合
        if (appUserType != null && AppUserTypeEnum.CONTRACTOR.getValue() == Integer.parseInt(appUserType)) {
            return AjaxResult.error("承包商不可以考试，请检查当前登录账号。");
        }
        // APP请求时
        else if (StringUtils.isNotBlank(requestToken)) {
            boolean leader = JWTUtil.isLeader();
            // 非领导角色不能查看全部，需要筛选
            if (leader) {
                //APP登录人员的岗位集合
                List<Long> postIdList = JWTUtil.getPostIdList();

                //岗位为空时返回空
                if (CollectionUtils.isEmpty(postIdList)) {
                    return AjaxResult.error("该账号未分配岗位，请联系管理员。");
                }

                //2、查询岗位与培训计划关联表，按岗位查询培训计划ID
                List<TrainingPlanPost> trainingPlanPostList = trainingPlanPostService.list(Wrappers.<TrainingPlanPost>lambdaQuery()
                        .in(TrainingPlanPost::getPostId, postIdList)
                        .eq(TrainingPlanPost::getIsDel, DeletedEnum.VALID.getValue()).orderByDesc(TrainingPlanPost::getCreateDate));
                if (CollectionUtils.isEmpty(trainingPlanPostList)) {
                    return AjaxResult.error("您是领导权限，不需要参与此考试。");
                }
                List<String> trainingPlanIds = trainingPlanPostList.stream().map(TrainingPlanPost::getTrainingPlanId).collect(Collectors.toList());
                if (!trainingPlanIds.contains(trainingPlanId)) {
                    return AjaxResult.error("您是领导权限，不需要参与此考试。");
                }
            }
        }

        TrainingPlan trainingPlan = trainingPlanService.getById(trainingPlanId);
        if (trainingPlan.getIsExamination().equals("2")) {
            return AjaxResult.error("该培训不需要考试。");
        }

        List<EmployeeCourseExamRecords> courseExamRecords = employeeCourseExamRecordsService.list(Wrappers.<EmployeeCourseExamRecords>lambdaQuery()
                .eq(EmployeeCourseExamRecords::getStaffId, JWTUtil.getUserId())
                .eq(EmployeeCourseExamRecords::getTrainingPlanId, trainingPlanId)
                .orderByDesc(EmployeeCourseExamRecords::getCreateDate));


//        if (courseExamRecords.size() > 0 && StringUtils.isNotBlank(courseExamRecords.get(0).getCertificateId())) {
        if (courseExamRecords.size() > 0 && courseExamRecords.get(0).getPass() != null && courseExamRecords.get(0).getPass() == 2) {
//            return AjaxResult.error("您已经通过考试，并获得证书，不需要再次考试。");
            return AjaxResult.error("您已经通过考试，不需要再次考试。");
        }

        //防止空指针，最大考试次数默认设置为1次
        String retakesNumber = trainingPlan.getRetakesNumber();
        if (StringUtils.isBlank(retakesNumber)) {
            retakesNumber = "1";
        }
//        if (trainingPlan.getRetakesNumber().equals(courseExamRecords.size() + "")) {
        if (courseExamRecords.size() >= Integer.parseInt(retakesNumber)) {
            return AjaxResult.error("考试已经到达最大次数，不允许再次考试。");
        }


        //培训计划分类ID
        String trainingPlanClassificationId = trainingPlan.getTrainingPlanClassificationId();
        //获取试卷
        List<Paper> paperList = paperService.list(Wrappers.<Paper>lambdaQuery()
                .eq(Paper::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(Paper::getIsDel, DeletedEnum.VALID.getValue())
                .eq(Paper::getPaperStatus, "0"));
        if (paperList.size() <= 0) {
            return AjaxResult.error("当前培训暂无试卷，请联系管理员处理。");
        }
        return AjaxResult.success();
    }

    /**
     * 考试题目提交
     *
     * @param employeeCourseExamDtoList
     * @return
     */
    @Override
    @Transactional
    public AjaxResult submitQuestionBank(List<EmployeeCourseExamDto> employeeCourseExamDtoList) {
        List<EmployeeCourseExamRecords> courseExamRecords = employeeCourseExamRecordsService.list(Wrappers.<EmployeeCourseExamRecords>lambdaQuery()
                .eq(EmployeeCourseExamRecords::getStaffId, JWTUtil.getUserId())
                .eq(EmployeeCourseExamRecords::getTrainingPlanId, employeeCourseExamDtoList.get(0).getTrainingPlanId()));
        List<String> employeeCourseExamRecordIds = courseExamRecords.stream().map(EmployeeCourseExamRecords::getId).collect(Collectors.toList());

        if (employeeCourseExamRecordIds.size() > 0) {
            EmployeeCourseExam employeeCourseExam = new EmployeeCourseExam();
            employeeCourseExam.setIsDel(DeletedEnum.INVALID.getValue());
            employeeCourseExamServer.update(employeeCourseExam, Wrappers.<EmployeeCourseExam>lambdaQuery().in(EmployeeCourseExam::getEmployeeCourseExamRecordsId, employeeCourseExamRecordIds));
            EmployeeCourseExamRecords employeeCourseExamRecords = new EmployeeCourseExamRecords();
            employeeCourseExamRecords.setIsDel(DeletedEnum.INVALID.getValue());
            employeeCourseExamRecordsService.update(employeeCourseExamRecords, Wrappers.<EmployeeCourseExamRecords>lambdaQuery().in(EmployeeCourseExamRecords::getId, employeeCourseExamRecordIds));
        }

        EmployeeCourseExamRecords employeeCourseExamRecords = new EmployeeCourseExamRecords();
        employeeCourseExamRecords.setUpdateDefaultForApp();
        employeeCourseExamRecords.setCreateDefaultForApp();
        employeeCourseExamRecords.setTrainingPlanId(employeeCourseExamDtoList.get(0).getTrainingPlanId());
        employeeCourseExamRecords.setStaffId(JWTUtil.getUserId());
        employeeCourseExamRecordsService.save(employeeCourseExamRecords);

        Integer fraction = 0;
        List<EmployeeCourseExam> employeeCourseExams = new ArrayList<>();
        for (EmployeeCourseExamDto employeeCourseExamDto : employeeCourseExamDtoList) {
            EmployeeCourseExam employeeCourseExam = new EmployeeCourseExam();
            BeanUtils.copyProperties(employeeCourseExamDto, employeeCourseExam);
            employeeCourseExam.setQuestionOption(String.join("##", employeeCourseExamDto.getQuestionOptions()));
            employeeCourseExam.setQuestionAnscha(String.join("##", employeeCourseExamDto.getQuestionAnschas()));
            employeeCourseExam.setSubmitAnswers(String.join("##", employeeCourseExamDto.getSubmitAnswers()));
            employeeCourseExam.setEmployeeCourseExamRecordsId(employeeCourseExamRecords.getId());
            employeeCourseExams.add(employeeCourseExam);

            List<String> questionAnschas = employeeCourseExamDto.getQuestionAnschas();
            List<String> submitAnswers = employeeCourseExamDto.getSubmitAnswers();

            if (questionAnschas.size() == submitAnswers.size() && questionAnschas.containsAll(submitAnswers)) {
                fraction = fraction + employeeCourseExam.getQuestionScore();
            }
        }

        TrainingPlan trainingPlan = trainingPlanService.getById(employeeCourseExamDtoList.get(0).getTrainingPlanId());
        String certificateId = trainingPlan.getCertificateId();
        String parperPassingMark = paperService.getById(employeeCourseExamDtoList.get(0).getPaperId()).getParperPassingMark();
        if (Integer.parseInt(parperPassingMark) <= fraction) {
            employeeCourseExamRecords.setCertificateId(certificateId);
            //考试通过：是否通过：1-不通过；2-通过
            employeeCourseExamRecords.setPass(2);
        } else {
            //考试不通过：是否通过：1-不通过；2-通过
            employeeCourseExamRecords.setPass(1);
        }
        employeeCourseExamRecords.setExamScore(fraction + "");
        employeeCourseExamRecordsService.updateById(employeeCourseExamRecords);

        //判断是否考试通过
//        String paperId = employeeCourseExamDtoList.get(0).getPaperId();


        //获取试卷
//        List<Paper> paperList = paperService.list(
//                Wrappers.<Paper>lambdaQuery()
//                        .eq(Paper::getId, paperId));
//        if (paperList.size() != 1) {
//            return AjaxResult.error("当前培训暂无试卷，请联系管理员处理。");
//        }
//        Paper paper = paperService.getById(paperId);
//        if(paper==null){
//
//        }

        return AjaxResult.success(employeeCourseExamServer.saveBatch(employeeCourseExams));
    }

    /**
     * 获取个人考试题列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public AjaxResult queryQuestionBankList(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<EmployeeCourseExamRecords> courseExamRecords = employeeCourseExamRecordsService.list(Wrappers.<EmployeeCourseExamRecords>lambdaQuery()
                .eq(EmployeeCourseExamRecords::getStaffId, JWTUtil.getUserId())
                .eq(EmployeeCourseExamRecords::getIsDel, DeletedEnum.VALID.getValue()).orderByDesc(EmployeeCourseExamRecords::getCreateDate));
        for (EmployeeCourseExamRecords courseExamRecord : courseExamRecords) {
            TrainingPlan trainingPlan = trainingPlanService.getById(courseExamRecord.getTrainingPlanId());
            courseExamRecord.setTrainingPlanName(trainingPlan.getTrainingPlanName());
            courseExamRecord.setTrainingPlanPhoto(trainingPlan.getCoverPhoto());

            if (StringUtils.isNotBlank(courseExamRecord.getCertificateId())) {
                Certificate certificate = certificateService.getById(courseExamRecord.getCertificateId());
                courseExamRecord.setCertificateName(certificate.getCertificateName());
            }
        }
        PageInfo<EmployeeCourseExamRecords> pageInfo = new PageInfo<>(courseExamRecords);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 获取考试详情
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult queryQuestionBankDetail(String id) {
        List<EmployeeCourseExam> employeeCourseExams = employeeCourseExamServer.list(Wrappers.<EmployeeCourseExam>lambdaQuery().eq(EmployeeCourseExam::getEmployeeCourseExamRecordsId, id));
        for (EmployeeCourseExam employeeCourseExam : employeeCourseExams) {
            employeeCourseExam.setQuestionAnschas(Arrays.asList(employeeCourseExam.getQuestionAnscha().split("##")));
            employeeCourseExam.setQuestionOptions(Arrays.asList(employeeCourseExam.getQuestionOption().split("##")));
            employeeCourseExam.setSubmitAnswersList(Arrays.asList(employeeCourseExam.getSubmitAnswers().split("##")));
        }
        return AjaxResult.success(employeeCourseExams);
    }

    /**
     * 查询证书列表
     *
     * @param certificateName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Certificate> getCertificateList(String certificateName, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Certificate> certificateList = certificateService.list(Wrappers.<Certificate>lambdaQuery()
                .like(StringUtils.isNotBlank(certificateName), Certificate::getCertificateName, certificateName)
                .eq(Certificate::getIsDel, DeletedEnum.VALID.getValue()));
        for (Certificate certificate : certificateList) {
            certificate.setUserName(sysUserService.selectUserById(certificate.getCreateUserId()).getNickName());
        }
        PageInfo<Certificate> pageInfo = new PageInfo<>(certificateList);
        return pageInfo;
    }

    /**
     * 删除证书
     *
     * @param id
     * @return
     */
    @Override
    public boolean delCertificateById(String id) {
        Certificate certificate = new Certificate();
        certificate.setId(id);
        certificate.setIsDel(DeletedEnum.INVALID.getValue());
        return certificateService.updateById(certificate);
    }

    /**
     * 获取证书
     *
     * @param id
     * @return
     */
    @Override
    public Certificate getCertificateById(String id) {
        return certificateService.getById(id);
    }

    /**
     * 新增/修改证书
     *
     * @param certificate
     * @return
     */
    @Override
    public boolean saveCertificate(Certificate certificate) {
        if (StringUtils.isBlank(certificate.getId())) {
            certificate.setCreateDefault();
        } else {
            certificate.setUpdateDefault();
        }
        return certificateService.saveOrUpdate(certificate);
    }

    /**
     * 培训计划审核
     *
     * @param trainingPlan
     * @return
     */
    @Override
    public boolean auditTrainingPlan(TrainingPlan trainingPlan) {
        TrainingPlan trainingPlanDB = trainingPlanService.getById(trainingPlan.getId());
        if (trainingPlanDB == null) {
            throw new BaseException("参数错误，未发现对应记录！");
        } else {
            Long approveBy = trainingPlanDB.getApproveBy();
            if (approveBy == null) {
                throw new BaseException("未设置审核人！");

            }
            if (approveBy != SecurityUtils.getUserId()) {
                throw new BaseException("无审核权限！");
            }
        }
        trainingPlan.setApproveTime(new Date());
        trainingPlan.setApproveBy(SecurityUtils.getUserId());
        return trainingPlanService.updateById(trainingPlan);
    }

    /**
     * 查询证书列表
     *
     * @param trainingPlanClassificationId
     * @return
     */
    @Override
    public List<Certificate> getCertificateSelect(String trainingPlanClassificationId) {
        return certificateService.list(Wrappers.<Certificate>lambdaQuery()
                .eq(Certificate::getTrainingPlanClassificationId, trainingPlanClassificationId)
                .eq(Certificate::getIsDel, DeletedEnum.VALID.getValue()));
    }

    /**
     * 查询试卷列表
     *
     * @param paperName
     * @param paperStatus
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Paper> getPaperList(String paperName, String paperStatus, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Paper> paperList = paperService.list(Wrappers.<Paper>lambdaQuery()
                .eq(Paper::getIsDel, DeletedEnum.VALID.getValue())
                .like(StringUtils.isNotBlank(paperName), Paper::getParperName, paperName)
                .eq(StringUtils.isNotBlank(paperStatus), Paper::getPaperStatus, paperStatus));
        for (Paper paper : paperList) {
            paper.setUserName(sysUserService.selectUserById(paper.getCreateUserId()).getNickName());
        }
        PageInfo<Paper> pageInfo = new PageInfo<>(paperList);
        return pageInfo;
    }

    /**
     * 停用/删除试卷
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    public boolean deactivatePaperById(String id, String type) {
        Paper paper = new Paper();
        paper.setId(id);
        if (type.equals("1")) {
            paper.setPaperStatus("1");
        } else if (type.equals("0")) {
            paper.setPaperStatus("0");
        } else {
            paper.setIsDel(DeletedEnum.INVALID.getValue());
        }
        return paperService.updateById(paper);
    }

    /**
     * 获取试卷
     *
     * @param id
     * @return
     */
    @Override
    public PaperDto getPaperById(String id) {
        PaperDto paperDto = new PaperDto();
        Paper paper = paperService.getById(id);
        BeanUtils.copyProperties(paper, paperDto);

        List<ParperQuestionBank> parperQuestionBankList = parperQuestionBankService.list(Wrappers.<ParperQuestionBank>lambdaQuery().eq(ParperQuestionBank::getPaperId, id));
        if (parperQuestionBankList.size() > 0) {
            List<String> questionBankIds = parperQuestionBankList.stream().map(ParperQuestionBank::getQuestionBankId).collect(Collectors.toList());
            List<QuestionBank> questionBankList = questionBankService.list(Wrappers.<QuestionBank>lambdaQuery().in(QuestionBank::getId, questionBankIds));
            for (QuestionBank questionBank : questionBankList) {
                questionBank.setQuestionAnschas(Arrays.asList(questionBank.getQuestionAnscha().split("##")));
                questionBank.setQuestionOptions(Arrays.asList(questionBank.getQuestionOption().split("##")));
            }
            paperDto.setParperQuestionBankList(questionBankList);
        }
        return paperDto;
    }

    /**
     * 新增/修改试卷
     *
     * @param paperDto
     * @return
     */
    @Override
    @Transactional
    public boolean savePaper(PaperDto paperDto) {
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperDto, paper);
        if (StringUtils.isBlank(paperDto.getId())) {
            paper.setCreateDefault();
        } else {
            paper.setUpdateDefault();
        }
        List<QuestionBank> parperQuestionBankList = paperDto.getParperQuestionBankList();
        paperService.saveOrUpdate(paper);
        parperQuestionBankService.remove(Wrappers.<ParperQuestionBank>lambdaQuery().eq(ParperQuestionBank::getPaperId, paper.getId()));

        List<ParperQuestionBank> parperQuestionBanks = new ArrayList<>();
        for (QuestionBank questionBank : parperQuestionBankList) {
            ParperQuestionBank parperQuestionBank = new ParperQuestionBank();
            parperQuestionBank.setPaperId(paper.getId());
            parperQuestionBank.setQuestionBankId(questionBank.getId());
            parperQuestionBanks.add(parperQuestionBank);
        }

        return parperQuestionBankService.saveBatch(parperQuestionBanks);
    }

    /**
     * 培训首页统计
     *
     * @return
     */
    @Override
    public Map<String, String> trainStatistics() {
        Map<String, String> map = new LinkedHashMap<>();
        List<TrainingPlan> trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery().eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue()));
        map.put("总数", trainingPlanList.size() + "");
        long count = trainingPlanList.stream().filter(trainingPlan -> trainingPlan.getLessonScheduleState().equals("1")).count();
        map.put("待审核", count + "");
        trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery()
                .eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue())
                .le(TrainingPlan::getLearningStartTime, new Date())
                .ge(TrainingPlan::getLearningEndTime, new Date())
                .eq(TrainingPlan::getLessonScheduleState, "2"));
        map.put("正在进行", trainingPlanList.size() + "");
        trainingPlanList = trainingPlanService.list(Wrappers.<TrainingPlan>lambdaQuery()
                .eq(TrainingPlan::getIsDel, DeletedEnum.VALID.getValue())
                .gt(TrainingPlan::getLearningStartTime, new Date())
                .eq(TrainingPlan::getLessonScheduleState, "2"));
        map.put("待开始", trainingPlanList.size() + "");
        return map;
    }
}
