package com.fdkankan.manage_jp.service.impl; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fdkankan.common.util.FileUtils; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.image.MatrixToImageWriterUtil; import com.fdkankan.manage_jp.common.ResultCode; import com.fdkankan.manage_jp.exception.BusinessException; import com.fdkankan.manage_jp.service.IScene3dNumService; import com.fdkankan.manage_jp.util.SceneResourcePath; import com.fdkankan.manage_jp.util.SnowflakeIdGenerator; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.util.Date; @Service @Slf4j public class SceneCommonService { @Autowired FYunFileServiceInterface fYunFileServiceInterface; @Autowired IScene3dNumService scene3dNumService; public String getNewNum(String oldNum ){ String newNum = scene3dNumService.generateNum(); if(oldNum.contains("-")){ String pre = oldNum.split("-")[0]; if(!"t".equals(pre)){ return pre +"-"+ newNum; } } return newNum; } /** * 生成新的dataSouce */ public String getNewDataSource(String oldDataSource){ String newDataSource = null; if(StringUtils.isBlank(oldDataSource)){ throw new BusinessException(ResultCode.SCENE_DATA_ERROR); } File oldDataSouceFile = new File(oldDataSource); if(!oldDataSouceFile.exists()){ throw new BusinessException(ResultCode.SCENE_DATA_ERROR); } if(!oldDataSource.contains("/")){ throw new BusinessException(ResultCode.SCENE_DATA_ERROR); } String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT); String[] split = oldDataSource.split("/"); if(split.length == 6 ){ String oldFileId = split[4]; Long fileId = SnowflakeIdGenerator.snowflakeIdGenerator.nextId(); newDataSource = oldDataSource.replace(oldFileId,fileId.toString()); String snCodeTime = split[5]; if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){ throw new BusinessException(ResultCode.SCENE_DATA_ERROR); } newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time); this.copyFdage(oldDataSource,newDataSource,time); } if(newDataSource == null){ throw new BusinessException(ResultCode.SCENE_DATA_ERROR); } return newDataSource; } public void createNewQrCode(String newNum, String webSite){ try { String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png"; String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png"; MatrixToImageWriterUtil.createQRCode(webSite, outPathZh, false,null); MatrixToImageWriterUtil.createQRCode(webSite + "&lang=en", outPathEn, false, null); fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png"); fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png"); }catch (Exception e){ log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e); } } public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String oldSceneName,String newSceneName,String sceneVersion) { String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + "scene.json"; File file = new File(localPath); if(!file.exists()){ log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum); return; } String fileContent = cn.hutool.core.io.FileUtil.readUtf8String(file); if(StringUtils.isNotBlank(fileContent)){ String newJson = fileContent.replaceAll(oldNum,newNum); newJson = newJson.replaceAll(oldSceneName,newSceneName); try { com.fdkankan.manage_jp.util.FileUtil.writeFile(localPath,newJson); }catch (Exception e){ log.error("writeFile-error:{}",e); } } if("v3".equals(sceneVersion)){ String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/scene.json", newNum); fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath); } if("v4".equals(sceneVersion)){ String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"scene.json", newNum); fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath); } } public void updateOssJson(String targetPath,String oldNum, String newNum,String sceneVersion) { String ossStatusJsonPath = targetPath + "/" + "status.json"; if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){ log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum); return; } String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath; File file = new File(localPath); if(!file.getParentFile().exists()){ file.mkdirs(); } String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath); if(StringUtils.isNotBlank(fileContent)){ String newJson = fileContent.replaceAll(oldNum,newNum); try { com.fdkankan.manage_jp.util.FileUtil.writeFile(localPath,newJson); fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath); }catch (Exception e){ log.error("writeFile-error:{}",e); } } } public void copyFdage(String oldDataSource,String newDataSource,String time){ String ossOldPath = oldDataSource.replace("/mnt/data","home"); String ossNewPath = newDataSource.replace("/mnt/data","home"); fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath); String fileName = "/data.fdage"; String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName); if(StringUtils.isNotBlank(fileContent)){ JSONObject jsonObject = JSONObject.parseObject(fileContent); jsonObject.put("uuidtime",time); FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString()); fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName); } } }