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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.grade.bo.UserPeriodQueryBo;
import com.zhongzheng.modules.grade.service.IUserPeriodStatusService;
import com.zhongzheng.modules.grade.vo.*;
import com.zhongzheng.modules.user.bo.SubjectStudyRecordQueryBo;
import com.zhongzheng.modules.user.domain.User;
import com.zhongzheng.modules.user.domain.UserPlan;
import com.zhongzheng.modules.user.domain.UserStudyRecordPhoto;
import com.zhongzheng.modules.user.service.IUserStudyRecordPhotoService;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.vo.SubjectStudyRecordVo;
import com.zhongzheng.modules.user.vo.UserStudyRecordPhotoVo;
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.grade.bo.ClassGradeUserAddBo;
import com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo;
import com.zhongzheng.modules.grade.bo.ClassGradeUserEditBo;
import com.zhongzheng.modules.grade.domain.ClassGradeUser;
import com.zhongzheng.modules.grade.mapper.ClassGradeUserMapper;
import com.zhongzheng.modules.grade.service.IClassGradeUserService;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学员记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-10
 */
@Service
public class ClassGradeUserServiceImpl extends ServiceImpl<ClassGradeUserMapper, ClassGradeUser> implements IClassGradeUserService {

    @Autowired
    private IUserStudyRecordService iUserStudyRecordService;

    @Autowired
    private IUserPeriodStatusService userPeriodStatusService;

    @Autowired
    private IUserStudyRecordPhotoService userStudyRecordPhotoService;

    @Override
    public ClassGradeUserVo queryById(Long id){
        ClassGradeUser db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, ClassGradeUserVo.class);
    }

    @Override
    public List<ClassGradeUserVo> queryList(ClassGradeUserQueryBo bo) {
        return entity2Vo(baseMapper.select(bo));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<ClassGradeUserVo> entity2Vo(Collection<ClassGradeUserVo> collection) {
        List<ClassGradeUserVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, ClassGradeUserVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<ClassGradeUserVo> page = (Page<ClassGradeUserVo>)collection;
            Page<ClassGradeUserVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(ClassGradeUserAddBo bo) {
        ClassGradeUser add = BeanUtil.toBean(bo, ClassGradeUser.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ClassGradeUserEditBo bo) {
        for (Long aLong : bo.getId()) {
            ClassGradeUser update = new ClassGradeUser();
            update.setUpdateTime(DateUtils.getNowTime());
            update.setId(aLong);
            if (bo.getStatus() != null) {
                update.setStatus(bo.getStatus());
            }
            if (bo.getFinishStatus() != null) {
                update.setFinishStatus(bo.getFinishStatus());
            }
            if (bo.getPeriodStatus() != null) {
                update.setPeriodStatus(bo.getPeriodStatus());
            }
            if (bo.getLearnStatus() != null) {
                update.setLearnStatus(bo.getLearnStatus());
            }
            this.updateById(update);
        }

        return true;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ClassGradeUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<ClassGradeUserGoodsVo> listUser(ClassGradeUserQueryBo bo) {
        return baseMapper.listUser(bo);
    }

    @Override
    public List<ClassPeriodStudentVo> listUserPeriod(ClassGradeUserQueryBo bo) {
        List<ClassPeriodStudentVo> classPeriodStudentVos = baseMapper.listUserPeriod(bo);
        for (ClassPeriodStudentVo classPeriodStudentVo : classPeriodStudentVos) {
            Long secLong = 0L;
            Long studyLong = 0L;

            SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
            subjectStudyRecordQueryBo.setGoodsId(classPeriodStudentVo.getGoodsId());
            subjectStudyRecordQueryBo.setUserId(bo.getUserId());
            subjectStudyRecordQueryBo.setGradeId(bo.getGradeId());
            List<SubjectStudyRecordVo> subjectStudyRecordVos = iUserStudyRecordService.listSubject(subjectStudyRecordQueryBo);
            for (SubjectStudyRecordVo subjectStudyRecordVo : subjectStudyRecordVos) {
                secLong = new BigDecimal(secLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getSectionNum().toString())).longValue();
                studyLong = new BigDecimal(studyLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getRecordNum().toString())).longValue();

            }
            //BigDecimal divide = new BigDecimal(studyLong.toString()).divide(new BigDecimal(secLong.toString()),2,BigDecimal.ROUND_HALF_UP);
            //classGradeStudentVo.setStudyNum(divide.multiply(new BigDecimal("100")).longValue());
            //总节数
            classPeriodStudentVo.setSecAllNum(secLong);
            //学习节数
            classPeriodStudentVo.setStuAllNum(studyLong);

            //总的审核状态按照策划规则
            ClassGradeUserQueryBo classGradeUserQueryBo = new ClassGradeUserQueryBo();
            classGradeUserQueryBo.setGradeId(bo.getGradeId());
            classGradeUserQueryBo.setUserId(classPeriodStudentVo.getUserId());
            classGradeUserQueryBo.setGoodsId(classPeriodStudentVo.getGoodsId());
            List<ClassPeriodUserVo> classPeriodUserVos1 = baseMapper.listPeriod(classGradeUserQueryBo);
            ClassPeriodUserVo classPeriodUserVos = classPeriodUserVos1.get(0);
            classPeriodUserVos.setPending(classPeriodStudentVo.getSecAllNum()-classPeriodUserVos.getPass()-classPeriodUserVos.getCheat());
            classPeriodUserVos.setExamPending(classPeriodUserVos.getExamNum()-classPeriodUserVos.getExamPass()-classPeriodUserVos.getExamCheat());
            if (classPeriodStudentVo.getStuAllNum().equals(classPeriodStudentVo.getSecAllNum()) && classPeriodStudentVo.getExamNum().equals(classPeriodStudentVo.getRecordNum())){
                if (classPeriodUserVos.getPending() != 0 || classPeriodUserVos.getExamPending() != 0){
                    classPeriodStudentVo.setAllStatus(0);
                }else if (classPeriodUserVos.getCheat() != 0 || classPeriodUserVos.getExamCheat() != 0){
                    classPeriodStudentVo.setAllStatus(1);
                }else {
                    classPeriodStudentVo.setAllStatus(2);
                }
            }else {
                classPeriodStudentVo.setAllStatus(-1);
            }
            classPeriodStudentVo.setWaitRebuildNum(classPeriodUserVos.getCheat());
            if (!CollectionUtils.isEmpty(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()))) {
                classPeriodStudentVo.setStartTime(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()).get(0));
            }
        }
        return classPeriodStudentVos;
    }

    @Override
    public List<ClassPeriodUserVo> listPeriod(ClassGradeUserQueryBo bo) {
        List<ClassPeriodUserVo> classPeriodUserVos = baseMapper.listPeriod(bo);
        for (ClassPeriodUserVo classPeriodUserVo : classPeriodUserVos) {
            Long secLong = 0L;
            Long studyLong = 0L;
            SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
            subjectStudyRecordQueryBo.setGoodsId(classPeriodUserVo.getGoodsId());
            subjectStudyRecordQueryBo.setUserId(bo.getUserId());
            subjectStudyRecordQueryBo.setGradeId(bo.getGradeId());
            List<SubjectStudyRecordVo> subjectStudyRecordVos = iUserStudyRecordService.listSubject(subjectStudyRecordQueryBo);
            for (SubjectStudyRecordVo subjectStudyRecordVo : subjectStudyRecordVos) {
                secLong = new BigDecimal(secLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getSectionNum().toString())).longValue();
                studyLong = new BigDecimal(studyLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getRecordNum().toString())).longValue();

            }
            //BigDecimal divide = new BigDecimal(studyLong.toString()).divide(new BigDecimal(secLong.toString()),2,BigDecimal.ROUND_HALF_UP);
            //classGradeStudentVo.setStudyNum(divide.multiply(new BigDecimal("100")).longValue());
            classPeriodUserVo.setSecAllNum(secLong);
            classPeriodUserVo.setStuAllNum(studyLong);
            classPeriodUserVo.setPending(classPeriodUserVo.getSecAllNum()-classPeriodUserVo.getPass()-classPeriodUserVo.getCheat());
            classPeriodUserVo.setExamPending(classPeriodUserVo.getExamNum()-classPeriodUserVo.getExamPass()-classPeriodUserVo.getExamCheat());

            if (classPeriodUserVo.getStuAllNum().equals(classPeriodUserVo.getSecAllNum()) && classPeriodUserVo.getExamNum().equals(classPeriodUserVo.getRecordNum())){
                if (classPeriodUserVo.getPending() != 0 || classPeriodUserVo.getExamPending() != 0){
                    classPeriodUserVo.setAllStatus(0);
                }else if (classPeriodUserVo.getCheat() != 0 || classPeriodUserVo.getExamCheat() != 0){
                    classPeriodUserVo.setAllStatus(1);
                }else {
                    classPeriodUserVo.setAllStatus(2);
                }
            }else {
                classPeriodUserVo.setAllStatus(-1);
            }
        }
        return classPeriodUserVos;
    }

    @Override
    public List<ClassPeriodVo> listPeriodAudit(ClassGradeUserQueryBo bo) {
        List<ClassPeriodVo> classPeriodVos = baseMapper.listPeriodAudit(bo);
        classPeriodVos.addAll(baseMapper.listPeriodAuditSection(bo));
        for (ClassPeriodVo classPeriodVo : classPeriodVos) {
            if (classPeriodVo.getType() == 1){
                List<ClassPeriodChapterVo> classPeriodChapterVos = baseMapper.listperiodChapter(classPeriodVo.getId(), bo.getGoodsId(),classPeriodVo.getCourseId(), bo.getUserId());
                for (ClassPeriodChapterVo classPeriodChapterVo : classPeriodChapterVos) {
                    if (classPeriodChapterVo.getType() == 2) {
                        List<ClassPeriodSectionVo> classPeriodSectionVos = baseMapper.listPeriodSection(classPeriodChapterVo.getId(), bo.getGoodsId(), classPeriodVo.getCourseId(), bo.getUserId());
                        classPeriodSectionVos.addAll(baseMapper.listperiodExam(classPeriodChapterVo.getId(), bo.getGoodsId(),classPeriodVo.getCourseId(), bo.getUserId(),classPeriodVo.getId()));
                        for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos) {
                            if (classPeriodSectionVo.getType() == 3) {
                                UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                                userPeriodQueryBo.setSectionId(classPeriodSectionVo.getId());
                                userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                                userPeriodQueryBo.setGoodsId(bo.getGoodsId());
                                userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                                userPeriodQueryBo.setChapterId(classPeriodChapterVo.getId());
                                userPeriodQueryBo.setModuleId(classPeriodVo.getId());
                                userPeriodQueryBo.setGradeId(bo.getGradeId());
                                List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                                if (!CollectionUtils.isEmpty(userPeriodStatusVos)) {
                                    int numIndex = userPeriodStatusVos.size() - 1;
                                    UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                                    LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
                                    lqw.eq(UserStudyRecordPhoto::getPeriodId, userPeriodStatusVo.getId());
                                    classPeriodSectionVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
                                    classPeriodSectionVo.setNumIndex(Convert.toLong(userPeriodStatusVos.size()));
                                    classPeriodSectionVo.setStatus(userPeriodStatusVo.getStatus());
                                    classPeriodSectionVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
                                    classPeriodSectionVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
                                    classPeriodSectionVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
                                    classPeriodSectionVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
                                    classPeriodSectionVo.setPeriodId(userPeriodStatusVo.getPeriodId());
                                    classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                                }
                                classPeriodSectionVo.setModuleId(classPeriodVo.getId());
                                classPeriodSectionVo.setChapterId(classPeriodChapterVo.getId());
                                classPeriodSectionVo.setGoodsId(bo.getGoodsId());
                                classPeriodSectionVo.setGradeId(bo.getGradeId());
                            }else {
                                UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                                userPeriodQueryBo.setExamId(classPeriodSectionVo.getId());
                                userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                                userPeriodQueryBo.setGoodsId(bo.getGoodsId());
                                userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                                userPeriodQueryBo.setChapterId(classPeriodChapterVo.getId());
                                userPeriodQueryBo.setModuleId(classPeriodVo.getId());
                                userPeriodQueryBo.setGradeId(bo.getGradeId());
                                List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                                if (!CollectionUtils.isEmpty(userPeriodStatusVos)) {
                                    int numIndex = userPeriodStatusVos.size() - 1;
                                    UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                                    LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
                                    lqw.eq(UserStudyRecordPhoto::getPeriodId, userPeriodStatusVo.getId());
                                    classPeriodSectionVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
                                    classPeriodSectionVo.setNumIndex(Convert.toLong(userPeriodStatusVos.size()));
                                    classPeriodSectionVo.setStatus(userPeriodStatusVo.getStatus());
                                    classPeriodSectionVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
                                    classPeriodSectionVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
                                    classPeriodSectionVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
                                    classPeriodSectionVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
                                    classPeriodSectionVo.setPeriodId(userPeriodStatusVo.getPeriodId());
                                    classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                                }
                                classPeriodSectionVo.setModuleId(classPeriodVo.getId());
                                classPeriodSectionVo.setChapterId(classPeriodChapterVo.getId());
                                classPeriodSectionVo.setGoodsId(bo.getGoodsId());
                                classPeriodSectionVo.setGradeId(bo.getGradeId());
                            }
                        }

                        classPeriodChapterVo.setClassPeriodSectionList(classPeriodSectionVos);
                    }
                }
                classPeriodVo.setClassPeriods(classPeriodChapterVos);
            }
            if (classPeriodVo.getType() == 2){
                List<ClassPeriodSectionVo> classPeriodSectionVos = baseMapper.listPeriodSection(classPeriodVo.getId(), bo.getGoodsId(), classPeriodVo.getCourseId(), bo.getUserId());
                for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos) {
                    UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                    userPeriodQueryBo.setSectionId(classPeriodSectionVo.getId());
                    userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                    userPeriodQueryBo.setGoodsId(bo.getGoodsId());
                    userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                    userPeriodQueryBo.setChapterId(classPeriodVo.getId());
                    userPeriodQueryBo.setModuleId(0L);
                    userPeriodQueryBo.setGradeId(bo.getGradeId());
                    List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                    if (!CollectionUtils.isEmpty(userPeriodStatusVos)){
                        int numIndex = userPeriodStatusVos.size() - 1;
                        UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                        LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
                        lqw.eq(UserStudyRecordPhoto::getPeriodId,userPeriodStatusVo.getId());
                        classPeriodSectionVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
                        classPeriodSectionVo.setNumIndex(Convert.toLong(userPeriodStatusVos.size()));
                        classPeriodSectionVo.setStatus(userPeriodStatusVo.getStatus());
                        classPeriodSectionVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
                        classPeriodSectionVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
                        classPeriodSectionVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
                        classPeriodSectionVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
                        classPeriodSectionVo.setPeriodId(userPeriodStatusVo.getPeriodId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                    }
                    classPeriodSectionVo.setModuleId(0L);
                    classPeriodSectionVo.setChapterId(classPeriodVo.getId());
                    classPeriodSectionVo.setGoodsId(bo.getGoodsId());
                    classPeriodSectionVo.setGradeId(bo.getGradeId());
                }
                List<ClassPeriodSectionVo> classPeriodSectionVos1 = baseMapper.listPeriodSectionExam(classPeriodVo.getId(), bo.getGoodsId(), classPeriodVo.getCourseId(), bo.getUserId());
                for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos1) {
                    UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                    userPeriodQueryBo.setExamId(classPeriodSectionVo.getId());
                    userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                    userPeriodQueryBo.setGoodsId(bo.getGoodsId());
                    userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                    userPeriodQueryBo.setChapterId(classPeriodVo.getId());
                    userPeriodQueryBo.setModuleId(0L);
                    userPeriodQueryBo.setGradeId(bo.getGradeId());
                    List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                    if (!CollectionUtils.isEmpty(userPeriodStatusVos)) {
                        int numIndex = userPeriodStatusVos.size() - 1;
                        UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                        LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
                        lqw.eq(UserStudyRecordPhoto::getPeriodId, userPeriodStatusVo.getId());
                        classPeriodSectionVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
                        classPeriodSectionVo.setNumIndex(Convert.toLong(userPeriodStatusVos.size()));
                        classPeriodSectionVo.setStatus(userPeriodStatusVo.getStatus());
                        classPeriodSectionVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
                        classPeriodSectionVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
                        classPeriodSectionVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
                        classPeriodSectionVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
                        classPeriodSectionVo.setPeriodId(userPeriodStatusVo.getPeriodId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                    }
                    classPeriodSectionVo.setModuleId(0L);
                    classPeriodSectionVo.setChapterId(classPeriodVo.getId());
                    classPeriodSectionVo.setGoodsId(bo.getGoodsId());
                    classPeriodSectionVo.setGradeId(bo.getGradeId());
                }
                classPeriodSectionVos.addAll(classPeriodSectionVos1);
                classPeriodVo.setClassPeriodSectionList(classPeriodSectionVos);
            }
            if (classPeriodVo.getType() == 3){
                UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                userPeriodQueryBo.setSectionId(classPeriodVo.getId());
                userPeriodQueryBo.setCourseId(classPeriodVo.getCourseId());
                userPeriodQueryBo.setGoodsId(bo.getGoodsId());
                userPeriodQueryBo.setUserId(classPeriodVo.getUserId());
                userPeriodQueryBo.setChapterId(0L);
                userPeriodQueryBo.setModuleId(0L);
                userPeriodQueryBo.setGradeId(bo.getGradeId());
                List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                if (!CollectionUtils.isEmpty(userPeriodStatusVos)){
                    int numIndex = userPeriodStatusVos.size() - 1;
                    UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                    LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
                    lqw.eq(UserStudyRecordPhoto::getPeriodId,userPeriodStatusVo.getId());
                    classPeriodVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
                    classPeriodVo.setNumIndex(Convert.toLong(userPeriodStatusVos.size()));
                    classPeriodVo.setStatus(userPeriodStatusVo.getStatus());
                    classPeriodVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
                    classPeriodVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
                    classPeriodVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
                    classPeriodVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
                    classPeriodVo.setPeriodId(userPeriodStatusVo.getPeriodId());
                    classPeriodVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                }
                classPeriodVo.setModuleId(0L);
                classPeriodVo.setChapterId(0L);
                classPeriodVo.setGoodsId(bo.getGoodsId());
                classPeriodVo.setGradeId(bo.getGradeId());
            }

        }
        return classPeriodVos;
    }

    @Override
    public List<ClassPeriodVo> listPeriodAuditOne(ClassGradeUserQueryBo bo) {
        List<ClassPeriodVo> classPeriodVos = baseMapper.listPeriodAudit(bo);
        classPeriodVos.addAll(baseMapper.listPeriodAuditSection(bo));
        return classPeriodVos;
    }

    @Override
    public List<ClassPeriodChapterVo> listPeriodAuditChapter(ClassGradeUserQueryBo bo) {
        List<ClassPeriodChapterVo> classPeriodChapterVos = baseMapper.listperiodChapter(bo.getModuleId(), bo.getGoodsId(),bo.getCourseId(), bo.getUserId());
        return classPeriodChapterVos;
    }

    @Override
    public List<ClassPeriodSectionVo> listPeriodAuditSection(ClassGradeUserQueryBo bo) {
        List<ClassPeriodSectionVo> classPeriodSectionVos = baseMapper.listPeriodSection(bo.getChapterId(), bo.getGoodsId(), bo.getCourseId(), bo.getUserId());
        return classPeriodSectionVos;
    }

    @Override
    public ClassPeriodSectionVo listPeriodAuditStatus(UserPeriodQueryBo bo) {
        List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(bo);
        for (UserPeriodStatusVo userPeriodStatusVo : userPeriodStatusVos) {
            LambdaQueryWrapper<UserStudyRecordPhoto> lqw = Wrappers.lambdaQuery();
            lqw.eq(UserStudyRecordPhoto::getPeriodId,userPeriodStatusVo.getId());
            userPeriodStatusVo.setUserStudyRecordPhotoList(entity2PhotoVo(userStudyRecordPhotoService.list(lqw)));
        }
        int numIndex = Convert.toInt(bo.getNumIndex()) - 1;
        UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
        ClassPeriodSectionVo classPeriodSectionVo = BeanUtil.toBean(bo, ClassPeriodSectionVo.class);
        classPeriodSectionVo.setStudyStartTime(userPeriodStatusVo.getRecordStartTime());
        classPeriodSectionVo.setStudyEndTime(userPeriodStatusVo.getRecordEndTime());
        classPeriodSectionVo.setTypeName(userPeriodStatusVo.getTypeName());

        classPeriodSectionVo.setStatus(userPeriodStatusVo.getStatus());
        classPeriodSectionVo.setNumList(Convert.toLong(userPeriodStatusVos.size()));
        classPeriodSectionVo.setNumIndex(bo.getNumIndex());
        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
        classPeriodSectionVo.setRealName(userPeriodStatusVo.getRealName());
        classPeriodSectionVo.setUserStudyRecordPhotoList(userPeriodStatusVo.getUserStudyRecordPhotoList());
        classPeriodSectionVo.setPeriodStatus(userPeriodStatusVo.getPeriodStatus());
        classPeriodSectionVo.setPeriodId(userPeriodStatusVo.getPeriodId());
        if (bo.getSectionId() != null) {
            classPeriodSectionVo.setId(bo.getSectionId());
            classPeriodSectionVo.setType(3L);
        }else {
            classPeriodSectionVo.setId(bo.getExamId());
            classPeriodSectionVo.setType(4L);
        }
        return classPeriodSectionVo;
    }

    @Override
    public List<ClassGradeUserGoodsVo> sendTwentyClassGradeUser() {
        return baseMapper.sendClassGradeUser();
    }

    @Override
    public List<ClassGradeUserGoodsVo> sendTenClassGradeUser() {
        return baseMapper.sendTenClassGradeUser();
    }

    @Override
    public List<ClassGradeUserGoodsVo> sendFiveClassGradeUser() {
        return baseMapper.sendFiveClassGradeUser();
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<UserStudyRecordPhotoVo> entity2PhotoVo(Collection<UserStudyRecordPhoto> collection) {
        List<UserStudyRecordPhotoVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, UserStudyRecordPhotoVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<UserStudyRecordPhoto> page = (Page<UserStudyRecordPhoto>)collection;
            Page<UserStudyRecordPhotoVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }
}
