package cn.com.qmth.examcloud.core.questions.api;

import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import cn.com.qmth.examcloud.core.questions.base.exception.PaperException;
import cn.com.qmth.examcloud.core.questions.dao.PaperRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
import cn.com.qmth.examcloud.core.questions.service.ClonePaperService;
import cn.com.qmth.examcloud.core.questions.service.ImportPaperService;
import cn.com.qmth.examcloud.core.questions.service.PaperService;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weiwenhai
 * @date 2017年4月14日 下午6:05:37
 * @company QMTH
 * @description 导入试卷控制器
 * @code 090
 */
@RestController
@RequestMapping("${api_cqb}/")
public class ImportPaperController extends ControllerSupport {
    protected static final Logger log = LoggerFactory.getLogger(ImportPaperController.class);
    @Autowired
    private ClonePaperService clonePaperService;
    @Autowired
    ImportPaperService importPaperService;
    @Autowired
    PaperRepo paperRepo;
    @Autowired
    PaperService paperService;

    /**
     * 导入试卷
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入试卷", notes = "导入试卷")
    @PostMapping(value = "/importPaper")
    public ResponseEntity<Object> importPaper(@ModelAttribute Paper paper, @RequestParam("file") CommonsMultipartFile file) {
        User user = getAccessUser();
        log.info("导入开始");
        File tempFile = null;
        try {
            tempFile = importPaperService.getUploadFile(file);
            Paper newPaper = importPaperService.importPaper(paper, user, tempFile);
            return new ResponseEntity<Object>(newPaper, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导入异常:" + e.getMessage());
            throw new StatusException("Q-090072", e.getMessage());
        } finally {
            log.info("导入结束");
        }
    }

    /**
     * 新增试卷
     *
     * @param courseNo
     * @param paperName
     * @return
     */
    @ApiOperation(value = "保存导入类型空白试卷", notes = "保存导入类型空白试卷")
    @PostMapping(value = "/importPaper/saveBlankPaper/{courseNo}/{paperName}")
    public ResponseEntity<Object> saveBlankPaper(@PathVariable String courseNo, @PathVariable String paperName) {
        User user = getAccessUser();
        Map<String, Object> returnMap = importPaperService.saveBlankPaper(courseNo, paperName, user);
        if (returnMap.get("msg").equals("success")) {
            return new ResponseEntity<Object>(returnMap, HttpStatus.OK);
        } else {
            return new ResponseEntity<Object>(returnMap, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 克隆试卷
     *
     * @return
     */
    @ApiOperation(value = "克隆试卷", notes = "克隆试卷")
    @PostMapping(value = "/clonePaper/{paperId}/{paperName}/{courseNo}")
    public ResponseEntity<Object> clonePaper(@PathVariable String paperId,
                                             @PathVariable String paperName, @PathVariable String courseNo) {
        User user = getAccessUser();
        Map<String, Object> map = new HashMap<>();
        try {
            clonePaperService.clonePaper(paperId, paperName, courseNo, user);
            map.put("msg", "success");
            return new ResponseEntity<Object>(map, HttpStatus.OK);
        } catch (PaperException e) {
            map.put("msg", e.getMessage());
            return new ResponseEntity<Object>(map, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}