package com.qmth.distributed.print.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.ExamCardParams;
import com.qmth.distributed.print.business.bean.params.GenericExamCardParams;
import com.qmth.distributed.print.business.entity.ExamCard;
import com.qmth.distributed.print.business.service.BasicPrintConfigService;
import com.qmth.distributed.print.business.service.ExamCardService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题卡 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "题卡Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_EXAM + "/card")
@Validated
public class ExamCardController {

    @Resource
    private ExamCardService examCardService;

    //====================================通卡接口（start）===================================

    /**
     * 题卡管理-分页查询
     */
    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Result page(@ApiParam(value = "学期ID") @RequestParam(required = false) Long semesterId,
                       @ApiParam(value = "考试ID") @RequestParam(required = false) Long examId,
                       @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                       @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                       @ApiParam(value = "题卡类型（通卡或专卡）") @RequestParam(value = "cardType", required = false) String cardType,
                       @ApiParam(value = "题卡名称模糊查询") @RequestParam(value = "title", required = false) String title,
                       @ApiParam(value = "题卡创建方式（上传、自定义）") @RequestParam(value = "createMethod", required = false) String createMethod,
                       @ApiParam(value = "1正常/0禁用") @RequestParam(value = "enable", required = false) Boolean enable,
                       @ApiParam(value = "创建时间（开始）") @RequestParam(value = "createStartTime", required = false) Long createStartTime,
                       @ApiParam(value = "创建时间（结束）") @RequestParam(value = "createEndTime", required = false) Long createEndTime,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<ExamCard> examCardIPage = examCardService.listPage(semesterId, examId, courseCode, paperNumber, cardType, title, createMethod, enable, createStartTime, createEndTime, pageNumber, pageSize);
        return ResultUtil.ok(examCardIPage);
    }

    /**
     * 题卡管理（通卡管理）-新增/修改
     *
     * @param params 保存传值参数
     * @return Result
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save_generic", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE, detail = "新增/修改操作，题卡ID：{{params.id}}、题卡标题：{{params.title}}")
    public Result save(@RequestBody GenericExamCardParams params) throws Exception {
        Map<String, String> map = examCardService.saveGeneric(params);
        return ResultUtil.ok(map);
    }

    /**
     * 题卡管理（通卡管理）-删除
     *
     * @param id 题卡ID
     * @return Result
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete_generic", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE, detail = "删除操作，题卡ID：{{id}}")
    public Result save(@RequestParam(value = "id") Long id) {
        return ResultUtil.ok(examCardService.deleteGeneric(id));
    }

    //====================================通卡接口↑（end）===================================

    /**
     * 新建专卡
     *
     * @param examCardParams
     */
    @ApiOperation(value = "新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE, detail = "新增/修改操作，题卡ID：{{examCardParams.id}}、题卡标题：{{examCardParams.title}}")
    public Result save(@RequestBody ExamCardParams examCardParams) throws Exception {
        Map<String, String> map = examCardService.saveExamCard(examCardParams);
        return ResultUtil.ok(map);
    }

    /**
     * 根据ID获取题卡详情
     *
     * @param cardId
     * @return
     */
    @ApiOperation(value = "根据ID获取题卡详情")
    @RequestMapping(value = "/get_one", method = RequestMethod.POST)
    public Result getOne(@RequestParam(value = "cardId", required = false) Long cardId) {
        ExamCard examCard = examCardService.getById(cardId);
        return ResultUtil.ok(examCard);
    }

    /**
     * 选择已有题卡列表
     *
     * @param examId     考试id
     * @param courseCode 课程编号
     * @return 结果
     */
    @ApiOperation(value = "选择已有题卡列表")
    @RequestMapping(value = "/select_card_list", method = RequestMethod.POST)
    public Result selectCardList(@RequestParam Long examId, @RequestParam String courseCode, @RequestParam(required = false) String paperNumber) {
        List<ExamCard> list = examCardService.listSelectCard(courseCode, examId, paperNumber);
        return ResultUtil.ok(list);
    }

    /**
     * 复制题卡
     *
     * @param id 题卡id
     */
    @ApiOperation(value = "复制题卡")
    @RequestMapping(value = "/copy", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "复制题卡操作，题卡ID：{{id}}")
    public Result copyCard(@RequestParam(value = "id") String id,
                           @RequestParam(value = "courseCode") String courseCode) {
        Long copyCardId = examCardService.copyCard(SystemConstant.convertIdToLong(id), courseCode);
        return ResultUtil.ok(String.valueOf(copyCardId), "");
    }

    /**
     * 生成图片
     *
     * @param id 题卡id
     */
    @ApiOperation(value = "题卡转图片")
    @RequestMapping(value = "/convert_image", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.UPDATE, detail = "生成图片操作，题卡ID：{{id}}")
    public Result convertImage(@RequestParam(value = "id") String id) {
        examCardService.convertImage(SystemConstant.convertIdToLong(id));
        return ResultUtil.ok(true, "");
    }

    /**
     * 下载（包含pdf,html,json,jpg）
     *
     * @param response
     * @param id       题卡ID
     */
    @ApiOperation(value = "导出题卡文件")
    @RequestMapping(value = "/download_card", method = RequestMethod.POST)
    public void downloadCard(HttpServletResponse response, @RequestParam(value = "id") String id) {
        examCardService.downloadCard(response, id);
    }

    /**
     * 获取评卷区题卡图片
     */
    @ApiOperation(value = "获取题卡图片")
    @RequestMapping(value = "/find_jpg_file", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result listCardImage(@ApiParam(value = "考试id", required = true) @RequestParam Long examId,
                                @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                                @ApiParam(value = "试卷类型", required = true) @RequestParam String paperType) {
        return ResultUtil.ok(examCardService.listCardImage(examId, paperNumber, paperType));
    }

    /**
     * 下载（包含pdf,html,json,jpg）
     *
     * @param response
     * @param id       题卡ID
     */
    @ApiOperation(value = "下载卡格式")
    @RequestMapping(value = "/download_card_json", method = RequestMethod.POST)
    public void downloadCardJson(HttpServletResponse response,
                                 @ApiParam(value = "题卡ID", required = true) @RequestParam(value = "id") String id) {
        examCardService.downloadCardJson(response, id);
    }
}
