package cn.com.qmth.stmms.student;

import javax.servlet.http.HttpServletRequest;

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

import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.query.ExamStudentSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.school.model.School;
import cn.com.qmth.stmms.biz.school.service.SchoolService;

@Controller("studentScoreController")
@RequestMapping("/student")
public class StudentScoreController {

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

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private ExamService examService;

    @RequestMapping(value = "/{schoolId}", method = RequestMethod.GET)
    public String login(Model model, HttpServletRequest request, @PathVariable Integer schoolId) {
        School school = schoolService.findById(schoolId);
        if (school == null) {
            model.addAttribute("message", "学校不存在");
            return "modules/student/login";
        }

        model.addAttribute("school", school);
        return "modules/student/login";
    }

    @RequestMapping(value = "/{schoolId}", method = RequestMethod.POST)
    public String getSubject(Model model, HttpServletRequest request, @PathVariable Integer schoolId,
            ExamStudentSearchQuery query) {
        School school = schoolService.findById(schoolId);
        if (school == null) {
            model.addAttribute("message", "学校为空");
            return "modules/student/login";
        }
        query.setSchoolId(schoolId);
        query = studentService.findByQuery(query);
        for (ExamStudent student : query.getResult()) {
            student.setExamName(examService.findById(student.getExamId()).getName());
        }
        model.addAttribute("school", schoolService.findById(schoolId));
        model.addAttribute("query", query);
        return "modules/student/list";
    }

}
