package com.zhongzheng.modules.grade.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.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhongzheng.common.core.redis.RedisCache;
import com.zhongzheng.common.core.redis.RedisLockEntity;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.AES;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.http.HttpUtils;
import com.zhongzheng.common.utils.polyv.PolyvUtils;
import com.zhongzheng.modules.course.vo.CourseChapterVo;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import com.zhongzheng.modules.grade.bo.*;
import com.zhongzheng.modules.grade.domain.StudyCountLog;
import com.zhongzheng.modules.grade.service.IClassGradeService;
import com.zhongzheng.modules.grade.service.IStudyCountLogService;
import com.zhongzheng.modules.grade.service.IUserPeriodStatusService;
import com.zhongzheng.modules.grade.vo.*;
import com.zhongzheng.modules.order.bo.OrderGoodsQueryBo;
import com.zhongzheng.modules.order.domain.OrderGoods;
import com.zhongzheng.modules.order.service.IOrderGoodsService;
import com.zhongzheng.modules.order.service.IOrderService;
import com.zhongzheng.modules.order.vo.OrderGoodsVo;
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.mapper.UserMapper;
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.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.domain.ClassGradeUser;
import com.zhongzheng.modules.grade.mapper.ClassGradeUserMapper;
import com.zhongzheng.modules.grade.service.IClassGradeUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
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;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    @Autowired
    private IClassGradeService iClassGradeService;

    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IStudyCountLogService iStudyCountLogService;

    @Autowired
    private IClassGradeUserService iClassGradeUserService;

    @Value("${aliyun.oss.endpoint}")
    private String ALIYUN_OSS_ENDPOINT;

    @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
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeGrade(ClassGradeUserChangeBo bo) {
        ClassGradeQueryBo classGradeQueryBo = new ClassGradeQueryBo();
        classGradeQueryBo.setUserId(bo.getUserId());
        classGradeQueryBo.setGoodsId(bo.getGoodsId());
        classGradeQueryBo.setPastDue(1L);
        List<ClassGradeVo> list = iClassGradeService.queryList(classGradeQueryBo);
        if(list!=null&&list.size()>0){
            throw new CustomException("包含购买商品班级尚未过期");
        }
        OrderGoodsVo orderGoodsVo = iOrderGoodsService.queryById(bo.getOrderGoodsId());
        if(Validator.isEmpty(orderGoodsVo)||Validator.isEmpty(orderGoodsVo.getStudyCount())||orderGoodsVo.getStudyCount()<1){
            throw new CustomException("学习次数不够");
        }
        GoodsVo goodsVo = iGoodsService.selectDetail(bo.getGoodsId());
        String requestId = IdUtil.simpleUUID();
        RedisLockEntity redisLockEntity = new RedisLockEntity();
        redisLockEntity.setLockKey(RedisLockEntity.KEY_LOCK_GRADE);
        redisLockEntity.setRequestId(requestId);
        if(redisCache.lock(redisLockEntity)){
            //分新班
            Long newGradeId = iOrderService.changeGrade(goodsVo.getGoodsName(),bo.getGoodsId(),bo.getOrderGoodsId(),bo.getGradeId(),bo.getUserId());
            if(newGradeId>0){
                //老班修改换班状态
                ClassGradeUser classGradeUser =  getOne(new LambdaQueryWrapper< ClassGradeUser>().eq(ClassGradeUser::getUserId,bo.getUserId())
                        .eq(ClassGradeUser::getStatus,1)
                        .eq(ClassGradeUser::getGradeId,bo.getOldGradeId())
                        .last("limit 1"));
                classGradeUser.setChangeGrade(1);
                classGradeUser.setUpdateTime(DateUtils.getNowTime());
                updateById(classGradeUser);
                //消耗学习次数
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setOrderGoodsId(bo.getOrderGoodsId());
                orderGoods.setStudyCount(orderGoodsVo.getStudyCount()-1);
                orderGoods.setUpdateTime(DateUtils.getNowTime());
                orderGoods.setGradeId(newGradeId);
                iOrderGoodsService.updateById(orderGoods);
                //记录消耗一次学习机会
                StudyCountLog studyCountLog = new StudyCountLog();
                studyCountLog.setGradeId(newGradeId);
                studyCountLog.setStatus(-1);
                studyCountLog.setOrderGoodsId(bo.getOrderGoodsId());
                studyCountLog.setStudyCount(1L);
                studyCountLog.setUserId(bo.getUserId());
                studyCountLog.setCreateTime(DateUtils.getNowTime());
                studyCountLog.setUpdateTime(DateUtils.getNowTime());
                studyCountLog.setOldGradeId(bo.getOldGradeId());
                iStudyCountLogService.save(studyCountLog);
            }
        }
        return true;
    }

    /**
     * 系统批量选新班
     * @param bo
     * @return
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sysChangeGrade(ClassGradeUserSysChangeBo bo) {
        for(ClassGradeUserChangeBo item : bo.getUserList()){
            ClassGradeQueryBo classGradeQueryBo = new ClassGradeQueryBo();
            classGradeQueryBo.setUserId(item.getUserId());
            classGradeQueryBo.setGoodsId(item.getGoodsId());
            classGradeQueryBo.setPastDue(1L);
            List<ClassGradeVo> list = iClassGradeService.queryList(classGradeQueryBo);
            if(list!=null&&list.size()>0){
                throw new CustomException("包含购买商品班级尚未过期");
            }
            OrderGoodsVo orderGoodsVo = iOrderGoodsService.queryById(item.getOrderGoodsId());
            if(Validator.isEmpty(orderGoodsVo)||Validator.isEmpty(orderGoodsVo.getStudyCount())||orderGoodsVo.getStudyCount()<1){
                throw new CustomException("学习次数不够");
            }
            GoodsVo goodsVo = iGoodsService.selectDetail(item.getGoodsId());
            String requestId = IdUtil.simpleUUID();
            RedisLockEntity redisLockEntity = new RedisLockEntity();
            redisLockEntity.setLockKey(RedisLockEntity.KEY_LOCK_GRADE);
            redisLockEntity.setRequestId(requestId);
            if(redisCache.lock(redisLockEntity)){
                //分新班
                Long newGradeId = iOrderService.sysChangeGrade(goodsVo.getGoodsName(),item.getGoodsId(),item.getOrderGoodsId(),item.getGradeId(),item.getUserId());
                if(newGradeId>0){
                    //老班修改换班状态
                    ClassGradeUser classGradeUser =  getOne(new LambdaQueryWrapper< ClassGradeUser>().eq(ClassGradeUser::getUserId,item.getUserId())
                            .eq(ClassGradeUser::getStatus,1)
                            .eq(ClassGradeUser::getGradeId,item.getOldGradeId())
                            .last("limit 1"));
                    classGradeUser.setChangeGrade(1);
                    classGradeUser.setUpdateTime(DateUtils.getNowTime());
                    updateById(classGradeUser);
                    //消耗学习次数
                    OrderGoods orderGoods = new OrderGoods();
                    orderGoods.setOrderGoodsId(item.getOrderGoodsId());
                    orderGoods.setStudyCount(orderGoodsVo.getStudyCount()-1);
                    orderGoods.setUpdateTime(DateUtils.getNowTime());
                    orderGoods.setGradeId(newGradeId);
                    iOrderGoodsService.updateById(orderGoods);
                    //记录消耗一次学习机会
                    StudyCountLog studyCountLog = new StudyCountLog();
                    studyCountLog.setGradeId(newGradeId);
                    studyCountLog.setStatus(-1);
                    studyCountLog.setOrderGoodsId(item.getOrderGoodsId());
                    studyCountLog.setStudyCount(1L);
                    studyCountLog.setUserId(item.getUserId());
                    studyCountLog.setCreateTime(DateUtils.getNowTime());
                    studyCountLog.setUpdateTime(DateUtils.getNowTime());
                    studyCountLog.setOldGradeId(item.getUserId());
                    iStudyCountLogService.save(studyCountLog);
                }
            }
        }
        return true;
    }

    /**
     * 获取单个用户的最新审核状态并更新
     * @param bo
     * @return
     */
    @Override
    public Integer updateUserPeriodStatus(ClassGradeUserQueryBo bo) {
        if(Validator.isEmpty(bo.getUserId())){
            throw new CustomException("参数错误");
        }
        if(Validator.isEmpty(bo.getGradeId())){
            throw new CustomException("参数错误");
        }
        ClassPeriodStudentVo classPeriodStudentVo = baseMapper.getUserPeriodStatus(bo);
        Long secLong = 0L;
        Long studyLong = 0L;

        SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
        subjectStudyRecordQueryBo.setGoodsId(classPeriodStudentVo.getGoodsId());
        subjectStudyRecordQueryBo.setUserId(classPeriodStudentVo.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());
        //當前班級學員審核狀態 -1不可审核 0待審  1未通过 2通过
        Integer gradePeriodStatus = 0; //班级里的审核状态字段，保持一致 0 未通过 1通过 2待审核 -1 不可审核
        if(Validator.isEmpty(classPeriodStudentVo.getSecAllNum())||classPeriodStudentVo.getSecAllNum()==0){
            //空课程
            classPeriodStudentVo.setAllStatus(-1);
            gradePeriodStatus = -1;
        }else{
            if (classPeriodStudentVo.getStuAllNum().equals(classPeriodStudentVo.getSecAllNum()) && classPeriodStudentVo.getExamNum().equals(classPeriodStudentVo.getRecordNum())){
                if (classPeriodUserVos.getPending() != 0 || classPeriodUserVos.getExamPending() != 0){
                    classPeriodStudentVo.setAllStatus(0);
                    gradePeriodStatus = 2;
                }else if (classPeriodUserVos.getCheat() != 0 || classPeriodUserVos.getExamCheat() != 0){
                    classPeriodStudentVo.setAllStatus(1);
                    gradePeriodStatus = 0;
                }else {
                    classPeriodStudentVo.setAllStatus(2);
                    gradePeriodStatus = 1;
                }
            }else {
                classPeriodStudentVo.setAllStatus(-1);
                gradePeriodStatus = -1;
            }
        }

        LambdaQueryWrapper<ClassGradeUser> classGradeUser = Wrappers.lambdaQuery();
        classGradeUser.eq(ClassGradeUser::getUserId, bo.getUserId());
        classGradeUser.eq(ClassGradeUser::getGradeId, bo.getGradeId());
        ClassGradeUser classGradeUser1 = new ClassGradeUser();
        classGradeUser1.setPeriodStatus(gradePeriodStatus);
        if(gradePeriodStatus==2){
            classGradeUser1.setPeriodWaitTime(DateUtils.getNowTime());
        }
        if(gradePeriodStatus==1){
            classGradeUser1.setPeriodTime(DateUtils.getNowTime());
            //判断结业
            long nowTime = System.currentTimeMillis()/1000;
            if(Validator.isNotEmpty(classPeriodStudentVo.getClassEndTime())&&Validator.isNotEmpty(classPeriodStudentVo.getClassStartTime())){
                if(nowTime<classPeriodStudentVo.getClassEndTime()&&nowTime>classPeriodStudentVo.getClassStartTime()){
                    classGradeUser1.setFinishStatus(1);
                }
            }
        }
        classGradeUser1.setUpdateTime(DateUtils.getNowTime());
        iClassGradeUserService.update(classGradeUser1, classGradeUser);
        return gradePeriodStatus;
    }

    @Override
    public Boolean pushOfficialInfo(ClassGradeUserQueryBo bo){
        ClassGradeUserVo userVo = this.baseMapper.selectUser(bo);
        if(Validator.isEmpty(userVo)){
            throw new CustomException("用户不存在");
        }

        Map<String, String> params =  new HashMap<>();
        params.put("zh","peixunjigou");
        params.put("bh",userVo.getCode());
        params.put("xm","何建林");
        params.put("sfz",userVo.getIdCard());
        params.put("dh",userVo.getTelPhone());
        params.put("bmrq","2022-11-11");
        String  dataTxt = params.get("zh")+params.get("bh")+params.get("xm")+params.get("sfz")+params.get("dh")+params.get("bmrq");
        System.out.println(dataTxt);
        String encrypted = dataSign(dataTxt,"0ca175b9c0f726a831d895e26933246");
        System.out.println(encrypted);
        params.put("SignMsg",encrypted);
        String respone = "";
        try{
         respone = HttpUtils.postFormBody("http://jypt-dev.gdcic.net/organjxjy/ShangBaoMingdan",params);
        System.out.println(respone);
        }catch (IOException e){
            throw new CustomException("请求错误");
        }
        /*ClassGradeUserOfficialBo officialBo = new ClassGradeUserOfficialBo();
        officialBo.setZh("peixunjigou");
        officialBo.setBh(userVo.getClassName());
        officialBo.setSfz(userVo.getIdCard());
        officialBo.setXm(userVo.getRealName());
        officialBo.setDh(userVo.getTelPhone());
        officialBo.setBmrq("2022-2-15");*/

        return true;
    }



    public String dataSign(String SourceMsg,String PKey)
    {
        try{
            String TempPKey;
            TempPKey = getMD5(PKey,"UTF-8");
            System.out.println(TempPKey);
            TempPKey = SourceMsg + TempPKey;
            System.out.println(TempPKey);
            TempPKey = getMD5(TempPKey,"UTF-8");
            System.out.println(TempPKey);
            return TempPKey;
        }catch (NoSuchAlgorithmException e){
            throw new CustomException("数据错误");
        }catch (UnsupportedEncodingException exception){
            throw new CustomException("数据错误");
        }catch (Exception exception2){
            throw new CustomException("数据错误");
        }

    }

    public static String MD5Encode(String origin) {
        StringBuffer sb = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(origin.getBytes("utf8"));
            byte[] result = md.digest();
            for (int i = 0; i < result.length; i++) {
                //int val = result[i] & 0xff;
                //sb.append(Integer.toHexString(val));
                int val = (result[i] & 0x000000ff) | 0xffffff00;
                sb.append(Integer.toHexString(val).substring(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public  String getMD5(String str, String encoding) throws Exception {

        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(str.getBytes(encoding));

        byte[] result = md.digest();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < result.length; i++) {

            int val = result[i] & 0xff;

            sb.append(Integer.toHexString(val));

        }
        return sb.toString();

    }

    @Override
    public List<ClassPeriodStudentExportVo> exportPo(ClassGradeUserQueryBo bo) {
        //查询班级学员信息
        List<ClassPeriodStudentVo> classPeriodStudentVos = baseMapper.listUserPeriod(bo);
        List<ClassPeriodStudentExportVo> exportList = new ArrayList<>();
        //查找学员学习记录
        for (ClassPeriodStudentVo classPeriodStudentVo : classPeriodStudentVos) {
            Long secLong = 0L;
            Long studyLong = 0L;

            SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
            subjectStudyRecordQueryBo.setGoodsId(classPeriodStudentVo.getGoodsId());
            subjectStudyRecordQueryBo.setUserId(classPeriodStudentVo.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());
            //當前班級學員審核狀態 -1不可审核 0待審  1未通过 2通过
            Integer gradePeriodStatus = 0; //班级里的审核状态字段，保持一致 0 未通过 1通过 2待审核 -1 不可审核
            if(Validator.isEmpty(classPeriodStudentVo.getSecAllNum())||classPeriodStudentVo.getSecAllNum()==0){
                //空课程
                classPeriodStudentVo.setAllStatus(-1);
                gradePeriodStatus = -1;
            }else{
                if (classPeriodStudentVo.getStuAllNum().equals(classPeriodStudentVo.getSecAllNum()) && classPeriodStudentVo.getExamNum().equals(classPeriodStudentVo.getRecordNum())){
                    if (classPeriodUserVos.getPending() != 0 || classPeriodUserVos.getExamPending() != 0){
                        classPeriodStudentVo.setAllStatus(0);
                        gradePeriodStatus = 2;
                    }else if (classPeriodUserVos.getCheat() != 0 || classPeriodUserVos.getExamCheat() != 0){
                        classPeriodStudentVo.setAllStatus(1);
                        gradePeriodStatus = 0;
                    }else {
                        classPeriodStudentVo.setAllStatus(2);
                        gradePeriodStatus = 1;
                    }
                }else {
                    classPeriodStudentVo.setAllStatus(-1);
                    gradePeriodStatus = -1;
                }
            }


            classPeriodStudentVo.setWaitRebuildNum(classPeriodUserVos.getCheat()+classPeriodUserVos.getExamCheat());
            if (!CollectionUtils.isEmpty(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()))) {
                //查找开始学习时间
                classPeriodStudentVo.setStartTime(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()).get(0));
            }
            if(Validator.isNotEmpty(bo.getUserPhoto())&&bo.getUserPhoto()==1){
                //解析身份证和一寸照
                if(Validator.isNotEmpty(classPeriodStudentVo.getKeyValue())){
                    JSONObject keyJSON = null;
                    try {
                        keyJSON = JSON.parseObject(classPeriodStudentVo.getKeyValue());
                        if(keyJSON.containsKey("recent_photos")){
                            String recent_photos_data = keyJSON.getString("recent_photos");
                            JSONObject recent_photos_jsondata= JSON.parseObject(recent_photos_data);
                            String recent_photos = recent_photos_jsondata.getString("value");
                            classPeriodStudentVo.setOneInchPhotos(ALIYUN_OSS_ENDPOINT+"/"+recent_photos);
                        }
                        if(keyJSON.containsKey("idcard_face_photo")){
                            String idcard_face_photo_data = keyJSON.getString("idcard_face_photo");
                            JSONObject idcard_face_photo_jsondata= JSON.parseObject(idcard_face_photo_data);
                            String idcard_face_photo = idcard_face_photo_jsondata.getString("value");
                            classPeriodStudentVo.setIdCardImg1(ALIYUN_OSS_ENDPOINT+"/"+idcard_face_photo);
                        }
                        classPeriodStudentVo.setKeyValue(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            ClassPeriodStudentExportVo addItem = BeanUtil.toBean(classPeriodStudentVo, ClassPeriodStudentExportVo.class);

            addItem.setStudyTimeStr(DateUtils.timestampToDate(classPeriodStudentVo.getStudyStartTime())+"-"+DateUtils.timestampToDate(classPeriodStudentVo.getStudyEndTime()));
            addItem.setClassTimeStr(DateUtils.timestampToDate(classPeriodStudentVo.getClassStartTime())+"-"+DateUtils.timestampToDate(classPeriodStudentVo.getClassEndTime()));

            ClassGradeUserQueryBo classGradeUserQueryBo1 = new ClassGradeUserQueryBo();
            classGradeUserQueryBo1.setUserId(classPeriodStudentVo.getUserId());
            classGradeUserQueryBo1.setGradeId(classPeriodStudentVo.getGradeId());
            classGradeUserQueryBo1.setGoodsId(classPeriodStudentVo.getGoodsId());
            addItem.setPeriodVoList(listUserStudyRecord(classGradeUserQueryBo1));
            exportList.add(addItem);
        }
        return exportList;
    }

    private UserPeriodExportVo mergeData(Long index,String moduleName,String chapterName,String sectionName,ClassPeriodSectionVo classPeriodSectionVo,BigDecimal performance,Boolean isExam){
        //录入学时记录
        UserPeriodExportVo userPeriodExportVo = new UserPeriodExportVo();
        userPeriodExportVo.setId(index);
        userPeriodExportVo.setModuleName(moduleName);
        userPeriodExportVo.setChapterName(chapterName);
        userPeriodExportVo.setSectionName(sectionName);
        userPeriodExportVo.setDurationTime(classPeriodSectionVo.getDurationTime());
        userPeriodExportVo.setStudyTimeTxt(DateUtils.timestampToDate(classPeriodSectionVo.getStudyStartTime())+"-"+DateUtils.timestampToDate(classPeriodSectionVo.getStudyEndTime()));
        if(Validator.isEmpty(classPeriodSectionVo.getStudyEndTime())||Validator.isEmpty(classPeriodSectionVo.getStudyStartTime())){
            userPeriodExportVo.setStudyTimeLongTxt("无"); //没有学习结束时间
            if(!isExam){
                userPeriodExportVo.setStudyStatusTxt("未完成");
            }else{
                userPeriodExportVo.setStudyStatusTxt("未做题");
            }
        }else {
            userPeriodExportVo.setStudyTimeLongTxt((classPeriodSectionVo.getStudyEndTime().longValue()-classPeriodSectionVo.getStudyStartTime().longValue())+"秒");
            if(!isExam){
                userPeriodExportVo.setStudyStatusTxt("完成");
            }else{
                userPeriodExportVo.setStudyStatusTxt("已做题");
            }
            userPeriodExportVo.setStudyStatusTxt("");
        }
        userPeriodExportVo.setScore(performance);

        return userPeriodExportVo;
    }

    @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(classPeriodStudentVo.getUserId());
            subjectStudyRecordQueryBo.setGradeId(classPeriodStudentVo.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(classPeriodStudentVo.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());
            //當前班級學員審核狀態 -1不可审核 0待審  1未通过 2通过
            Integer gradePeriodStatus = 0; //班级里的审核状态字段，保持一致 0 未通过 1通过 2待审核 -1 不可审核
            if(Validator.isEmpty(classPeriodStudentVo.getSecAllNum())||classPeriodStudentVo.getSecAllNum()==0){
                //空课程
                classPeriodStudentVo.setAllStatus(-1);
                gradePeriodStatus = -1;
            }else{
                if (classPeriodStudentVo.getStuAllNum().equals(classPeriodStudentVo.getSecAllNum()) && classPeriodStudentVo.getExamNum().equals(classPeriodStudentVo.getRecordNum())){
                    if (classPeriodUserVos.getPending() != 0 || classPeriodUserVos.getExamPending() != 0){
                        classPeriodStudentVo.setAllStatus(0);
                        gradePeriodStatus = 2;
                    }else if (classPeriodUserVos.getCheat() != 0 || classPeriodUserVos.getExamCheat() != 0){
                        classPeriodStudentVo.setAllStatus(1);
                        gradePeriodStatus = 0;
                    }else {
                        classPeriodStudentVo.setAllStatus(2);
                        gradePeriodStatus = 1;
                    }
                }else {
                    classPeriodStudentVo.setAllStatus(-1);
                    gradePeriodStatus = -1;
                }
            }

            //更新班级学时状态
           /* if(!gradePeriodStatus.equals(classPeriodStudentVo.getPeriodStatus())){
                LambdaQueryWrapper<ClassGradeUser> classGradeUser = Wrappers.lambdaQuery();
                classGradeUser.eq(ClassGradeUser::getUserId, classPeriodStudentVo.getUserId());
                classGradeUser.eq(ClassGradeUser::getGradeId, classPeriodStudentVo.getGradeId());
                ClassGradeUser classGradeUser1 = new ClassGradeUser();
                classGradeUser1.setPeriodStatus(gradePeriodStatus);
                if(gradePeriodStatus==2){
                    classGradeUser1.setPeriodWaitTime(DateUtils.getNowTime());
                }
                classGradeUser1.setUpdateTime(DateUtils.getNowTime());
                iClassGradeUserService.update(classGradeUser1, classGradeUser);
            }*/
           /* 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()+classPeriodUserVos.getExamCheat());
            if (!CollectionUtils.isEmpty(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()))) {
                //查找开始学习时间
                classPeriodStudentVo.setStartTime(baseMapper.selectStart(classPeriodStudentVo.getUserId(),classPeriodStudentVo.getGoodsId(),classPeriodStudentVo.getGradeId()).get(0));
            }

            if(Validator.isNotEmpty(bo.getUserPhoto())&&bo.getUserPhoto()==1){
                //解析身份证和一寸照
                if(Validator.isNotEmpty(classPeriodStudentVo.getKeyValue())){
                    JSONObject keyJSON = null;
                    try {
                        keyJSON = JSON.parseObject(classPeriodStudentVo.getKeyValue());
                        if(keyJSON.containsKey("recent_photos")){
                            String recent_photos_data = keyJSON.getString("recent_photos");
                            JSONObject recent_photos_jsondata= JSON.parseObject(recent_photos_data);
                            String recent_photos = recent_photos_jsondata.getString("value");
                            classPeriodStudentVo.setOneInchPhotos(recent_photos);
                        }
                        if(keyJSON.containsKey("idcard_face_photo")){
                            String idcard_face_photo_data = keyJSON.getString("idcard_face_photo");
                            JSONObject idcard_face_photo_jsondata= JSON.parseObject(idcard_face_photo_data);
                            String idcard_face_photo = idcard_face_photo_jsondata.getString("value");
                            classPeriodStudentVo.setIdCardImg1(idcard_face_photo);
                        }
                        classPeriodStudentVo.setKeyValue(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return classPeriodStudentVos;
    }

    @Override
    public List<UserPeriodExportVo> listUserStudyRecord(ClassGradeUserQueryBo bo) {
        //学习记录
        ClassGradeUserQueryBo cGQB = new ClassGradeUserQueryBo();
        cGQB.setGoodsId(bo.getGoodsId());
        cGQB.setUserId(bo.getUserId());
        cGQB.setGradeId(bo.getGradeId());
        List<ClassPeriodVo> list = listPeriodAudit(cGQB);
        String moduleName = "";
        String chapterName = "";
        String sectionName = "";
        List<UserPeriodExportVo> studyList = new ArrayList<>();
        long i = 0;
        Long gradeId = bo.getGradeId();
        Long userId = bo.getUserId();
        Long goodsId = bo.getGoodsId();
        for(ClassPeriodVo classPeriodVo : list){
            //为模块搜索下面的章 和节 并搜索学时记录
            if (classPeriodVo.getType() == 1){
                moduleName = classPeriodVo.getTypeName();
                List<ClassPeriodChapterVo> classPeriodChapterVos = baseMapper.listperiodChapter(classPeriodVo.getId(), goodsId,classPeriodVo.getCourseId(), userId,gradeId);
                for (ClassPeriodChapterVo classPeriodChapterVo : classPeriodChapterVos) {
                    if (classPeriodChapterVo.getType() == 2) {
                        chapterName = classPeriodChapterVo.getTypeName();
                        List<ClassPeriodSectionVo> classPeriodSectionVos = baseMapper.listPeriodSection(classPeriodChapterVo.getId(), goodsId, classPeriodVo.getCourseId(), userId);
                        classPeriodSectionVos.addAll(baseMapper.listperiodExam(classPeriodChapterVo.getId(), goodsId,classPeriodVo.getCourseId(), userId,classPeriodVo.getId()));
                        for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos) {
                            i++;
                            if (classPeriodSectionVo.getType() == 3) {
                                sectionName = classPeriodSectionVo.getTypeName();
                                UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                                userPeriodQueryBo.setSectionId(classPeriodSectionVo.getId());
                                userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                                userPeriodQueryBo.setGoodsId(goodsId);
                                userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                                userPeriodQueryBo.setChapterId(classPeriodChapterVo.getId());
                                userPeriodQueryBo.setModuleId(classPeriodVo.getId());
                                userPeriodQueryBo.setGradeId(gradeId);
                                List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                                if (!CollectionUtils.isEmpty(userPeriodStatusVos)) {
                                    int numIndex = userPeriodStatusVos.size() - 1;
                                    UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                                    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.setPeriodStatusId(userPeriodStatusVo.getId());
                                    classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                                    classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                                    classPeriodSectionVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                                }
                                classPeriodSectionVo.setModuleId(classPeriodVo.getId());
                                classPeriodSectionVo.setChapterId(classPeriodChapterVo.getId());
                                classPeriodSectionVo.setGoodsId(goodsId);
                                classPeriodSectionVo.setGradeId(gradeId);
                                studyList.add(mergeData(i,moduleName,chapterName,sectionName,classPeriodSectionVo,(new BigDecimal(0)),false));
                            }else {
                                //章卷
                                sectionName = "";
                                UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                                userPeriodQueryBo.setExamId(classPeriodSectionVo.getId());
                                userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                                userPeriodQueryBo.setGoodsId(goodsId);
                                userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                                userPeriodQueryBo.setChapterId(classPeriodChapterVo.getId());
                                userPeriodQueryBo.setModuleId(classPeriodVo.getId());
                                userPeriodQueryBo.setGradeId(gradeId);
                                List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                                if (!CollectionUtils.isEmpty(userPeriodStatusVos)) {
                                    int numIndex = userPeriodStatusVos.size() - 1;
                                    UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                                    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.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                                    classPeriodSectionVo.setPeriodStatusId(userPeriodStatusVo.getId());
                                    classPeriodChapterVo.setPerformance(Convert.toBigDecimal(userPeriodStatusVo.getPerformance()));
                                }
                                classPeriodSectionVo.setModuleId(classPeriodVo.getId());
                                classPeriodSectionVo.setChapterId(classPeriodChapterVo.getId());
                                classPeriodSectionVo.setGoodsId(goodsId);
                                classPeriodSectionVo.setGradeId(gradeId);
                                studyList.add(mergeData(i,moduleName,chapterName,sectionName,classPeriodSectionVo,classPeriodChapterVo.getPerformance(),true));
                            }
                        }
                    }
                }
            }
            //为章搜索节记录和学时记录
            if (classPeriodVo.getType() == 2){
                moduleName = "";
                chapterName = classPeriodVo.getTypeName();
                List<ClassPeriodSectionVo> classPeriodSectionVos = baseMapper.listPeriodSection(classPeriodVo.getId(), goodsId, classPeriodVo.getCourseId(), userId);
                for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos) {
                    i++;
                    sectionName = classPeriodSectionVo.getTypeName();
                    UserPeriodQueryBo userPeriodQueryBo = new UserPeriodQueryBo();
                    userPeriodQueryBo.setSectionId(classPeriodSectionVo.getId());
                    userPeriodQueryBo.setCourseId(classPeriodSectionVo.getCourseId());
                    userPeriodQueryBo.setGoodsId(goodsId);
                    userPeriodQueryBo.setUserId(classPeriodSectionVo.getUserId());
                    userPeriodQueryBo.setChapterId(classPeriodVo.getId());
                    userPeriodQueryBo.setModuleId(0L);
                    userPeriodQueryBo.setGradeId(gradeId);
                    List<UserPeriodStatusVo> userPeriodStatusVos = userPeriodStatusService.selectPeriodStatus(userPeriodQueryBo);
                    if (!CollectionUtils.isEmpty(userPeriodStatusVos)){
                        int numIndex = userPeriodStatusVos.size() - 1;
                        UserPeriodStatusVo userPeriodStatusVo = userPeriodStatusVos.get(numIndex);
                        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.setPeriodStatusId(userPeriodStatusVo.getId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                        classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                        classPeriodSectionVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                    }
                    classPeriodSectionVo.setModuleId(0L);
                    classPeriodSectionVo.setChapterId(classPeriodVo.getId());
                    classPeriodSectionVo.setGoodsId(goodsId);
                    classPeriodSectionVo.setGradeId(gradeId);
                    studyList.add(mergeData(i,moduleName,chapterName,sectionName,classPeriodSectionVo,(new BigDecimal(0)),false));
                }
                List<ClassPeriodSectionVo> classPeriodSectionVos1 = baseMapper.listPeriodSectionExam(classPeriodVo.getId(), goodsId, classPeriodVo.getCourseId(), userId);
                for (ClassPeriodSectionVo classPeriodSectionVo : classPeriodSectionVos1) {
                    //章卷
                    i++;
                    sectionName = "";
                    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);
                        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.setPeriodStatusId(userPeriodStatusVo.getId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                        classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                        classPeriodVo.setPerformance(Convert.toBigDecimal(userPeriodStatusVo.getPerformance()));
                    }
                    classPeriodSectionVo.setModuleId(0L);
                    classPeriodSectionVo.setChapterId(classPeriodVo.getId());
                    classPeriodSectionVo.setGoodsId(goodsId);
                    classPeriodSectionVo.setGradeId(gradeId);
                    studyList.add(mergeData(i,moduleName,chapterName,sectionName,classPeriodSectionVo,classPeriodVo.getPerformance(),true));
                }
                classPeriodSectionVos.addAll(classPeriodSectionVos1);
                classPeriodVo.setClassPeriodSectionList(classPeriodSectionVos);
            }
            //为节搜索学时记录
            if (classPeriodVo.getType() == 3){
                i++;
                sectionName = classPeriodVo.getTypeName();
                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);
                    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.setPeriodStatusId(userPeriodStatusVo.getId());
                    classPeriodVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                    classPeriodVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                    classPeriodVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                }
                classPeriodVo.setModuleId(0L);
                classPeriodVo.setChapterId(0L);
                classPeriodVo.setGoodsId(bo.getGoodsId());
                classPeriodVo.setGradeId(bo.getGradeId());
                ClassPeriodSectionVo classPeriodSectionVo = BeanUtil.toBean(classPeriodVo, ClassPeriodSectionVo.class);
                studyList.add(mergeData(i,moduleName,chapterName,sectionName,classPeriodSectionVo,(new BigDecimal(0)),false));
            }
        }
        return studyList;
    }

    @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());
                //當前班級學員審核狀態 -1不可审核 0待審  1未通过 2通过
            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(),bo.getGradeId());
                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.setPeriodStatusId(userPeriodStatusVo.getId());
                                    classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                                    classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                                    classPeriodSectionVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                                }
                                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.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                                    classPeriodSectionVo.setPeriodStatusId(userPeriodStatusVo.getId());
                                    classPeriodChapterVo.setPerformance(Convert.toBigDecimal(userPeriodStatusVo.getPerformance()));
                                }
                                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.setPeriodStatusId(userPeriodStatusVo.getId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                        classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                        classPeriodSectionVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                    }
                    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.setPeriodStatusId(userPeriodStatusVo.getId());
                        classPeriodSectionVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                        classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                        classPeriodVo.setPerformance(Convert.toBigDecimal(userPeriodStatusVo.getPerformance()));
                    }
                    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.setPeriodStatusId(userPeriodStatusVo.getId());
                    classPeriodVo.setAuditTime(userPeriodStatusVo.getAuditTime());
                    classPeriodVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
                    classPeriodVo.setDurationTime(userPeriodStatusVo.getDurationTime());
                }
                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(),bo.getGradeId());
        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());
        classPeriodSectionVo.setPeriodStatusId(userPeriodStatusVo.getId());
        classPeriodSectionVo.setAuditUserName(userPeriodStatusVo.getAuditUserName());
        //判断是节还是试卷
        if (bo.getSectionId() != null) {
            classPeriodSectionVo.setId(bo.getSectionId());
            classPeriodSectionVo.setDurationTime(userPeriodStatusVo.getDurationTime());
            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();
    }

    @Override
    public Long selectList(User bo) {

        return baseMapper.selectUserCount(bo);
    }

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

    /**
     * 实体类转化成视图对象
     *
     * @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;
    }
}
