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

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.qmth.stmms.biz.campus.model.Campus;
import cn.com.qmth.stmms.biz.campus.service.CampusService;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.exam.service.TagService;
import cn.com.qmth.stmms.biz.mark.model.MarkLibrary;
import cn.com.qmth.stmms.biz.mark.query.MarkLibrarySearchQuery;
import cn.com.qmth.stmms.biz.mark.service.MarkLibraryService;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.utils.DateUtils;
import cn.com.qmth.stmms.common.utils.PictureUrlBuilder;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller("tagController")
@RequestMapping("/admin/exam/tag")
public class TagController extends BaseExamController {

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

    @Autowired
    private CampusService campusService;

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private MarkLibraryService libraryService;

    @Autowired
    private TagService tagService;

    @Value("${sheet.image.server}")
    private String imageServer;

    /**
     * 标记试卷处理
     * 
     * @param request
     * @param model
     * @param query
     * @return
     */
    @RequestMapping
    public String list(HttpServletRequest request, Model model, MarkLibrarySearchQuery query) {
        WebUser wu = RequestUtils.getWebUser(request);
        int examId = getSessionExamId(request);
        query.setExamId(examId);
        query.setTagNotNull(true);
        query.orderByExamNumber();
        if (query.getTagId() == 0) {
            query.setTagId(1);
        }
        subjectFilter(query, wu);
        query = libraryService.findByQuery(query);

        List<ExamStudent> list = new LinkedList<ExamStudent>();
        for (MarkLibrary library : query.getResult()) {
            ExamStudent student = studentService.findById(library.getStudentId());
            student.setSheetUrls(PictureUrlBuilder.getSheetUrls(examId, library.getCampusId(), student.getSubjectCode(),
                    student.getExamNumber(), student.getSheetCount()));
            student.setMarkTime(DateUtils.formatDateTime(library.getMarkerTime()));
            student.setLibraryId(library.getId());
            list.add(student);
        }
        model.addAttribute("resultList", list);
        model.addAttribute("query", query);
        model.addAttribute("subjectList", getTagSubject(examId, wu));
        model.addAttribute("campusList", getTagCampus(examId));
        model.addAttribute("tagList", tagService.findAll());
        model.addAttribute("imageServer", imageServer);
        return "modules/exam/tagInfo";
    }

    private List<Campus> getTagCampus(int examId) {
        List<Campus> list = new LinkedList<Campus>();
        List<Integer> ids = libraryService.findTagCampusId(examId);
        for (Integer id : ids) {
            Campus campus = campusService.findById(id);
            if (campus != null) {
                list.add(campus);
            }
        }
        return list;
    }

}
