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

import java.math.BigDecimal;
import java.math.RoundingMode;
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.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.exam.BaseExamController;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectRange;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectRangeQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectRangeService;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.utils.ExportExcel;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * 课程分段统计
 *
 * @author xuwenjin
 * @date 2019-04-12 17:05
 */
@Controller
@RequestMapping("/admin/exam/reportSubjectRange")
public class ReportSubjectRangeController extends BaseExamController {

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

    @Autowired
    private ReportSubjectRangeService reportSubjectRangeService;

    @RequestMapping
    public String list(Model model, HttpServletRequest request, ReportSubjectRangeQuery query) {
        WebUser webuser = RequestUtils.getWebUser(request);
        int examId = getSessionExamId(request);
        // reportSubjectRangeService.saveReportRangeSubjectData(1);
        query.setExamId(examId);
        query = reportSubjectRangeService.findByQuery(query);
        if (query.getCurrentCount() > 0) {
            model.addAttribute("list", query.getResult());
        }
        model.addAttribute("subjectList", getExamSubject(examId, webuser));
        model.addAttribute("query", query);
        return "modules/report/reportSubjectRange";
    }

    @RequestMapping("/export")
    public String export(ReportSubjectRangeQuery query, HttpServletRequest request, HttpServletResponse response,
            RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        query.setExamId(examId);
        query.setPageNumber(1);
        query.setPageSize(Integer.MAX_VALUE);
        query = reportSubjectRangeService.findByQuery(query);
        List<ReportSubjectRange> list = query.getResult();
        for (ReportSubjectRange r : list) {
            r.setPercent0_49(new BigDecimal(r.getPercent0_49()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent50_59(new BigDecimal(r.getPercent50_59()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent60_69(new BigDecimal(r.getPercent60_69()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent70_79(new BigDecimal(r.getPercent70_79()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent80_89(new BigDecimal(r.getPercent80_89()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent90_100(new BigDecimal(r.getPercent90_100()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent_lt60(new BigDecimal(r.getPercent_lt60()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPercent_mte60(new BigDecimal(r.getPercent_mte60()*100).setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
        String fileName = "课程分段统计.xlsx";
        try {
            new ExportExcel("课程分段统计", ReportSubjectRange.class).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出成绩失败！" + e.getMessage());
            return "redirect:/admin/exam/reportSubjectRange";
        }
    }

}
