package com.xhkjedu.smarking.service.exam;

import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectBadstudentMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectInvigilateMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.mapper.system.SchoolMapper;
import com.xhkjedu.smarking.mapper.system.UserMapper;
import com.xhkjedu.smarking.model.exam.MsSubjectBadstudent;
import com.xhkjedu.smarking.model.exam.MsSubjectInvigilate;
import com.xhkjedu.smarking.utils.MarkingUtil;
import com.xhkjedu.smarking.vo.system.SchoolCloudVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 阅卷科目违规学生表 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
public class MsSubjectBadstudentService {
    @Resource
    private MsSubjectBadstudentMapper msSubjectBadstudentMapper;
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private MsSubjectMapper msSubjectMapper;
    @Resource
    private MsClassMapper msClassMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private MsSubjectInvigilateMapper msSubjectInvigilateMapper;

    /*
     * @Description 学生-保存违规记录
     * @Date 2024/11/13 17:12:47
     * @Author WN
     * @Param [msSubjectBadstudent]
     * @Return void
     **/
    public void add(MsSubjectBadstudent msSubjectBadstudent){
        //获取学校对应区域码，
        SchoolCloudVo schoolCloudVo = schoolMapper.getSchoolCloudcode(msSubjectBadstudent.getMpid());
        String saveFloder = schoolCloudVo.getCloudcode() + "/" + schoolCloudVo.getSchoolid() + "/marking/invigilate";

        String picpath = MarkingUtil.uploadBase64Img(msSubjectBadstudent.getPicpath(), 1, saveFloder);
        if(N_Utils.isNotEmpty(picpath)){
            msSubjectBadstudent.setPicpath(picpath);
        }

        msSubjectBadstudent.setCreatetime(N_Utils.getSecondTimestamp());
        msSubjectBadstudentMapper.insert(msSubjectBadstudent);
    }

    /*
     * @Description 实时监控-考试信息、监控班级
     * @Date 2024/11/13 17:42:36
     * @Author WN
     * @Param [msi]
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> listJkSchoolClass(MsSubjectInvigilate msi) {
        Integer msid = msi.getMsid();
        Integer teacherid = msi.getTeacherid();
        Map map = msSubjectMapper.getExamSubjectByMsid(msid);//考试科目基本信息
        String classids = "";
        Integer examid = (Integer) map.get("examid");
        //考试学校班级学生
        List<Map<String,Object>> schoolClasses = msClassMapper.listSchoolClassByExamId((Integer) map.get("schoolid"),examid);

        Integer createid= (Integer) map.get("createid");
        if (!N_Utils.isEmptyInteger(teacherid) && !createid.equals(teacherid)
                && userMapper.getUsertypeByUserid(teacherid) == 1) {
            Map claMap = msSubjectInvigilateMapper.getClassidsForJk(msid, teacherid);//教师监考班级
            if (claMap == null) {
                schoolClasses = new ArrayList<>();
            } else if (claMap.get("majored").equals(0)) {//教师不是主监考
                classids = claMap.get("classids").toString();
                String[] classIds = classids.split(",");

                List<Map<String,Object>> schoolClasses2 = new ArrayList<>();
                for(Map scMap : schoolClasses){
                    List<Map> classes2 = new ArrayList<>();
                    List<Map> classMap = (List<Map>) scMap.get("classes");//获取学校下班级
                    for (String classid : classIds) {
                        for (Map cla : classMap) {
                            if(cla.get("classid").toString().equals(classid)){
                                classes2.add(cla);
                                break;
                            }
                        }
                    }
                    if(N_Utils.isListEmpty(classes2)) continue;
                    scMap.put("classes",classes2);
                    schoolClasses2.add(scMap);
                }

                schoolClasses = schoolClasses2;
            }
        } else {
            List<String> classidList = new ArrayList<>();
            for(Map scMap : schoolClasses){
                List<Map> classMap = (List<Map>) scMap.get("classes");//获取学校下班级
                for(Map cMap : classMap){
                    classidList.add(cMap.get("classid").toString());
                }
            }

            classids = String.join(",",classidList);
        }
        map.put("classids", classids);
        map.put("schools", schoolClasses);
        return map;
    }

    /*
     * @Description 实时监控-班级学生
     * @Date 2024/11/13 17:57:47
     * @Author WN
     * @Param [msi]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listClassStus(MsSubjectInvigilate msi){
        return msSubjectBadstudentMapper.listClassStus(msi);
    }

    /*
     * @Description 实时监控-学生进场、提交情况
     * @Date 2024/11/14 9:35:45
     * @Author WN
     * @Param [msi]
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> getSubjectStuInRate(MsSubjectInvigilate msi){
        return msSubjectBadstudentMapper.getSubjectStuInRate(msi);
    }

    /*
     * @Description 实时监控-学生违规照片
     * @Date 2024/11/14 9:54:43
     * @Author WN
     * @Param [msi]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listSubjectStuBadPics(MsSubjectInvigilate msi){
        return msSubjectBadstudentMapper.listSubjectStuBadPics(msi);
    }
}
