package com.qmth.themis.admin.api;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.qmth.boot.core.solar.config.SolarProperties;
import com.qmth.boot.core.solar.service.SolarService;
import com.qmth.themis.admin.config.DictionaryConfig;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.AuthDto;
import com.qmth.themis.business.dto.response.RoomCodeQueryDto;
import com.qmth.themis.business.dto.response.TEExamQueryDto;
import com.qmth.themis.business.entity.*;
import com.qmth.themis.business.enums.DownloadFileEnum;
import com.qmth.themis.business.enums.InvigilateMonitorStatusEnum;
import com.qmth.themis.business.enums.RoleEnum;
import com.qmth.themis.business.enums.UploadFileEnum;
import com.qmth.themis.business.service.*;
import com.qmth.themis.business.util.*;
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 com.tencentcloudapi.vod.v20180717.models.MediaBasicInfo;
import com.tencentcloudapi.vod.v20180717.models.MediaInfo;
import com.tencentcloudapi.vod.v20180717.models.SearchMediaResponse;
import io.swagger.annotations.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Description: 系统信息 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/27
 */
@Api(tags = "系统信息Controller")
@RestController
@RequestMapping("/${prefix.url.admin}/sys")
public class SysController {

    @Resource
    TBPrivilegeService tbPrivilegeService;

    @Resource
    TBAttachmentService tbAttachmentService;

    @Resource
    TBTaskHistoryService tbTaskHistoryService;

    @Resource
    DictionaryConfig dictionaryConfig;

    @Resource
    OssUtil ossUtil;

    @Resource
    TBOrgService tbOrgService;

    @Resource
    TBRoleService tbRoleService;

    @Resource
    TEExamActivityService teExamActivityService;

    @Resource
    TEExamService teExamService;

    @Resource
    TBExamInvigilateUserService tbExamInvigilateUserService;

    @Resource
    TEExamStudentService teExamStudentService;

    @Resource
    CacheService cacheService;

    @Resource
    TOeExamRecordService tOeExamRecordService;

    @Resource
    TencentYunUtil tencentYunUtil;

    @Resource
    RedisUtil redisUtil;

    @Resource
    TMTencentVideoMessageService tencentVideoMessageService;

    @Resource
    AuthInfoService authInfoService;

    @Resource
    TIeInvigilateWarnInfoService tIeInvigilateWarnInfoService;

    @Resource
    SolarService solarService;

    @Resource
    SolarProperties solarProperties;

    @ApiOperation(value = "同步机构接口")
    @RequestMapping(value = "/sync/org", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "菜单信息", response = Result.class)})
    public Result syncOrg() {
        solarService.update(solarProperties.getAccessKey(), solarProperties.getAccessSecret());
        authInfoService.appInfoInit();
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "菜单查询接口")
    @RequestMapping(value = "/getMenu", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "菜单信息", response = TBPrivilege.class)})
    public Result getMenu() {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        if (Objects.isNull(tbUser)) {
            throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
        }
        return ResultUtil.ok(tbPrivilegeService.getMenu(tbUser.getId()));
    }

    @ApiOperation(value = "获取环境接口")
    @RequestMapping(value = "/env", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "环境信息", response = Result.class)})
    public Result env(@ApiParam(value = "机构id", required = true) @RequestParam Long orgId) {
        if (Objects.isNull(orgId) || Objects.equals(orgId, "")) {
            throw new BusinessException(ExceptionResultEnum.ORG_ID_IS_NULL);
        }
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "上传文件接口")
    @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
    @Transactional
    @ApiResponses({@ApiResponse(code = 200, message = "{\"id\":0}", response = Result.class)})
    public Result fileUpload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                             @ApiParam(value = "上传文件类型", required = true) @RequestParam UploadFileEnum type) throws IOException {
        if (Objects.isNull(file) || Objects.equals(file, "")) {
            throw new BusinessException(ExceptionResultEnum.ATTACHMENT_IS_NULL);
        }
        if (Objects.isNull(type) || Objects.equals(type, "")) {
            throw new BusinessException(ExceptionResultEnum.FILE_TYPE_IS_NULL);
        }
        Map<String, Object> mapParameter = null;
        if (type.getId() == UploadFileEnum.file.getId()) {
            throw new BusinessException("只能上传upload附件");
        }
        mapParameter = ossUtil.getAliYunOssPublicDomain().getMap();
        mapParameter.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.upload.name());
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        TBAttachment tbAttachment = tbAttachmentService
                .saveAttachment(file, ServletUtil.getRequestMd5(), ServletUtil.getRequestPath(), mapParameter, type,
                        tbUser.getOrgId(), tbUser.getId());
        if (Objects.isNull(tbAttachment)) {
            throw new BusinessException(ExceptionResultEnum.ATTACHMENT_ERROR);
        }
        JSONObject jsonObject = JSONObject.parseObject(tbAttachment.getRemark());
        String attachmentType = String.valueOf(jsonObject.get(SystemConstant.TYPE));
        String url = null;
        String filePath = (String) jsonObject.get(SystemConstant.PATH);
        if (Objects.equals(attachmentType, SystemConstant.LOCAL)) {
            url = "http://" + dictionaryConfig.sysDomain().getFileHost() + File.separator + filePath;
        } else {
            if (Objects.nonNull(mapParameter.get(SystemConstant.UPLOAD_TYPE))) {
                Integer fileId = UploadFileEnum.convertToId((String) mapParameter.get(SystemConstant.UPLOAD_TYPE));
                if (fileId.intValue() == 3) {
                    url = dictionaryConfig.aliYunOssDomain().getPrivateUrl() + File.separator + filePath;
                } else {
                    url = dictionaryConfig.aliYunOssDomain().getPublicUrl() + File.separator + filePath;
                }
            }
        }
        Map map = new HashMap();
        map.put(SystemConstant.ID, String.valueOf(tbAttachment.getId()));
        map.put(SystemConstant.NAME, tbAttachment.getName());
        map.put(SystemConstant.TYPE, tbAttachment.getType());
        map.put(SystemConstant.URL, url);
        return ResultUtil.ok(map);
    }

    @ApiOperation(value = "文件下载接口")
    @RequestMapping(value = "/file/download", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"url\":string}", response = Result.class)})
    public Result fileDownload(@ApiParam(value = "任务id", required = true) @RequestParam Long id,
                               @ApiParam(value = "下载文件类型", required = true) @RequestParam DownloadFileEnum type) {
        if (Objects.isNull(id) || Objects.equals(id, "")) {
            throw new BusinessException(ExceptionResultEnum.TASK_ID_IS_NULL);
        }
        if (Objects.isNull(type) || Objects.equals(type, "")) {
            throw new BusinessException(ExceptionResultEnum.FILE_TYPE_IS_NULL);
        }
        if (Objects.isNull(DownloadFileEnum.convertToName(type.getId()))) {
            throw new BusinessException(ExceptionResultEnum.DOWNLOAD_FILE_TYPE_ERROR);
        }
        TBTaskHistory tbTaskHistory = tbTaskHistoryService.getById(id);
        if (Objects.isNull(tbTaskHistory)) {
            throw new BusinessException(ExceptionResultEnum.TASK_NO);
        }
        boolean oss = dictionaryConfig.sysDomain().isOss();
        JSONObject jsonObject = null;
        switch (type.getId()) {
            case 0:
                jsonObject = JSONObject.parseObject(tbTaskHistory.getImportFilePath());
                break;
            case 1:
                jsonObject = JSONObject.parseObject(tbTaskHistory.getReportFilePath());
                break;
            default:
                jsonObject = JSONObject.parseObject(tbTaskHistory.getResultFilePath());
                break;
        }
        if (Objects.isNull(jsonObject) || Objects.isNull(jsonObject.get(SystemConstant.PATH))) {
            throw new BusinessException("下载文件地址不存在");
        }
        String filePath = (String) jsonObject.get(SystemConstant.PATH);
        String url = null;
        if (oss) {
            Integer uploadType = (Integer) jsonObject.get(SystemConstant.UPLOAD_TYPE);
            if (uploadType == UploadFileEnum.file.ordinal()) {
                url = ossUtil.getPrivateUrl(filePath);
            } else {
                url = dictionaryConfig.aliYunOssDomain().getPublicUrl() + File.separator + filePath;
            }
        } else {
            url = "http://" + dictionaryConfig.sysDomain().getFileHost() + File.separator + filePath;
        }
        return ResultUtil.ok(Collections.singletonMap(SystemConstant.URL, url));
    }

    @ApiOperation(value = "根据机构代码查询机构信息接口")
    @RequestMapping(value = "/org/queryByOrgCode", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "机构信息", response = TBOrg.class)})
    public Result queryByOrgCode(@ApiParam(value = "机构code", required = true) @RequestParam String code) {
        if (Objects.isNull(code) || Objects.equals(code, "")) {
            throw new BusinessException(ExceptionResultEnum.ORG_CODE_IS_NULL);
        }
        if (!Objects.equals(code.toUpperCase(), SystemConstant.ADMIN)) {
            TBOrg tbOrg = cacheService.addOrgCodeCache(code);
            Optional.ofNullable(tbOrg).orElseThrow(() -> new BusinessException(ExceptionResultEnum.ORG_NO));
            authInfoService.appHasExpired(code);
            return ResultUtil.ok(Collections.singletonMap("logo", tbOrg.getLogo()));
        } else {
            return ResultUtil.ok(Collections.singletonMap("logo", dictionaryConfig.sysDomain().getAdminLogoUrl()));
        }
    }

    @ApiOperation(value = "机构查询接口")
    @RequestMapping(value = "/org/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "机构信息", response = TBOrg.class)})
    public Result query(@ApiParam(value = "机构id", required = false) @RequestParam(required = false) Long id,
                        @ApiParam(value = "机构代码", required = false) @RequestParam(required = false) String code,
                        @ApiParam(value = "机构名称", required = false) @RequestParam(required = false) String name,
                        @ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Integer enable) {
        QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(id)) {
            tbOrgQueryWrapper.lambda().eq(TBOrg::getId, id);
        }
        if (Objects.nonNull(code)) {
            tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, code);
        }
        if (Objects.nonNull(name)) {
            tbOrgQueryWrapper.lambda().like(TBOrg::getName, name);
        }
        if (Objects.nonNull(enable)) {
            tbOrgQueryWrapper.lambda().eq(TBOrg::getEnable, enable);
        }
        tbOrgQueryWrapper.lambda().orderByAsc(TBOrg::getCode);
        return ResultUtil.ok(tbOrgService.list(tbOrgQueryWrapper));
    }

    @ApiOperation(value = "角色查询接口")
    @RequestMapping(value = "/role/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = TBRole.class)})
    @Cacheable(value = SystemConstant.roleCache, key = "methodName", unless = "#result == null")
    public Result roleList() {
        QueryWrapper<TBRole> tbRoleQueryWrapper = new QueryWrapper<>();
        tbRoleQueryWrapper.lambda().notIn(TBRole::getRoleCode, RoleEnum.SUPER_ADMIN.name(), RoleEnum.STUDENT.name());
        tbRoleQueryWrapper.lambda().orderByAsc(TBRole::getRoleCode);
        return ResultUtil.ok(tbRoleService.list(tbRoleQueryWrapper));
    }

    @ApiOperation(value = "考试场次查询接口")
    @RequestMapping(value = "/examActivity/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = TEExamActivity.class)})
    public Result examActivityQuery() {
        return ResultUtil.ok(teExamActivityService.list());
    }

    @ApiOperation(value = "考试批次查询接口")
    @RequestMapping(value = "/exam/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = TEExamQueryDto.class)})
    public Result examQuery(@ApiParam(value = "用户id") @RequestParam(required = false) Long userId,
                            @ApiParam(value = "批次名称") @RequestParam(required = false) String name,
                            @ApiParam(value = "类型(区分实时监考台和考务)") @RequestParam(required = false) String type,
                            @ApiParam(value = "是否查询预警消息条数") @RequestParam(required = false) Boolean warnCount) {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        AuthDto authDto = cacheService.addAccountAuthCache(tbUser.getId());
        List<TEExam> teExamList = null;
        if (authDto.getRoleCodes().toString().contains(RoleEnum.ADMIN.name()) || authDto.getRoleCodes().toString()
                .contains(RoleEnum.SUPER_ADMIN.name())) {//管理员
            QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
            teExamQueryWrapper.lambda().eq(TEExam::getOrgId, tbUser.getOrgId()).eq(TEExam::getEnable, 1);
            if (Objects.nonNull(name) && !Objects.equals(name, "")) {
                teExamQueryWrapper.lambda().like(TEExam::getName, name);
            }
            teExamQueryWrapper.lambda().orderByDesc(TEExam::getCreateTime);
            teExamList = teExamService.list(teExamQueryWrapper);
        } else {
            if (Objects.nonNull(userId) && !Objects.equals(userId, "")) {
                teExamList = teExamService.examPrivilegeQuery(userId, tbUser.getOrgId(), type);
            } else {
                QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
                teExamQueryWrapper.lambda().eq(TEExam::getOrgId, tbUser.getOrgId());
                if (Objects.nonNull(name) && !Objects.equals(name, "")) {
                    teExamQueryWrapper.lambda().like(TEExam::getName, name);
                }
                if (Objects.nonNull(type) && !Objects.equals(type, "") && Objects.equals(type, "monitor")) {
                    teExamQueryWrapper.lambda()
                            .ne(TEExam::getMonitorStatus, InvigilateMonitorStatusEnum.FINISHED.name());
                }
                teExamQueryWrapper.lambda().orderByDesc(TEExam::getCreateTime);
                teExamList = teExamService.list(teExamQueryWrapper);
            }
        }
        List<TEExamQueryDto> teExamQueryDtoList = null;
        if (!CollectionUtils.isEmpty(teExamList)) {
            Gson gson = new Gson();
            teExamQueryDtoList = gson.fromJson(gson.toJson(teExamList), new TypeToken<List<TEExamQueryDto>>() {
            }.getType());
            if (Objects.nonNull(warnCount) && warnCount) {
                List<Long> examIdList = new ArrayList<>(teExamQueryDtoList.size());
                Map<Long, TEExamQueryDto> map = new HashMap<>(teExamQueryDtoList.size());
                for (TEExamQueryDto t : teExamQueryDtoList) {
                    examIdList.add(t.getId());
                    map.computeIfAbsent(t.getId(), v -> t);
                }
                Long warnUserId = null, warnOrgId = null;
                if (authDto.getRoleCodes().toString().contains(RoleEnum.INSPECTION.name()) || authDto.getRoleCodes().toString()
                        .contains(RoleEnum.INVIGILATE.name())) {
                    warnUserId = tbUser.getId();
                    warnOrgId = tbUser.getOrgId();
                }
                List<TEExamQueryDto> warnCountList = tIeInvigilateWarnInfoService.warningCountByList(examIdList, warnUserId, warnOrgId);
                if (!CollectionUtils.isEmpty(warnCountList)) {
                    for (TEExamQueryDto t : warnCountList) {
                        map.get(t.getId()).setWarnCount(t.getWarnCount());
                    }
                    teExamQueryDtoList = new ArrayList<>(map.values());
                }
            }
        }
        return ResultUtil.ok(teExamQueryDtoList);
    }

    @ApiOperation(value = "考场查询接口")
    @RequestMapping(value = "/examRoom/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = RoomCodeQueryDto.class)})
    public Result examRoomQuery(
            @ApiParam(value = "考场名称", required = false) @RequestParam(required = false) String roomName) {
        return ResultUtil.ok(teExamStudentService.examRoomQuery(roomName));
    }

    @ApiOperation(value = "根据权限获取场次和考场接口")
    @RequestMapping(value = "/exam/privilegeQuery", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "场次和考场信息", response = Result.class)})
    public Result examPrivilegeQuery(@ApiParam(value = "考试id", required = true) @RequestParam Long examId) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
        if (Objects.isNull(tbSession)) {
            throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
        }
        //首先查询当前用户所要监控的roomCode
        QueryWrapper<TBExamInvigilateUser> examInvigilateUserQueryWrapper = new QueryWrapper<>();
        AuthDto authDto = cacheService.addAccountAuthCache(tbUser.getId());
        examInvigilateUserQueryWrapper.lambda().eq(TBExamInvigilateUser::getOrgId, tbUser.getOrgId())
                .eq(TBExamInvigilateUser::getExamId, examId);
        if (authDto.getRoleCodes().toString().contains(RoleEnum.INVIGILATE.name())) {
            examInvigilateUserQueryWrapper.lambda().eq(TBExamInvigilateUser::getUserId, tbUser.getId());
        }
        List<TBExamInvigilateUser> tbExamInvigilateUserList = tbExamInvigilateUserService
                .list(examInvigilateUserQueryWrapper);
        Set<String> roomCodeSet = null;
        if (Objects.nonNull(tbExamInvigilateUserList) && tbExamInvigilateUserList.size() > 0) {
            Map<String, TBExamInvigilateUser> tbExamInvigilateUserMap = tbExamInvigilateUserList.stream().collect(
                    Collectors.toMap(TBExamInvigilateUser::getRoomCode, Function.identity(), (dto1, dto2) -> dto1));
            roomCodeSet = new HashSet<>();
            tbExamInvigilateUserList.clear();
            Set<String> finalRoomCodeSet = roomCodeSet;
            List<TBExamInvigilateUser> finalTbExamInvigilateUserList = tbExamInvigilateUserList;
            tbExamInvigilateUserMap.forEach((k, v) -> {
                finalRoomCodeSet.add(k);
                finalTbExamInvigilateUserList.add(v);
            });
        } else {
            tbExamInvigilateUserList = null;
        }
        List<TEExamActivity> teExamActivityList = null;
        Set<Map> courseSet = null;
        if (Objects.nonNull(roomCodeSet)) {
            //根据roomCode获取当前老师所要监考的全部应考学生数
            QueryWrapper<TEExamStudent> teExamStudentQueryWrapper = new QueryWrapper<>();
            teExamStudentQueryWrapper.lambda().eq(TEExamStudent::getExamId, examId)
                    .in(TEExamStudent::getRoomCode, roomCodeSet);
            List<TEExamStudent> teExamStudentList = teExamStudentService.list(teExamStudentQueryWrapper);
            Set<Long> examActivityIdSet = null;
            if (Objects.nonNull(teExamStudentList) && teExamStudentList.size() > 0) {
                examActivityIdSet = new HashSet<>();
                courseSet = new HashSet<>();
                Set<Long> finalExamActivityIdSet = examActivityIdSet;
                Set<Map> finalCourseSet = courseSet;
                teExamStudentList.forEach(s -> {
                    finalExamActivityIdSet.add(s.getExamActivityId());
                    Map map = new HashMap();
                    map.put(SystemConstant.COURSE_CODE, s.getCourseCode());
                    map.put(SystemConstant.COURSE_NAME, s.getCourseName());
                    finalCourseSet.add(map);
                });
                teExamActivityList = teExamActivityService.listByIds(examActivityIdSet);
            }
        }
        Map map = new HashMap();
        map.put("examActivitys", teExamActivityList);
        map.put("examRooms", tbExamInvigilateUserList);
        map.put("examCourses", courseSet);
        return ResultUtil.ok(map);
    }

    @ApiOperation(value = "获取播放域接口")
    @RequestMapping(value = "/getPlayUrls", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "播放域接口", response = Result.class)})
    public Result getPlayUrls() {
        return ResultUtil.ok(dictionaryConfig.tencentYunDomain().getUrls());
    }

    @ApiOperation(value = "查询已监考结束的考试批次接口")
    @RequestMapping(value = "/exam/finish/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "角色信息", response = TEExamQueryDto.class)})
    public Result examFinishQuery() {
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
        teExamQueryWrapper.lambda().eq(TEExam::getMonitorStatus, InvigilateMonitorStatusEnum.FINISHED)
                .eq(TEExam::getEnable, 1);
        if (Objects.nonNull(tbUser)) {
            teExamQueryWrapper.lambda().eq(TEExam::getOrgId, tbUser.getOrgId());
        }
        List<TEExam> teExamList = teExamService.list(teExamQueryWrapper);
        List<TEExamQueryDto> teExamQueryDtoList = null;
        if (!CollectionUtils.isEmpty(teExamList)) {
            Gson gson = new Gson();
            teExamQueryDtoList = gson.fromJson(gson.toJson(teExamList), new TypeToken<List<TEExamQueryDto>>() {
            }.getType());
        }
        return ResultUtil.ok(teExamQueryDtoList);
    }

    @ApiOperation(value = "获取腾讯视频接口")
    @RequestMapping(value = "/get_tencent_video", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "腾讯视频接口", response = Result.class)})
    @Transactional
    public Result getTencentVideo(@ApiParam(value = "考试批次id", required = true) @RequestParam Long examId) {
        List<TOeExamRecord> tOeExamRecordList = tOeExamRecordService.findExamRecordByNotVideoUrl(examId);

        if (!CollectionUtils.isEmpty(tOeExamRecordList)) {
            for (TOeExamRecord t : tOeExamRecordList) {
                Map<String, Object> resultMap = tencentYunUtil.callTencentVideo(t.getId());
                if (!CollectionUtils.isEmpty(resultMap)) {
                    SearchMediaResponse response = (SearchMediaResponse) resultMap.get("object");
                    if (Objects.nonNull(response) && response.getTotalCount().intValue() > 0) {
                        MediaInfo[] mediaInfos = response.getMediaInfoSet();
                        for (int i = 0; i < mediaInfos.length; i++) {
                            MediaBasicInfo mediaBasicInfo = mediaInfos[i].getBasicInfo();
                            String mediaBasicName = mediaBasicInfo.getName();
                            String videoSource = SystemConstant.getMonitorRecordStreamId(mediaBasicName);
                            String[] timeArrays = mediaBasicName.split("_");
                            Long startTime = DateUtil.parse(timeArrays[6], SystemConstant.VIDEO_TIME_FORMAT).getTime();
                            Long endTime = DateUtil.parse(timeArrays[7], SystemConstant.VIDEO_TIME_FORMAT).getTime();
                            boolean lock = false;
                            for (int y = 0; y < SystemConstant.CONSUME_MESSAGE_BATCH_MAX_SIZE; y++) {
                                lock = redisUtil.lock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_PREFIX + videoSource, SystemConstant.REDIS_LOCK_TENCENT_VIDEO_TIME_OUT);
                                if (lock) {
                                    try {
                                        tencentVideoMessageService.saveAndCount(new TMTencentVideoMessage(response.getRequestId(), JacksonUtil.parseJson(response), t.getId(), videoSource, startTime, endTime, mediaBasicInfo.getMediaUrl()));
                                        break;
                                    } finally {
                                        redisUtil.releaseLock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_PREFIX + videoSource);
                                    }
                                } else {
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return ResultUtil.ok(true);
    }
}
