package com.qmth.distributed.print.business.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qmth.distributed.print.business.bean.dto.*;
import com.qmth.distributed.print.business.bean.params.SerialNumberParams;
import com.qmth.distributed.print.business.bean.result.ExaminationDetailResult;
import com.qmth.distributed.print.business.bean.result.ExaminationResult;
import com.qmth.distributed.print.business.bean.result.SummarizedDataResult;
import com.qmth.distributed.print.business.entity.ExamDetail;
import com.qmth.distributed.print.business.entity.ExamDetailCourse;
import com.qmth.distributed.print.business.enums.ExamDataSourceEnum;
import com.qmth.teachcloud.common.entity.SysUser;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 考务数据 服务类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
public interface ExamDetailService extends IService<ExamDetail> {
    /**
     * 计算印刷计划下的总科次
     * @param printPlanId 印刷计划id
     * @return 该印刷计划下总科次
     */
    double calculateTotalSubjects(Long printPlanId);

    /**
     * 计算印刷计划下的总卷袋数
     * @param printPlanId 印刷计划id
     * @return 该印刷计划下总卷袋数
     */
    double calculateTotalPackages(Long printPlanId);

    IPage<PrintTaskDto> listPrintTask(Long printPlanId, String status, String courseCode, String paperNumber, String examPlace, String examRoom, Long examStartTime, Long examEndTime, Long printStartTime, Long printEndTime,Long printHouseId, Integer pageNumber, Integer pageSize);

    PrintTaskTotalDto taskTotalData(Long printPlanId, String status, String courseCode, String paperNumber, String examPlace, String examRoom, Long examStartTime, Long examEndTime, Long printStartTime, Long printEndTime,Long printHouseId);

    List<JSONObject> taskViewPDF(Long examDetailId);

    /**
     * 查询有效的考务字段
     * @param schoolId 学校id
     * @return 考务字段
     */
    List<FieldsDto> findExaminationFields(Long schoolId);

    /**
     * 考务数据模板下载
     * @param schoolId 学校id
     * @return 是否成功
     */
    void downLoadExaminationTemplate(Long schoolId, HttpServletResponse response) throws IOException;


    /**
     * 查询考务数据-简略
     * @param schoolId 学校id
     * @param printPlanIdList 印刷计划id集合
     * @param courseCode 课程代码
     * @param paperNumber 试卷编号
     * @param examPlace 考点
     * @param examRoom 考场
     * @param packageCode 卷袋编号
     * @param pageNumber 分页页码
     * @param startDate 考试开始日期
     * @param endDate 考试结束日期
     * @param pageSize 分页数量
     * @param orgIds 权限控制参数
     * @return 查询结果
     */
    IPage<ExaminationResult> findExaminationBriefPage(Long schoolId, List<Long> printPlanIdList, String courseCode, String paperNumber,
                                                      String examPlace, String examRoom, String packageCode,Long startDate,Long endDate, int pageNumber, int pageSize, Set<Long> orgIds);

    /**
     * 查询考务数据-汇总数据查询
     * @param schoolId 学校id
     * @param printPlanIdList 印刷计划id集合
     * @param courseCode 课程代码
     * @param paperNumber 试卷编号
     * @param examPlace 考点
     * @param examRoom 考场
     * @param packageCode 卷袋编号
     * @param startDate 考试日期-起始
     * @param endDate 考试日期-终止
     * @param orgIds 权限控制参数
     * @return 查询结果
     */
    SummarizedDataResult findSummarizedData(Long schoolId,List<Long> printPlanIdList,String courseCode,String paperNumber,
                                            String examPlace,String examRoom,String packageCode,Long startDate,Long endDate,Set<Long> orgIds);


    /**
     * 查询考务数据-明细
     * @param printPlanIdList 印刷计划id集合
     * @param courseCode 课程代码
     * @param paperNumber 试卷编号
     * @param examPlace 考点
     * @param examRoom 考场
     * @param studentParams 学生查询参数
     * @param startDate 考试日期-起始
     * @param endDate 考试日期-终止
     * @param pageNumber 分页页码
     * @param pageSize 分页数量
     * @return 查询结果
     */
    IPage<ExaminationDetailResult> findExaminationDetail(Long schoolId, List<Long> printPlanIdList,String courseCode,String paperNumber,String examPlace,
                                                         String examRoom,String studentParams,Long startDate,Long endDate, int pageNumber,int pageSize);

    IPage<ExaminationDetailResult> findExaminationDetail(Long convertIdToLong, int pageNumber, int pageSize);

    /**
     * 查询印刷计划下考点数据源
     * @return 考点集合
     * @param param
     * @param printPlanIdList
     */
    List<String> findExamPlaceDatasource(String param, List<Long> printPlanIdList);

    /**
     * 查询印刷计划下考场数据源
     * @return 考场集合
     * @param param
     * @param printPlanId
     */
    List<String> findExamRoomDatasource(String param, List<Long> printPlanId);

    boolean submitTask(ExamDetail examDetail) throws IOException;

    boolean taskCancel(ExamDetail examDetail);

    boolean resubmitTask(ExamDetail examDetail) throws IOException;

    /**
     * 根据考务数据Excel数据处理考务-场次表
     * @param dataList Excel处理后的数据
     * @param userId 当前用户id
     * @param serialNumberParams 序列号生成参数
     * @param sysUser
     */
    List<Long> disposeExamDetailByExaminationExcel(List<ExaminationImportDto> dataList, Long userId, SerialNumberParams serialNumberParams, SysUser sysUser);

    /**
     * 根据考务数据Excel数据处理考务-科目表
     * @param dataList Excel处理后的数据
     * @param userId 当前用户id
     */
    void disposeExamDetailCourseByExaminationExcel(List<ExaminationImportDto> dataList,Long userId, SysUser sysUser);

    /**
     * 根据考务数据Excel数据处理考务-学生表
     * @param dataList Excel处理后的数据
     * @param userId 当前用户id
     */
    void disposeExamStudentByExaminationExcel(List<ExaminationImportDto> dataList,Long userId, SysUser sysUser);

    /**
     * 删除考务数据
     * @param printPlanId 印刷计划id
     * @param source
     */
    void deleteExaminationData(Long printPlanId, ExamDataSourceEnum source);



    List<ExamDetail> listByPrintPlanIdAndCourseCodeAndPaperNumber(Long schoolId, Long printPlanId, String courseCode, String paperNumber);


    List<ExamDetail> listByExamPrintId(Long printPlanId);

    /**
     * 查询考务数据-场次导出数据源
     * @param map 考务数据查询条件键值对
     * @return 导出数据源
     */
    List<ExaminationExportDto> findExaminationExportDtoDatasource(Map<String, Object> map);

    Boolean updatePrintProgress(Long schoolId, Long examDetailId, String machineCode, Integer printProgress, Boolean isPrint);

    IPage<ClientExamStudentDto> listStudent(Long schoolId, Long examDetailId, String ticketNumber, String studentName, String courseCode, Integer pageNumber, Integer pageSize);

    List<Map> listStudentByExamDetailCourseId(Long schoolId, Long examDetailCourseId, String ticketNumber, String type);

    List<ExamDetail> listByCourseCodeAndPaperNumberAndPaperTypeIsNull(Long schoolId, String courseCode, String paperNumber);

    List<ExamDetail> listByCourseCodeAndPaperNumber(Long schoolId, String courseCode, String paperNumber);

    List<ExamDetailCourse> listSyncPaperNumberByPrintPlanId(Long printPlanId);

    boolean taskNormal(ExamDetail examDetail);

    /**
     * 根据班级id组查找班级名称组
     * @param clazzIds 班级id组（字符串）
     * @param separator 分隔符 默认","
     * @return 班级名称组
     */
    String findClazzNamesByClazzIds(String clazzIds, String separator);

    /**
     * 根据班级id组查找班级名称组
     * @param clazzIdList 班级id组（集合）
     * @param separator 分隔符 默认","
     * @return 班级名称组
     */
    String findClazzNamesByClazzIds(List<Long> clazzIdList, String separator);
}
