package com.qmth.jkserver.controller.system;

import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.core.kw.IKwTempDataGenerator;
import com.qmth.jkserver.core.kw.KwTempDataGeneratorFactory;
import com.qmth.jkserver.core.upload.FileUploader;
import com.qmth.jkserver.enums.ExamType;
import com.qmth.jkserver.model.Attachment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 页面考务文件上传接口
 * 供前台页面新增考试任务的时候调用
 *
 * @author tianjun
 */

@Controller
@RequestMapping("/system/kw/file/upload")
public class KwFileUploadController {

    private static final Logger LOGGER = LoggerFactory.getLogger(KwFileUploadController.class);

    // 上传考务考生文件接口 uploadType such as:kc,student,paper,studentAdditional,roomMonitor
    @RequestMapping("/{examCode}/{examType}/{uploadType}")
    @ResponseBody
    public Object uploadStudent(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable(value = "examCode", required = true) String examCode,
                                @PathVariable(value = "examType", required = true) String examType,
                                @PathVariable(value = "uploadType", required = true) String uploadType)
            throws Exception {
        FileUploader fileUploader = new FileUploader(request, examCode);
        List<Attachment> attachments = fileUploader.execute();
        if (attachments.size() > 0) {
            if (!uploadType.equals("sceneScore")) {
                //生成临时数据，为数据校验做准备
                IKwTempDataGenerator kwTempDataGenerator = KwTempDataGeneratorFactory.getInstance(uploadType);
                kwTempDataGenerator.execute(examCode, ExamType.valueOf(examType), attachments.get(0).getId());
            }
            return attachments.get(0);
        }
        throw new JkServerException("文件上传失败,考试识别码:" + examCode + ",上传类型:" + uploadType);
    }
}
