123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- package com.fdkankan.ucenter.service.impl;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.common.constant.SceneConstant;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.redis.constant.RedisKey;
- import com.fdkankan.redis.util.RedisUtil;
- import com.fdkankan.ucenter.common.DownloadStatusEnum;
- import com.fdkankan.ucenter.common.constants.ResultCode;
- import com.fdkankan.ucenter.constant.LoginConstant;
- import com.fdkankan.ucenter.entity.*;
- import com.fdkankan.ucenter.httpClient.service.LaserService;
- import com.fdkankan.ucenter.httpClient.vo.SSDownSceneVo;
- import com.fdkankan.ucenter.service.*;
- import com.fdkankan.ucenter.vo.response.DownVo;
- import com.fdkankan.ucenter.vo.response.DownloadProcessVo;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.ibatis.annotations.Case;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.HashMap;
- import java.util.Map;
- @Service
- public class DownService implements IDownService {
- @Autowired
- ISceneProService sceneProService;
- @Autowired
- ISceneProEditService sceneProEditService;
- @Autowired
- ISceneDownloadLogService sceneDownloadLogService;
- @Autowired
- IScenePlusService scenePlusService;
- @Autowired
- RedisUtil redisUtil;
- @Autowired
- ISceneEditInfoService sceneEditInfoService;
- @Autowired
- IUserService userService;
- @Autowired
- LaserService laserService;
- @Override
- public DownVo checkDownLoad(String sceneNum) {
- if(StringUtils.isEmpty(sceneNum)){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
- }
- ScenePro scenePro = sceneProService.getByNum(sceneNum);
- ScenePlus plus = scenePlusService.getByNum(sceneNum);
- if(scenePro == null && plus == null){
- throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
- }
- Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : plus.getSceneSource();
- if(sceneType == 4){ //深时场景
- return SSCheckDownload(sceneNum);
- }
- SceneDownloadLog sceneDownloadLog;
- int isUp = 0;
- if(scenePro == null){
- isUp = 1;
- }
- Integer sceneVersion = getSceneVersion(scenePro, plus);
- sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
- DownVo downVo = new DownVo();
- if(sceneDownloadLog != null){
- downVo.setDownloadStatus(1);
- return downVo;
- }
- sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,isUp);
- //3下载过,并且没有修改过
- if(sceneDownloadLog != null && sceneDownloadLog.getSceneVersion().intValue() == sceneVersion){
- downVo.setDownloadStatus(3);
- downVo.setDownloadUrl(sceneDownloadLog.getDownloadUrl());
- return downVo;
- }
- //下载过,有更改
- if(sceneDownloadLog != null){
- String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
- if(isUp == 1){
- redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
- }
- downVo.setDownloadStatus(2);
- redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
- return downVo;
- }
- return downVo;
- }
- private Integer getSceneVersion(ScenePro scenePro,ScenePlus scenePlus){
- Integer version = 0;
- if(scenePro != null){
- SceneProEdit proEdit = sceneProEditService.getByProId(scenePro.getId());
- if(proEdit == null){
- throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
- }
- version = proEdit.getVersion();
- }
- if(scenePro == null && scenePlus !=null){
- SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
- if(sceneEditInfo == null){
- throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
- }
- version = sceneEditInfo.getVersion();
- }
- return version;
- }
- @Override
- public DownVo down(String sceneNum,String userName) {
- if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(userName)){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
- }
- ScenePro scenePro = sceneProService.getByNum(sceneNum);
- ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
- if(scenePro == null && scenePlus == null){
- throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
- }
- Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
- if(sceneType == 4){ //深时场景
- return SSDownload(sceneNum,userName);
- }
- DownVo downVo = new DownVo();
- User user = userService.getByUserName(userName);
- if(user == null || user.getDownloadNumTotal() - user.getDownloadNum() <= 0){
- downVo.setDownloadStatus(-1);
- return downVo;
- }
- Integer sceneVersion = getSceneVersion(scenePro, scenePlus);
- Map<String,String> params = new HashMap<>(2);
- params.put("type","local");
- params.put("sceneNum",sceneNum);
- String progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS, sceneNum);
- String downloadTaskKey = RedisKey.DOWNLOAD_TASK;
- String sysVersion = "v3";
- if(scenePro == null){
- params.put("num",sceneNum);
- progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, sceneNum);
- downloadTaskKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
- sysVersion = "v4";
- }
- //先删除之前的下载进度
- redisUtil.del(progressKey);
- //入队
- redisUtil.lRightPush(downloadTaskKey, JSONObject.toJSONString(params));
- //修改用户的下载次数
- user.setDownloadNum(user.getDownloadNum() + 1);
- userService.updateById(user);
- SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
- sceneDownloadLogEntity.setUserId(user.getId());
- sceneDownloadLogEntity.setSceneNum(sceneNum);
- sceneDownloadLogEntity.setSceneVersion(sceneVersion);
- sceneDownloadLogEntity.setStatus(0);
- sceneDownloadLogEntity.setSysVersion(sysVersion);
- sceneDownloadLogService.save(sceneDownloadLogEntity);
- downVo.setDownloadStatus(1);
- return downVo;
- }
- @Override
- public DownloadProcessVo downloadProcess(Long userId, String sceneNum) {
- if (StringUtils.isEmpty(sceneNum)) {
- throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
- }
- ScenePro scenePro = sceneProService.getByNum(sceneNum);
- ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
- if(scenePro == null && scenePlus == null){
- throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
- }
- Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
- if(sceneType == 4){ //深时场景
- return SSDownloadProcess(sceneNum);
- }
- String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
- if(scenePro == null){
- redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
- }
- // 获取下载进度
- String result = redisUtil.get(String.format(redisKey,sceneNum));
- if(StringUtils.isEmpty(result)){
- return new DownloadProcessVo();
- }
- int isUp = 0;
- if(scenePro == null){
- isUp = 1;
- }
- SceneDownloadLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
- 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.setDownloadUrl(url);
- sceneDownloadLog.setStatus(1);
- break;
- }
- case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
- sceneDownloadLog.setStatus(-1);
- userService.updateDownloadNum(userId, -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.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
- }
- 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,String userName) {
- DownVo downVo = new DownVo();
- User user = userService.getByUserName(userName);
- if(user == null || user.getSsDownloadNumTotal() - user.getSsDownloadNum() <= 0){
- downVo.setDownloadStatus(-1);
- return downVo;
- }
- //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
- SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
- if(vo == null){
- throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
- }
- LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(User::getId,user.getId());
- wrapper.set(User::getSsDownloadNum ,user.getSsDownloadNum() + 1);
- userService.update(wrapper);
- SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
- sceneDownloadLogEntity.setUserId(user.getId());
- sceneDownloadLogEntity.setSceneNum(sceneNum);
- sceneDownloadLogEntity.setSceneVersion(0);
- sceneDownloadLogEntity.setStatus(0);
- sceneDownloadLogEntity.setSysVersion("ss");
- sceneDownloadLogService.save(sceneDownloadLogEntity);
- downVo.setDownloadStatus(1);
- return downVo;
- }
- public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
- private DownloadProcessVo SSDownloadProcess(String sceneNum) {
- DownloadProcessVo downVo = new DownloadProcessVo();
- SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
- if(vo == null){
- throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
- }
- downVo.setStatus(1003);
- if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
- ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
- Integer percent = ssNumProcessNumMap.get(sceneNum);
- if(percent >100){
- percent = 99;
- }
- downVo.setStatus(1001);
- downVo.setPercent(percent);
- }
- if(vo.getStatus() == 1){ //下载完成
- ssNumProcessNumMap.remove(sceneNum);
- downVo.setPercent(100);
- downVo.setUrl(vo.getUrl());
- downVo.setStatus(1002);
- LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
- wrapper.eq(SceneDownloadLog::getStatus,0);
- wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
- wrapper.set(SceneDownloadLog::getStatus,1);
- sceneDownloadLogService.update(wrapper);
- }
- return downVo;
- }
- }
|