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

import cn.com.qmth.stmms.ms.admin.dto.*;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.domain.*;
import cn.com.qmth.stmms.ms.core.repository.ChangeLevelRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkLogRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkSubjectRepo;
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by yuanpan on 2017/12/23.
 */
@RestController
@RequestMapping("api/export/paper")
public class PaperExporter {

    @Autowired
    private PaperRepo paperRepo;

    @Autowired
    MarkLogRepo markLogRepo;

    @Autowired
    private MarkSubjectRepo markSubjectRepo;

    @Autowired
    private ChangeLevelRepo changeLevelRepo;

    @Value("${sys.config.imageDir}")
    private String imagesFolder;

    @GetMapping
    public void export(@RequestParam Long workId, HttpServletResponse response) {

        List<PaperExpDTO> expPapers = new ArrayList<>();
        List<Paper> allPapers = paperRepo.findByWorkId(workId);
        for (Paper paper : allPapers) {
            String filePath = imagesFolder + "/" + paper.getWorkId() + "/" + paper.getSubject().toString() + "/" + paper.getAreaCode() + "/" + paper.getExamNumber() + ".jpg";

            File file = new File(filePath);
            if (!file.exists()) {

                expPapers.add(new PaperExpDTO(paper.getStudentName(), paper.getExamNumber(), paper.getSubject().getName()));
            }

        }

        ExportUtils.exportEXCEL("错误试卷", PaperExpDTO.class, expPapers.stream().sorted((p1, p2) -> p1.getExamNumber().compareTo(p2.getExamNumber())).collect(Collectors.toList()), response);
    }

    /**
     * 导出标准卷信息
     *
     * @param workId
     * @param subject
     * @param response
     */
    @GetMapping("{workId}/{subject}/sample")
    public void exportSample(@PathVariable Long workId, @PathVariable Subject subject, HttpServletResponse response) {
        MarkSubject markSubject = markSubjectRepo.findOne(workId + "-" + subject.name());
        //科目名称
        String subjectName = Objects.isNull(markSubject) ? subject.name() : markSubject.getName();
        List<SampleExpDTO> sampleExpDTOS = new ArrayList<>();
        List<Paper> papers = paperRepo.findByWorkIdAndSubjectAndIsSampleTrue(workId, subject);
        for (Paper paper : papers) {
            SampleExpDTO sampleExpDTO = new SampleExpDTO();
            sampleExpDTO.setSubject(subjectName);
            sampleExpDTO.setExamNumber(paper.getExamNumber());
            sampleExpDTO.setLevel(paper.getLevel());
            sampleExpDTOS.add(sampleExpDTO);
        }
        ExportUtils.exportEXCEL("标准卷信息", SampleExpDTO.class, sampleExpDTOS, response);
    }

    /**
     * 一键定档导出
     *
     * @param workId
     * @param subject
     * @param response
     */
    @GetMapping("{workId}/{subject}/oneClick")
    public void exportOneClick(@PathVariable Long workId, @PathVariable Subject subject, HttpServletResponse response) {
        MarkSubject markSubject = markSubjectRepo.findOne(workId + "-" + subject.name());
        //科目名称
        String subjectName = Objects.isNull(markSubject) ? subject.name() : markSubject.getName();
        List<OneClickExpDTO> oneClickExpDTOS = new ArrayList<>();
        List<Paper> papers = paperRepo.findByWorkIdAndSubjectAndIsOneClickTrue(workId, subject);
        if (papers != null && papers.size() > 0) {
            List<Long> paperIds = papers.stream().map(Paper::getId).collect(Collectors.toList());
            List<MarkLog> marklogs = markLogRepo.findByWorkIdAndSubjectAndOperTypeAndPaperIdIn(workId, subject.name(), MarkLogOperType.ONE_CLICK_LEVEl.getId(), paperIds);
            for (int i = 0; i < marklogs.size(); i++) {
                MarkLog markLog = marklogs.get(i);
                OneClickExpDTO oneClickExpDTO = new OneClickExpDTO();
                oneClickExpDTO.setSubject(subjectName);
                oneClickExpDTO.setSeqNo(i + 1);
                oneClickExpDTO.setExamNumber(markLog.getExamNumber());
                oneClickExpDTO.setOriginLevel(markLog.getOperDataBefore() == null ? "" : markLog.getOperDataBefore());
                oneClickExpDTO.setLevel(markLog.getOperDataAfter());
                oneClickExpDTOS.add(oneClickExpDTO);
            }
            ExportUtils.exportEXCEL("专家复评导出", OneClickExpDTO.class, oneClickExpDTOS, response);
        }
    }

    /**
     * 改档信息导出
     *
     * @param workId
     * @param subject
     * @param response
     */
    @GetMapping("{workId}/{subject}/changeLevel")
    public void exportChangeLevel(@PathVariable Long workId, @PathVariable Subject subject, HttpServletResponse response) {
        MarkSubject markSubject = markSubjectRepo.findOne(workId + "-" + subject.name());
        //科目名称
        String subjectName = Objects.isNull(markSubject) ? subject.name() : markSubject.getName();
        List<ChangeLevelExpDTO> changeLevelExpDTOS = new ArrayList<>();
        List<ChangeLevel> changeLevels = changeLevelRepo.findByWorkIdAndSubjectAndAuditStatus(workId, subject.name());
        for (int i = 0; i < changeLevels.size(); i++) {
            ChangeLevel changeLevel = changeLevels.get(i);
            ChangeLevelExpDTO changeLevelExpDTO = new ChangeLevelExpDTO();
            changeLevelExpDTO.setSubject(subjectName);
            changeLevelExpDTO.setSeqNo(i + 1);
            Paper paper = paperRepo.findOne(changeLevel.getPaperId());
            changeLevelExpDTO.setExamNumber(paper.getExamNumber());
            changeLevelExpDTO.setOriginLevel(changeLevel.getOriginalLevel());
            changeLevelExpDTO.setLevel(changeLevel.getSuggestLevel());
            changeLevelExpDTOS.add(changeLevelExpDTO);
        }
        ExportUtils.exportEXCEL("改档信息导出", ChangeLevelExpDTO.class, changeLevelExpDTOS, response);
    }
}
