package com.qmth.themis.admin.api;

import com.qmth.themis.business.bean.admin.ExamBreachListExportBean;
import com.qmth.themis.business.bean.admin.ExamDeficiencyListBean;
import com.qmth.themis.business.bean.admin.ExamViewCountListBean;
import com.qmth.themis.business.bean.admin.InvigilateListPatrolReportBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.TBUser;
import com.qmth.themis.business.excel.ExportUtils;
import com.qmth.themis.business.service.TIeReportService;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
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 javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Objects;

/**
 * 监考报表
 *
 * @Description:
 * @Author: xiatian
 * @Date: 2020-08-28
 */
@Api(tags = "监考报表")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_ADMIN + "/report")
@Validated
public class TIeReportController {

    @Resource
    private TIeReportService reportService;

    @ApiOperation(value = "考试概览")
    @RequestMapping(value = "/exam_view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examView(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                           @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                           @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                           @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                           @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                           @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        return ResultUtil.ok(reportService.examView(tbUser.getOrgId(), examId, examActivityId, roomCode, courseCode, name, identity));
    }

    @ApiOperation(value = "情况统计")
    @RequestMapping(value = "/exam_view_count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examViewCount(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        return ResultUtil.ok(reportService.examViewCount(tbUser.getOrgId(), examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "情况统计导出")
    @RequestMapping(value = "/exam_view_count/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public void examViewCountExport(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                    @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                    @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                    @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                    @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                    @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity) throws Exception {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        List<ExamViewCountListBean> examViewCountListBeanList = reportService.examViewCountExport(tbUser.getOrgId(), examId, examActivityId, roomCode, courseCode, name, identity);
        ExportUtils.exportEXCEL("情况统计", ExamViewCountListBean.class, examViewCountListBeanList, ServletUtil.getResponse());
    }

    @ApiOperation(value = "缺考名单")
    @RequestMapping(value = "/exam_deficiency_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examDeficiencyList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                     @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                     @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                     @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                     @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                     @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                     @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                     @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        return ResultUtil.ok(reportService.examDeficiencyList(tbUser.getOrgId(), examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "缺考名单导出")
    @RequestMapping(value = "/exam_deficiency_list/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public void examDeficiencyListExport(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                         @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                         @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                         @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                         @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                         @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity) throws Exception {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        List<ExamDeficiencyListBean> examDeficiencyListBeanList = reportService.examDeficiencyListExport(tbUser.getOrgId(), examId, examActivityId, roomCode, courseCode, name, identity);
        ExportUtils.exportEXCEL("缺考名单", ExamDeficiencyListBean.class, examDeficiencyListBeanList, ServletUtil.getResponse());
    }

    @ApiOperation(value = "异常处理")
    @RequestMapping(value = "/exam_exception_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examExceptionList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                    @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                    @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                    @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                    @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                    @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                    @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                    @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        return ResultUtil.ok(reportService.examExceptionList(examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "异常处理明细")
    @RequestMapping(value = "/exam_exception_list_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examExceptionListDetail(@ApiParam(value = "考生id", required = true) @RequestParam Long examStudentId,
                                          @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                          @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        return ResultUtil.ok(reportService.examExceptionDetailList(examStudentId, pageNumber, pageSize));
    }

    @ApiOperation(value = "重考处理")
    @RequestMapping(value = "/exam_reexam_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examReexamList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                 @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                 @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                 @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                 @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                 @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                 @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                 @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        return ResultUtil.ok(reportService.examReexamList(examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "违纪名单")
    @RequestMapping(value = "/exam_breach_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examBreachList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                 @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                 @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                 @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                 @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                 @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                 @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                 @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        return ResultUtil.ok(reportService.examBreachList(examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "违纪名单明细")
    @RequestMapping(value = "/exam_breach_list_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examBreachListDetail(@ApiParam(value = "考生id", required = true) @RequestParam Long examStudentId,
                                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        return ResultUtil.ok(reportService.examBreachListDetail(examStudentId, 0, pageNumber, pageSize));
    }

    @ApiOperation(value = "违纪名单导出")
    @RequestMapping(value = "/exam_breach_list/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public void examBreachListExport(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                     @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                     @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                     @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                     @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                     @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity) throws Exception {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        List<ExamBreachListExportBean> examDeficiencyListBeanList = reportService.examBreachListExport(examId, examActivityId, roomCode, courseCode, name, identity);
        ExportUtils.exportEXCEL("违纪名单", ExamBreachListExportBean.class, examDeficiencyListBeanList, ServletUtil.getResponse());
    }

    @ApiOperation(value = "撤销违纪名单")
    @RequestMapping(value = "/exam_revoke_breach_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examRevokeBreachList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                       @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                       @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                       @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                       @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                       @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        return ResultUtil.ok(reportService.examRevokeBreachList(examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "撤销违纪名单明细")
    @RequestMapping(value = "/exam_revoke_breach_list_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examRevokeBreachListDetail(@ApiParam(value = "考生id", required = true) @RequestParam Long examStudentId,
                                             @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                             @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        return ResultUtil.ok(reportService.examRevokeBreachListDetail(examStudentId, 1, pageNumber, pageSize));
    }

    @ApiOperation(value = "考生日志")
    @RequestMapping(value = "/exam_student_log_list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examStudentLogList(@ApiParam(value = "考试id", required = true) @RequestParam(required = true) Long examId,
                                     @ApiParam(value = "考场场次id", required = false) @RequestParam(required = false) Long examActivityId,
                                     @ApiParam(value = "虚拟考场代码", required = false) @RequestParam(required = false) String roomCode,
                                     @ApiParam(value = "科目编码", required = false) @RequestParam(required = false) String courseCode,
                                     @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name,
                                     @ApiParam(value = "唯一码", required = false) @RequestParam(required = false) String identity,
                                     @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                     @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        return ResultUtil.ok(reportService.examStudentLogList(examId, examActivityId, roomCode, courseCode, name, identity, pageNumber, pageSize));
    }

    @ApiOperation(value = "考生日志明细")
    @RequestMapping(value = "/exam_student_log_list_detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examStudentLogListDetail(@ApiParam(value = "考生id", required = true) @RequestParam Long examStudentId,
                                           @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) int pageNumber,
                                           @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) int pageSize) {
        return ResultUtil.ok(reportService.examStudentLogListDetail(examStudentId, pageNumber, pageSize));
    }

    @ApiOperation(value = "在线巡考报表接口")
    @RequestMapping(value = "/patrol", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "在线巡考报表信息", response = InvigilateListPatrolReportBean.class)})
    public Result patrolReport(@ApiParam(value = "考试批次id", required = false) @RequestParam(required = false) Long examId) {
        return ResultUtil.ok(reportService.patrolReport(examId, null));
    }

    @ApiOperation(value = "考情监控-人数")
    @RequestMapping(value = "/examination_monitor/count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result examinationMonitorCount() {
        return ResultUtil.ok(reportService.examinationMonitorCount());
    }

    @ApiOperation(value = "考情监控-预警类型排行")
    @RequestMapping(value = "/examination_monitor/warn_distribution", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result warnDistribution() {
        return ResultUtil.ok(reportService.warnDistribution());
    }

    @ApiOperation(value = "考情监控-在线人数/预警时间趋势")
    @RequestMapping(value = "/examination_monitor/warn_trend", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result warnTrend() {
        return ResultUtil.ok(reportService.warnTrend());
    }

//    @ApiOperation(value = "考情监控-在线人数/预警时间趋势test")
//    @RequestMapping(value = "/examination_monitor/warn_trend_test", method = RequestMethod.POST)
//    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
//    public void warnTrendTest() {
//        List<TimeScaleDataResult> timeScaleDataResultList = reportService.warnTrend();
//        try {
//            ExportUtils.exportEXCEL("在线人数/预警时间趋势", TimeScaleDataResult.class, timeScaleDataResultList, ServletUtil.getResponse());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    @ApiOperation(value = "考情监控-预警消息")
    @RequestMapping(value = "/examination_monitor/warn_msg", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result warnMsg() {
        return ResultUtil.ok(reportService.warnMsg());
    }

    @ApiOperation(value = "考情监控-预警地区排行")
    @RequestMapping(value = "/examination_monitor/warn_region_distribution", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result warnRegionDistribution() {
        return ResultUtil.ok(reportService.warnRegionDistribution());
    }

    @ApiOperation(value = "考情监控-地图数据")
    @RequestMapping(value = "/examination_monitor/map", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "结果信息")})
    public Result map() {
        return ResultUtil.ok(reportService.map());
    }
}
