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.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.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.exam.BaseExamController;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectTeacherClass;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectTeacherClassService;
import cn.com.qmth.stmms.common.annotation.Logging;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.LogType;
import cn.com.qmth.stmms.common.utils.ExportExcel;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * 课程总评机组 成绩分析
 *
 * @author xuwenjin
 * @date 2019-04-17
 */
@Controller
@RequestMapping("/admin/exam/reportSubjectTeacherClass")
public class ReportSubjectTeacherClassController extends BaseExamController {

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

    @Autowired
    private ReportSubjectTeacherClassService reportSubjectTeacherClassService;

    @Autowired
    private ExamSubjectService subjectService;

    @Logging(menu = "课程教师机组分析", type = LogType.QUERY)
    @RequestMapping
    public String list(Model model, HttpServletRequest request, ReportSubjectQuery query) {
        WebUser webuser = RequestUtils.getWebUser(request);
        int examId = getSessionExamId(request);
        query.setExamId(examId);
        query.setPageNumber(1);
        query.setPageSize(Integer.MAX_VALUE);
        if (StringUtils.isNotBlank(query.getSubjectCode()) && StringUtils.isNotBlank(query.getTeacherName())) {
            List<ReportSubjectTeacherClass> reportSubjectTeacherClasses = reportSubjectTeacherClassService
                    .findByQuery(query);
            model.addAttribute("list", reportSubjectTeacherClasses);
            ExamSubject subject = subjectService.find(examId, query.getSubjectCode());
            model.addAttribute("subjectName", subject.getCode() + "-" + subject.getName());
        }

        model.addAttribute("subjectList", getExamSubject(examId, webuser));
        model.addAttribute("query", query);
        return "modules/report/reportSubjectTeacherClass";
    }

    @Logging(menu = "课程教师机组分析导出", type = LogType.EXPORT)
    @RequestMapping("/export")
    public String export(ReportSubjectQuery query, HttpServletRequest request, HttpServletResponse response,
            RedirectAttributes redirectAttributes) {
        int examId = getSessionExamId(request);
        query.setExamId(examId);
        query.setPageNumber(1);
        query.setPageSize(Integer.MAX_VALUE);
        List<ReportSubjectTeacherClass> list = reportSubjectTeacherClassService.findByQuery(query);
        for (ReportSubjectTeacherClass r : list) {
            r.setAvgScore(new BigDecimal(r.getAvgScore()).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setExcellentRate(new BigDecimal(r.getExcellentRate()).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setPassRate(new BigDecimal(r.getPassRate()).setScale(2, RoundingMode.HALF_UP).doubleValue());
            r.setRelativeAvgScore(new BigDecimal(r.getRelativeAvgScore()).setScale(2, RoundingMode.HALF_UP)
                    .doubleValue());
        }
        String fileName = "课程总评机组成绩分析.xlsx";
        try {
            new ExportExcel("课程总评机组成绩分析", ReportSubjectTeacherClass.class).setDataList(list).write(response, fileName)
                    .dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出成绩失败！" + e.getMessage());
            return "redirect:/admin/exam/reportSubjectTeacherClass";
        }
    }
}
