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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qmth.distributed.print.business.bean.dto.*;
import com.qmth.distributed.print.business.cache.CreatePdfCacheUtil;
import com.qmth.distributed.print.business.entity.*;
import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
import com.qmth.distributed.print.business.enums.ExamNumberStyleEnum;
import com.qmth.distributed.print.business.service.*;
import com.qmth.teachcloud.common.bean.vo.FilePathVo;
import com.qmth.teachcloud.common.bean.vo.OriginalVo;
import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
import com.qmth.teachcloud.common.bean.vo.PrintPathVo;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicAttachment;
import com.qmth.teachcloud.common.entity.BasicPrintConfig;
import com.qmth.teachcloud.common.entity.BasicSemester;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.*;
import com.qmth.teachcloud.common.service.BasicAttachmentService;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.common.service.TeachcloudCommonService;
import com.qmth.teachcloud.common.util.ConvertUtil;
import com.qmth.teachcloud.common.util.ExamTaskUtil;
import com.qmth.teachcloud.common.util.FileUtil;
import com.qmth.teachcloud.common.util.RedisUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 创建pdf util
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/4/14
 */
@Component
public class CreatePdfUtil {
    private final static Logger log = LoggerFactory.getLogger(CreatePdfUtil.class);

    @Resource
    BasicAttachmentService basicAttachmentService;
    @Resource
    @Lazy
    PrintCommonService printCommonService;

    @Resource
    ExamDetailService detailService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    TeachcloudCommonService teachcloudCommonService;

    @Resource
    ExamStudentService examStudentService;

    @Resource
    BasicTemplateService basicTemplateService;

    @Resource
    BasicSemesterService basicSemesterService;

    @Resource
    ExamTaskService examTaskService;

    @Resource
    ExamTaskDetailService examTaskDetailService;


    /**
     * 创建登记表
     *
     * @param examDetail
     * @param basicAttachment
     * @param ordinaryPdfList
     * @param printCount
     * @param fileTempList
     */
    public void createCheckIn(ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> ordinaryPdfList, Integer printCount, List<File> fileTempList) throws Exception {
        Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception());

        String type = basicAttachment.getType();
        FilePathVo filePathVo = JSON.parseObject(basicAttachment.getPath(), FilePathVo.class);

        if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.PDF_PREFIX)) {
            String filePath = filePathVo.getPath();
            PrintPathVo printPathVo = new PrintPathVo(ClassifyEnum.CHECK_IN, filePathVo.getUploadType(), filePathVo.getType(), filePath, basicAttachment.getMd5(), null, null);
            examDetail.setAttachmentPath(printCommonService.parseAttachmentPath(examDetail.getAttachmentPath(), printPathVo));

            for (int i = 0; i < printCount; i++) {
                ordinaryPdfList.add(new PdfDto(filePath, PageSizeEnum.A4, 0));
            }
        } else {
            printCommonService.saveAttachmentPdf(ClassifyEnum.CHECK_IN, examDetail, basicAttachment, ordinaryPdfList, printCount, 0);
        }
    }


    /**
     * 生成卷袋贴
     *
     * @param schoolName
     * @param examStudentList
     * @param variablePdfList
     * @param printCount
     * @param examDetailCourseList
     * @param fileTempList
     */
    public void createPaperPackage(Long templateId, String schoolName, ExamDetail examDetail, List<ExamStudentCourseDto> examStudentList, List<PdfDto> variablePdfList, Integer backupCount, Integer printCount, List<ExamDetailCourse> examDetailCourseList, Long examId, List<File> fileTempList) {
        BasicTemplate basicTemplate = basicTemplateService.getById(templateId);
        if (Objects.isNull(basicTemplate)) {
            throw ExceptionResultEnum.ERROR.exception("印品数据有误");
        }

        JSONObject object = JSON.parseObject(basicTemplate.getDisplayRange());

        PdfPackageDto pdfPackageDto = new PdfPackageDto();
        pdfPackageDto.setTitle(schoolName);
        pdfPackageDto.setPackageNumber(examDetail.getPackageCode());

        boolean isTag = false;
        for (ExamDetailCourse detailCourse : examDetailCourseList) {
            ExamTask examTask = examTaskService.getByExamIdAndCourseCodeAndPaperNumber(examId, detailCourse.getCourseCode(), detailCourse.getPaperNumber());
            ExamTaskDetail examTaskDetail = examTaskDetailService.getByExamTaskId(examTask.getId());
            List<PaperInfoVo> paperInfoVoList = ExamTaskUtil.parsePaperAttachmentPath(examTaskDetail.getPaperAttachmentIds());
            for (PaperInfoVo paperInfoVo : paperInfoVoList) {
                int pages = paperInfoVo.getPages();
                isTag = pages > 2;
                pdfPackageDto.setTag(isTag);
                if (isTag) {
                    break;
                }
            }
        }

        // 标题信息
        List<JSONObject> objectTitleList = JSON.parseArray(object.getString("title"), JSONObject.class).stream().filter(m -> m.getBoolean("enable")).collect(Collectors.toList());
        List<Map<String, String>> titlePlate = new ArrayList<>();
        for (JSONObject jsonObject : objectTitleList) {
            Map<String, String> titleMap = new HashMap<>();
            String code = jsonObject.getString("code");
            String name = jsonObject.getString("name");
            titleMap.put("code", code);
            titleMap.put("name", name);
            if ("semesterName".equals(code)) {
                BasicSemester basicSemester = basicSemesterService.selectByExamId(examId);
                titleMap.put("value", basicSemester == null ? null : basicSemester.getName() + "  课程考试");
            }
            titlePlate.add(titleMap);
        }
        pdfPackageDto.setTitlePlate(titlePlate);

        List<JSONObject> objectList = JSON.parseArray(object.getString("basic"), JSONObject.class).stream().filter(m -> m.getBoolean("enable")).collect(Collectors.toList());

        boolean isCourseHasTwo = objectList.stream().filter(m -> "courseCode".equals(m.getString("code")) || "courseName".equals(m.getString("code"))).count() == 2;
        boolean isDateHasTwo = objectList.stream().filter(m -> "examDate".equals(m.getString("code")) || "examTime".equals(m.getString("code"))).count() == 2;
        boolean isCourseFill = false;
        boolean isDateFill = false;
        // 基础信息
        List<Map<String, String>> basicPlate = new ArrayList<>();
        for (JSONObject jsonObject : objectList) {
            String code = jsonObject.getString("code");
            String name = jsonObject.getString("name");
            if ("examDate".equals(code) || "examTime".equals(code)) {
                String startDate = DateUtil.format(new Date(examDetail.getExamStartTime()), SystemConstant.DATE_PATTERN);
                String endDate = DateUtil.format(new Date(examDetail.getExamEndTime()), SystemConstant.DATE_PATTERN);
                String startTime = DateUtil.format(new Date(examDetail.getExamStartTime()), SystemConstant.TIME_PATTERN);
                String endTime = DateUtil.format(new Date(examDetail.getExamEndTime()), SystemConstant.TIME_PATTERN);
                Map<String, String> basicMap = new HashMap<>();
                if (isDateHasTwo) {
                    if (isDateFill) {
                        continue;
                    }
                    String examTime;
                    if (startDate.equals(endDate)) {
                        examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endTime;
                    } else {
                        examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endDate + " " + endTime;
                    }
                    basicMap.put("code", "examTime");
                    basicMap.put("name", "考试时间");
                    basicMap.put("value", examTime);
                    isDateFill = true;
                } else {
                    String examTime = "";
                    if ("examDate".equals(code)) {
                        if (startDate.equals(endDate)) {
                            examTime = startDate;
                        } else {
                            examTime = startDate + SystemConstant.HYPHEN + endDate;
                        }
                    } else if ("examTime".equals(code)) {
                        if (startDate.equals(endDate)) {
                            examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endTime;
                        } else {
                            examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endDate + " " + endTime;
                        }
                    }
                    basicMap.put("code", code);
                    basicMap.put("name", name);
                    basicMap.put("value", examTime);
                }
                basicPlate.add(basicMap);
            } else if ("courseCode".equals(code) || "courseName".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                if (isCourseHasTwo) {
                    if (isCourseFill) {
                        continue;
                    }
                    List<String> courseNames = examDetailCourseList.stream().map(m -> String.format("%s（%s）", m.getCourseName(), m.getCourseCode())).collect(Collectors.toList());
                    basicMap.put("code", "courseName");
                    basicMap.put("name", "课程名称");
                    basicMap.put("value", CollectionUtils.isNotEmpty(courseNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, courseNames) : "");
                    isCourseFill = true;
                } else {
                    List<String> courseNames = examDetailCourseList.stream().map(m -> "courseCode".equals(code) ? m.getCourseCode() : "courseName".equals(code) ? m.getCourseName() : "").collect(Collectors.toList());
                    basicMap.put("code", code);
                    basicMap.put("name", name);
                    basicMap.put("value", CollectionUtils.isNotEmpty(courseNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, courseNames) : "");
                }
                basicPlate.add(basicMap);
            } else if ("paperNumber".equals(code)) {
                List<String> paperNumbers = examDetailCourseList.stream().map(ExamDetailCourse::getPaperNumber).collect(Collectors.toList());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(paperNumbers) ? String.join(SystemConstant.COMMA_OF_CHINESE, paperNumbers) : "");
                basicPlate.add(basicMap);
            } else if ("examPlace".equals(code) || "campusName".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", examDetail.getExamPlace());
                basicPlate.add(basicMap);
            } else if ("examRoom".equals(code) || "examClassroomName".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", examDetail.getExamRoom());
                basicPlate.add(basicMap);
            } else if ("collegeName".equals(code)) {
                Set<String> collegeNames = examStudentList.stream().map(ExamStudent::getCollegeName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(collegeNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, collegeNames) : "");
                basicPlate.add(basicMap);
            } else if ("majorName".equals(code)) {
                Set<String> stringSet = new HashSet<>();
                Set<String> majorNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getMajorName())).map(ExamStudent::getMajorName).collect(Collectors.toSet());
                if (!majorNames.isEmpty()) {
                    stringSet.addAll(majorNames);
                }
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(stringSet) ? String.join(SystemConstant.COMMA_OF_CHINESE, stringSet) : "");
                basicPlate.add(basicMap);
            } else if ("clazzName".equals(code)) {
                Set<String> clazzNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getClazzName())).map(ExamStudent::getClazzName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(clazzNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, clazzNames) : "");
                basicPlate.add(basicMap);
            } else if ("teachClazzName".equals(code)) {
                Set<String> teachClazzNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getTeachClazzName())).map(ExamStudent::getTeachClazzName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(teachClazzNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, teachClazzNames) : "");
                basicPlate.add(basicMap);
            } else {
                // 扩展字段走本校验，进行数据组装
                Set<String> extendFieldsSet = examStudentList.stream().map(ExamStudentCourseDto::getExtendFields).collect(Collectors.toSet());
                Set<String> finalSet = new HashSet<>();
                for (String extendField : extendFieldsSet) {
                    List<JSONObject> jsonObjects = JSON.parseArray(extendField, JSONObject.class);
                    Set<String> values = jsonObjects.stream().filter(m -> code.equals(m.getString("code"))).map(m -> m.getString("value")).collect(Collectors.toSet());
                    finalSet.addAll(values);
                }
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", String.join(SystemConstant.COMMA_OF_CHINESE, finalSet));
                basicPlate.add(basicMap);
            }

            if ("paperCount".equals(code)) {
                Map<String, String> basicMap1 = new HashMap<>();
                basicMap1.put("code", "paperCount");
                basicMap1.put("name", "试卷数量");
                basicMap1.put("value", examStudentList.size() + " + " + backupCount);
                basicPlate.add(basicMap1);
            } else if ("examCount".equals(code)) {
                Map<String, String> basicMap2 = new HashMap<>();
                basicMap2.put("code", "examCount");
                basicMap2.put("name", "应考人数");
                basicMap2.put("value", String.valueOf(examStudentList.size()));
                basicPlate.add(basicMap2);
            } else if ("actualExamCount".equals(code)) {
                Map<String, String> basicMap3 = new HashMap<>();
                basicMap3.put("code", "actualExamCount");
                basicMap3.put("name", "实考人数");
                basicMap3.put("value", "");
                basicPlate.add(basicMap3);
            }
        }

        pdfPackageDto.setBasicPlate(basicPlate);
        printCommonService.saveAttachmentPackagePdf(pdfPackageDto, examDetail, variablePdfList, printCount, fileTempList);
    }

    /**
     * 创建签到表
     *
     * @param templateId
     * @param schoolName
     * @param examDetail
     * @param examStudentList
     * @param variablePdfList
     * @param printCount
     * @param examDetailCourseList
     * @param fileTempList
     */
    public void createSignBook(Long templateId, String schoolName, ExamDetail examDetail, List<ExamStudentCourseDto> examStudentList, List<PdfDto> variablePdfList, Integer printCount, List<ExamDetailCourse> examDetailCourseList, List<File> fileTempList) {
        BasicTemplate basicTemplate = basicTemplateService.getById(templateId);
        if (Objects.isNull(basicTemplate)) {
            throw ExceptionResultEnum.ERROR.exception("印品数据有误");
        }
        PdfSignDto pdfFillDto = new PdfSignDto();
        pdfFillDto.setTitle(schoolName + "签到表");
        pdfFillDto.setPackageNumber(examDetail.getPackageCode());
        pdfFillDto.setTextDesc(basicTemplate.getTextDesc());

        JSONObject jsonObject = JSON.parseObject(basicTemplate.getDisplayRange());
        List<JSONObject> objectList = JSON.parseArray(jsonObject.getString("basic"), JSONObject.class).stream().filter(m -> m.getBoolean("enable")).collect(Collectors.toList());

        boolean isCourseHasTwo = objectList.stream().filter(m -> "courseCode".equals(m.getString("code")) || "courseName".equals(m.getString("code"))).count() == 2;
        boolean isDateHasTwo = objectList.stream().filter(m -> "examDate".equals(m.getString("code")) || "examTime".equals(m.getString("code"))).count() == 2;
        boolean isPlaceHasTwo = objectList.stream().filter(m -> "examPlace".equals(m.getString("code")) || "examRoom".equals(m.getString("code"))).count() == 2;

        // 基础信息
        List<Map<String, String>> basicPlate = new ArrayList<>();
        boolean isCourseFill = false;
        boolean isDateFill = false;
        boolean isPlaceFill = false;
        for (JSONObject object : objectList) {
            String code = object.getString("code");
            String name = object.getString("name");
            if ("courseCode".equals(code) || "courseName".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                if (isCourseHasTwo) {
                    if (isCourseFill) {
                        continue;
                    }
                    List<String> courseNames = examDetailCourseList.stream().map(m -> String.format("%s（%s）", m.getCourseName(), m.getCourseCode())).collect(Collectors.toList());
                    basicMap.put("code", "courseName");
                    basicMap.put("name", "课程名称");
                    basicMap.put("value", CollectionUtils.isNotEmpty(courseNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, courseNames) : "");
                    isCourseFill = true;
                } else {
                    List<String> courseNames = examDetailCourseList.stream().map(m -> "courseCode".equals(code) ? m.getCourseCode() : "courseName".equals(code) ? m.getCourseName() : "").collect(Collectors.toList());
                    basicMap.put("code", code);
                    basicMap.put("name", name);
                    basicMap.put("value", CollectionUtils.isNotEmpty(courseNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, courseNames) : "");
                }
                basicPlate.add(basicMap);
            } else if ("paperNumber".equals(code)) {
                List<String> paperNumbers = examDetailCourseList.stream().map(ExamDetailCourse::getPaperNumber).collect(Collectors.toList());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(paperNumbers) ? String.join(SystemConstant.COMMA_OF_CHINESE, paperNumbers) : "");
                basicPlate.add(basicMap);
            } else if ("examDate".equals(code) || "examTime".equals(code)) {
                String startDate = DateUtil.format(new Date(examDetail.getExamStartTime()), SystemConstant.DATE_PATTERN);
                String endDate = DateUtil.format(new Date(examDetail.getExamEndTime()), SystemConstant.DATE_PATTERN);
                String startTime = DateUtil.format(new Date(examDetail.getExamStartTime()), SystemConstant.TIME_PATTERN);
                String endTime = DateUtil.format(new Date(examDetail.getExamEndTime()), SystemConstant.TIME_PATTERN);

                Map<String, String> basicMap = new HashMap<>();
                if (isDateHasTwo) {
                    if (isDateFill) {
                        continue;
                    }
                    String examTime;
                    if (startDate.equals(endDate)) {
                        examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endTime;
                    } else {
                        examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endDate + " " + endTime;
                    }
                    basicMap.put("code", "examTime");
                    basicMap.put("name", "考试时间");
                    basicMap.put("value", examTime);
                    isDateFill = true;
                } else {
                    String examTime = "";
                    if ("examDate".equals(code)) {
                        if (startDate.equals(endDate)) {
                            examTime = startDate;
                        } else {
                            examTime = startDate + SystemConstant.HYPHEN + endDate;
                        }
                    } else if ("examTime".equals(code)) {
                        if (startDate.equals(endDate)) {
                            examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endTime;
                        } else {
                            examTime = startDate + " " + startTime + SystemConstant.HYPHEN + endDate + " " + endTime;
                        }
                    }
                    basicMap.put("code", code);
                    basicMap.put("name", name);
                    basicMap.put("value", examTime);
                }
                basicPlate.add(basicMap);
            } else if ("examPlace".equals(code) || "examRoom".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                if (isPlaceHasTwo) {
                    if (isPlaceFill) {
                        continue;
                    }
                    StringJoiner stringJoiner = new StringJoiner("");
                    if (StringUtils.isNotBlank(examDetail.getExamPlace())) {
                        stringJoiner.add(examDetail.getExamPlace());
                    }
                    if (StringUtils.isNotBlank(examDetail.getExamRoom())) {
                        stringJoiner.add(examDetail.getExamRoom());
                    }
                    basicMap.put("code", "examRoom");
                    basicMap.put("name", "考试地点");
                    basicMap.put("value", stringJoiner.toString());
                    isPlaceFill = true;
                } else {
                    StringJoiner stringJoiner = new StringJoiner("");
                    if ("examPlace".equals(code) && StringUtils.isNotBlank(examDetail.getExamPlace())) {
                        stringJoiner.add(examDetail.getExamPlace());
                    }
                    if ("examRoom".equals(code) && StringUtils.isNotBlank(examDetail.getExamRoom())) {
                        stringJoiner.add(examDetail.getExamRoom());
                    }
                    basicMap.put("code", code);
                    basicMap.put("name", name);
                    basicMap.put("value", stringJoiner.toString());
                }
                basicPlate.add(basicMap);
            } else if ("collegeName".equals(code)) {
                Set<String> collegeNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getCollegeName())).map(ExamStudentCourseDto::getCollegeName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(collegeNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, collegeNames) : "");
                basicPlate.add(basicMap);
            } else if ("majorName".equals(code)) {
                Set<String> majorNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getMajorName())).map(ExamStudentCourseDto::getMajorName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(majorNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, majorNames) : "");
                basicPlate.add(basicMap);
            } else if ("teachClazzName".equals(code)) {
                Set<String> teachClassNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getTeachClazzName())).map(ExamStudentCourseDto::getTeachClazzName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(teachClassNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, teachClassNames) : "");
                basicPlate.add(basicMap);
            } else if ("clazzName".equals(code)) {
                Set<String> classNames = examStudentList.stream().filter(m -> StringUtils.isNotBlank(m.getClazzName())).map(ExamStudentCourseDto::getClazzName).collect(Collectors.toSet());
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", CollectionUtils.isNotEmpty(classNames) ? String.join(SystemConstant.COMMA_OF_CHINESE, classNames) : "");
                basicPlate.add(basicMap);
            } else if ("examCount".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", String.valueOf(examStudentList.size()));
                basicPlate.add(basicMap);
            } else if ("actualExamCount".equals(code)) {
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", "");
                basicPlate.add(basicMap);
            } else {
                // 扩展字段走本校验，进行数据组装
                Set<String> extendFieldsSet = examStudentList.stream().map(ExamStudentCourseDto::getExtendFields).collect(Collectors.toSet());
                Set<String> finalSet = new HashSet<>();
                for (String extendField : extendFieldsSet) {
                    List<JSONObject> jsonObjects = JSON.parseArray(extendField, JSONObject.class);
                    Set<String> values = jsonObjects.stream().filter(m -> code.equals(m.getString("code"))).map(m -> m.getString("value")).collect(Collectors.toSet());
                    finalSet.addAll(values);
                }
                Map<String, String> basicMap = new HashMap<>();
                basicMap.put("code", code);
                basicMap.put("name", name);
                basicMap.put("value", String.join(SystemConstant.COMMA_OF_CHINESE, finalSet));
                basicPlate.add(basicMap);
            }
        }
        pdfFillDto.setBasicPlate(basicPlate);

        // 表头信息
        List<JSONObject> tableList = JSON.parseArray(jsonObject.getString("table"), JSONObject.class).stream().filter(m -> m.getBoolean("enable")).collect(Collectors.toList());
        Map<String, String> studentHeadPlateMap = new LinkedHashMap<>();
        for (JSONObject object : tableList) {
            studentHeadPlateMap.put(object.getString("code"), object.getString("name"));
        }
        studentHeadPlateMap.put("studentSign", "签名");
        pdfFillDto.setStudentHeadPlate(studentHeadPlateMap);

        // 考生信息
        List<Map<String, String>> studentPlate = new ArrayList<>();
        for (ExamStudentCourseDto examStudentCourseDto : examStudentList) {
            Map<String, String> studentMap = new LinkedHashMap<>();
            if (studentHeadPlateMap.containsKey("siteNumber")) {
                studentMap.put("siteNumber", examStudentCourseDto.getSiteNumber());
            }
            if (studentHeadPlateMap.containsKey("studentName")) {
                studentMap.put("studentName", examStudentCourseDto.getStudentName());
            }
            if (studentHeadPlateMap.containsKey("studentCode")) {
                studentMap.put("studentCode", examStudentCourseDto.getStudentCode());
            }
            if (studentHeadPlateMap.containsKey("courseName")) {
                studentMap.put("courseName", examStudentCourseDto.getCourseName());
            }
            if (studentHeadPlateMap.containsKey("clazzName")) {
                studentMap.put("clazzName", examStudentCourseDto.getClazzName());
            }
            if (studentHeadPlateMap.containsKey("teachClazzName")) {
                studentMap.put("teachClazzName", examStudentCourseDto.getTeachClazzName());
            }
            if (studentHeadPlateMap.containsKey("ticketNumber")) {
                studentMap.put("ticketNumber", examStudentCourseDto.getTicketNumber());
            }
            if (studentHeadPlateMap.containsKey("studentSign")) {
                studentMap.put("studentSign", "");
            }
            studentPlate.add(studentMap);
        }
        pdfFillDto.setStudentPlate(studentPlate);

        printCommonService.saveAttachmentSignPdf(pdfFillDto, examDetail, variablePdfList, printCount, fileTempList, basicTemplate);
    }

    /**
     * 替换html通用模版中的条码信息
     *
     * @param content 题卡详细内容
     */
    public String resetHtmlTemplateBar(String content) {
        content = content.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
        content = content.replaceAll("\\$\\{examNumberStr\\}", "");
        return content;
    }

    /**
     * 替换自定义题卡参数
     *
     * @param examCard
     * @return
     */
    public String replaceHtmlCardAllParams(ExamCard examCard, BasicCardRule basicCardRule) {
        //通用题卡
        String cardTemp = examCard.getHtmlContent();
        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", "");
        //随机生成试卷条码并将图片转成base64
        cardTemp = cardTemp.replaceAll("\\$\\{paperType\\}", "");
        cardTemp = cardTemp.replaceAll("<img src=\"data:image/png;base64,\\$\\{examNumber\\}\">", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examNumberStr\\}", "");
        if (Objects.nonNull(basicCardRule) && Objects.nonNull(basicCardRule.getExtendFields())) {
            JSONArray jsonObjectExtend = (JSONArray) JSONArray.parse(basicCardRule.getExtendFields());//扩展字段
            if (Objects.nonNull(jsonObjectExtend)) {
                for (int i = 0; i < jsonObjectExtend.size(); i++) {
                    JSONObject object = (JSONObject) jsonObjectExtend.get(i);
                    cardTemp = cardTemp.replaceAll("\\$\\{" + object.get("code") + "\\}", "");
                }
            }
        }
        cardTemp = cardTemp.replaceAll("\\$\\{examDate\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examTime\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{ticketNumber\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{siteNumber\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{studentCode\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{studentName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{courseName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{courseCode\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examPlace\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examRoom\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{paperNumber\\}", "");

        // 根据题卡规则必选字段，替换相应值，没有则“”
        List<StudentExtendDto> studentExtendDtos = createExtendObjectNull(basicCardRule);
        if (studentExtendDtos != null) {
            for (StudentExtendDto extendDto : studentExtendDtos) {
                cardTemp = cardTemp.replaceAll("\\$\\{" + extendDto.getFieldName() + "\\}", String.valueOf(extendDto.getValue()));
            }
        }

        return cardTemp;
    }

    /**
     * 获取题卡attachmentId
     *
     * @param examCard
     * @param attachmentIds
     */
    public void getCardAttachmentId(ExamCard examCard, Set<Long> attachmentIds) {
        if (Objects.nonNull(examCard.getBackupAttachment()) && !Objects.equals("", examCard.getBackupAttachment())) {
            JSONObject jsonObjectCard = JSONObject.parseObject(examCard.getBackupAttachment());
            JSONArray jsonArrayCard = (JSONArray) jsonObjectCard.get("card");
            for (int i = 0; i < jsonArrayCard.size(); i++) {
                JSONObject object = (JSONObject) jsonArrayCard.get(i);
                if (Objects.nonNull(object.get("attachmentId")) && !Objects.equals("", object.get("attachmentId"))) {
                    attachmentIds.add((Long) (object.get("attachmentId")));
                }
            }
        }
    }

    /**
     * 获取试卷pdf
     *
     * @param stuPaperType   考生卷型
     * @param paperPdfDto    抽取的试卷文件信息
     * @param pdfList        考生试卷集合
     * @param waterMarkNames 水印内容
     */
    public PdfDto getExamStudentPaperPdf(String stuPaperType, List<PaperPdfDto> paperPdfDto, List<PdfDto> pdfList, String[] waterMarkNames) throws IOException {
        Set<Integer> pagesList = new HashSet<>();
        if (!CollectionUtils.isEmpty(paperPdfDto)) {
            for (PaperPdfDto dto : paperPdfDto) {
                if (stuPaperType.equals(dto.getType())) {
                    int pages = dto.getPages();
                    pagesList.add(pages);
                    PdfDto pdfDto = PdfUtil.addPdfPage(dto.getFile());
                    File file = paperPdfDto.size() > 1 ? PdfUtil.addWaterMark(dto.getFile(), waterMarkNames, 0.5f, 12, 0) : dto.getFile();
                    pdfList.add(new PdfDto(file.getPath(), dto.getPageSize(), pdfDto.getPageCount()));
                }
            }
            int pageCount = pagesList.stream().mapToInt(Integer::intValue).sum();
            return new PdfDto(paperPdfDto.get(0).getPageSize(), pageCount);
        } else {
            return null;
        }
    }

    /**
     * 获取试卷pdf
     *
     * @param paperPdfDto 抽取的试卷文件信息
     * @param backupCount 备份数量
     * @param pdfList     备份试卷集合
     */
    public PdfDto getPaperPdf(List<PaperPdfDto> paperPdfDto, Integer backupCount, List<PdfDto> pdfList) throws
            IOException {
        Set<Integer> pagesList = new HashSet<>();
        boolean tag = false;
        if (!CollectionUtils.isEmpty(paperPdfDto)) {
            for (PaperPdfDto dto : paperPdfDto) {
                int pages = dto.getPages();
                pagesList.add(pages);
                tag = pages > 2;
                PdfDto pdfDto = PdfUtil.addPdfPage(dto.getFile());
                for (int j = 1; j <= backupCount; j++) {
                    pdfList.add(new PdfDto(dto.getFile().getPath(), dto.getPageSize(), pdfDto.getPageCount()));
                }
            }
            int pageCount = pagesList.stream().mapToInt(Integer::intValue).sum();
            return new PdfDto(paperPdfDto.get(0).getPageSize(), pageCount, tag);
        } else {
            return null;
        }
    }

    /**
     * 获取考试试卷
     *
     * @param paperType      抽取的试卷型（多个用,分隔）
     * @param examTaskDetail 命题任务上传试卷信息
     * @param fileTempList
     */
    public List<PaperPdfDto> getPaperPdfFile(String paperType, ExamTaskDetail examTaskDetail, List<File> fileTempList) throws Exception {
        String[] paperTypes = paperType.split(",");
        List<PaperPdfDto> paperPdfDtoList = new ArrayList<>();
        PaperPdfDto paperPdfDto;
        List<PaperInfoVo> paperInfoVoList = ExamTaskUtil.parsePaperAttachmentPath(examTaskDetail.getPaperAttachmentIds());
        for (PaperInfoVo paperInfoVo : paperInfoVoList) {
            String attachmentId = paperInfoVo.getAttachmentId();
            if (StringUtils.isNotBlank(attachmentId)) {
                BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
                Optional.ofNullable(basicAttachment).orElseThrow(() -> ExceptionResultEnum.ATTACHMENT_IS_NULL.exception("所在路径不存在"));

                String name = paperInfoVo.getName();
                OriginalVo original = paperInfoVo.getOriginal();
                // 返回抽中的试卷
                for (String type : paperTypes) {
                    if (Objects.equals(name.toUpperCase(), type.toUpperCase())) {
                        File file = teachcloudCommonService.getFile(basicAttachment.getPath(), false, fileTempList);
                        int pages = original.getPages();

                        PageSizeEnum pageSizeEnum = PageSizeEnum.valueOf(original.getPageSize());
                        Optional.ofNullable(pageSizeEnum).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未设置pdf格式"));

                        paperPdfDto = new PaperPdfDto(type, file, pages, pageSizeEnum);
                        paperPdfDtoList.add(paperPdfDto);
                        break;
                    }
                }
            }
        }
        return paperPdfDtoList;
    }

    /**
     * 抽取试卷
     *
     * @param drawRule
     * @param examTaskDetail
     * @param key
     * @return
     */
    public String getPaperType(DrawRuleEnum drawRule, ExamTaskDetail examTaskDetail, String key, String specifyPaperType) {
        String paperType = null;
        if (Objects.nonNull(specifyPaperType)) {
            CreatePdfCacheUtil.setPaperType(key, paperType);
            return specifyPaperType;
        }
        //抽取卷型
        drawRule = Objects.nonNull(drawRule) ? drawRule : DrawRuleEnum.ONE;
        //未曝光卷型
        String unexposedPaperType = examTaskDetail.getUnexposedPaperType();
        //已曝光卷型
        String exposedPaperType = examTaskDetail.getExposedPaperType();

        if (drawRule == DrawRuleEnum.ONE) {
            if (Objects.isNull(unexposedPaperType) || Objects.equals(unexposedPaperType.trim(), "")) {
//                CreatePdfCacheUtil.deletePaperType(key);
                throw ExceptionResultEnum.PAPER_ERROR.exception();
            }
        } else {
            if ((Objects.isNull(exposedPaperType) || Objects.equals(exposedPaperType.trim(), "")) && (Objects.isNull(unexposedPaperType) || Objects.equals(unexposedPaperType.trim(), ""))) {
//                CreatePdfCacheUtil.deletePaperType(key);
                throw ExceptionResultEnum.PAPER_ERROR.exception();
            }
        }
        // 单次抽取套数
        int drawCount = examTaskDetail.getDrawCount().intValue();

        String[] paperTypes;
        boolean lock = true;
        for (int i = 0; i < SystemConstant.MAX_RETRY_COUNT; i++) {
            lock = redisUtil.lock(key, SystemConstant.REDIS_CACHE_TIME_OUT);
            if (lock) {
                try {
                    paperType = CreatePdfCacheUtil.getPaperType(key);
                    if (StringUtils.isBlank(paperType)) {
                        if (drawRule == DrawRuleEnum.ONE) {
                            paperTypes = unexposedPaperType.split(",");
                            if (paperTypes.length - drawCount < 0) {
                                throw ExceptionResultEnum.ERROR.exception("没有可用卷型");
                            }
                        } else {
                            if (Objects.nonNull(unexposedPaperType)) {
                                paperTypes = unexposedPaperType.split(",");
                            } else {
                                paperTypes = exposedPaperType.split(",");
                            }
                        }
                        paperType = randomPaperType(paperTypes, exposedPaperType, drawCount);
                        CreatePdfCacheUtil.setPaperType(key, paperType);
                    }
                    break;
                } catch (Exception e) {
                    log.error(SystemConstant.LOG_ERROR, e);
                } finally {
                    redisUtil.releaseLock(key);
                }
            } else {//未获取到锁，等待500毫秒继续获取
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    log.error(SystemConstant.LOG_ERROR, e);
                }
            }
        }
        log.info("getPaperType key:{},paperType:{},lock:{}", key, paperType, lock);
        if (!lock) {
            log.info("getPaperType 未获取到锁,key:{}", key);
            throw ExceptionResultEnum.PAPER_TYPE_ERROR.exception();
        }
        return paperType;
    }

    private String randomPaperType(String[] unexposedPaperTypes, String exposedPaperTypes, int drawCount) {
        List<String> finalTypes = new ArrayList<>();
        List<String> unexposedPaperTypesList = new ArrayList<>(Arrays.asList(unexposedPaperTypes));
        List<String> exposedPaperTypesList = StringUtils.isBlank(exposedPaperTypes) ? new ArrayList<>() : new ArrayList<>(Arrays.asList(exposedPaperTypes.split(",")));
        if (unexposedPaperTypesList.size() - drawCount >= 0) {
            for (int i = 0; i < drawCount; i++) {
                int paperRandom = new Random().nextInt(unexposedPaperTypesList.size());
                finalTypes.add(unexposedPaperTypesList.get(paperRandom));
                unexposedPaperTypesList.remove(paperRandom);
            }

        } else {
            for (int i = 0; i < unexposedPaperTypesList.size(); i++) {
                int paperRandom = new Random().nextInt(unexposedPaperTypesList.size());
                finalTypes.add(unexposedPaperTypesList.get(paperRandom));
                unexposedPaperTypesList.remove(paperRandom);
            }
            for (int i = 0; i < drawCount - unexposedPaperTypesList.size(); i++) {
                int paperRandom = new Random().nextInt(exposedPaperTypesList.size());
                finalTypes.add(exposedPaperTypesList.get(paperRandom));
                exposedPaperTypesList.remove(paperRandom);
            }
        }
        finalTypes = finalTypes.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
        return String.join(",", finalTypes);
    }

    /**
     * 合并A3 pdf
     *
     * @param fileTempList
     * @param list
     * @return
     * @throws IOException
     */
    public String mergeA3Pdf(List<File> fileTempList, List<PdfDto>... list) throws IOException {
        List<PdfDto> mergePdfA3List = new ArrayList<>();
        for (int i = 0; i < list.length; i++) {
            mergePdfA3List.addAll(list[i]);
        }
        List<String> pathA3List = mergePdfA3List.stream().map(PdfDto::getPath).collect(Collectors.toList());
        return PdfUtil.mergePdf(pathA3List.toArray(new String[mergePdfA3List.size()]), null, fileTempList);
    }

    /**
     * 合并A4 pdf
     *
     * @param list
     * @return
     * @throws IOException
     */
    public String mergeA4Pdf(List<File> fileTempList, List<PdfDto>... list) throws IOException {
        List<PdfDto> mergePdfA4List = new ArrayList<>();
        for (int i = 0; i < list.length; i++) {
            mergePdfA4List.addAll(list[i]);
        }
        List<String> pathA4List = mergePdfA4List.stream().map(PdfDto::getPath).collect(Collectors.toList());
        return PdfUtil.mergePdf(pathA4List.toArray(new String[mergePdfA4List.size()]), null, fileTempList);
    }

    /**
     * 合并pdf后保存数据库
     *
     * @param dirNamePaper
     * @param dirNameCardA3
     * @param tbTask
     * @param userId
     * @param examDetail
     * @param basicPrintConfig
     * @param fileTempList
     * @return
     * @throws IOException
     */
    @Transactional
    public BasicAttachment mergePdfSaveDb(String dirNamePaper,
                                          String dirNameCardA3,
                                          TBTask tbTask,
                                          Long userId,
                                          ExamDetail examDetail,
                                          BasicPrintConfig basicPrintConfig,
                                          List<File> fileTempList) throws Exception {
        File localA4PdfFile = null, localPaperPdfFile = null, localA3PdfCardFile = null;
        BasicAttachment basicAttachment = null;
        // 试卷+题卡
        if (StringUtils.isNotBlank(dirNamePaper)) {
            dirNamePaper = FileUtil.replaceSplit(dirNamePaper);
            localPaperPdfFile = new File(dirNamePaper);
            examDetail.setPaperPages(PdfUtil.getPdfPages(localPaperPdfFile));
            basicAttachment = basicAttachmentService.saveAttachmentPdf(dirNamePaper, userId);
            if (StringUtils.isBlank(tbTask.getImportFileName())) {
                tbTask.setImportFileName(basicAttachment.getName());
            }
            if (StringUtils.isBlank(tbTask.getImportFilePath())) {
                tbTask.setImportFilePath(basicAttachment.getPath());
            }
            examDetail.setAttachmentId(basicAttachment.getId());
        }
        // 题卡
        if (StringUtils.isNotBlank(dirNameCardA3)) {
            localA3PdfCardFile = new File(FileUtil.replaceSplit(dirNameCardA3));
            examDetail.setCardPages(PdfUtil.getPdfPages(localA3PdfCardFile));
            basicAttachment = basicAttachmentService.saveAttachmentPdf(dirNameCardA3, userId);
            if (StringUtils.isBlank(tbTask.getImportFileName())) {
                tbTask.setImportFileName(basicAttachment.getName());
            }
            if (StringUtils.isBlank(tbTask.getImportFilePath())) {
                tbTask.setImportFilePath(basicAttachment.getPath());
            }
            examDetail.setCardAttachmentId(basicAttachment.getId());
        }
//        examDetail.setPagesA4(PdfUtil.getPdfPages(localA4PdfFile));
        if (PrintMethodEnum.AUTO == basicPrintConfig.getPrintMethod()) {
            examDetail.setStatus(ExamDetailStatusEnum.WAITING);
        } else {
            examDetail.setStatus(ExamDetailStatusEnum.READY);
        }
        examDetail.setNormal(true);
        detailService.saveOrUpdate(examDetail);
        return basicAttachment;
    }

    /**
     * 考生题卡html
     *
     * @param studentContent     题卡html内容
     * @param examStudent        考生对象
     * @param examDetail         考场对象
     * @param examDetailCourse   考场课程对象
     * @param userId             当前用户ID
     * @param examStudentPdfList 考生题卡集合
     * @param basicCardRule      题卡规则
     */
    public BasicAttachment examStudentHtml(String studentContent, ExamStudent examStudent, String
            paperType, ExamDetail examDetail, ExamDetailCourse examDetailCourse,
                                           Long userId, List<PdfDto> examStudentPdfList, BasicCardRule basicCardRule,
                                           List<File> fileTempList) throws IOException {
        String studentHtml = studentContent;

        if (ExamNumberStyleEnum.PRINT.equals(basicCardRule.getExamNumberStyle())) {
            // 生成学生考号条码并将图片转成base64
            studentHtml = studentHtml.replaceAll("\\$\\{studentCodeBarcode\\}", examStudent != null && examStudent.getStudentCode() != null ? GoogleBarCodeUtil.createBarCode(examStudent.getStudentCode(), false) : "");
            studentHtml = studentHtml.replaceAll("\\$\\{ticketNumberBarcode\\}", examStudent != null && examStudent.getTicketNumber() != null ? GoogleBarCodeUtil.createBarCode(examStudent.getTicketNumber(), false) : "");
            studentHtml = studentHtml.replaceAll("\\$\\{examNumber\\}", examStudent != null && examStudent.getStudentCode() != null ? GoogleBarCodeUtil.createBarCode(examStudent.getStudentCode(), false) : "");
            studentHtml = studentHtml.replaceAll("\\$\\{examNumberStr\\}", examStudent != null && examStudent.getStudentCode() != null ? examStudent.getStudentCode() : "");
            // 随机生成学生试卷条码并将图片转成base64
            studentHtml = studentHtml.replaceAll("\\$\\{paperType\\}", examStudent != null && examStudent.getPaperType() != null ? GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(examStudent.getPaperType()), false) : StringUtils.isNotBlank(paperType) ? GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false) : "");
            // 替换考生卷型
            studentHtml = studentHtml.replaceAll("\\$\\{paperTypeName\\}", examStudent != null && examStudent.getPaperType() != null ? examStudent.getPaperType() : StringUtils.isNotBlank(paperType) ? paperType : "");

            if (examStudent != null) {
                // 根据题卡规则必选字段，替换相应值，没有则“”
                List<StudentExtendDto> studentExtendDtos = createExtendObject(examDetail, examStudent, basicCardRule);
                if (studentExtendDtos != null) {
                    for (StudentExtendDto extendDto : studentExtendDtos) {
                        studentHtml = studentHtml.replaceAll("\\$\\{" + extendDto.getFieldName() + "\\}", String.valueOf(extendDto.getValue()));
                    }
                }
            }
        }

        studentHtml = studentHtml.replaceAll("\\$\\{packageCodeDom\\}", "");

        // 将其它未匹配到值的参数，填入空值
        studentHtml = studentHtml.replaceAll("\\$\\{[A-Za-z0-9]+\\}", "");

        //学生题卡
        BasicAttachment examStudentAttachment = printCommonService.saveAttachmentHtml(examDetail.getSchoolId() + "|" + examDetailCourse.getCourseCode() + (examStudent != null && examStudent.getTicketNumber() != null ? "|" + examStudent.getTicketNumber() : ""), studentHtml, userId, examStudentPdfList, fileTempList);
        if (examStudent != null) {
            examStudent.setAttachmentId(examStudentAttachment.getId());
        }
        return examStudentAttachment;
    }

    /**
     * 题卡规则字段
     *
     * @param examDetail
     * @param examStudent   考生对象
     * @param basicCardRule 题卡规则对象
     */
    private List<StudentExtendDto> createExtendObject(ExamDetail examDetail, ExamStudent examStudent, BasicCardRule
            basicCardRule) {
        ExamStudentPdfInfoDto examStudentPdfInfoDto = examStudent == null ? null : examStudentService.getByStudentId(examStudent.getId());
        if (basicCardRule == null) {
            return null;
        }
        List<JSONObject> requiredFieldsJson = JSONArray.parseArray(basicCardRule.getRequiredFields(), JSONObject.class);
        List<StudentExtendDto> studentExtendDtos = new ArrayList<>();
        if (requiredFieldsJson.size() > 0) {
            for (JSONObject jsonObject : requiredFieldsJson) {
                StudentExtendDto studentExtendDto = new StudentExtendDto();
                String code = String.valueOf(jsonObject.get("code"));
                studentExtendDto.setFieldName(code);
                Object value = examStudentPdfInfoDto == null ? null : ReflectUtil.getFieldValue(examStudentPdfInfoDto, code);
                studentExtendDto.setValue(value == null ? "" : value);
                studentExtendDtos.add(studentExtendDto);
            }
        }
        List<JSONObject> extendFieldsJson = JSONArray.parseArray(basicCardRule.getExtendFields(), JSONObject.class);
        if (extendFieldsJson.size() > 0) {
            Map<String, Object> studentExtendMap = null;
            if (examStudentPdfInfoDto != null && StringUtils.isNotBlank(examStudentPdfInfoDto.getExtendFields())) {
                studentExtendMap = JSONArray.parseArray(examStudentPdfInfoDto.getExtendFields(), JSONObject.class).stream().collect(Collectors.toMap(k -> String.valueOf(k.get("code")), v -> v.get("value")));
            }

            Map<String, Object> map = ConvertUtil.analyzeDateAndTime(examDetail.getExamStartTime(), examDetail.getExamEndTime());

            for (JSONObject jsonObject : extendFieldsJson) {
                boolean enable = Boolean.parseBoolean(jsonObject.get("enable").toString());
                StudentExtendDto studentExtendDto = new StudentExtendDto();
                String code = String.valueOf(jsonObject.get("code"));

                studentExtendDto.setFieldName(code);
                Object value = examStudentPdfInfoDto == null ? null : ReflectUtil.getFieldValue(examStudentPdfInfoDto, code);
                // 时间需要特殊处理
                if ("examDate".equals(code)) {
                    value = Objects.nonNull(map.get("date")) ? (String) map.get("date") : "";
                }
                if ("examTime".equals(code)) {
                    value = Objects.nonNull(map.get("time")) ? (String) map.get("time") : "";
                }
                if (value == null) {
                    value = studentExtendMap == null ? null : studentExtendMap.get(code);
                }
                studentExtendDto.setValue(value == null ? "" : value);
                studentExtendDtos.add(studentExtendDto);
            }
        }
        return studentExtendDtos;
    }

    /**
     * 题卡规则字段
     *
     * @param basicCardRule 题卡规则对象
     */
    private List<StudentExtendDto> createExtendObjectNull(BasicCardRule basicCardRule) {
        if (basicCardRule == null) {
            return null;
        }
        List<JSONObject> requiredFieldsJson = JSONArray.parseArray(basicCardRule.getRequiredFields(), JSONObject.class);
        List<StudentExtendDto> studentExtendDtos = new ArrayList<>();
        if (requiredFieldsJson.size() > 0) {
            for (JSONObject jsonObject : requiredFieldsJson) {
                StudentExtendDto studentExtendDto = new StudentExtendDto();
                String code = String.valueOf(jsonObject.get("code"));
                studentExtendDto.setFieldName(code);
                studentExtendDto.setValue("");
                studentExtendDtos.add(studentExtendDto);
            }
        }
        List<JSONObject> extendFieldsJson = JSONArray.parseArray(basicCardRule.getExtendFields(), JSONObject.class);
        if (extendFieldsJson.size() > 0) {
            for (JSONObject jsonObject : extendFieldsJson) {
                StudentExtendDto studentExtendDto = new StudentExtendDto();
                String code = String.valueOf(jsonObject.get("code"));
                studentExtendDto.setFieldName(code);
                studentExtendDto.setValue("");
                studentExtendDtos.add(studentExtendDto);
            }
        }
        return studentExtendDtos;
    }

    /**
     * 通用题卡html
     *
     * @param sequence         序号
     * @param cardContent      题卡内容
     * @param examDetail       考场对象
     * @param examDetailCourse 考场课程对象
     * @param jsonArray        组装json返回
     * @param userId           当前用户ID
     * @param cardPdfList      备用题卡集合
     * @param basicCardRule    题卡规则对象
     * @param fileTempList
     */
    public BasicAttachment cardHtml(String sequence, String cardContent, ExamDetail examDetail, ExamDetailCourse examDetailCourse, String paperType, JSONArray jsonArray, Long userId, List<PdfDto> cardPdfList, BasicCardRule basicCardRule, List<File> fileTempList) throws
            IOException {
        //通用题卡
        String cardTemp = cardContent;
        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", paperType);
        //随机生成试卷条码并将图片转成base64
        if (Objects.nonNull(paperType)) {
            cardTemp = cardTemp.replaceAll("\\$\\{paperType\\}", GoogleBarCodeUtil.createBarCode(SystemConstant.convertPaperType(paperType), false));
        }
        if (Objects.nonNull(basicCardRule) && Objects.nonNull(basicCardRule.getExtendFields())) {
            JSONArray jsonObjectExtend = (JSONArray) JSONArray.parse(basicCardRule.getExtendFields());//扩展字段
            if (Objects.nonNull(jsonObjectExtend)) {
                int i = 0;
                while (i < jsonObjectExtend.size()) {
                    JSONObject object = (JSONObject) jsonObjectExtend.get(i);
                    cardTemp = cardTemp.replaceAll("\\$\\{" + object.get("code") + "\\}", "");
                    i++;
                }
            }
        }
        cardTemp = cardTemp.replaceAll("\\$\\{examDate\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examTime\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{ticketNumber\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{siteNumber\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{paperTypeName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{studentCode\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{studentName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{courseName\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{courseCode\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examPlace\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{examRoom\\}", "");
        cardTemp = cardTemp.replaceAll("\\$\\{paperNumber\\}", "");

        //通用题卡生成卷袋贴条码
        String packageCode = examDetail.getPackageCode() + sequence;
        String packageCodeImg = GoogleBarCodeUtil.createBarCode(packageCode, false);
        cardTemp = cardTemp.replaceAll("\\$\\{packageCodeDom\\}", "<img src='" + "data:image/png;base64," + packageCodeImg + "'><p>" + packageCode + "</p>");
//        cardTemp = cardTemp.replaceAll("\\$\\{packageCode\\}", packageCode);
        // 上传通卡
//        if (cardTemp.contains("<div class=\"page-box page-box-0\">")) {
//            String packageCodeDiv = "<div class=\"page-box page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 10px;right: 25%;margin-left: -100px;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + packageCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + packageCode + "</p></div>";
//            cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-0\">", packageCodeDiv);
//        } else if (cardTemp.contains("<div class=\"page-box page-box-A3 page-box-0\">")) {
//            String packageCodeDiv = "<div class=\"page-box page-box-A3 page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 10px;right: 25%;margin-left: -100px;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + packageCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + packageCode + "</p></div>";
//            cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-A3 page-box-0\">", packageCodeDiv);
//        } else if (cardTemp.contains("<div class=\"page-box page-box-A3 page-box-0 page-box-less\">")) {
//            String packageCodeDiv = "<div class=\"page-box page-box-A3 page-box-0 page-box-less\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 10px;right: 25%;margin-left: -100px;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + packageCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + packageCode + "</p></div>";
//            cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-A3 page-box-0 page-box-less\">", packageCodeDiv);
//        } else if (cardTemp.contains("<div class=\"page-box page-box-A4 page-box-0\">")) {
//            String packageCodeDiv = "<div class=\"page-box page-box-A4 page-box-0\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 10px;right: 25%;margin-left: -100px;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + packageCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + packageCode + "</p></div>";
//            cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-A4 page-box-0\">", packageCodeDiv);
//        } else if (cardTemp.contains("<div class=\"page-box page-box-A4 page-box-0 page-box-less\">")) {
//            String packageCodeDiv = "<div class=\"page-box page-box-A4 page-box-0 page-box-less\"><div class=\"package-number\" style=\"position: absolute;width: 200px;height: 40px;top: 10px;right: 25%;margin-left: -100px;text-align: center;z-index: 99;\"><img src=\"data:image/png;base64," + packageCodeImg + "\" style=\"display: block; height: 28px; width: 100%\" /><p style=\"line-height: 1; font-size: 12px; margin: 0;\">" + packageCode + "</p></div>";
//            cardTemp = cardTemp.replaceAll("<div class=\"page-box page-box-A4 page-box-0 page-box-less\">", packageCodeDiv);
//        }

        BasicAttachment cardAttachment = printCommonService.saveAttachmentHtml(examDetail.getSchoolId() + "|" + examDetailCourse.getCourseCode(), cardTemp, userId, cardPdfList, fileTempList);
        JSONObject object = new JSONObject();
        object.put("name", paperType);
        object.put("examDetailCourseId", examDetailCourse.getId());
        object.put("attachmentId", cardAttachment.getId());
        object.put("packageCode", packageCode);
        jsonArray.add(object);
        return cardAttachment;
    }
}
