瀏覽代碼

删除视频优化

wangliang 2 年之前
父節點
當前提交
2347e0ed68
共有 1 個文件被更改,包括 127 次插入91 次删除
  1. 127 91
      themis-admin/src/main/java/com/qmth/themis/admin/api/SysController.java

+ 127 - 91
themis-admin/src/main/java/com/qmth/themis/admin/api/SysController.java

@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.google.common.reflect.TypeToken;
 import com.qmth.boot.core.solar.config.SolarProperties;
@@ -46,8 +47,6 @@ import javax.validation.constraints.Min;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
-import java.util.function.Function;
-import java.util.stream.Collectors;
 
 /**
  * @Description: 系统信息 前端控制器
@@ -591,48 +590,21 @@ public class SysController {
             Boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_PREFIX + examId, SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_TIME_OUT);
             if (lock) {
                 try {
-                    Set<Long> tencentVideoMessageIds = new HashSet<>(tmTencentVideoMessageList.size());
-                    QueryWrapper<TOeExamRecord> tOeExamRecordQueryWrapper = new QueryWrapper<>();
-                    tOeExamRecordQueryWrapper.lambda().eq(TOeExamRecord::getExamId, examId);
-                    List<TOeExamRecord> tOeExamRecordList = tOeExamRecordService.list(tOeExamRecordQueryWrapper);
-                    Map<Long, TOeExamRecord> tOeExamRecordMap = tOeExamRecordList.stream().collect(
-                            Collectors.toMap(TOeExamRecord::getId, Function.identity(), (dto1, dto2) -> dto1));
-                    for (TMTencentVideoMessage t : tmTencentVideoMessageList) {
-                        tencentVideoMessageIds.add(t.getId());
-                        TOeExamRecord tOeExamRecord = tOeExamRecordMap.get(t.getExamRecordId());
-                        if (Objects.nonNull(t.getObject()) && !Objects.equals(t.getObject().trim(), "")) {
-                            JSONObject jsonObject = JSONObject.parseObject(t.getObject());
-                            if (Objects.nonNull(jsonObject.get("mediaInfoSet")) && !Objects.equals(jsonObject.getString("mediaInfoSet").trim(), "")) {
-                                JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("mediaInfoSet"));
-                                for (int i = 0; i < jsonArray.size(); i++) {
-                                    JSONObject object = jsonArray.getJSONObject(i);
-                                    if (Objects.nonNull(object.get("fileId")) && !Objects.equals(object.getString("fileId").trim(), "")) {
-                                        tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
-                                                tencentYunUtil.getSecretKey(),
-                                                tencentYunUtil.getQueryUrl(),
-                                                "",
-                                                tencentYunUtil.getVodAppId(),
-                                                object.getString("fileId"));
-                                        if (Objects.nonNull(tOeExamRecord)) {
-                                            tOeExamRecord.setVideoStatus(true);
-                                        }
-                                    }
-                                }
-                            } else if (Objects.nonNull(jsonObject.get("file_id")) && !Objects.equals(jsonObject.getString("file_id").trim(), "")) {
-                                tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
-                                        tencentYunUtil.getSecretKey(),
-                                        tencentYunUtil.getQueryUrl(),
-                                        "",
-                                        tencentYunUtil.getVodAppId(),
-                                        jsonObject.getString("file_id"));
-                                if (Objects.nonNull(tOeExamRecord)) {
-                                    tOeExamRecord.setVideoStatus(true);
-                                }
-                            }
+                    int min = 0, max = SystemConstant.MAX_IMPORT_SIZE, size = tmTencentVideoMessageList.size();
+                    if (max >= size) {
+                        max = size;
+                    }
+                    while (max <= size) {
+                        deleteNewMedia(tmTencentVideoMessageList.subList(min, max));
+                        if (max == size) {
+                            break;
+                        }
+                        min = max;
+                        max += SystemConstant.MAX_IMPORT_SIZE;
+                        if (max >= size) {
+                            max = size;
                         }
                     }
-                    tmTencentVideoMessageService.removeByIds(tencentVideoMessageIds);
-                    tOeExamRecordService.updateBatchById(tOeExamRecordMap.values());
                 } finally {
                     redisUtil.releaseLock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_PREFIX + examId);
                 }
@@ -640,64 +612,31 @@ public class SysController {
                 throw new BusinessException("该考试批次正在删除视频,请稍候再试");
             }
         }
+
         //老考试视频
         QueryWrapper<TOeExamRecord> tOeExamRecordQueryWrapper = new QueryWrapper<>();
-        tOeExamRecordQueryWrapper.lambda().eq(TOeExamRecord::getExamId, examId)
-                .isNotNull(TOeExamRecord::getTencentVideoUrl);
+        tOeExamRecordQueryWrapper.lambda().eq(TOeExamRecord::getExamId, examId).isNotNull(TOeExamRecord::getTencentVideoUrl)
+                .and(w -> w.eq(TOeExamRecord::getVideoStatus, false).or().isNull(TOeExamRecord::getVideoStatus));
         List<TOeExamRecord> tOeExamRecordList = tOeExamRecordService.list(tOeExamRecordQueryWrapper);
         if (!CollectionUtils.isEmpty(tOeExamRecordList)) {
             Boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_PREFIX + examId, SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_TIME_OUT);
             if (lock) {
                 try {
-                    Map<Long, TMTencentVideoMessage> tmTencentVideoMessageMap = new HashMap<>();
-                    Set<Long> tencentVideoMessageIds = new HashSet<>();
-                    Map<Long, TOeExamRecord> tOeExamRecordMap = tOeExamRecordList.stream().collect(
-                            Collectors.toMap(TOeExamRecord::getId, Function.identity(), (dto1, dto2) -> dto1));
-                    for (TOeExamRecord t : tOeExamRecordList) {
-                        List<TEStudentMonitorRecordDto> monitorRecordList = GsonUtil.fromJson(t.getTencentVideoUrl(), new TypeToken<List<TEStudentMonitorRecordDto>>() {
-                        }.getType());
-                        for (TEStudentMonitorRecordDto teStudentMonitorRecordDto : monitorRecordList) {
-                            QueryWrapper<TMTencentVideoMessage> tencentVideoMessageQueryWrapper = new QueryWrapper<>();
-                            tencentVideoMessageQueryWrapper.lambda().like(TMTencentVideoMessage::getObject, teStudentMonitorRecordDto.getVideoSource());
-                            List<TMTencentVideoMessage> tencentVideoMessageList = tmTencentVideoMessageService.list(tencentVideoMessageQueryWrapper);
-                            for (TMTencentVideoMessage tencentVideoMessage : tencentVideoMessageList) {
-                                if (!tmTencentVideoMessageMap.containsKey(tencentVideoMessage.getId())) {
-                                    tmTencentVideoMessageMap.put(tencentVideoMessage.getId(), tencentVideoMessage);
-                                } else {
-                                    continue;
-                                }
-                                tencentVideoMessageIds.add(t.getId());
-                                if (Objects.nonNull(tencentVideoMessage.getObject()) && !Objects.equals(tencentVideoMessage.getObject().trim(), "")) {
-                                    JSONObject jsonObject = JSONObject.parseObject(tencentVideoMessage.getObject());
-                                    if (Objects.nonNull(jsonObject.get("mediaInfoSet")) && !Objects.equals(jsonObject.getString("mediaInfoSet").trim(), "")) {
-                                        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("mediaInfoSet"));
-                                        for (int i = 0; i < jsonArray.size(); i++) {
-                                            JSONObject object = jsonArray.getJSONObject(i);
-                                            if (Objects.nonNull(object.get("fileId")) && !Objects.equals(object.getString("fileId").trim(), "")) {
-                                                tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
-                                                        tencentYunUtil.getSecretKey(),
-                                                        tencentYunUtil.getQueryUrl(),
-                                                        "",
-                                                        tencentYunUtil.getVodAppId(),
-                                                        object.getString("fileId"));
-                                                t.setVideoStatus(true);
-                                            }
-                                        }
-                                    } else if (Objects.nonNull(jsonObject.get("file_id")) && !Objects.equals(jsonObject.getString("file_id").trim(), "")) {
-                                        tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
-                                                tencentYunUtil.getSecretKey(),
-                                                tencentYunUtil.getQueryUrl(),
-                                                "",
-                                                tencentYunUtil.getVodAppId(),
-                                                jsonObject.getString("file_id"));
-                                        t.setVideoStatus(true);
-                                    }
-                                }
-                            }
+                    int min = 0, max = SystemConstant.MAX_IMPORT_SIZE, size = tOeExamRecordList.size();
+                    if (max >= size) {
+                        max = size;
+                    }
+                    while (max <= size) {
+                        deleteOldMedia(tOeExamRecordList.subList(min, max));
+                        if (max == size) {
+                            break;
+                        }
+                        min = max;
+                        max += SystemConstant.MAX_IMPORT_SIZE;
+                        if (max >= size) {
+                            max = size;
                         }
                     }
-                    tmTencentVideoMessageService.removeByIds(tencentVideoMessageIds);
-                    tOeExamRecordService.updateBatchById(tOeExamRecordMap.values());
                 } finally {
                     redisUtil.releaseLock(SystemConstant.REDIS_LOCK_TENCENT_VIDEO_DELETE_PREFIX + examId);
                 }
@@ -715,4 +654,101 @@ public class SysController {
         teExamService.updateExamCacheBean(examId);
         return ResultUtil.ok(true);
     }
+
+    /**
+     * 删除老考试视频
+     *
+     * @param tOeExamRecordList
+     */
+    private void deleteOldMedia(List<TOeExamRecord> tOeExamRecordList) {
+        Set<Long> tencentVideoMessageIds = new HashSet<>(tOeExamRecordList.size());
+        Map<Long, TMTencentVideoMessage> tmTencentVideoMessageMap = new HashMap<>(tOeExamRecordList.size());
+        for (TOeExamRecord t : tOeExamRecordList) {
+            List<TEStudentMonitorRecordDto> monitorRecordList = GsonUtil.fromJson(t.getTencentVideoUrl(), new TypeToken<List<TEStudentMonitorRecordDto>>() {
+            }.getType());
+            for (TEStudentMonitorRecordDto teStudentMonitorRecordDto : monitorRecordList) {
+                QueryWrapper<TMTencentVideoMessage> tencentVideoMessageQueryWrapper = new QueryWrapper<>();
+                tencentVideoMessageQueryWrapper.lambda().like(TMTencentVideoMessage::getObject, teStudentMonitorRecordDto.getVideoSource());
+                List<TMTencentVideoMessage> tencentVideoMessageList = tmTencentVideoMessageService.list(tencentVideoMessageQueryWrapper);
+                for (TMTencentVideoMessage tencentVideoMessage : tencentVideoMessageList) {
+                    if (!tmTencentVideoMessageMap.containsKey(tencentVideoMessage.getId())) {
+                        tmTencentVideoMessageMap.put(tencentVideoMessage.getId(), tencentVideoMessage);
+                    } else {
+                        continue;
+                    }
+                    tencentVideoMessageIds.add(t.getId());
+                    if (Objects.nonNull(tencentVideoMessage.getObject()) && !Objects.equals(tencentVideoMessage.getObject().trim(), "")) {
+                        JSONObject jsonObject = JSONObject.parseObject(tencentVideoMessage.getObject());
+                        if (Objects.nonNull(jsonObject.get("mediaInfoSet")) && !Objects.equals(jsonObject.getString("mediaInfoSet").trim(), "")) {
+                            JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("mediaInfoSet"));
+                            for (int i = 0; i < jsonArray.size(); i++) {
+                                JSONObject object = jsonArray.getJSONObject(i);
+                                if (Objects.nonNull(object.get("fileId")) && !Objects.equals(object.getString("fileId").trim(), "")) {
+                                    tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
+                                            tencentYunUtil.getSecretKey(),
+                                            tencentYunUtil.getQueryUrl(),
+                                            "",
+                                            tencentYunUtil.getVodAppId(),
+                                            object.getString("fileId"));
+                                    t.setVideoStatus(true);
+                                }
+                            }
+                        } else if (Objects.nonNull(jsonObject.get("file_id")) && !Objects.equals(jsonObject.getString("file_id").trim(), "")) {
+                            tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
+                                    tencentYunUtil.getSecretKey(),
+                                    tencentYunUtil.getQueryUrl(),
+                                    "",
+                                    tencentYunUtil.getVodAppId(),
+                                    jsonObject.getString("file_id"));
+                            t.setVideoStatus(true);
+                        }
+                    }
+                }
+            }
+        }
+        tmTencentVideoMessageService.removeByIds(tencentVideoMessageIds);
+        tOeExamRecordService.updateBatchById(tOeExamRecordList);
+    }
+
+    /**
+     * 删除新考试视频
+     *
+     * @param tmTencentVideoMessageList
+     */
+    private void deleteNewMedia(List<TMTencentVideoMessage> tmTencentVideoMessageList) {
+        Set<Long> tencentVideoMessageIds = new HashSet<>(tmTencentVideoMessageList.size());
+        List<Long> tOeExamRecordIdList = new ArrayList<>(tmTencentVideoMessageList.size());
+        for (TMTencentVideoMessage t : tmTencentVideoMessageList) {
+            tencentVideoMessageIds.add(t.getId());
+            tOeExamRecordIdList.add(t.getExamRecordId());
+            if (Objects.nonNull(t.getObject()) && !Objects.equals(t.getObject().trim(), "")) {
+                JSONObject jsonObject = JSONObject.parseObject(t.getObject());
+                if (Objects.nonNull(jsonObject.get("mediaInfoSet")) && !Objects.equals(jsonObject.getString("mediaInfoSet").trim(), "")) {
+                    JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("mediaInfoSet"));
+                    for (int i = 0; i < jsonArray.size(); i++) {
+                        JSONObject object = jsonArray.getJSONObject(i);
+                        if (Objects.nonNull(object.get("fileId")) && !Objects.equals(object.getString("fileId").trim(), "")) {
+                            tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
+                                    tencentYunUtil.getSecretKey(),
+                                    tencentYunUtil.getQueryUrl(),
+                                    "",
+                                    tencentYunUtil.getVodAppId(),
+                                    object.getString("fileId"));
+                        }
+                    }
+                } else if (Objects.nonNull(jsonObject.get("file_id")) && !Objects.equals(jsonObject.getString("file_id").trim(), "")) {
+                    tencentYunUtil.deleteMedia(tencentYunUtil.getSecretId(),
+                            tencentYunUtil.getSecretKey(),
+                            tencentYunUtil.getQueryUrl(),
+                            "",
+                            tencentYunUtil.getVodAppId(),
+                            jsonObject.getString("file_id"));
+                }
+            }
+        }
+        tmTencentVideoMessageService.removeByIds(tencentVideoMessageIds);
+        UpdateWrapper<TOeExamRecord> tOeExamRecordUpdateWrapper = new UpdateWrapper<>();
+        tOeExamRecordUpdateWrapper.lambda().set(TOeExamRecord::getVideoStatus, true).in(TOeExamRecord::getId, tOeExamRecordIdList);
+        tOeExamRecordService.update(tOeExamRecordUpdateWrapper);
+    }
 }