SceneDownloadLogServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  8. import com.fdkankan.common.constant.*;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.util.JwtUtil;
  11. import com.fdkankan.model.constants.UploadFilePath;
  12. import com.fdkankan.redis.constant.RedisKey;
  13. import com.fdkankan.redis.util.RedisUtil;
  14. import com.fdkankan.scene.bean.SceneJsonBean;
  15. import com.fdkankan.scene.config.FdkkLaserConfig;
  16. import com.fdkankan.scene.config.ServiceConfig;
  17. import com.fdkankan.scene.entity.*;
  18. import com.fdkankan.scene.mapper.ISceneDownloadLogMapper;
  19. import com.fdkankan.scene.oss.OssUtil;
  20. import com.fdkankan.scene.service.*;
  21. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  22. import com.fdkankan.scene.vo.DownloadVO;
  23. import com.fdkankan.scene.vo.SceneDownloadParamVO;
  24. import com.fdkankan.web.bean.DownLoadProgressBean;
  25. import com.fdkankan.web.response.ResultData;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.stereotype.Service;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.Objects;
  34. /**
  35. * <p>
  36. * 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2023-03-06
  41. */
  42. @Service
  43. @Slf4j
  44. public class SceneDownloadLogServiceImpl extends ServiceImpl<ISceneDownloadLogMapper, SceneDownloadLog> implements ISceneDownloadLogService {
  45. private static String keyFormat = "touch:scene:download:num:%s";
  46. @Value("${download.config.public-url}")
  47. private String publicUrl;
  48. @Autowired
  49. private IScenePlusService scenePlusService;
  50. @Autowired
  51. private ISceneEditInfoService sceneEditInfoService;
  52. @Autowired
  53. private OssUtil ossUtil;
  54. @Autowired
  55. private RedisUtil redisUtil;
  56. @Autowired
  57. private ServiceConfig serviceConfig;
  58. @Autowired
  59. private IScenePlusExtService scenePlusExtService;
  60. @Autowired
  61. private ISceneService sceneService;
  62. @Autowired
  63. private FdkkLaserConfig fdkkLaserConfig;
  64. public static void main(String[] args) {
  65. String lang = null;
  66. System.out.println("123123".equals(lang));
  67. }
  68. @Override
  69. public ResultData downOfflineScene(String num, String lang, String resultPath) {
  70. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  71. if(Objects.isNull(scenePlus)){
  72. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  73. }
  74. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  75. String bucket = scenePlusExt.getYunFileBucket();
  76. Scene scene = sceneService.getBySceneCode(num);
  77. String mapping = scene.getMapping();
  78. Map<String, Object> result = new HashMap<>();
  79. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  80. String sceneJson = ossUtil.getFileContent(bucket, sceneJsonPath);
  81. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  82. int version = sceneJsonBean.getVersion();
  83. redisUtil.set(String.format(keyFormat, num), "1");
  84. SceneDownloadLog sceneDownloadLog = this.getOne(
  85. new LambdaQueryWrapper<SceneDownloadLog>()
  86. .eq(SceneDownloadLog::getSceneNum, num)
  87. .orderByDesc(SceneDownloadLog::getId)
  88. .last("limit 1"));
  89. boolean download = false;//是否需要生成
  90. if(Objects.nonNull(sceneDownloadLog)){
  91. if(sceneDownloadLog.getStatus() == 0){
  92. result.put("status", 0);
  93. return ResultData.ok(result);
  94. }
  95. if(version == sceneDownloadLog.getSceneVersion() && lang.equals(sceneDownloadLog.getLang())){
  96. String url=this.publicUrl+":"+fdkkLaserConfig.getLaserPort()+"/" + mapping +sceneDownloadLog.getDownloadUrl();
  97. result.put("status", 2);
  98. result.put("url",url);
  99. String key=String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4,num);
  100. DownLoadProgressBean progress = new DownLoadProgressBean(sceneDownloadLog.getDownloadUrl(),100, SceneDownloadProgressStatus.DOWNLOAD_SUCCESS.code());
  101. redisUtil.set(key, JSONUtil.toJsonStr(progress));
  102. return ResultData.ok(result);
  103. }else{
  104. result.put("status", 3);
  105. download = true;
  106. }
  107. }else{
  108. result.put("status", 1);
  109. download = true;
  110. }
  111. if(download){
  112. //清除之前的下载记录
  113. this.remove(new LambdaQueryWrapper<SceneDownloadLog>().eq(SceneDownloadLog::getSceneNum, num));
  114. //写入新的记录
  115. sceneDownloadLog = new SceneDownloadLog();
  116. sceneDownloadLog.setSceneNum(num);
  117. sceneDownloadLog.setSceneVersion(version);
  118. sceneDownloadLog.setSysVersion("v4");
  119. sceneDownloadLog.setLang(lang);
  120. sceneDownloadLog.setDownloadUrl(resultPath);
  121. this.save(sceneDownloadLog);
  122. Map<String,String> params = new HashMap<>(2);
  123. params.put("type","local");
  124. params.put("num",num);
  125. params.put("lang", lang);
  126. params.put("resultPath", resultPath);
  127. redisUtil.lRightPush(RedisKey.SCENE_DOWNLOADS_TASK_V4, JSONObject.toJSONString(params));
  128. }
  129. return ResultData.ok(result);
  130. }
  131. @Override
  132. public ResultData downloadProcess(String num) {
  133. String key=String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4,num);
  134. DownLoadProgressBean downLoadProgressBean = new DownLoadProgressBean();
  135. if (!redisUtil.hasKey(key)){
  136. return ResultData.ok(downLoadProgressBean);
  137. }
  138. String processStr = redisUtil.get(key);
  139. log.info("downloadProcess-processStr-{}",processStr);
  140. downLoadProgressBean = JSONObject.parseObject(processStr, DownLoadProgressBean.class);
  141. if (downLoadProgressBean.getStatus()== 1002){
  142. final Scene scene = sceneService.getBySceneCode(num);
  143. //写库
  144. update(
  145. new LambdaUpdateWrapper<SceneDownloadLog>()
  146. .eq(SceneDownloadLog::getSceneNum,num)
  147. .set(SceneDownloadLog::getDownloadUrl,downLoadProgressBean.getUrl())
  148. .set(SceneDownloadLog::getStatus,1)
  149. );
  150. downLoadProgressBean.setUrl(this.publicUrl+":"+fdkkLaserConfig.getLaserPort()+ "/" + scene.getMapping() +downLoadProgressBean.getUrl());
  151. }
  152. return ResultData.ok(downLoadProgressBean);
  153. }
  154. @Override
  155. public ResultData downloadUpDateStatus(String num) {
  156. redisUtil.del(String.format(keyFormat, num));
  157. redisUtil.del(String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4,num));
  158. return ResultData.ok();
  159. }
  160. @Override
  161. public ResultData downOfflineSceneCheck(String num, String lang) {
  162. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  163. if(Objects.isNull(scenePlus)){
  164. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  165. }
  166. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  167. Scene scene = sceneService.getBySceneCode(num);
  168. String mapping = scene.getMapping();
  169. Map<String, Object> result = new HashMap<>();
  170. SceneDownloadLog sceneDownloadLog = this.getOne(
  171. new LambdaQueryWrapper<SceneDownloadLog>()
  172. .eq(SceneDownloadLog::getSceneNum, num)
  173. .orderByDesc(SceneDownloadLog::getId)
  174. .last("limit 1"));
  175. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  176. String sceneJson = ossUtil.getFileContent(scenePlusExt.getYunFileBucket(), sceneJsonPath);
  177. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  178. int version = sceneJsonBean.getVersion();
  179. if(Objects.nonNull(sceneDownloadLog) && sceneDownloadLog.getStatus() == 1 && version == sceneDownloadLog.getSceneVersion() && lang.equals(sceneDownloadLog.getLang())){
  180. String url=this.publicUrl+":"+fdkkLaserConfig.getLaserPort()+"/" + mapping +sceneDownloadLog.getDownloadUrl();
  181. result.put("status", 2);
  182. result.put("url",url);
  183. }else{
  184. result.put("status", 1);
  185. }
  186. return ResultData.ok(result);
  187. }
  188. @Override
  189. public Map<String, Object> downOfflineSceneDetail(SceneDownloadParamVO param) {
  190. String num = param.getSceneCode();
  191. // String lang = param.getLang();
  192. Map<String, Object> result = new HashMap<>();
  193. result.put("rebuildOffline", true);
  194. SceneDownloadLog sceneDownloadLog = this.getOne(
  195. new LambdaQueryWrapper<SceneDownloadLog>()
  196. .eq(SceneDownloadLog::getSceneNum, num)
  197. // .eq(SceneDownloadLog::getLang, lang)
  198. .last("limit 1"));
  199. if(Objects.isNull(sceneDownloadLog)){//如果没有记录,则没有生成过
  200. result.put("status", -1);
  201. return result;
  202. }
  203. Integer status = sceneDownloadLog.getStatus();
  204. //成功或者失败,直接返回
  205. if(status == DownloadStatus.SUCCESS.code() || status == DownloadStatus.FAILD.code()){
  206. result.put("status", status);
  207. result.put("path", sceneDownloadLog.getDownloadUrl());
  208. if(status == DownloadStatus.SUCCESS.code()){//如果是成功,需要匹配版本号,如果版本号不一致,需要返回需要重新生成表示
  209. result.put("rebuildOffline", this.rebuildOffline(sceneDownloadLog));
  210. }
  211. return result;
  212. }
  213. //下载中,从redis中获取下载进度,并根据状态更新数据库
  214. String key=String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4,num);
  215. if (!redisUtil.hasKey(key)){
  216. result.put("status", DownloadStatus.DOWNLOADING.code());
  217. result.put("percent", 0);
  218. return result;
  219. }
  220. DownLoadProgressBean downLoadProgressBean = new DownLoadProgressBean();
  221. String processStr = redisUtil.get(key);
  222. downLoadProgressBean = JSONObject.parseObject(processStr, DownLoadProgressBean.class);
  223. if (downLoadProgressBean.getStatus()== 1002){//下载成功,更新数据库表
  224. //写库
  225. sceneDownloadLog.setStatus(DownloadStatus.SUCCESS.code());
  226. sceneDownloadLog.setDownloadUrl(downLoadProgressBean.getUrl());
  227. this.updateById(sceneDownloadLog);
  228. // final Scene scene = sceneService.getBySceneCode(num);
  229. // downLoadProgressBean.setUrl(this.publicUrl+":"+fdkkLaserConfig.getLaserPort()+ "/" + scene.getMapping() +downLoadProgressBean.getUrl());
  230. result.put("status", DownloadStatus.SUCCESS.code());
  231. result.put("percent", 100);
  232. result.put("rebuildOffline", this.rebuildOffline(sceneDownloadLog));
  233. result.put("path", downLoadProgressBean.getUrl());
  234. return result;
  235. }
  236. if(downLoadProgressBean.getStatus()== 1003){//下载失败,更新数据库表
  237. //写库
  238. sceneDownloadLog.setStatus(DownloadStatus.FAILD.code());
  239. this.updateById(sceneDownloadLog);
  240. result.put("status", DownloadStatus.SUCCESS.code());
  241. result.put("percent", downLoadProgressBean.getPercent());
  242. return result;
  243. }
  244. //下载中
  245. result.put("status", DownloadStatus.DOWNLOADING.code());
  246. result.put("percent", downLoadProgressBean.getPercent());
  247. return result;
  248. }
  249. private boolean rebuildOffline(SceneDownloadLog sceneDownloadLog){
  250. String num = sceneDownloadLog.getSceneNum();
  251. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  252. if(Objects.isNull(scenePlus)){
  253. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  254. }
  255. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  256. String bucket = scenePlusExt.getYunFileBucket();
  257. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  258. String sceneJson = ossUtil.getFileContent(bucket, sceneJsonPath);
  259. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  260. int version = sceneJsonBean.getVersion();
  261. if(version != sceneDownloadLog.getSceneVersion()){
  262. return true;
  263. }
  264. return false;
  265. }
  266. @Override
  267. public ResultData downloadScene(SceneDownloadParamVO param) {
  268. String num = param.getSceneCode();
  269. String lang = param.getLang();
  270. String resultPath = param.getDir();
  271. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  272. if(Objects.isNull(scenePlus)){
  273. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  274. }
  275. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  276. String bucket = scenePlusExt.getYunFileBucket();
  277. Scene scene = sceneService.getBySceneCode(num);
  278. String mapping = scene.getMapping();
  279. Map<String, Object> result = new HashMap<>();
  280. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  281. String sceneJson = ossUtil.getFileContent(bucket, sceneJsonPath);
  282. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  283. int version = sceneJsonBean.getVersion();
  284. // redisUtil.set(String.format(keyFormat, num), "1");
  285. //清除之前的下载记录
  286. this.remove(new LambdaQueryWrapper<SceneDownloadLog>().eq(SceneDownloadLog::getSceneNum, num));
  287. //写入新的记录
  288. SceneDownloadLog sceneDownloadLog = new SceneDownloadLog();
  289. sceneDownloadLog.setSceneNum(num);
  290. sceneDownloadLog.setSceneVersion(version);
  291. sceneDownloadLog.setSysVersion("v4");
  292. sceneDownloadLog.setLang(param.getLang());
  293. sceneDownloadLog.setDownloadUrl(resultPath);
  294. this.save(sceneDownloadLog);
  295. Map<String,String> params = new HashMap<>(2);
  296. params.put("type","local");
  297. params.put("num",num);
  298. params.put("lang", lang);
  299. params.put("resultPath", resultPath);
  300. redisUtil.lRightPush(RedisKey.SCENE_DOWNLOADS_TASK_V4, JSONObject.toJSONString(params));
  301. return ResultData.ok(result);
  302. }
  303. }