package com.xhkjedu.sstudy.service.paperstudent;

import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentMapper;
import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentQuestionMapper;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion;
import com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo;
import com.xhkjedu.sstudy.vo.paperstudent.StudentVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname PaperStudentQuestionService
 * @description 
 * @date 2020/6/10 15:21
 **/
@Service
public class PaperStudentQuestionService {
    @Resource
    private PaperStudentQuestionMapper paperStudentQuestionMapper;
    @Resource
    PaperStudentMapper paperStudentMapper;

    //添加
   /* public Integer batchSave(Integer psid ) {
        List<TPaperStudentQuestion> list = listPaperQuestion(psid);
        return paperStudentQuestionMapper.batchSave(list);
    }*/

    //删除
    public Integer deleteById(Integer id) {
        return paperStudentQuestionMapper.deleteByPrimaryKey(id);
    }

    //修改
    public Integer update(TPaperStudentQuestion model) {
        return paperStudentQuestionMapper.updateByPrimaryKeySelective(model);
    }

    //获取详情
    public TPaperStudentQuestion findById(Integer id) {
        return paperStudentQuestionMapper.selectByPrimaryKey(id);
    }

    //获取列表
    public List <TPaperStudentQuestion> findAll() {
        return paperStudentQuestionMapper.selectAll();
    }


    /**
     * 获取学生待批改试题列表
     * @Param [psid]
     * @Author ywx
     * @Date 2020/6/26 13:21
     * @return java.util.List<com.xhkjedu.vo.paperstudent.PaperStudentQuestionVo>
     **/
    public List<PaperStudentQuestionVo> listCorrectQuestion(Integer psid) {
        //2024-09-19产品南总确认获取试题时不获取未作答自动批改的试题
        return paperStudentQuestionMapper.listCorrectQuestion(psid);
    }

    /**
     * 作业关联试题列表
     * @Param [psid]
     * @Author ywx
     * @Date 2020/6/11 8:40
     * @return java.util.List<com.xhkjedu.model.paperstudent.TPaperStudentQuestion>
     **/
    public List<TPaperStudentQuestion> listPaperQuestion(Integer psid) {
        return paperStudentQuestionMapper.listPaperQuestion(psid);
    }

    /**
     * 获取学生未提交试题集合
     * @Param [psid]
     * @Author ywx
     * @Date 2020/6/11 10:24
     * @return java.util.List<com.xhkjedu.model.paperstudent.TPaperStudentQuestion>
     **/
    public List<TPaperStudentQuestion> listPSQByNotDo(Integer psid) {
        return paperStudentQuestionMapper.listPSQByNotDo(psid);
    }

    /**
     * 自动批改学生未做试题
     * @Param [psid]
     * @Author ywx
     * @Date 2020/6/11 10:27
     * @return java.lang.Integer
     **/
    public Integer updatePSQByNotDo(Integer psid) {
        Integer timestamp = N_Utils.getSecondTimestamp();
        return paperStudentQuestionMapper.updatePSQByNotDo(psid,timestamp);
    }

    /**
     * 获取该作业该学生未批改作业数量
     * @Param [psid]
     * @Author ywx
     * @Date 2020/6/11 10:31
     * @return java.lang.Integer
     **/
    public Integer getNotCorrect(Integer psid) {
        return paperStudentQuestionMapper.getNotCorrect(psid);
    }

    /**
     *功能描述  计算作业中所有试题耗费时间
     * @author WN
     * @date 2020/6/12
     * @param psid
     * @return java.lang.Integer
     */
    public Integer getSumQuesitonsCosttime(Integer psid){
        return paperStudentQuestionMapper.getSumQuesitonsCosttime(psid);
    }

    /**
     *功能描述 修改学生答案（教师添加批注）
     * @author WN
     * @date 2020/8/24
     * @param  * @param useranswer
     * @param psqid
     * @return void
     */
    public void updateUseranswer(String useranswer,Integer psqid){
        paperStudentQuestionMapper.updateUseranswer(useranswer,psqid);
    }

    //题库作业--按题批阅列表
    public List<Map> qcorrectList(Integer paperid, Integer classid) {
        List<StudentVo> stus = paperStudentMapper.listStu(paperid, classid);
        int num = stus.size();
        List<Integer> psids = new ArrayList<>();
        Map<Integer, String> stuMap = new HashMap<>();
        for (StudentVo vo : stus) {
            if (N_Utils.isNotEmpty(vo.getStudentname())) {
                psids.add(vo.getPsid());
                stuMap.put(vo.getPsid(), vo.getStudentname());
            }
        }
        if(N_Utils.isListEmpty(psids)) return new ArrayList<>();
        List<Map> list = paperStudentQuestionMapper.qcorrectList(psids);
        for (Map map : list) {
            List<Map> students = (List<Map>) map.get("students");
            map.put("num", num);//总人数
            students = students.stream().filter(m -> m.get("answered").equals(1))
                    .collect(Collectors.toList());
            int ytjnum = students.size();
            map.put("wtjnum", num - ytjnum);//未提交人数
            map.put("ytjnum", ytjnum);//已提交人数
            int ypynum = students.stream().filter(m -> m.get("checked").equals(1)).collect(Collectors.toList()).size();
            map.put("ypynum", ypynum);//已批阅人数
            map.put("pyrate", N_Utils.getIntegerDivideAndMulitiply(ypynum, ytjnum));
            students.forEach(s -> {
                s.put("studentname", stuMap.get((Integer) s.get("psid")));
            });
            map.put("students", students);
        }
        return list.stream().filter(m -> !m.get("num").equals(m.get("wtjnum"))).collect(Collectors.toList());
    }

    //获取学生作业未批改试题数量
    public Integer getNoCheckNumByPsId(Integer psid) {
        return paperStudentQuestionMapper.getNoCheckNumByPsId(psid);
    }

    /**
     * 获取未合并的学生答案
     *
     * @param [psid]
     * @return java.util.List<com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion>
     * @author ywx
     * @date 2022/5/9 15:07
     */
    public List<TPaperStudentQuestion> listNoMergeUserAnswer(Integer psid) {
        return paperStudentQuestionMapper.listNoMergeUserAnswer(psid);
    }
}
