package com.fdkankan.download.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.thread.ExecutorBuilder; import cn.hutool.core.util.ArrayUtil; import cn.hutool.system.HostInfo; import cn.hutool.system.SystemUtil; import com.fdkankan.common.constant.CommonStatus; import com.fdkankan.common.constant.CommonSuccessStatus; import com.fdkankan.common.constant.SceneSource; import com.fdkankan.download.entity.*; import com.fdkankan.download.service.*; import com.fdkankan.redis.util.RedisLockUtil; import com.fdkankan.redis.util.RedisUtil; import com.fdkankan.rabbitmq.util.RabbitMqProducer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.concurrent.ThreadPoolExecutor; import java.util.stream.Collectors; @Slf4j @Component public class GenSceneRunnerImpl implements CommandLineRunner { @Autowired private ISnService snService; @Autowired private IScenePlusService scenePlusService; @Autowired private ICameraService cameraService; @Autowired private IScenePlusExtService scenePlusExtService; @Autowired private IDownloadLogService downloadLogService; @Autowired private RedisUtil redisUtil; @Autowired private RedisLockUtil redisLockUtil; @Autowired private IDownloadService downloadService; @Autowired private RabbitMqProducer mqProducer; private final static ThreadPoolExecutor threadPoolExecutor = ExecutorBuilder.create().setCorePoolSize(5).setMaxPoolSize(5).build(); @Override public void run(String... args) throws Exception { List snList = snService.list(); if (CollUtil.isEmpty(snList)) { return; } for (Sn sn : snList) { List cameras = cameraService.listBySnCodeList(Arrays.asList(sn.getSn())); if (CollUtil.isEmpty(cameras)) { continue; } Camera camera = cameras.get(0); List scenePluses = scenePlusService.listByCameraIdList(Arrays.asList(camera.getId())); if (CollUtil.isEmpty(scenePluses)) { continue; } for (ScenePlus scenePlus : scenePluses) { //上锁 HostInfo hostInfo = SystemUtil.getHostInfo(); String key = "download:tool:" + scenePlus.getNum(); String lockVal = hostInfo.getAddress(); try { boolean lock = redisLockUtil.lock(key, lockVal, 24 * 60 * 60); if(!lock){ continue; } List downloadLogList = downloadLogService.getByNum(scenePlus.getNum()); Set types = downloadLogList.stream().map(DownloadLog::getType).collect(Collectors.toSet()); int isObj = CommonStatus.NO.code(); //下载点云场景 if (scenePlus.getSceneSource() == SceneSource.JG.code() || scenePlus.getSceneSource() == SceneSource.SG.code()) { if(!types.contains("laser")){ threadPoolExecutor.submit(() -> { try { // TODO: 2024/1/3 文杰实现 downloadLogService.saveLog(scenePlus.getNum(), "laser", CommonSuccessStatus.SUCCESS.code(), null); } catch (Exception e) { downloadLogService.saveLog(scenePlus.getNum(), "laser", CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000)); log.error("点云场景打包失败,num:{}", scenePlus.getNum(), e); } }); } ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId()); isObj = scenePlusExt.getIsObj(); } //下载看看、看见、mesh if (scenePlus.getSceneSource() == SceneSource.BM.code() || scenePlus.getSceneSource() == SceneSource.ZT.code() || isObj == CommonStatus.YES.code()) { if(!types.contains("kankan")) { threadPoolExecutor.submit(() -> { try { String zipPath = downloadService.downloadHandler(scenePlus.getNum()); send(zipPath); downloadLogService.saveLog(scenePlus.getNum(), "kankan", CommonSuccessStatus.SUCCESS.code(), null); } catch (Exception e) { log.error("看看场景打包失败,num:{}", scenePlus.getNum(), e); downloadLogService.saveLog(scenePlus.getNum(), "kankan", CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000)); } }); } } }catch (Exception e){ log.error("场景打包失败:{}", scenePlus.getNum(), e); } } } } private void send(String zipPath){ mqProducer.sendByWorkQueue("rsync-scene", zipPath); } }