package com.qmth.distributed.print.api.mark;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.mark.bean.archivescore.ArchiveStudentQuery;
import com.qmth.teachcloud.mark.bean.document.ArchivePaperQuery;
import com.qmth.teachcloud.mark.bean.document.MarkDocumantQuery;
import com.qmth.teachcloud.mark.entity.MarkDocument;
import com.qmth.teachcloud.mark.entity.MarkPaper;
import com.qmth.teachcloud.mark.enums.DocumentType;
import com.qmth.teachcloud.mark.service.MarkDocumentService;
import com.qmth.teachcloud.mark.service.MarkPaperService;
import com.qmth.teachcloud.mark.service.MarkStudentService;
import com.qmth.teachcloud.mark.service.ScanPackageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 归档管理 前端控制器
 * </p>
 */
@Api(tags = "归档管理")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_MARK + "/archive/document")
public class MarkDocumentController {

    @Autowired
    private MarkPaperService markPaperService;
    @Resource
    private MarkDocumentService markDocumentService;
    @Resource
    private MarkStudentService markStudentService;
    @Resource
    private ScanPackageService scanPackageService;

    @ApiOperation(value = "文档管理列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@Validated ArchivePaperQuery query) {
        return ResultUtil.ok(markPaperService.documentList(query));
    }

    @ApiOperation(value = "文档详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result detail(@Validated MarkDocumantQuery query) {
        return ResultUtil.ok(markDocumentService.listByExamIdAndPaperNumber(query.getExamId(),query.getPaperNumber()));
    }

    @ApiOperation(value = "文档上传")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.ADD, detail = "文档上传操作，文档ID：{{id}}")
    public Result importFile(@ApiParam(value = "文档ID", required = true) Long id,
                           @ApiParam(value = "文件md5", required = true) String md5,
                           @ApiParam(value = "文件", required = true) @RequestParam MultipartFile file) {
        return ResultUtil.ok(markDocumentService.importById(id, file, md5));
    }

    @ApiOperation(value = "文档新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.ADD, detail = "文档新建操作，考试ID：{{examId}}、试卷编号：{{paperNumber}}、文档名称：{{documentName}}")
    public Result save(@ApiParam(value = "考试ID", required = true) Long examId,
                       @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                       @ApiParam(value = "文档名称", required = true) String documentName,
                       @ApiParam(value = "文件md5", required = true) String md5,
                       @ApiParam(value = "文件", required = true) @RequestParam MultipartFile file) {
        return ResultUtil.ok(markDocumentService.save(examId, paperNumber,documentName, file,md5), null);
    }

    @ApiOperation(value = "文档删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE, detail = "文档删除操作，文档ID：{{id}}")
    public Result delete(@ApiParam(value = "文档ID", required = true) Long id) {
        markDocumentService.deleteById(id);
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "文档下载")
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.EXPORT, detail = "文档下载操作，文档ID：{{id}}")
    public void download(@ApiParam(value = "文档ID", required = true) Long id, HttpServletResponse response) {
        MarkDocument document = markDocumentService.getById(id);
        if(document==null){
            throw ExceptionResultEnum.ERROR.exception("文档不存在");
        }
        if(document.getType().equals(DocumentType.SCORE)){
            ArchiveStudentQuery query = new ArchiveStudentQuery();
            query.setExamId(document.getExamId());
            query.setPaperNumber(document.getPaperNumber());
            markStudentService.scoreExport(query, response);
        }
        MarkPaper markPaper = markPaperService.getByExamIdAndPaperNumber(document.getExamId(),document.getPaperNumber());
        if(markPaper==null){
            throw ExceptionResultEnum.ERROR.exception("试卷不存在");
        }
        if(document.getType().equals(DocumentType.SHEET)){
            markStudentService.sheetExport(document.getExamId(),markPaper.getCoursePaperId(),response);
        }
        if(document.getType().equals(DocumentType.TRACK)){
            markStudentService.trackExport(document.getExamId(),markPaper.getCoursePaperId(),response);
        }
        if(document.getType().equals(DocumentType.SIGN)){
            scanPackageService.export(markPaper.getExamId(),markPaper.getCoursePaperId(),response);
        }
    }
}
