package cn.com.qmth.stmms.ms.admin.exporter;

import cn.com.qmth.stmms.ms.admin.dto.MarkUserDTO;
import cn.com.qmth.stmms.ms.admin.dto.PaperCounter;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.domain.Work;
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengmin on 2017/1/10.
 */
@RestController
@RequestMapping("api/export/works")
public class WorkExporter {

    @Autowired
    private PaperRepo paperRepo;

    @RequestMapping(value = "{work}/papercount",method = RequestMethod.GET)
    public void countGropuByQuestion(@PathVariable Work work, HttpServletResponse response){
        if(work != null) {
            List<PaperCounter> paperCounters = new ArrayList<>();
            List<Object[]> result = paperRepo.countGroupByQuestion(work.getId());
            result.forEach(i -> {
                PaperCounter paperCounter = new PaperCounter();
                paperCounter.setAreaName(String.valueOf(i[1]));
                paperCounter.setAreaCode(String.valueOf(i[0]));
                paperCounter.setQuestionName(String.valueOf(i[2]));
                paperCounter.setPaperCount(Long.parseLong(String.valueOf(i[3])));
                paperCounters.add(paperCounter);
            });
            String fileName = "各省各科试卷数量";
            ExportUtils.exportEXCEL(fileName,PaperCounter.class,paperCounters,response);
        }
    }
}
