SceneDownloadLogServiceImpl.java 15 KB

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