package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.ExamDetailService;
import com.qmth.distributed.print.business.service.ExamStudentService;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.distributed.print.business.templete.execute.AsyncExaminationExportTemplateService;
import com.qmth.distributed.print.business.templete.execute.AsyncExaminationImportTemplateService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.TaskTypeEnum;
import com.qmth.teachcloud.common.service.TBTaskService;
import com.qmth.teachcloud.common.service.TeachcloudCommonService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 考务数据 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "考务数据管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.exam}/print")
@Validated
public class ExamDetailController {
    @Resource
    private TBTaskService tbTaskService;
    @Resource
    private AsyncExaminationImportTemplateService asyncExaminationImportTemplateService;
    @Resource
    private AsyncExaminationExportTemplateService asyncExaminationExportTemplateService;
    @Resource
    private ExamDetailService examDetailService;
    @Resource
    private PrintCommonService printCommonService;
    @Resource
    private ExamStudentService examStudentService;

    @Resource
    TeachcloudCommonService teachcloudCommonService;

    @ApiOperation(value = "异步考务数据批量导入接口")
    @RequestMapping(value = "/data_import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result examinationImportAysnc(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                                         @ApiParam(value = "印刷计划id", required = true) @RequestParam String printPlanId) throws Exception {
        Map<String, Object> map = printCommonService.saveTask(SystemConstant.convertIdToLong(printPlanId), file, TaskTypeEnum.EXAMINATION_IMPORT);
        asyncExaminationImportTemplateService.importTask(map);
        TBTask tbTask = Objects.nonNull(map.get(SystemConstant.TASK)) ? (TBTask) map.get(SystemConstant.TASK) : null;
        return Objects.nonNull(tbTask) ? ResultUtil.ok(tbTask.getId()) : ResultUtil.error("创建任务失败");
    }

    @ApiOperation(value = "考务数据导入-查询")
    @RequestMapping(value = "/data_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findExaminationBrief(@ApiParam(value = "印刷计划id集合") @RequestParam(required = false) List<String> printPlanIdList,
                                       @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                                       @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                                       @ApiParam(value = "考点") @RequestParam(required = false) String examPlace,
                                       @ApiParam(value = "考场") @RequestParam(required = false) String examRoom,
                                       @ApiParam(value = "卷袋号") @RequestParam(required = false) String packageCode,
                                       @ApiParam(value = "考试日期-起始") @RequestParam(required = false) Long startDate,
                                       @ApiParam(value = "考试日期-终止") @RequestParam(required = false) Long endDate,
                                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        if (Objects.isNull(printPlanIdList)) {
            printPlanIdList = new ArrayList<>();
        }
        List<Long> idList = printPlanIdList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        Set<Long> orgIds = teachcloudCommonService.listSubOrgIds(null);
        return ResultUtil.ok(examDetailService.findExaminationBriefPage(schoolId,
                idList, courseCode, paperNumber, examPlace, examRoom, packageCode, startDate, endDate, pageNumber, pageSize, orgIds));
    }

    @ApiOperation(value = "考务数据导入-汇总数据查询")
    @RequestMapping(value = "/data_total", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findSummarizedData(@ApiParam(value = "印刷计划id集合") @RequestParam(required = false) List<String> printPlanIdList,
                                     @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                                     @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                                     @ApiParam(value = "考点") @RequestParam(required = false) String examPlace,
                                     @ApiParam(value = "考场") @RequestParam(required = false) String examRoom,
                                     @ApiParam(value = "卷袋号") @RequestParam(required = false) String packageCode,
                                     @ApiParam(value = "考试日期-起始") @RequestParam(required = false) Long startDate,
                                     @ApiParam(value = "考试日期-终止") @RequestParam(required = false) Long endDate) {

        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        if (Objects.isNull(printPlanIdList)) {
            printPlanIdList = new ArrayList<>();
        }
        List<Long> idList = printPlanIdList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        Set<Long> orgIds = teachcloudCommonService.listSubOrgIds(null);
        return ResultUtil.ok(examDetailService.findSummarizedData(schoolId, idList, courseCode, paperNumber, examPlace, examRoom, packageCode, startDate, endDate, orgIds));
    }

    @ApiOperation(value = "考务数据导入-查看明细")
    @RequestMapping(value = "/data_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findExaminationDetail(@ApiParam(value = "印刷计划主键集合") @RequestParam(required = false) List<String> printPlanIdList,
                                        @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                                        @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                                        @ApiParam(value = "考点") @RequestParam(required = false) String examPlace,
                                        @ApiParam(value = "考场") @RequestParam(required = false) String examRoom,
                                        @ApiParam(value = "学生查询参数") @RequestParam(required = false) String studentParams,
                                        @ApiParam(value = "考试日期-起始") @RequestParam(required = false) Long startDate,
                                        @ApiParam(value = "考试日期-终止") @RequestParam(required = false) Long endDate,
                                        @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                        @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {

        if (Objects.isNull(printPlanIdList)) {
            printPlanIdList = new ArrayList<>();
        }
        List<Long> idList = printPlanIdList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        return ResultUtil.ok(examDetailService.findExaminationDetail(schoolId, idList,
                courseCode, paperNumber, examPlace, examRoom, studentParams, startDate, endDate, pageNumber, pageSize));
    }

    @ApiOperation(value = "查看考生明细")
    @RequestMapping(value = "/get_student_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result getStudentDetail(@ApiParam(value = "考生表主键") @RequestParam(required = false) Long id) {
        Map<String, Object> map = examStudentService.getStudentDetail(id);
        return ResultUtil.ok(map);
    }


    @ApiOperation(value = "考务数据导入-查看明细弹窗")
    @RequestMapping(value = "/data_detail_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findExaminationDetailById(@ApiParam(value = "考务场次id", required = true) @RequestParam String examDetailId,
                                            @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {

        return ResultUtil.ok(examDetailService.findExaminationDetail(SystemConstant.convertIdToLong(examDetailId), pageNumber, pageSize));
    }

    @ApiOperation(value = "考务数据导入-考点模糊查询")
    @RequestMapping(value = "/data_place_query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findExamPlaceDatasource(@RequestParam(value = "param", required = false) String param,
                                          @RequestParam(value = "printPlanId", required = false) List<String> printPlanId) {
        if (Objects.isNull(printPlanId)) {
            printPlanId = new ArrayList<>();
        }
        List<Long> printPlanIdList = printPlanId.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        return ResultUtil.ok(examDetailService.findExamPlaceDatasource(param, printPlanIdList));
    }

    @ApiOperation(value = "考务数据导入-考场模糊查询")
    @RequestMapping(value = "/data_room_query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findExamRoomDatasource(@RequestParam(value = "param", required = false) String param,
                                         @RequestParam(value = "printPlanId", required = false) List<String> printPlanId) {
        if (Objects.isNull(printPlanId)) {
            printPlanId = new ArrayList<>();
        }
        List<Long> printPlanIdList = printPlanId.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        return ResultUtil.ok(examDetailService.findExamRoomDatasource(param, printPlanIdList));
    }

    @ApiOperation(value = "考务数据导入-模板下载")
    @RequestMapping(value = "/template_download", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "下载成功", response = EditResult.class)})
    public void downLoadExaminationTemplate() throws IOException {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        examDetailService.downLoadExaminationTemplate(schoolId, ServletUtil.getResponse());
    }

    @ApiOperation(value = "考务数据导入-结果导出")
    @RequestMapping(value = "/data_export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result dataExport(@ApiParam(value = "印刷计划id集合") @RequestParam(required = false) List<String> printPlanIdList,
                             @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                             @ApiParam(value = "试卷编号") @RequestParam(required = false) String paperNumber,
                             @ApiParam(value = "考点") @RequestParam(required = false) String examPlace,
                             @ApiParam(value = "考场") @RequestParam(required = false) String examRoom,
                             @ApiParam(value = "卷袋号") @RequestParam(required = false) String packageCode,
                             @ApiParam(value = "考试日期-起始") @RequestParam(required = false) Long startDate,
                             @ApiParam(value = "考试日期-终止") @RequestParam(required = false) Long endDate) {
        Set<Long> orgIds = teachcloudCommonService.listSubOrgIds(null);
        if (Objects.isNull(printPlanIdList)) {
            printPlanIdList = new ArrayList<>();
        }
        List<Long> idList = printPlanIdList.stream().map(SystemConstant::convertIdToLong).collect(Collectors.toList());
        Long printId = null;
        if (idList.size() > 0) {
            printId = idList.get(0);
        }
        Map<String, Object> map = tbTaskService.saveTask(TaskTypeEnum.EXAMINATION_EXPORT, printId);
        map.put("printPlanIdList", idList);
        map.put("courseCode", courseCode);
        map.put("paperNumber", paperNumber);
        map.put("examPlace", examPlace);
        map.put("examRoom", examRoom);
        map.put("packageCode", packageCode);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("orgIds", orgIds);
        asyncExaminationExportTemplateService
                .exportTask(map);
        TBTask tbTask = Objects.nonNull(map.get(SystemConstant.TASK)) ? (TBTask) map.get(SystemConstant.TASK) : null;
        return Objects.nonNull(tbTask) ? ResultUtil.ok(tbTask.getId()) : ResultUtil.error("创建任务失败");
    }
}

