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.dto.CardCustDto;
import com.qmth.distributed.print.business.bean.dto.CardDetailDto;
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.bean.params.ArraysParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicPrintConfig;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

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

    @Resource
    private ExamCardService examCardService;

    @Resource
    private BasicPrintConfigService basicPrintConfigService;

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

    /**
     * 题卡管理（通卡管理）
     * @param type 题卡类型（通卡或专卡）
     * @param title 题卡名称模糊查询
     * @param createMethod 题卡创建方式（上传、自定义）
     * @param enable 1正常/0禁用
     * @param createStartTime 创建时间（开始）
     * @param createEndTime 创建时间（结束）
     * @param pageNumber 分页参数
     * @param pageSize 分页参数
     * @return Result
     */
    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Result page(@RequestParam(value = "type", defaultValue = "GENERIC") String type,
                       @RequestParam(value = "title", required = false) String title,
                       @RequestParam(value = "createMethod", required = false) String createMethod,
                       @RequestParam(value = "enable", required = false) Boolean enable,
                       @RequestParam(value = "createStartTime", required = false) Long createStartTime,
                       @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(type, title, createMethod, enable, createStartTime, createEndTime, pageNumber, pageSize);
        return ResultUtil.ok(examCardIPage);
    }

    /**
     * 题卡管理（通卡管理）-新增/修改
     * @param params 保存传值参数
     * @return Result
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save_generic", method = RequestMethod.POST)
    public Result save(@RequestBody GenericExamCardParams params) throws Exception {
        Long id = examCardService.saveGeneric(params);
        return ResultUtil.ok(String.valueOf(id),"");
    }

    /**
     * 题卡管理（通卡管理）-删除
     * @param id 题卡ID
     * @return Result
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/delete_generic", method = RequestMethod.POST)
    public Result save(@RequestParam(value = "id") Long id) {
        Boolean success = examCardService.deleteGeneric(id);
        return ResultUtil.ok(success);
    }

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

    /**
     * 新建
     *
     * @param examCardParams
     * @return
     */
    @ApiOperation(value = "新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody ExamCardParams examCardParams) throws Exception {
        String cardId = examCardService.saveExamCard(examCardParams);
        return ResultUtil.ok(cardId, "");
    }

    /**
     * 客服制卡申请
     *
     * @param examCardParams
     * @return
     */
    @ApiOperation(value = "客服制卡申请")
    @RequestMapping(value = "/cust_save", method = RequestMethod.POST)
    public Result custSave(@RequestBody ExamCardParams examCardParams) {
        String cardId = examCardService.saveExamCardCust(examCardParams);
        return ResultUtil.ok(cardId, "");
    }

    /**
     * 客服制卡审核查询
     *
     * @param schoolId
     * @param status
     * @param paperNumber
     * @param userId
     * @param applyStartTime
     * @param applyEndTime
     * @param finishStartTime
     * @param finishEndTime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "客服制卡审核查询")
    @RequestMapping(value = "/cust_list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "schoolId", required = false) String schoolId,
                       @RequestParam(value = "status", required = false) String status,
                       @RequestParam(value = "paperNumber", required = false) String paperNumber,
                       @RequestParam(value = "userId", required = false) String userId,
                       @RequestParam(value = "applyStartTime", required = false) Long applyStartTime,
                       @RequestParam(value = "applyEndTime", required = false) Long applyEndTime,
                       @RequestParam(value = "finishStartTime", required = false) Long finishStartTime,
                       @RequestParam(value = "finishEndTime", required = false) Long finishEndTime,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<CardCustDto> cardCustDtoIPage = examCardService.listCardCust(schoolId, status, paperNumber, userId, applyStartTime, applyEndTime, finishStartTime, finishEndTime, pageNumber, pageSize);
        return ResultUtil.ok(cardCustDtoIPage);
    }

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

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

    /**
     * 批量下载客服制卡文件
     *
     * @param response
     * @param arraysParams
     */
    @ApiOperation(value = "批量下载文件")
    @RequestMapping(value = "/download_files", method = RequestMethod.POST)
    public void taskPaperDownload(HttpServletResponse response, @RequestBody ArraysParams arraysParams) throws Exception {
        examCardService.downloadFiles(response, arraysParams);
    }

}
