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

import cn.com.qmth.stmms.ms.admin.assembler.MarkUserAssembler;
import cn.com.qmth.stmms.ms.admin.dto.MarkUserDTO;
import cn.com.qmth.stmms.ms.admin.dto.StudentDTO;
import cn.com.qmth.stmms.ms.commons.config.ImageConfig;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.domain.MarkSubject;
import cn.com.qmth.stmms.ms.core.domain.Work;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.repository.MarkSubjectRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import cn.com.qmth.stmms.ms.core.repository.StudentRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * 导出用户信息
 * Created by zhengmin on 2016/11/18.
 */
@RestController
@RequestMapping("api/export/users")
public class UserExporter {

    @Autowired
    private MarkUserRepo markUserRepo;

    @Autowired
    private StudentRepo studentRepo;

    @Autowired
    private MarkUserAssembler markUserAssembler;

    @Resource
    MarkSubjectRepo markSubjectRepo;

    @Resource
    ImageConfig imageConfig;

    @RequestMapping(method = RequestMethod.GET)
    public void export(@RequestParam Long workId, @RequestParam(required = false) Subject subject, HttpServletResponse response) {
        List<MarkUserDTO> markUserDTOs = new ArrayList<>();
        Stream<MarkUser> markUserStream = markUserRepo.findByWorkId(workId).stream();
        if (subject != null) {
            markUserStream = markUserStream.filter(u -> u.getSubject().name() == subject.name());
        }
        markUserStream.forEach(u -> {
            markUserDTOs.add(markUserAssembler.toDTO(u));
        });
        String fileName = "用户列表";
        ExportUtils.exportEXCEL(fileName, MarkUserDTO.class, markUserDTOs, response);
    }

    @RequestMapping(value = "{work}/{subject}/export", method = RequestMethod.GET)
    public void countGropuByQuestion(@PathVariable Work work, @PathVariable Subject subject, HttpServletResponse response) {
        if (work != null) {
            List<StudentDTO> list = new ArrayList<>();
            List<Object[]> result = studentRepo.listMissingBySubject(work.getId(), subject.toString());
            result.forEach(i -> {
                StudentDTO studentDTO = new StudentDTO();
                studentDTO.setExamNumber(String.valueOf(i[0]));
                studentDTO.setName(String.valueOf(i[1]));
                studentDTO.setAreaName(String.valueOf(i[2]));
                studentDTO.setAreaCode(String.valueOf(i[3]));
                studentDTO.setExamRoom(String.valueOf(i[4]));
                studentDTO.setSourceName(String.valueOf(i[5]));
                studentDTO.setSchool(String.valueOf(i[6]));
                list.add(studentDTO);
            });
            String fileName = null;
            if (!imageConfig.isCustomSubject()) {
                fileName = subject.toString() + "缺考名单";
            } else {
                MarkSubject markSubject = markSubjectRepo.findOne(work.getId() + "-" + Subject.CUSTOM.name());
                fileName = markSubject.getName() + "缺考名单";
            }
            ExportUtils.exportEXCEL(fileName, StudentDTO.class, list, response);
        }
    }
}
