package jnpf.cultivate.v2.controller.web.exam;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.v2.service.V2CultivateExamStatisticsService;
import jnpf.model.cultivate.v2.exam.req.V2ExamStatisticsForOrgReq;
import jnpf.model.cultivate.v2.exam.req.V2ExamStatisticsForPersonReq;
import jnpf.model.cultivate.v2.exam.vo.V2ExamStatisticsForOrgExcelVo;
import jnpf.model.cultivate.v2.exam.vo.V2ExamStatisticsForOrgVo;
import jnpf.model.cultivate.v2.exam.vo.V2ExamStatisticsForPersonExcelVo;
import jnpf.model.cultivate.v2.exam.vo.V2ExamStatisticsForPersonVo;
import jnpf.util.ConstantUtil;
import jnpf.util.FtbUtil;
import jnpf.util.QuestionAnalysisUtil;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * web数据统计考试关联
 *
 * @author xgl
 */
@RestController
@RequestMapping("/v2/web/exam/statistics")
public class V2CultivateExamStatisticsController {
    @Autowired
    private V2CultivateExamStatisticsService statisticsService;


    /**
     * 考试统计，组织维度
     */
    @GetMapping("/queryExamStatisticsForOrg")
    public ActionResult<PageListVO<V2ExamStatisticsForOrgVo>> queryExamStatisticsForOrg(V2ExamStatisticsForOrgReq dto) {
        return ActionResult.success(statisticsService.queryExamStatisticsForOrg(dto));
    }

    /**
     * 考试统计，组织维度,导出
     */
    @GetMapping("/queryExamStatisticsForOrg/export")
    public void exportOrg(V2ExamStatisticsForOrgReq dto, HttpServletResponse response) throws IOException {
        String fileName = "考试统计_组织维度_" + DateUtil.format(new Date(), "yyyyMMdd");
        dto.setPageSize(ConstantUtil.TEN_MILLION);
        dto.setCurrentPage(1);
        PageListVO<V2ExamStatisticsForOrgVo> pageVo = statisticsService.queryExamStatisticsForOrg(dto);
        List<V2ExamStatisticsForOrgExcelVo> excelList = BeanUtil.copyToList(pageVo.getList(), V2ExamStatisticsForOrgExcelVo.class);
        EasyExcelUtils.exportExcel(response, fileName, excelList, V2ExamStatisticsForOrgExcelVo.class);
    }

    /**
     * 考试统计，个人维度
     */
    @PostMapping("/queryExamStatisticsForPerson")
    public ActionResult<PageListVO<V2ExamStatisticsForPersonVo>> queryExamStatisticsForPerson(@RequestBody V2ExamStatisticsForPersonReq dto) {
        PageInfo<V2ExamStatisticsForPersonVo> pageVo = statisticsService.queryExamStatisticsForPerson(dto);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 考试统计，个人维度,导出
     */
    @PostMapping("/queryExamStatisticsForPerson/export")
    public void exportPerson(@RequestBody V2ExamStatisticsForPersonReq dto, HttpServletResponse response) throws IOException {

        PageInfo<V2ExamStatisticsForPersonVo> pageVo = statisticsService.queryExamStatisticsForPerson(dto);
        List<V2ExamStatisticsForPersonVo> list = pageVo.getList();
        List<V2ExamStatisticsForPersonExcelVo> excelVOList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (V2ExamStatisticsForPersonVo vo : list) {
                excelVOList.add(QuestionAnalysisUtil.convertToExcelPersonvoV2(vo));
            }
        }
        String fileName = "考试统计_个人维度_" + DateUtil.format(new Date(), "yyyyMMdd");
        EasyExcelUtils.exportExcel(response, fileName, excelVOList, V2ExamStatisticsForPersonExcelVo.class);
    }


}
