package com.xhkjedu.smarking.service.exam;

import com.xhkjedu.smarking.mapper.exam.SubjectScoreMapper;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.vo.exam.MissExamVo;
import com.xhkjedu.smarking.vo.exam.SCStudentVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @Description：成绩管理
 * @Author: WN
 * @Date: 2024/11/11 15:57:31
 **/
@Service
public class SubjectScoreService {
    @Resource
    private SubjectScoreMapper subjectScoreMapper;


    /*
     * @Description 成绩管理-科目成绩列表
     * @Date 2024/11/11 15:24:34
     * @Author WN
     * @Param [examid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listExamSubjectScore(Integer examid){
        //成绩管理-平均分计算：0分参与，缺考不参与
        //科目下成绩信息
        List<Map<String, Object>> list = subjectScoreMapper.listExamSubjectScore(examid);
        //获取不缺考学生的所有成绩情况，计算客观题平均分、主观题平均分
        List<Map<String,Object>> quelist = subjectScoreMapper.listSubjectQuestionAvgScore(examid);
        for(Map<String,Object> map : list){
            Object msid = map.get("msid");
            Map<String,Object> queMap = quelist.stream().filter(q -> q.get("msid").equals(msid)).findFirst().orElse(null);
            double avgobj = 0.0;
            double avgsub = 0.0;
            if(queMap!= null){
                if(queMap.get("avgobj")!= null){
                    avgobj = (double) queMap.get("avgobj");
                }
                if(queMap.get("avgsub") != null){
                    avgsub = (double) queMap.get("avgsub");
                }
            }
            map.put("avgobj", avgobj);
            map.put("avgsub", avgsub);
        }

        return list;
    }

    /*
     * @Description 成绩管理-缺考学生
     * @Date 2024/11/11 15:54:50
     * @Author WN
     * @Param [cvo]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listExamSubjectScoreByClass(MsClassStudent cvo){
        return subjectScoreMapper.listSubjectMissStudents(cvo);
    }

    /*
     * @Description 成绩管理-缺考学生-导出
     * @Date 2024/11/11 16:54:12
     * @Author WN
     * @Param [cvo]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listSubjectMissStudentExport(SCStudentVo cvo){
        List<MissExamVo> missList = subjectScoreMapper.listSubjectMissStudentExport(cvo);
        //获取所有的科目信息
        List<String> sublist = missList.stream().map(MissExamVo::getSubjectname).distinct().collect(Collectors.toList());
        Map<String,List<MissExamVo>> stuMap = missList.stream().collect(Collectors.groupingBy(MissExamVo::getSubjectid));
        List<Map<String, Object>> rtnMap = new ArrayList<>();

        for(Map.Entry<String,List<MissExamVo>> entry : stuMap.entrySet()){
            Map<String, Object> map = new LinkedHashMap<>();
            List<MissExamVo> stuList = entry.getValue();
            MissExamVo mevo = stuList.get(0);
            map.put("studentid",mevo.getStudentid());
            map.put("studentname",mevo.getStudentname());
            map.put("classname",mevo.getClassname());
            map.put("usersex",mevo.getUsersex());
            map.put("examno",mevo.getExamno());

            for(String subjectname : sublist){
                MissExamVo subVo = stuList.stream().filter(vo -> vo.getSubjectname().equals(subjectname)).findFirst().orElse(null);
                if(subVo == null){
                    map.put(subjectname,"-");
                }else{
                    String str = "未上传";
                    if(N_Utils.isTrueInteger(subVo.getBatchid())){
                        str = "已上传";
                    }

                    map.put(subjectname,str);
                }
            }
            rtnMap.add(map);
        }

        return rtnMap;
    }

}
