package com.fdkankan.extend.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.ZipUtil; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.common.constant.CommonSuccessStatus; import com.fdkankan.common.util.DateExtUtil; import com.fdkankan.extend.TowerSceneBean; import com.fdkankan.extend.entity.Camera; import com.fdkankan.extend.entity.CameraDetail; import com.fdkankan.extend.entity.ScenePlus; import com.fdkankan.extend.service.ICameraDetailService; import com.fdkankan.extend.service.ICameraService; import com.fdkankan.extend.service.IScenePlusService; import com.fdkankan.extend.service.ITowerService; import com.fdkankan.extend.util.RsaCryptTools; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.redis.constant.RedisKey; import com.fdkankan.redis.util.RedisUtil; import com.fdkankan.web.response.ResultData; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; import com.fdkankan.model.constants.UploadFilePath; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.*; import java.util.stream.Collectors; @RefreshScope @Slf4j @Service public class TowerServiceImpl implements ITowerService { private static String parentPath = "/home/backend/4dkankan_v4/modeling-extend/download/"; private static String ossParentPath = "customer-projects/china-tower/"; private static String ossScenePoolFilePath = ossParentPath.concat("scene.txt"); private static String ossCameraPoolFilePath = ossParentPath.concat("camera.txt"); private static String pushLogPath = parentPath.concat("log.txt"); @Value("${tower.manufact}") private String manufact; @Value("${tower.private-key}") private String privateKey; @Value("${tower.url}") private String url; @Autowired private FYunFileServiceInterface fYunFileService; @Autowired private RedisUtil redisUtil; @Autowired private ICameraDetailService cameraDetailService; @Autowired private IScenePlusService scenePlusService; @Autowired private ITowerService towerService; @Autowired private ICameraService cameraService; @Override public String packSceneDataHandler(String num, String title) throws Exception { //校验场景名称是否正确 String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath); List towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class); if(CollUtil.isEmpty(towerSceneBeans)){ return null; } TowerSceneBean towerSceneBean = towerSceneBeans.stream().filter(bean -> title.equals(bean.getName())).findFirst().get(); if(Objects.isNull(towerSceneBean)){ return null; } log.info("开始下载:{}", num); //场景本地数据父目录 String scenePath = parentPath.concat(num).concat("/"); String sceneSourcePath = scenePath.concat(towerSceneBean.getSceneId()).concat("_").concat(towerSceneBean.getRoomId()); String zipPath = sceneSourcePath.concat(".zip"); //data数据下载 String dataPath = sceneSourcePath + "/data/"; String ossDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num); //下载floorplan.json fYunFileService.downloadFile(ossDataPath.concat("floorplan.json"), dataPath.concat("floorplan.json")); //下载scene.json String sceneJsonPath = dataPath.concat("scene.json"); String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, num)); if(StrUtil.isEmpty(sceneJson)){ sceneJson = fYunFileService.getFileContent(ossDataPath.concat("scene.json")); } JSONObject sceneJsonObj = JSON.parseObject(sceneJson); sceneJsonObj.put("sceneKind", "pano"); FileUtil.writeUtf8String(sceneJsonObj.toJSONString(), sceneJsonPath); //下载mesh FileUtil.mkdir(dataPath.concat("mesh/")); List meshList = fYunFileService.listRemoteFiles(ossDataPath.concat("mesh")); meshList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, dataPath.concat("mesh/"))); //images数据下载 String imagesPath = sceneSourcePath + "/images/"; String ossImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num); //下载high图 List highList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/high/")); FileUtil.mkdir(imagesPath.concat("4k/")); highList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("4k/"))); //下载low图 FileUtil.mkdir(imagesPath.concat("512/")); List lowList = fYunFileService.listRemoteFiles(ossImagesPath.concat("pan/low/")); lowList.parallelStream().forEach(key -> fYunFileService.downloadFile(key, imagesPath.concat("512/"))); //下载vision.txt fYunFileService.downloadFile(ossImagesPath.concat("vision.txt"), imagesPath.concat("vision.txt")); //打包 ZipUtil.zip(sceneSourcePath, zipPath, true); log.info("结束下载,开始推送:{}", num); try { // this.dataPush(num, title, towerSceneBean.getSceneId(), towerSceneBean.getRoomId(), zipPath); }finally { // FileUtil.del(scenePath); // FileUtil.del(sceneSourcePath); } // FileUtil.del(scenePath); FileUtil.copy(zipPath, parentPath, true); FileUtil.del(scenePath); return zipPath; } @Override public void packSceneData(Long companyId, List nums) { String key = "tower:scene:data:download"; List scenePlusList = null; if(Objects.nonNull(companyId)){ //根据客户企业id查出所有相机 List cameraDetailList = cameraDetailService.getByCompanyId(companyId); if(CollUtil.isEmpty(cameraDetailList)){ return; } Set cameraIds = cameraDetailList.stream().map(detail -> detail.getCameraId()).collect(Collectors.toSet()); //根据相机id查出所有待下载场景 scenePlusList = scenePlusService.getByCameraIds(cameraIds); }else if(CollUtil.isNotEmpty(nums)){ scenePlusList = scenePlusService.list(new LambdaQueryWrapper().in(ScenePlus::getNum, nums)); }else{ String snCodeStr = fYunFileService.getFileContent(ossCameraPoolFilePath); List snCodeList = Arrays.asList(snCodeStr.split(",")); List cameraList = cameraService.listBySnCodes(snCodeList); List cameraIdList = cameraList.stream().map(Camera::getId).collect(Collectors.toList()); String sceneStr = fYunFileService.getFileContent(ossScenePoolFilePath); List towerSceneBeans = JSON.parseArray(sceneStr, TowerSceneBean.class); List titleList = towerSceneBeans.stream().map(TowerSceneBean::getName).collect(Collectors.toList()); scenePlusList = scenePlusService.list(new LambdaQueryWrapper().in(ScenePlus::getCameraId, cameraIdList).in(ScenePlus::getTitle, titleList)); List dbTitleList = scenePlusList.stream().map(ScenePlus::getTitle).collect(Collectors.toList()); List notFindList = titleList.stream().filter(title -> { if (dbTitleList.contains(title)) { return false; }else{ return true; } }).collect(Collectors.toList()); if(CollUtil.isNotEmpty(notFindList)){ fYunFileService.uploadFile(JSON.toJSONString(notFindList).getBytes(), ossParentPath.concat("notFind") .concat("-") .concat(DateExtUtil.format(new Date(), DateExtUtil.dateStyle7)) .concat(".txt")); } } if(CollUtil.isEmpty(scenePlusList)){ return; } //遍历场景列表开始下载 scenePlusList.stream().forEach(plus -> { JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(); jsonObject.put("title", plus.getTitle()); jsonObject.put("num", plus.getNum()); String zipPath = towerService.packSceneDataHandler(plus.getNum(), plus.getTitle()); jsonObject.put("status", CommonSuccessStatus.SUCCESS.code()); jsonObject.put("zipPath", zipPath); redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject)); }catch (Exception e){ log.error("推送失败,num:{},title:{}", plus.getNum(), plus.getTitle(), e); jsonObject.put("status", CommonSuccessStatus.FAIL.code()); jsonObject.put("error", ExceptionUtil.stacktraceToString(e, 3000)); redisUtil.hset(key, plus.getNum(), JSON.toJSONString(jsonObject)); } }); } @Override public void dataPush(String num, String title, String sceneId, String roomId, String zipPath) throws Exception { log.info("开始组装参数"); String fileMd5 = com.fdkankan.extend.util.FileUtil.getMulFileMD5(FileUtil.getInputStream(zipPath)); String compose = sceneId.concat("+").concat(roomId).concat("+").concat(fileMd5); String encrypt = RsaCryptTools.encryptData(compose, privateKey); Map params = new HashMap<>(); params.put("stationCode", sceneId); params.put("roomEntityID", roomId); params.put("file", FileUtil.file(zipPath)); params.put("manufact", manufact); params.put("encrypt", encrypt); params.put("upUserAcct", "xxx"); log.info("encrypt:{}", encrypt); log.info("开始推送"); String post = HttpUtil.post(url, params); JSONObject jsonObject = JSON.parseObject(post); this.dataPushLog(num, title, jsonObject.getString("resultCode"), jsonObject.getString("resultMsg")); } public static void main(String[] args) { Map params = new HashMap<>(); params.put("stationCode", "410192010000001484"); params.put("roomEntityID", "000102050000000003085104"); params.put("file", FileUtil.file("D:\\test\\410192010000001484_000102050000000003085104.zip")); params.put("manufact", "3"); params.put("upUserAcct", "111"); params.put("encrypt", "oNFZonGRrum3hldhU9AcMwP0JsXoxZocaTSTgj1h1Ag9pJzro9us0udinPr058y9KcwSPtkO58TKVE4csnasTZT00jghJQqsk0ATiNzX8QZh+F1iH33j6I3NHZu3vzjAV4lVHSo23WcEMo4s9G5dxymagYl8FKd7ua0/CYNysrs="); String post = HttpUtil.post("http://120.52.136.13:2808/fileManager/uploadRsaFileZipView", params); JSONObject jsonObject = JSON.parseObject(post); System.out.println(jsonObject.toJSONString()); } private void dataPushLog(String num, String title, String resultCode, String resultMsg){ String content = num.concat("-").concat(title).concat(" : ").concat(resultCode); if(StrUtil.isNotEmpty(resultMsg)){ content = content.concat("-").concat(resultMsg); } content = content.concat("\r\n"); FileUtil.writeUtf8String(content, parentPath.concat("download.log")); } }