SceneAsynOperLogServiceImpl.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.ArrayUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  9. import com.fdkankan.common.constant.CommonOperStatus;
  10. import com.fdkankan.common.constant.CommonStatus;
  11. import com.fdkankan.common.constant.SceneAsynFuncType;
  12. import com.fdkankan.common.constant.SceneAsynModuleType;
  13. import com.fdkankan.common.constant.SceneAsynOperType;
  14. import com.fdkankan.common.constant.*;
  15. import com.fdkankan.common.exception.BusinessException;
  16. import com.fdkankan.scene.entity.SceneAsynOperLog;
  17. import com.fdkankan.scene.entity.ScenePlus;
  18. import com.fdkankan.scene.entity.ScenePlusExt;
  19. import com.fdkankan.scene.mapper.ISceneAsynOperLogMapper;
  20. import com.fdkankan.scene.oss.OssUtil;
  21. import com.fdkankan.scene.service.ISceneAsynOperLogService;
  22. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  23. import com.fdkankan.scene.service.IScenePlusExtService;
  24. import com.fdkankan.scene.service.IScenePlusService;
  25. import com.fdkankan.scene.vo.SceneAsynOperLogParamVO;
  26. import com.fdkankan.web.response.ResultData;
  27. import java.io.IOException;
  28. import java.nio.file.FileSystemException;
  29. import java.util.Arrays;
  30. import java.util.Calendar;
  31. import java.util.List;
  32. import java.util.stream.Collectors;
  33. import lombok.extern.slf4j.Slf4j;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Service;
  36. /**
  37. * <p>
  38. * 服务实现类
  39. * </p>
  40. *
  41. * @author
  42. * @since 2022-12-07
  43. */
  44. @Slf4j
  45. @Service
  46. public class SceneAsynOperLogServiceImpl extends ServiceImpl<ISceneAsynOperLogMapper, SceneAsynOperLog> implements ISceneAsynOperLogService {
  47. @Autowired
  48. private OssUtil ossUtil;
  49. @Autowired
  50. private IScenePlusService scenePlusService;
  51. @Autowired
  52. private IScenePlusExtService scenePlusExtService;
  53. @Override
  54. public ResultData getAsynOperLog(SceneAsynOperLogParamVO param) {
  55. LambdaQueryWrapper<SceneAsynOperLog> queryWrapper = new LambdaQueryWrapper<>();
  56. queryWrapper.eq(SceneAsynOperLog::getNum, param.getNum());
  57. if(StrUtil.isNotEmpty(param.getOperType())){
  58. queryWrapper.eq(SceneAsynOperLog::getOperType, param.getOperType());
  59. }
  60. if(StrUtil.isNotEmpty(param.getModule())){
  61. queryWrapper.eq(SceneAsynOperLog::getModule, param.getModule());
  62. }
  63. if(StrUtil.isNotEmpty(param.getFunc())){
  64. queryWrapper.eq(SceneAsynOperLog::getFunc, param.getFunc());
  65. }
  66. //需要弹窗的异步操作列表
  67. List<SceneAsynOperLog> list = this.list(queryWrapper);
  68. //如果列表中有需要弹窗并且处理完毕的,需要把这些数据改为不需要弹窗了,因为前端请求到之后就会弹出提示,下次不需要弹窗了
  69. if(CollUtil.isNotEmpty(list)){
  70. List<Long> idList = list.stream().filter(log -> {
  71. if(!log.getState().equals(CommonOperStatus.WAITING.code())){
  72. return true;
  73. }
  74. return false;
  75. }).map(log->log.getId()).collect(Collectors.toList());
  76. if(CollUtil.isNotEmpty(idList)){
  77. this.update(new LambdaUpdateWrapper<SceneAsynOperLog>()
  78. .set(SceneAsynOperLog::getPop, CommonStatus.NO.code())
  79. .in(SceneAsynOperLog::getId, idList));
  80. }
  81. }
  82. return ResultData.ok(list);
  83. }
  84. @Override
  85. public void cleanDownloadOssPage(String asynFuncType, int preMonth) {
  86. List<SceneAsynOperLog> downloadList = this.list(
  87. new LambdaQueryWrapper<SceneAsynOperLog>()
  88. .eq(SceneAsynOperLog::getOperType, SceneAsynOperType.DOWNLOAD.code())
  89. .eq(SceneAsynOperLog::getModule, SceneAsynModuleType.UPLOAD_DOWNLOAD.code())
  90. .eq(SceneAsynOperLog::getFunc, asynFuncType));
  91. if(CollUtil.isEmpty(downloadList)){
  92. return;
  93. }
  94. DateTime preDate = DateUtil.offsetMonth(Calendar.getInstance().getTime(), -preMonth);
  95. List<SceneAsynOperLog> deleteList = downloadList.parallelStream().filter(log -> {
  96. if (log.getCreateTime().before(preDate)) {
  97. return true;
  98. }
  99. return false;
  100. }).collect(Collectors.toList());
  101. if(CollUtil.isEmpty(deleteList)){
  102. return;
  103. }
  104. //删除数据库记录
  105. List<Long> deleteIdList = deleteList.parallelStream().map(item -> item.getId()).collect(Collectors.toList());
  106. this.removeByIds(deleteIdList);
  107. deleteList.parallelStream().forEach(item -> {
  108. if(StrUtil.isNotEmpty(item.getUrl())){
  109. try{
  110. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(item.getNum());
  111. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  112. ossUtil.deleteObject(scenePlusExt.getYunFileBucket(), item.getUrl());
  113. }catch (FileSystemException e){
  114. log.warn("删除oss下载压缩包失败,key:{}", item.getUrl());
  115. }
  116. }
  117. });
  118. }
  119. @Override
  120. public void checkSceneAsynOper(String num, String operType, String module, String function) {
  121. LambdaQueryWrapper<SceneAsynOperLog> queryWrapper =
  122. new LambdaQueryWrapper<SceneAsynOperLog>()
  123. .eq(SceneAsynOperLog::getNum,num)
  124. .eq(SceneAsynOperLog::getState, CommonOperStatus.WAITING.code());
  125. if(StrUtil.isNotEmpty(operType)){
  126. queryWrapper.eq(SceneAsynOperLog::getOperType, operType);
  127. }
  128. if(StrUtil.isNotEmpty(module)){
  129. queryWrapper.eq(SceneAsynOperLog::getModule, module);
  130. }
  131. if(StrUtil.isNotEmpty(function)){
  132. queryWrapper.eq(SceneAsynOperLog::getFunc, function);
  133. }
  134. List<SceneAsynOperLog> waittingLogList = this.list(queryWrapper);
  135. if(CollUtil.isNotEmpty(waittingLogList)){
  136. throw new BusinessException(ErrorCode.FAILURE_CODE_5066);
  137. }
  138. }
  139. @Override
  140. public void cleanLog(String num, String moduleType, String funcType, String... operTypes) {
  141. LambdaQueryWrapper<SceneAsynOperLog> wrapper = new LambdaQueryWrapper<SceneAsynOperLog>()
  142. .eq(SceneAsynOperLog::getNum, num)
  143. .eq(SceneAsynOperLog::getModule, moduleType)
  144. .eq(SceneAsynOperLog::getFunc, funcType);
  145. if(ArrayUtil.isNotEmpty(operTypes)){
  146. wrapper.in(SceneAsynOperLog::getOperType, operTypes);
  147. }
  148. this.remove(wrapper);
  149. }
  150. }