package com.qmth.jkserver.controller.system;

import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.model.ExamPaperUncoiling;
import com.qmth.jkserver.params.ExamPaperUncoilingParams;
import com.qmth.jkserver.service.KWService;
import org.slf4j.Logger;
import org.springframework.context.annotation.Lazy;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * 开卷管理
 *
 * @author tianjun
 */
@RestController
@RequestMapping("/system/paperUncoiling")
public class ExamPaperUncoilingController extends BaseSystemController {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ExamPaperUncoilingController.class);

    @Resource
    @Lazy
    private KWService kWService;

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Object pageQuery(HttpServletRequest request, ExamPaperUncoilingParams params
    ) throws ParseException {
        return kWService.queryExamPaperUncoiling(params);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(
            HttpServletRequest request,
            ExamPaperUncoiling examPaperUncoiling
    ) throws ParseException {
        try {
            return kWService.save(examPaperUncoiling);
        } catch (Exception e) {
            LOGGER.info("error:", e);
            e.printStackTrace();
            if (e instanceof JkServerException) {
                throw (JkServerException) e;
            } else {
                throw JkServerException.withMessage("开卷失败");
            }
        }
    }

    @RequestMapping(value = "/clean", method = RequestMethod.POST)
    public Object clean(
            HttpServletRequest request,
            @RequestParam(required = true) String id
    ) throws ParseException {
        kWService.removeExamPaperUncoiling(id);
        return true;
    }

}
