package com.xhkjedu.sstudy.service.paperstudent;

import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentMapper;
import com.xhkjedu.sstudy.mapper.paperstudent.PaperStudentScantronMapper;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentScantron;
import com.xhkjedu.sstudy.vo.paperstudent.PaperStudentScantronVo;
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;

/**
 * @ClassName PaperStudentScantronService
 * Description 答题卡--学生
 * Author WN
 * Date 2020/6/12 15:04
 **/
@Service
public class PaperStudentScantronService {

    @Resource
    private PaperStudentScantronMapper paperStudentScantronMapper;
    @Resource
    private PaperStudentMapper paperStudentMapper;

    /**
     *功能描述 获取学生作业试题详情
     * @author WN
     * @date 2020/6/12
     * @param  psid
     * @return java.util.List<com.xhkjedu.vo.paperstudent.PaperStudentScantronVo>
     */
    public List<PaperStudentScantronVo> listPaperStudentScantrons(Integer psid) {
        return paperStudentScantronMapper.listPaperStudentScantrons(psid);
    }

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

    /**
     *功能描述 批量保存学生试题信息
     * @author WN
     * @date 2020/6/12
     * @param  * @param psid
     * @return void
     */
    public void saveBatchStudentScantron(Integer psid){
        List<TPaperStudentScantron> lst = paperStudentScantronMapper.listPaperScantrons(psid);
        paperStudentScantronMapper.saveBatchStudentScantron(lst);
    }

    /**
     *功能描述  根据主键获取对象
     * @author WN
     * @date 2020/6/12
     * @param  * @param pssid
     * @return com.xhkjedu.model.paperstudent.TPaperStudentScantron
     */
    public TPaperStudentScantron getObjById(Integer pssid){
        return paperStudentScantronMapper.selectByPrimaryKey(pssid);
    }

    /**
     *功能描述 修改对象
     * @author WN
     * @date 2020/6/12
     * @param  * @param pss
     * @return java.lang.Integer
     */
    public Integer updateStudentScantron(TPaperStudentScantron pss){
        return paperStudentScantronMapper.updateByPrimaryKeySelective(pss);
    }

    /**
     *功能描述  自动批改学生未做作业
     * @author WN
     * @date 2020/6/12
     * @param psid
     * @return java.lang.Integer
     */
    public Integer updateScantronPSQByNotDo(Integer psid){
        Integer timestamp = N_Utils.getSecondTimestamp();
        return paperStudentScantronMapper.updateScantronPSQByNotDo(psid,timestamp);
    }

    /**
     *功能描述 学生作业未批改试题数量
     * @author WN
     * @date 2020/6/12
     * @param psid
     * @return java.lang.Integer
     */
    public Integer getScantronNotCorrect(Integer psid){
        return paperStudentScantronMapper.getScantronNotCorrect(psid);
    }

    /**
     *功能描述 批改学生作业（）
     * @author WN
     * @date 2020/6/12
     * @param  * @param list
     * @return void
     */
    public void updateScantronQuestionBatch(List<TPaperStudentScantron> list){
        paperStudentScantronMapper.updateScantronQuestionBatch(list);
    }

    /**
     *功能描述 计算所有试题耗时
     * @author WN
     * @date 2020/6/12
     * @param psid
     * @return java.lang.Integer
     */
    public Integer getSumScantronsCosttime(Integer psid){
        return paperStudentScantronMapper.getSumScantronsCosttime(psid);
    }

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


    public void batchUpdateQuestion(List<TPaperStudentScantron> list) {
        paperStudentScantronMapper.batchUpdateQuestion(list);
    }

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

    public List<Map> sqcorrectList(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 = paperStudentScantronMapper.sqcorrectList(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 paperStudentScantronMapper.getNoCheckNumByPsId(psid);
    }

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