package com.xhkjedu.smarking.service.exam;

import com.xhkjedu.exception.ServiceException;
import com.xhkjedu.smarking.mapper.exam.MsExamMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.mapper.papercheck.MsPaperCheckMapper;
import com.xhkjedu.smarking.mapper.system.UserMapper;
import com.xhkjedu.smarking.model.exam.MsSubject;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description 阅卷考试科目表 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
public class MsSubjectService {
    @Resource
    private MsSubjectMapper msSubjectMapper;
    @Resource
    private MsPaperCheckMapper msPaperCheckMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private MsExamMapper msExamMapper;


    /**
     * @Description 考试进度总览
     * @Date 2024/10/17 14:50
     * @Author YWX
     * @Param [subject]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map<String, Object>> examOverview(MsSubject subject) {
        Integer examid = subject.getExamid();
        //获取所有考试科目信息
        List<Map<String, Object>> rtnList = msSubjectMapper.examOverview(examid);
        //获取阅卷模式1线上阅卷2先阅后扫3在线考试
        String exammode = rtnList.get(0).get("exammode").toString();
        List<Map<String, Object>> numlist;
        if("3".equals(exammode)){
            numlist = msSubjectMapper.examOverviewProblemForOnline(examid);
        }else{
            numlist = msSubjectMapper.examOverviewProblemForOnline(examid);
        }
        Map<String,Map<String,Object>> numMap = numlist.stream().collect(HashMap::new, (m, n) -> m.put(n.get("msid").toString(), n), HashMap::putAll);
        for (Map<String, Object> map : rtnList) {
            String msid = map.get("msid").toString();
            if(N_Utils.isEmpty(map.get("scannum"))){
                map.put("scannum",0);
            }
            Map<String,Object> numMapValue = numMap.get(msid);
            map.put("tjnum",numMapValue.get("tjnum"));
            map.put("pynum",numMapValue.get("pynum"));
            map.put("arbitratenum",numMapValue.get("arbitratenum"));
            map.put("problemnum",numMapValue.get("problemnum"));
        }
        return rtnList;
    }

    public void updateById(MsSubject msSubject) {
        msSubject.setHandletime(N_Utils.getSecondTimestamp());
        msSubjectMapper.updateByPrimaryKeySelective(msSubject);
    }

    /**
     * @Description 设置考试时间
     * @Date 2024/10/17 16:02
     * @Author YWX
     * @Param [exam]
     * @Return void
     **/
    public void setExamDate(List<MsSubject> subjects, Integer handleid) {
        int timestamp = N_Utils.getSecondTimestamp();
        msSubjectMapper.setExamDate(subjects, handleid, timestamp);
    }

    /*
     * @Description 获取考下是学科组长权限科目
     * @Date 2024/10/18 15:10:06
     * @Author WN
     * @Param [examid, leaderid]
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> getExamAndSubjectByLeaderid(Integer examid, Integer leaderid) {
        return msSubjectMapper.getExamLeadSubjects(examid, leaderid);
    }

    /**
     * @Description 修改阅卷状态
     * @Date 2024/10/25 9:53
     * @Author YWX
     * @Param [msid, handleid, checkstate]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void updateCheckState(Integer msid, Integer handleid, int checkstate) {
        MsSubject subject = msSubjectMapper.selectByPrimaryKey(msid);
        if (subject == null) {
            throw new ServiceException("考试科目不存在");
        }

        MsSubject msSubject = new MsSubject();
        msSubject.setMsid(msid);
        msSubject.setHandleid(handleid);
        msSubject.setCheckstate(checkstate);
        msSubject.setHandletime(N_Utils.getSecondTimestamp());
        msSubject.setCheckset(2);
        msSubjectMapper.updateByPrimaryKeySelective(msSubject);

        msPaperCheckMapper.updateCheckStateByMsId(msid, checkstate);//更新题块阅卷状态
    }

    /**
     * @Description 考试科目选择列表
     * @Date 2024/12/3 11:26
     * @Author YWX
     * @Param [examid, handleid]
     * @Return java.lang.Object
     **/
    public Object listForSel(Integer examid, Integer handleid) {
        Integer usertype = userMapper.getUsertypeByUserid(handleid);
        if (usertype.equals(0)) {
            return msSubjectMapper.listSubjectNameByExamId(examid);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * @Description 单科设置信息
     * @Date 2024/12/3 17:56
     * @Author YWX
     * @Param [examid, subjectid]
     * @Return java.util.Map
     **/
    public Map getSet(Integer examid, String subjectid) {
        Map map = msExamMapper.findById(examid);
        Integer exammode = N_Utils.obj2Int(map.get("exammode"));
        Integer monitored = N_Utils.obj2Int(map.get("monitored"));
        Map set = msSubjectMapper.getSet(examid, subjectid, exammode, monitored);
        set.put("exammode", exammode);
        set.put("monitored", monitored);
        set.put("examstate", map.get("examstate"));
        return set;
    }
}
