package cn.com.qmth.stmms.subjectheader.progress;

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

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.biz.distributpaper.service.ProgressService;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveBlock;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.SubjectiveBlockService;
import cn.com.qmth.stmms.biz.subjectheader.model.SubjectHeader;
import cn.com.qmth.stmms.biz.subjectheader.service.SubjectHeaderService;
import cn.com.qmth.stmms.common.enums.LibraryStatus;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/subjectheader/progress")
public class ProgressController {

    @Autowired
    private SubjectHeaderService subjectHeaderService;

    @Autowired
    private ProgressService progressService;

    @Autowired
    private SubjectiveBlockService subjectiveBlockService;

    @Autowired
    private ExamSubjectService examSubjectService;

    @RequestMapping(value = "/progress-view", method = RequestMethod.GET)
    public ModelAndView progressView() {
        ModelAndView view = new ModelAndView("subjectheader/progress/progress");
        return view;
    }

    @RequestMapping(value = "/progress-list", method = RequestMethod.POST)
    @ResponseBody
    public JSONArray progressDisplay(HttpServletRequest request) {
        StmmsSession session = RequestUtils.getSession(request);
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();
        int examId = Integer.parseInt(session.getParameter("examId"));
        String subjectCode = session.getParameter("subjectCode");

        List<SubjectiveBlock> sbList = null;
        sbList = subjectiveBlockService.findBlockByExamIdAndSubjectCode(examId, subjectCode);
        JSONArray list = new JSONArray();
        long marked = 0;
        long all = 0;
        for (SubjectiveBlock subjectiveBlock : sbList) {
            JSONObject object = new JSONObject();
            long markedCount = progressService.formallyLibraryMarkedCount(subjectHeader.getExamId(),
                    subjectHeader.getSubjectCode(), subjectiveBlock.getId(), LibraryStatus.findByValue(3));
            long allCount = progressService.formallyLibraryCount(subjectHeader.getExamId(),
                    subjectHeader.getSubjectCode(), subjectiveBlock.getId());
            marked += markedCount;
            all += allCount;
            float ratio = 0;
            if (allCount != 0) {
                ratio = (float) markedCount / (float) allCount;
                BigDecimal b = new BigDecimal(ratio * 100);
                ratio = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            }
            object.accumulate("name", subjectiveBlock.getTitle());
            object.accumulate("ratio", new Float(ratio).toString());
            list.add(object);
        }
        float ratio = 0;
        if (all != 0) {
            ratio = (float) marked / (float) all;
            BigDecimal b = new BigDecimal(ratio * 100);
            ratio = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        }
        JSONObject object = new JSONObject();
        object.accumulate("name", "总进度");
        object.accumulate("ratio", new Float(ratio).toString());
        list.add(object);
        return list;
    }
}
