package cn.com.qmth.stmms.admin.exam;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.vo.ExamStudentVO;
import cn.com.qmth.stmms.admin.vo.UploadStudentVO;
import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.model.ExamPackage;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.model.MarkGroup;
import cn.com.qmth.stmms.biz.exam.query.ExamStudentSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.ExamPackageService;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.MarkGroupService;
import cn.com.qmth.stmms.biz.file.service.FileService;
import cn.com.qmth.stmms.biz.mark.service.MarkService;
import cn.com.qmth.stmms.common.annotation.Logging;
import cn.com.qmth.stmms.common.annotation.RoleRequire;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.LogType;
import cn.com.qmth.stmms.common.enums.MarkStatus;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.enums.SubjectiveStatus;
import cn.com.qmth.stmms.common.utils.ExportExcel;
import cn.com.qmth.stmms.common.utils.ImportExcel;
import cn.com.qmth.stmms.common.utils.RequestUtils;

import com.google.common.collect.Lists;

@Controller("examStudentController")
@RequestMapping("/admin/exam/student")
public class StudentController extends BaseExamController {

    protected static Logger log = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private ExamSubjectService subjectService;

    @Autowired
    private ExamService examService;

    @Autowired
    private MarkService markService;

    @Autowired
    private ExamPackageService packageService;

    @Autowired
    private FileService fileService;

    @Autowired
    private MarkGroupService groupService;

    private static final String NULL_PAPER_TYPE_PLACEHOLDER = "#";

    private static final String PAPER_TYPES_REGEX = "[a-zA-Z#]";

    @Logging(menu = "查询考生", type = LogType.QUERY)
    @RequestMapping
    public String list(Model model, HttpServletRequest request, ExamStudentSearchQuery query) {
        WebUser wu = RequestUtils.getWebUser(request);
        int examId = getSessionExamId(request);
        query.setExamId(examId);
        if (query.getBatchCode() != null) {
            query.setBatchCode(StringUtils.upperCase(query.getBatchCode()));
        }
        query.addSort("id", Direction.ASC);
        query = studentService.findByQuery(query);
        if (query.getCurrentCount() > 0) {
            for (ExamStudent student : query.getResult()) {
                student.setSubject(subjectService.find(student.getExamId(), student.getSubjectCode()));
                buildSheetUrl(student);
                buildPackageUrl(student);
            }
            model.addAttribute("studentList", query.getResult());
        }
        model.addAttribute("query", query);
        model.addAttribute("subjectList", getExamSubject(examId, wu));
        model.addAttribute("levelList", subjectService.listLevel(examId));
        model.addAttribute("categoryList", subjectService.listCategory(examId));
        model.addAttribute("fileServer", fileService.getFileServer());
        Exam exam = examService.findById(examId);
        model.addAttribute("examType", exam.getType());
        return "modules/exam/studentList";
    }

    @RequestMapping(value = "/add")
    @RoleRequire(Role.SCHOOL_ADMIN)
    public String form(HttpServletRequest request, ExamStudent student, Model model) {
        Exam exam = examService.findById(getSessionExamId(request));
        model.addAttribute("student", student);
        model.addAttribute("subjectList", getExamSubject(exam.getId(), RequestUtils.getWebUser(request)));
        return "modules/exam/studentForm";
    }

    @RequestMapping(value = "/update")
    @RoleRequire(Role.SCHOOL_ADMIN)
    public String update(HttpServletRequest request, Integer id, Model model) {
        Exam exam = examService.findById(getSessionExamId(request));
        ExamStudent student = studentService.findById(id);
        model.addAttribute("student", student);
        model.addAttribute("subjectList", getExamSubject(exam.getId(), RequestUtils.getWebUser(request)));
        return "modules/exam/studentForm";
    }

    @Logging(menu = "新增（修改）考生", type = LogType.UPDATE)
    @RequestMapping(value = "/save")
    @RoleRequire(Role.SCHOOL_ADMIN)
    public String save(ExamStudent student, Model model, RedirectAttributes redirectAttributes,
            HttpServletRequest request, HttpServletResponse response) {
        int examId = getSessionExamId(request);
        Exam exam = examService.findById(examId);
        ExamSubject subject = subjectService.find(examId, student.getSubjectCode());
        if (subject == null) {
            addMessage(redirectAttributes, "科目不存在");
        } else {
            if (student.getId() != null) {
                ExamStudent previous = studentService.findById(student.getId());
                if (previous != null) {
                    // String previousSubjectCode = previous.getSubjectCode();
                    student.setExamId(examId);
                    ExamStudent old = checkExamNumber(student, new HashMap<String, ExamStudent>(),
                            new HashMap<String, ExamStudent>());
                    if (old != null && !old.getExamNumber().equals(previous.getExamNumber())) {
                        addMessage(redirectAttributes, "准考证号" + student.getExamNumber() + "已经存在");
                        return "redirect:/admin/exam/student";
                    }
                    if (!previous.isUpload()) {
                        // 还未扫描上传的考生，才能修改准考证号、学习中心、科目
                        previous.setCampusName(student.getCampusName());
                        previous.setSubjectCode(subject.getCode());
                        previous.setSubjectName(subject.getName());
                        previous.setExamNumber(student.getExamNumber());
                    }
                    previous.setStudentCode(student.getStudentCode());
                    previous.setName(student.getName());
                    previous.setPackageCode(student.getPackageCode());
                    previous.setCollege(student.getCollege());
                    previous.setClassName(student.getClassName());
                    previous.setTeacher(student.getTeacher());
                    previous = studentService.save(previous);
                    // if
                    // (!previousSubjectCode.equals(previous.getSubjectCode()))
                    // {
                    // // 更新科目上传考生数量
                    // subjectService.updateUploadCount(examId,
                    // previousSubjectCode);
                    // subjectService.updateUploadCount(examId,
                    // previous.getSubjectCode());
                    // // 删除已生成的评卷任务
                    // libraryService.deleteByStudent(previous.getId());
                    // // 更新科目大题任务数量
                    // groupService.updateLibraryCount(examId,
                    // previousSubjectCode);
                    // // 重置科目所有大题构造评卷任务时间
                    // groupService.resetBuildTime(examId,
                    // previous.getSubjectCode());
                    // }
                    // addMessage(redirectAttributes, "考生" + previous.getName()
                    // + "保存成功");
                } else {
                    addMessage(redirectAttributes, "考生不存在");
                }
            } else {
                student.setExamId(examId);
                student.setSchoolId(exam.getSchoolId());
                student.setSubjectName(subject.getName());
                student.setAbsent(false);
                student.setUpload(false);
                student.setException(false);
                student.setSliceCount(0);
                student.setSheetCount(0);
                student.setObjectiveScore(0d);
                student.setSubjectiveScore(0d);
                student.setSubjectiveStatus(SubjectiveStatus.UNMARK);
                student.setPaperType(NULL_PAPER_TYPE_PLACEHOLDER);
                student.setInspectCount(0);
                ExamStudent old = checkExamNumber(student, new HashMap<String, ExamStudent>(),
                        new HashMap<String, ExamStudent>());
                if (old != null) {
                    addMessage(redirectAttributes, "准考证号" + student.getExamNumber() + "已经存在");
                    return "redirect:/admin/exam/student";
                } else {
                    studentService.save(student);
                    examService.updateExamUpdateTime(examId);
                }
                addMessage(redirectAttributes, "考生" + student.getName() + "保存成功");
            }
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "删除考生", type = LogType.DELETE)
    @RequestMapping(value = "/delete")
    @RoleRequire(Role.SCHOOL_ADMIN)
    public String delete(Integer id, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        ExamStudent student = studentService.findById(id);
        if (student != null) {
            studentService.deleteById(id);
            markService.deleteByStudent(student);
            subjectService.updateUploadCount(
                    student.getExamId(),
                    student.getSubjectCode(),
                    (int) studentService.countUploadedByExamIdAndSubjectCode(student.getExamId(),
                            student.getSubjectCode()));
            addMessage(redirectAttributes, "删除考生成功");
            RequestUtils.setLog(request, "删除成功，id:" + id);
        } else {
            addMessage(redirectAttributes, "找不到对应的考生");
        }
        return "redirect:/admin/exam/student";
    }

    @RequestMapping(value = "/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "考生数据导入模板.xlsx";
            List<ExamStudent> list = Lists.newArrayList();
            list.add(new ExamStudent());
            new ExportExcel("考生数据", ExamStudent.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "导入考生", type = LogType.IMPORT_FILE)
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @RoleRequire(Role.SCHOOL_ADMIN)
    public String importFile(HttpServletRequest request, MultipartFile file, RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        Exam exam = examService.findById(examId);
        try {
            int successNum = 0;
            int updateNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<ExamStudent> list = ei.getDataList(ExamStudent.class);
            List<ExamStudent> saveList = new LinkedList<ExamStudent>();
            List<ExamStudent> updateList = new LinkedList<ExamStudent>();
            Map<String, ExamStudent> saveMap = new HashMap<String, ExamStudent>();
            Map<String, ExamStudent> current = null;
            // 当数据库中已存在考生小于一定数量，则一次性取出所有考生，用于校验准考证号是否重复
            if (studentService.countByExamId(examId) < 10000) {
                current = new HashMap<String, ExamStudent>();
                List<ExamStudent> list2 = studentService.findByExamId(examId);
                for (ExamStudent s : list2) {
                    current.put(s.getExamNumber(), s);
                }
            }

            for (ExamStudent student : list) {
                if (StringUtils.isBlank(student.getExamNumber()) || StringUtils.isBlank(student.getName())
                        || StringUtils.isBlank(student.getSubjectCode())
                        || StringUtils.isBlank(student.getSubjectName()) || StringUtils.isBlank(student.getCollege())
                        || StringUtils.isBlank(student.getClassName()) || StringUtils.isBlank(student.getTeacher())
                        || StringUtils.isBlank(student.getStudentCode())) {
                    continue;
                }

                student.setExamId(examId);
                student.setSchoolId(exam.getSchoolId());
                student.setAbsent(false);
                student.setUpload(false);
                student.setException(false);
                student.setSliceCount(0);
                student.setSheetCount(0);
                student.setObjectiveScore(0d);
                student.setSubjectiveScore(0d);
                student.setSubjectiveStatus(SubjectiveStatus.UNMARK);
                student.setPaperType(NULL_PAPER_TYPE_PLACEHOLDER);
                student.setInspectCount(0);
                ExamStudent previous = checkExamNumber(student, current, saveMap);
                if (previous != null && previous.getId() != null) {
                    student = updatePrevious(student, previous);
                    updateList.add(student);
                } else {
                    saveList.add(student);
                }
                saveMap.put(student.getExamNumber(), student);
            }

            successNum = studentService.batchSave(saveList);
            updateNum = studentService.batchUpdate(updateList);
            examService.updateExamUpdateTime(examId);
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条用户");
            }
            String message = "已成功导入 " + (successNum + updateNum) + " 条用户";
            addMessage(redirectAttributes, message + failureMsg);
            RequestUtils.setLog(request, message);
        } catch (Exception e) {
            log.error("Batch import student error!", e);
            addMessage(redirectAttributes, "导入考生失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "导出考生", type = LogType.EXPORT)
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public String exportFile(ExamStudentSearchQuery query, HttpServletRequest request, HttpServletResponse response,
            RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        try {
            query.setPageNumber(1);
            query.setPageSize(Integer.MAX_VALUE);
            query.setExamId(examId);
            query.addSort("id", Direction.ASC);
            query = studentService.findByQuery(query);
            for (ExamStudent student : query.getResult()) {
                ExamSubject subject = subjectService.find(student.getExamId(), student.getSubjectCode());
                student.setSubjectName(subject != null ? subject.getName() : "");
                student.setSubjectLevel(subject != null ? subject.getLevel() : "");
                student.setSubjectCategory(subject != null ? subject.getCategory() : "");
                student.setSubjectRemark(subject != null ? subject.getRemark() : "");
            }
            String fileName = "考生数据.xlsx";
            new ExportExcel("考生数据", ExamStudent.class).setDataList(query.getResult()).write(response, fileName)
                    .dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出考生数据失败！" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @RequestMapping("/sheetTag")
    @ResponseBody
    public Object getSheetTag(HttpServletRequest request, @RequestParam Integer id) {
        ExamStudent student = studentService.findById(id);
        if (student != null) {
            return studentService.buildSheetTags(student, false, false);
        } else {
            return new HashMap<>();
        }
    }

    @RequestMapping(value = "/absentTemplate")
    public String absentTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "缺考考生导入模板.xlsx";
            List<ExamStudentVO> list = Lists.newArrayList();
            list.add(new ExamStudentVO());
            new ExportExcel("缺考考生", ExamStudentVO.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "导入缺考名单", type = LogType.IMPORT_FILE)
    @RequestMapping(value = "/absentImport", method = RequestMethod.POST)
    public String absentImportFile(HttpServletRequest request, MultipartFile file, RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<ExamStudentVO> list = ei.getDataList(ExamStudentVO.class);
            studentService.clearManualAbsent(examId);
            for (ExamStudentVO studentVO : list) {
                if (StringUtils.isBlank(studentVO.getExamNumber())) {
                    continue;
                }
                ExamStudent student = studentService.findByExamIdAndExamNumber(examId, studentVO.getExamNumber());
                if (student != null) {
                    studentService.updateManualAbsent(examId, student.getExamNumber(), true);
                    successNum++;
                } else {
                    failureMsg.append("<br/>准考证号 " + studentVO.getExamNumber() + " 不存在; ");
                    failureNum++;
                }

            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条用户");
            }
            String message = "已成功导入 " + successNum + " 条用户";
            addMessage(redirectAttributes, message + failureMsg);
            RequestUtils.setLog(request, message);
        } catch (Exception e) {
            log.error("Batch import absentStudent error!", e);
            addMessage(redirectAttributes, "导入缺考考生失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @RequestMapping(value = "/breachTemplate")
    public String breachTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "违纪考生导入模板.xlsx";
            List<ExamStudentVO> list = Lists.newArrayList();
            list.add(new ExamStudentVO());
            new ExportExcel("违纪考生", ExamStudentVO.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "导入违纪名单", type = LogType.IMPORT_FILE)
    @RequestMapping(value = "/breachImport", method = RequestMethod.POST)
    public String breachImportFile(HttpServletRequest request, MultipartFile file, RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        // Exam exam = examService.findById(examId);
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<ExamStudentVO> list = ei.getDataList(ExamStudentVO.class);
            for (ExamStudentVO studentVO : list) {
                if (StringUtils.isBlank(studentVO.getExamNumber())) {
                    continue;
                }
                ExamStudent student = studentService.findByExamIdAndExamNumber(examId, studentVO.getExamNumber());
                if (student != null) {
                    studentService.updateBreach(student.getId(), true);
                    successNum++;
                } else {
                    failureMsg.append("<br/>准考证号 " + studentVO.getExamNumber() + " 不存在; ");
                    failureNum++;
                }

            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条用户");
            }
            String message = "已成功导入 " + successNum + " 条用户";
            addMessage(redirectAttributes, message + failureMsg);
            RequestUtils.setLog(request, message);
        } catch (Exception e) {
            log.error("Batch import BreachStudent error!", e);
            addMessage(redirectAttributes, "导入违纪考生失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @Logging(menu = "违纪重置", type = LogType.UPDATE)
    @RequestMapping(value = "/updateBreach", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateBreach(HttpServletRequest request, Integer id) {
        JSONObject result = new JSONObject();
        ExamStudent student = studentService.findById(id);
        if (student != null && studentService.updateBreach(id, false)) {
            List<MarkGroup> groupList = groupService.findByExamAndSubjectAndStatus(student.getExamId(),
                    student.getSubjectCode(), MarkStatus.FINISH);
            for (MarkGroup markGroup : groupList) {
                groupService.updateStatus(student.getExamId(), student.getSubjectCode(), markGroup.getNumber(),
                        MarkStatus.FORMAL, MarkStatus.FINISH);
            }
            result.accumulate("message", "重置成功！");
            result.accumulate("success", true);
        } else {
            result.accumulate("message", "将违纪考生信息重置为正常时出错!");
            result.accumulate("success", false);
        }
        return result;
    }

    @RequestMapping(value = "/uploadTemplate")
    public String uploadTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "多媒体考生上传导入模板.xlsx";
            List<UploadStudentVO> list = Lists.newArrayList();
            list.add(new UploadStudentVO());
            new ExportExcel("多媒体考生上传", UploadStudentVO.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    @RequestMapping(value = "/uploadImport", method = RequestMethod.POST)
    public String uploadImportFile(HttpServletRequest request, MultipartFile file, RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        // Exam exam = examService.findById(examId);
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<UploadStudentVO> list = ei.getDataList(UploadStudentVO.class);
            for (UploadStudentVO studentVO : list) {
                if (StringUtils.isBlank(studentVO.getExamNumber())) {
                    continue;
                }
                ExamStudent student = studentService.findByExamIdAndExamNumber(examId, studentVO.getExamNumber());
                if (student != null) {
                    student.setUpload(true);
                    student.setAbsent(false);
                    student.setAnswers(null);
                    student.setBatchCode(null);
                    student.setSliceCount(0);
                    student.setSheetCount(0);
                    student.setInspectCount(0);
                    if (student.getPaperType() != null && student.getPaperType().matches(PAPER_TYPES_REGEX)) {
                        student.setPaperType(StringUtils.trimToNull(studentVO.getPaperType()));
                    } else {
                        student.setPaperType(NULL_PAPER_TYPE_PLACEHOLDER);
                    }
                    // 同步更新评卷任务
                    if (saveUploadStudent(student)) {
                        successNum++;
                    }
                } else {
                    failureMsg.append("<br/>准考证号 " + studentVO.getExamNumber() + " 不存在; ");
                    failureNum++;
                }

            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条用户");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条用户" + failureMsg);
        } catch (Exception e) {
            log.error("Batch import BreachStudent error!", e);
            addMessage(redirectAttributes, "导入上传考生失败！失败信息：" + e.getMessage());
        }
        return "redirect:/admin/exam/student";
    }

    private ExamStudent checkExamNumber(ExamStudent student, Map<String, ExamStudent> current,
            Map<String, ExamStudent> saveMap) {
        ExamStudent previous = saveMap.get(student.getExamNumber());

        if (previous != null) {
            return previous;
        }
        if (current != null && !current.isEmpty()) {
            previous = current.get(student.getExamNumber());
        } else {
            previous = studentService.findByExamIdAndExamNumber(student.getExamId(), student.getExamNumber());
        }
        if (previous != null) {
            return previous;
        }
        return null;
    }

    private ExamStudent updatePrevious(ExamStudent student, ExamStudent previous) {
        previous.setCollege(student.getCollege());
        previous.setClassName(student.getClassName());
        previous.setTeacher(student.getTeacher());
        previous.setExamRoom(student.getExamRoom());
        previous.setExamSite(student.getExamSite());
        previous.setRemark(student.getRemark());
        previous.setSubjectLevel(student.getSubjectLevel());
        previous.setSubjectCategory(student.getSubjectCategory());
        previous.setSubjectRemark(student.getSubjectRemark());
        return previous;
    }

    private void buildSheetUrl(ExamStudent student) {
        student.setSheetUrls(fileService.getSheetUris(student.getExamId(), student.getExamNumber(), 1,
                student.getSheetCount()));
    }

    private void buildPackageUrl(ExamStudent student) {
        if (StringUtils.isNotBlank(student.getPackageCode())) {
            ExamPackage ep = packageService.find(student.getExamId(), student.getPackageCode());
            if (ep != null && ep.getPicCount() > 0) {
                student.setPackageUrls(fileService.getPackageUris(ep.getExamId(), ep.getCode(), 1, ep.getPicCount()));
            }
        }
    }
}
