package com.fdkankan.fusion.service.impl; import cn.dev33.satoken.stp.StpUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.common.enums.DownloadStatusEnum; import com.fdkankan.fusion.entity.*; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.httpClient.LaserService; import com.fdkankan.fusion.httpClient.response.SSDownSceneVo; import com.fdkankan.fusion.response.DownVo; import com.fdkankan.fusion.response.DownloadProcessVo; import com.fdkankan.fusion.service.*; import com.fdkankan.redis.constant.RedisKey; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @Service @Slf4j public class DownService implements IDownService { @Autowired ISceneDownLogService sceneDownloadLogService; @Autowired IScenePlusService scenePlusService; @Autowired ISceneEditInfoService sceneEditInfoService; @Autowired RedisUtil redisUtil; @Autowired LaserService laserService; @Autowired ITmDepartmentService tmDepartmentService; @Autowired ICameraService cameraService; private Integer getSceneVersion( ScenePlus scenePlus) { Integer version = 0; if(scenePlus !=null){ SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId()); if(sceneEditInfo == null){ throw new BusinessException(ResultCode.SCENE_NOT_EXIST); } version = sceneEditInfo.getVersion(); } return version; } @Override public DownVo checkDownLoad(String sceneNum, Integer isObj) { if(StringUtils.isEmpty(sceneNum)){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } ScenePlus plus = scenePlusService.getByNum(sceneNum); if(plus == null){ throw new BusinessException(ResultCode.SCENE_NOT_EXIST); } Integer sceneType = plus.getSceneSource(); log.info("checkDownLoad--sceneType:{},isObj:{}",sceneType,isObj); if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景 return SSCheckDownload(sceneNum); } SceneDownLog sceneDownloadLog; Integer sceneVersion = getSceneVersion(plus); sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,sceneVersion); DownVo downVo = new DownVo(); if(sceneDownloadLog != null){ downVo.setDownloadStatus(1); return downVo; } sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,sceneVersion); //下载过,有更改 if(sceneDownloadLog == null){ String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS; downVo.setDownloadStatus(2); redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息 return downVo; } //3下载过,并且没有修改过 downVo.setDownloadStatus(3); downVo.setDownloadUrl(sceneDownloadLog.getDownUrl()); return downVo; } @Override public DownVo down(String sceneNum,Integer isObj) { if(StringUtils.isEmpty(sceneNum) ){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } ScenePlus scenePlus = scenePlusService.getByNum(sceneNum); if( scenePlus == null){ throw new BusinessException(ResultCode.SCENE_NOT_EXIST); } Long userId = scenePlus.getUserId(); Integer sceneType = scenePlus.getSceneSource(); Integer sceneVersion = getSceneVersion( scenePlus); log.info("down--sceneType:{},isObj:{}",sceneType,isObj); saveLog(scenePlus,sceneVersion); if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景 return SSDownload(sceneNum,userId); } DownVo downVo = new DownVo(); String redisKey = RedisKey.SCENE_DOWNLOADS_TASK_V4; Map params = new HashMap<>(2); params.put("type","local"); params.put("num",sceneNum); redisUtil.lRightPush(redisKey, JSONObject.toJSONString(params)); downVo.setDownloadStatus(1); return downVo; } private void saveLog(ScenePlus scenePlus,Integer sceneVersion){ String userName = (String) StpUtil.getExtra("userName"); String nickName = (String) StpUtil.getExtra("nickName"); String deptId = tmDepartmentService.getDeptId(); TmDepartment tmDepartment = tmDepartmentService.getById(deptId); Camera camera = cameraService.getByCameraId(scenePlus.getCameraId()); SceneDownLog sceneDownloadLogEntity = new SceneDownLog(); sceneDownloadLogEntity.setSceneNum(scenePlus.getNum()); sceneDownloadLogEntity.setSceneTitle(scenePlus.getTitle()); sceneDownloadLogEntity.setUserName(userName); sceneDownloadLogEntity.setNickName(nickName); sceneDownloadLogEntity.setSnCode(camera.getSnCode()); sceneDownloadLogEntity.setDeptName(tmDepartment.getName()); sceneDownloadLogEntity.setDeptLevel(tmDepartment.getLevel()); sceneDownloadLogEntity.setDeptId(deptId); sceneDownloadLogEntity.setStatus(0); sceneDownloadLogEntity.setVersion(sceneVersion); sceneDownloadLogService.save(sceneDownloadLogEntity); } @Override public DownloadProcessVo downloadProcess(String sceneNum, Integer isObj) { if (StringUtils.isEmpty(sceneNum)) { throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } ScenePlus scenePlus = scenePlusService.getByNum(sceneNum); if( scenePlus == null){ throw new BusinessException(ResultCode.SCENE_NOT_EXIST); } Integer sceneType = scenePlus.getSceneSource(); log.info("downloadProcess--sceneType:{},isObj:{}",sceneType,isObj); if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景 return SSDownloadProcess(sceneNum); } String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4; // 获取下载进度 String result = redisUtil.get(String.format(redisKey,sceneNum)); if(StringUtils.isEmpty(result)){ return new DownloadProcessVo(); } SceneDownLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,getSceneVersion(scenePlus)); DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class); if(sceneDownloadLog != null){ switch (downloadProcessVo.getStatus()) { case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE: String url = downloadProcessVo.getUrl(); if (!StringUtils.isEmpty(url)) { sceneDownloadLog.setDownUrl(url); sceneDownloadLog.setStatus(1); break; } case DownloadStatusEnum.DOWNLOAD_FAILED_CODE: sceneDownloadLog.setStatus(-1); break; } sceneDownloadLogService.updateById(sceneDownloadLog); } return downloadProcessVo; } /** * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中 */ private DownVo SSCheckDownload(String sceneNum) { DownVo downVo = new DownVo(); SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum); if(vo == null){ throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR); } downVo.setDownloadStatus(0); if(vo.getStatus() == 1){ downVo.setDownloadStatus(3); downVo.setDownloadUrl(vo.getUrl()); } if(vo.getStatus() == 2){ downVo.setDownloadStatus(2); } if(vo.getStatus() == 3){ downVo.setDownloadStatus(1); } return downVo; } /** * downloadStatus -1下载失败 1下载成功 */ private DownVo SSDownload(String sceneNum,Long userId) { DownVo downVo = new DownVo(); //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成 SSDownSceneVo vo = laserService.downOfflineScene(sceneNum); if(vo == null){ throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR); } downVo.setDownloadStatus(1); return downVo; } public static HashMap ssNumProcessNumMap = new HashMap<>(); private DownloadProcessVo SSDownloadProcess(String sceneNum) { DownloadProcessVo downVo = new DownloadProcessVo(); SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum); if(vo == null){ throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR); } downVo.setStatus(1003); if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中 ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum); Integer percent = ssNumProcessNumMap.get(sceneNum); percent = percent /2; if(percent >50){ percent = 50; } downVo.setStatus(1001); downVo.setPercent(percent); } if(vo.getStatus() == 1){ //下载完成 ssNumProcessNumMap.remove(sceneNum); downVo.setPercent(100); downVo.setUrl(vo.getUrl()); downVo.setStatus(1002); LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(SceneDownLog::getSceneNum,sceneNum); wrapper.eq(SceneDownLog::getStatus,0); wrapper.set(SceneDownLog::getDownUrl,vo.getUrl()); wrapper.set(SceneDownLog::getStatus,1); sceneDownloadLogService.update(wrapper); } return downVo; } }