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

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.admin.exam.parameter.BaseParameterController;
import cn.com.qmth.stmms.admin.vo.ScanInfoVO;
import cn.com.qmth.stmms.biz.exam.model.ExamPackage;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.query.ExamPackageSearchQuery;
import cn.com.qmth.stmms.biz.exam.query.ExamStudentSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.ExamPackageService;
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.utils.PictureUrlBuilder;

@Controller("scanInfoController")
@RequestMapping("/admin/exam/scan")
public class ScanController extends BaseParameterController {

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

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private ExamSubjectService subjectService;

    @Autowired
    private ExamPackageService packageService;

    @Value("${package.image.server}")
    private String packageServer;

    /**
     * 按科目统计扫描进度
     * 
     * @param request
     * @param model
     * @param query
     * @return
     */
    @RequestMapping(value = { "", "/subject" })
    public ModelAndView progress(HttpServletRequest request, Model model, ExamSubjectSearchQuery query) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
            List<ScanInfoVO> list = new LinkedList<ScanInfoVO>();
            query.setExamId(examId);
            query = subjectService.findByQuery(query);
            for (ExamSubject subject : query.getResult()) {
                ScanInfoVO vo = new ScanInfoVO();
                vo.setName(subject.getCode() + "-" + subject.getName() + (StringUtils.isNotBlank(subject.getRemark())
                        ? ("-" + StringUtils.trimToEmpty(subject.getRemark())) : ""));
                vo.setTotalCount(studentService.countByExamIdAndSubjectCode(examId, subject.getCode()));
                vo.setScanCount(studentService.countByExamIdAndSubjectCode(examId, subject.getCode(), true));
                int percent = vo.getTotalCount() > 0 ? (int) (vo.getScanCount() * 100.00 / vo.getTotalCount()) : 0;
                vo.setPercent(percent);
                list.add(vo);
            }

            ModelAndView view = new ModelAndView("modules/exam/scanInfo");
            view.addObject("subjectList", getExamSubject(examId));
            view.addObject("levelList", subjectService.listLevel(examId));
            view.addObject("categoryList", subjectService.listCategory(examId));
            view.addObject("infoList", list);
            view.addObject("query", query);
            view.addObject("type", "subject");
            return view;
        } else {
            return new ModelAndView("redirect:/admin/exam/list");
        }
    }

    /**
     * 按学习中心统计扫描进度
     * 
     * @param request
     * @param subjectCode
     * @return
     */
    @RequestMapping("/campus")
    public ModelAndView campus(HttpServletRequest request, ExamStudentSearchQuery query) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
            List<ScanInfoVO> list = new LinkedList<ScanInfoVO>();
            if (StringUtils.isBlank(query.getCampusName())) {
                query.setExamId(examId);
                query = studentService.findDistinctCampusName(query);
                for (ExamStudent s : query.getResult()) {
                    ScanInfoVO vo = new ScanInfoVO();
                    vo.setName(s.getCampusName());
                    vo.setTotalCount(studentService.countByExamIdAndCampusName(examId, s.getCampusName()));
                    vo.setScanCount(studentService.countByExamIdAndCampusName(examId, s.getCampusName(), true));
                    int percent = vo.getTotalCount() > 0 ? (int) (vo.getScanCount() * 100.00 / vo.getTotalCount()) : 0;
                    vo.setPercent(percent);
                    list.add(vo);
                }
            } else {
                ScanInfoVO vo = new ScanInfoVO();
                vo.setName(query.getCampusName());
                vo.setTotalCount(studentService.countByExamIdAndCampusName(examId, query.getCampusName()));
                vo.setScanCount(studentService.countByExamIdAndCampusName(examId, query.getCampusName(), true));
                int percent = vo.getTotalCount() > 0 ? (int) (vo.getScanCount() * 100.00 / vo.getTotalCount()) : 0;
                vo.setPercent(percent);
                list.add(vo);

                query.setTotalCount(1);
                query.setTotalPage(1);
                query.setCurrentCount(1);
            }

            ModelAndView view = new ModelAndView("modules/exam/scanInfo");
            view.addObject("campusList", studentService.findDistinctCampusName(examId));
            view.addObject("infoList", list);
            view.addObject("query", query);
            view.addObject("type", "campus");
            return view;
        } else {
            return new ModelAndView("redirect:/admin/exam/list");
        }
    }

    /**
     * 统计签到表扫描进度
     * 
     * @param request
     * @return
     */
    @RequestMapping("/package")
    public ModelAndView packageProgress(HttpServletRequest request, ExamPackageSearchQuery query) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
            query.setExamId(examId);
            query = packageService.findByQuery(query);
            for (ExamPackage ep : query.getResult()) {
                ep.setUrls(PictureUrlBuilder.getPackageUrls(examId, ep.getCode(), ep.getPicCount()));
            }
            ModelAndView view = new ModelAndView("modules/exam/scanPackage");
            view.addObject("packageList", query.getResult());
            view.addObject("query", query);
            view.addObject("packageServer", packageServer);
            return view;
        } else {
            return new ModelAndView("redirect:/admin/exam/list");
        }
    }
}
