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

import cn.com.qmth.stmms.ms.admin.dto.StudentDTO;
import cn.com.qmth.stmms.ms.admin.service.DataUploadService;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelError;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

//@Api(tags = "学生导入接口controller")
@RestController
@RequestMapping("api/import/students")
public class StudentImporter {

    @Autowired
    private DataUploadService dataUploadService;

    @RequestMapping(method = RequestMethod.POST)
    public List<ExcelError> batchAll(@RequestParam Long workId,
                                     @RequestParam(required = false, defaultValue = "false") boolean isAbsent, @RequestParam MultipartFile file)
            throws Exception {
        return dataUploadService.uploadStudents(workId, isAbsent, file.getInputStream());
    }

    /**
     * 试评考生导入
     *
     * @param workId
     * @param subject
     * @param file
     * @return
     * @throws Exception
     */
//    @ApiOperation(value = "试评学生导入接口")
    @RequestMapping(value = "batchAllForTrial", method = RequestMethod.POST)
    public List<ExcelError> batchAllForTrial(
//            @ApiParam(value = "workId", required = true)
            @RequestParam Long workId,
//            @ApiParam(value = "科目", required = true)
            @RequestParam Subject subject,
//            @ApiParam(value = "excel文件", required = true)
            @RequestParam MultipartFile file)
            throws Exception {
        return dataUploadService.uploadStudentsForTrial(workId, subject, file.getInputStream());
    }

    @RequestMapping(method = RequestMethod.GET)
    public void download(HttpServletResponse response) {
        List<StudentDTO> studentDTOs = new ArrayList<StudentDTO>();
        StudentDTO dto = new StudentDTO();

        dto.setExamNumber("5360001");
        dto.setName("张三");
        dto.setAreaName("四川音乐学院");
        dto.setAreaCode("1");
        dto.setExamRoom("第1考场");
        dto.setSourceName("四川");

        studentDTOs.add(dto);
        ExportUtils.exportEXCEL("考生导入模板", StudentDTO.class, studentDTOs, response);
    }

    /**
     * 导入考生关联信息
     * @param workId
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/relateStudent", method = RequestMethod.POST)
    public List<ExcelError> batchAll(@RequestParam Long workId,
                                     @RequestParam MultipartFile file)
            throws Exception {
        return dataUploadService.uploadStudentRelate(workId, file.getInputStream());
    }
}
