package com.fdkankan.scene.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.CommonOperStatus;
import com.fdkankan.common.constant.CommonStatus;
import com.fdkankan.common.constant.SceneAsynFuncType;
import com.fdkankan.common.constant.SceneAsynModuleType;
import com.fdkankan.common.constant.SceneAsynOperType;
import com.fdkankan.common.constant.*;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.scene.entity.SceneAsynOperLog;
import com.fdkankan.scene.entity.ScenePlus;
import com.fdkankan.scene.entity.ScenePlusExt;
import com.fdkankan.scene.mapper.ISceneAsynOperLogMapper;
import com.fdkankan.scene.oss.OssUtil;
import com.fdkankan.scene.service.ISceneAsynOperLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.service.IScenePlusExtService;
import com.fdkankan.scene.service.IScenePlusService;
import com.fdkankan.scene.vo.SceneAsynOperLogParamVO;
import com.fdkankan.web.response.ResultData;
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-12-07
*/
@Slf4j
@Service
public class SceneAsynOperLogServiceImpl extends ServiceImpl implements ISceneAsynOperLogService {
@Autowired
private OssUtil ossUtil;
@Autowired
private IScenePlusService scenePlusService;
@Autowired
private IScenePlusExtService scenePlusExtService;
@Override
public ResultData getAsynOperLog(SceneAsynOperLogParamVO param) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SceneAsynOperLog::getNum, param.getNum());
if(StrUtil.isNotEmpty(param.getOperType())){
queryWrapper.eq(SceneAsynOperLog::getOperType, param.getOperType());
}
if(StrUtil.isNotEmpty(param.getModule())){
queryWrapper.eq(SceneAsynOperLog::getModule, param.getModule());
}
if(StrUtil.isNotEmpty(param.getFunc())){
queryWrapper.eq(SceneAsynOperLog::getFunc, param.getFunc());
}
//需要弹窗的异步操作列表
List list = this.list(queryWrapper);
//如果列表中有需要弹窗并且处理完毕的,需要把这些数据改为不需要弹窗了,因为前端请求到之后就会弹出提示,下次不需要弹窗了
if(CollUtil.isNotEmpty(list)){
List idList = list.stream().filter(log -> {
if(!log.getState().equals(CommonOperStatus.WAITING.code())){
return true;
}
return false;
}).map(log->log.getId()).collect(Collectors.toList());
if(CollUtil.isNotEmpty(idList)){
this.update(new LambdaUpdateWrapper()
.set(SceneAsynOperLog::getPop, CommonStatus.NO.code())
.in(SceneAsynOperLog::getId, idList));
}
}
return ResultData.ok(list);
}
@Override
public void cleanDownloadOssPage(String asynFuncType, int preMonth) {
List downloadList = this.list(
new LambdaQueryWrapper()
.eq(SceneAsynOperLog::getOperType, SceneAsynOperType.DOWNLOAD.code())
.eq(SceneAsynOperLog::getModule, SceneAsynModuleType.UPLOAD_DOWNLOAD.code())
.eq(SceneAsynOperLog::getFunc, asynFuncType));
if(CollUtil.isEmpty(downloadList)){
return;
}
DateTime preDate = DateUtil.offsetMonth(Calendar.getInstance().getTime(), -preMonth);
List deleteList = downloadList.parallelStream().filter(log -> {
if (log.getCreateTime().before(preDate)) {
return true;
}
return false;
}).collect(Collectors.toList());
if(CollUtil.isEmpty(deleteList)){
return;
}
//删除数据库记录
List deleteIdList = deleteList.parallelStream().map(item -> item.getId()).collect(Collectors.toList());
this.removeByIds(deleteIdList);
deleteList.parallelStream().forEach(item -> {
if(StrUtil.isNotEmpty(item.getUrl())){
try{
ScenePlus scenePlus = scenePlusService.getScenePlusByNum(item.getNum());
ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
ossUtil.deleteObject(scenePlusExt.getYunFileBucket(), item.getUrl());
}catch (FileSystemException e){
log.warn("删除oss下载压缩包失败,key:{}", item.getUrl());
}
}
});
}
@Override
public void checkSceneAsynOper(String num, String operType, String module, String function) {
LambdaQueryWrapper queryWrapper =
new LambdaQueryWrapper()
.eq(SceneAsynOperLog::getNum,num)
.eq(SceneAsynOperLog::getState, CommonOperStatus.WAITING.code());
if(StrUtil.isNotEmpty(operType)){
queryWrapper.eq(SceneAsynOperLog::getOperType, operType);
}
if(StrUtil.isNotEmpty(module)){
queryWrapper.eq(SceneAsynOperLog::getModule, module);
}
if(StrUtil.isNotEmpty(function)){
queryWrapper.eq(SceneAsynOperLog::getFunc, function);
}
List waittingLogList = this.list(queryWrapper);
if(CollUtil.isNotEmpty(waittingLogList)){
throw new BusinessException(ErrorCode.FAILURE_CODE_5066);
}
}
@Override
public void cleanLog(String num, String moduleType, String funcType, String... operTypes) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper()
.eq(SceneAsynOperLog::getNum, num)
.eq(SceneAsynOperLog::getModule, moduleType)
.eq(SceneAsynOperLog::getFunc, funcType);
if(ArrayUtil.isNotEmpty(operTypes)){
wrapper.in(SceneAsynOperLog::getOperType, operTypes);
}
this.remove(wrapper);
}
}