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.entity.ExamCard;
import com.qmth.distributed.print.business.service.ExamCardService;
import com.qmth.teachcloud.common.bean.params.ArraysParams;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
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")
public class ExamCardController {

    @Autowired
    private ExamCardService examCardService;

    /**
     * 新建
     *
     * @param examCardParams
     * @return
     */
    @ApiOperation(value = "新建")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody ExamCardParams examCardParams) {
        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(value = "pageNumber") Integer pageNumber,
                       @RequestParam(value = "pageSize") 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 courseCode
     * @param cardRuleId
     * @param paperType
     * @return
     */
    @ApiOperation(value = "选择已有题卡列表")
    @RequestMapping(value = "/select_card_list", method = RequestMethod.POST)
    public Result selectCardList(@RequestParam String courseCode,
                                 @RequestParam Long cardRuleId,
                                 @RequestParam String paperType) {
        List<ExamCard> list = examCardService.listSelectCard(courseCode, cardRuleId, paperType);
        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);
    }

}
