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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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

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.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.vo.StatisticsVO;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
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.query.ExamSubjectSearchQuery;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.utils.DateUtils;
import cn.com.qmth.stmms.common.utils.ExportExcel;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * 分段统计
 */
@Controller
@RequestMapping("/admin/exam/statistics")
public class StatisticsController extends BaseExamController {

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

    @Autowired
    private ExamSubjectService subjectService;
    @Autowired
    private ExamStudentService examStudentService;

    @RequestMapping
    public String list(Model model, HttpServletRequest request, ExamSubjectSearchQuery query,
    		@RequestParam(required = false) Boolean upload) {
    	WebUser wu = RequestUtils.getWebUser(request);
        int examId = getSessionExamId(request);
        if (examId > 0) {
            query.setExamId(examId);
            query = subjectService.findByQuery(query);
            List<StatisticsVO> statisticsVOs = new ArrayList<StatisticsVO>(); 
            for (ExamSubject subject : query.getResult()) {
            	StatisticsVO vo = new StatisticsVO(subject);
            	vo = getStatisticsInfo(vo,upload);
            	statisticsVOs.add(vo);
			}
            model.addAttribute("statisticsVOs", statisticsVOs);
            model.addAttribute("query", query);
            model.addAttribute("upload", upload);
            model.addAttribute("subjectList", getExamSubject(examId,wu));
            model.addAttribute("levelList", subjectService.listLevel(examId));
            model.addAttribute("categoryList", subjectService.listCategory(examId));
            return "modules/exam/statisticsInfo";
        } else {
            return "redirect:/admin/exam/list";
        }
    }

	private StatisticsVO getStatisticsInfo(StatisticsVO vo,Boolean upload) {
		List<Object[]> list = null;
		if(upload!=null){
			vo.setStudentCount(examStudentService.countByExamIdAndSubjectCode(vo.getExamId(), vo.getCode(), upload,false));
			list = examStudentService.statisticsByExamIdAndSubjectCode(vo.getExamId(), vo.getCode(),upload,false);
		}else{
			vo.setStudentCount(examStudentService.countByExamIdAndSubjectCode(vo.getExamId(), vo.getCode()));
			list = examStudentService.statisticsByExamIdAndSubjectCode(vo.getExamId(), vo.getCode());
		}
		vo.setCount0_49((long) (list.get(0)[0]==null?0L:list.get(0)[0]));
		vo.setCount50_59((long) (list.get(0)[1]==null?0L:list.get(0)[1]));
		vo.setCount60_69((long) (list.get(0)[2]==null?0L:list.get(0)[2]));
		vo.setCount70_79((long) (list.get(0)[3]==null?0L:list.get(0)[3]));
		vo.setCount80_89((long) (list.get(0)[4]==null?0L:list.get(0)[4]));
		vo.setCount90_100((long) (list.get(0)[5]==null?0L:list.get(0)[5]));
		if(vo.getStudentCount()!=0){
			vo.setPercent0_49(new BigDecimal(vo.getCount0_49()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
			vo.setPercent50_59(new BigDecimal(vo.getCount50_59()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
			vo.setPercent60_69(new BigDecimal(vo.getCount60_69()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
			vo.setPercent70_79(new BigDecimal(vo.getCount70_79()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
			vo.setPercent80_89(new BigDecimal(vo.getCount80_89()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
			vo.setPercent90_100(new BigDecimal(vo.getCount90_100()*100.0/vo.getStudentCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
		}
		return vo;
	}

	@RequestMapping(value = "/export", method = RequestMethod.POST)
	public String exportFile(ExamSubjectSearchQuery query,
			@RequestParam(required = false) Boolean upload,
			HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		int examId = getSessionExamId(request);
		try {
			query.setPageNumber(1);
			query.setPageSize(Integer.MAX_VALUE);
			query.setExamId(examId);
			String fileName = "分段统计" + DateUtils.getDate("yyyyMMdd") + ".xlsx";
			query = subjectService.findByQuery(query);
			List<StatisticsVO> statisticsVOs = new ArrayList<StatisticsVO>();
			for (ExamSubject subject : query.getResult()) {
				StatisticsVO vo = new StatisticsVO(subject);
				vo = getStatisticsInfo(vo, upload);
				statisticsVOs.add(vo);
			}
			new ExportExcel("分段统计", StatisticsVO.class)
					.setDataList(statisticsVOs).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！" + e.getMessage());
		}
		return "redirect:/admin/exam/statistics";
	}
}
