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

import java.util.ArrayList;
import java.util.List;

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

import org.apache.commons.lang.StringUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.dto.ScoreRateDTO;
import cn.com.qmth.stmms.admin.exam.parameter.BaseParameterController;
import cn.com.qmth.stmms.biz.exam.model.ScoreRate;
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.ScoreRateService;
import cn.com.qmth.stmms.biz.exam.service.query.ScoreRateSearchQuery;
import cn.com.qmth.stmms.common.utils.DateUtils;
import cn.com.qmth.stmms.common.utils.ExportExcel;
/**
 * 得分率 
 */
@Controller
@RequestMapping("/admin/exam/rate")
public class ScoreRateController extends BaseParameterController {

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

    @Autowired
    private ScoreRateService scoreRateService;
    @Autowired
    private ExamStudentService studentService;
    @Autowired
    private ExamSubjectService subjectService;

    @RequestMapping
    public String list(Model model, HttpServletRequest request, ScoreRateSearchQuery query) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
        	if (StringUtils.isNotEmpty(query.getCampusName())) {
	            query.setExamId(examId);
	            query.setObjective(false);
	            query = scoreRateService.findByQuery(query);
	            for (ScoreRate s : query.getResult()) {
	                s.setSubject((subjectService.find(s.getExamId(), s.getSubjectCode())));
	            }
        	}
            model.addAttribute("query", query);
            model.addAttribute("campusList", studentService.findDistinctCampusName(examId));
            model.addAttribute("subjectList", getExamSubject(examId));
            return "modules/exam/scoreRateInfo";
        } else {
            return "redirect:/admin/exam/list";
        }
    }
    
	@RequestMapping(value = "/export", method = RequestMethod.POST)
	public String exportFile(ScoreRateSearchQuery query,
			HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		int examId = getSessionExamId(request);
		try {
        	if (StringUtils.isNotEmpty(query.getCampusName())) {
	            query.setExamId(examId);
	            query.setObjective(false);
	            query.setPageNumber(1);
	            query.setPageSize(Integer.MAX_VALUE);
	            String fileName = "得分率" + DateUtils.getDate("yyyyMMdd") + ".xlsx";
	            query = scoreRateService.findByQuery(query);
	            List<ScoreRateDTO> list = new ArrayList<>();
	            for (ScoreRate s : query.getResult()) {
	            	list.add(new ScoreRateDTO(s, subjectService.find(s.getExamId(), s.getSubjectCode())));
	            }
	            new ExportExcel("得分率", ScoreRateDTO.class)
	            .setDataList(list).write(response, fileName)
	            .dispose();
	            return null;
        	}else{
        		addMessage(redirectAttributes, "请选择学习中心，导出数据失败！");
        	}
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！" + e.getMessage());
		}
		return "redirect:/admin/exam/rate";
	}

}
