package com.fdkankan.ucenter.service.impl; import com.alibaba.fastjson.JSONObject; 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.constant.LoginConstant; import com.fdkankan.ucenter.entity.*; 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.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; @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); } 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); } 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 params = new HashMap<>(2); params.put("type","local"); params.put("sceneNum",sceneNum); String redisKey = RedisKey.DOWNLOAD_TASK; String sysVersion = "v3"; if(scenePro == null){ params.put("num",sceneNum); redisKey = RedisKey.SCENE_DOWNLOADS_TASK_V4; sysVersion = "v4"; } redisUtil.lRightPush(redisKey, 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(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); } 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(); } return JSONObject.parseObject(result,DownloadProcessVo.class); } }