package com.fdkankan.contro.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSONObject; import com.fdkankan.common.util.FileUtils; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.common.constant.CommonStatus; import com.fdkankan.common.constant.PayStatus; import com.fdkankan.common.constant.SceneStatus; import com.fdkankan.common.constant.SpaceType; import com.fdkankan.common.util.FileUtils; import com.fdkankan.contro.entity.*; import com.fdkankan.contro.factory.UserEditData.UserEditDataHandler; import com.fdkankan.contro.factory.UserEditData.UserEditDataHandlerFactory; import com.fdkankan.contro.service.ICommonService; import com.fdkankan.contro.service.*; import com.fdkankan.contro.vo.ScenePlusVO; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.model.constants.ConstantFilePath; import com.fdkankan.model.constants.UploadFilePath; import com.fdkankan.model.utils.FloorPlanUserUtil; import com.fdkankan.sms.SendMailAcceUtils; 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 java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.Objects; import java.util.*; @Slf4j @Service @RefreshScope public class CommonServiceImpl implements ICommonService { @Value("${main.url}") private String mainUrl; @Value("${env:gn}") private String env; @Value("${helpLink.zh-cn:https://docs.4dkankan.com/#/product/4dkk/zh-cn/README}") private String helpLinkZh; @Value("${helpLink.en-us:https://docs.4dkankan.com/#/product/4dkk/en-us/README}") private String helpLinkEn; @Autowired private FYunFileServiceInterface fYunFileServiceInterface; @Autowired private IScenePlusService scenePlusService; @Autowired private IScenePlusExtService scenePlusExtService; @Autowired private IMailTemplateService mailTemplateService; @Autowired private IUserService userService; @Autowired private ISceneProService sceneProService; @Autowired private ICameraDetailService cameraDetailService; @Autowired private IUserIncrementService userIncrementService; @Autowired private IIncrementTypeService incrementTypeService; @Autowired private ICameraService cameraService; @Override public Long uploadBuildResultData(String num, String dataSource, String version) { Long cacheSize = 0L; Map uploadMap = new HashMap<>(); String ossResultPath = String.format(UploadFilePath.scene_result_data_path, num); //删除旧的文件目录 fYunFileServiceInterface.deleteFolder(ossResultPath); //上传caches/images String localCachesImagePath = dataSource + "/caches/images/"; String ossCachesImagePath = ossResultPath + "caches/images/"; //先清除旧的全景图 if(FileUtil.exist(localCachesImagePath)){ cacheSize += FileUtil.size(new File(localCachesImagePath)); fYunFileServiceInterface.uploadFileByCommand(localCachesImagePath, ossCachesImagePath); } //上传project.json String localProjectJsonPath = dataSource + "/project.json"; String ossProjectJsonPath = ossResultPath + "project.json"; if(FileUtil.exist(localProjectJsonPath)){ uploadMap.put(localProjectJsonPath, ossProjectJsonPath); } //上传data.json String localDataJsonPath = dataSource + "/data.json"; String ossDataJsonPath = ossResultPath + "data.json"; if(FileUtil.exist(localDataJsonPath)){ uploadMap.put(localDataJsonPath, ossDataJsonPath); } //户型图上传 String floorplanCadPath = ossResultPath + "floorplan_cad"; String cadPicPathFormat = floorplanCadPath + "/floor-cad-%s.%s"; String floorCadPath = dataSource + "/results/floorplan_cad"; //清除原有旧的cad图 fYunFileServiceInterface.deleteFolder(floorplanCadPath); List floorCadList = FileUtils.getFileList(floorCadPath); if(CollUtil.isNotEmpty(floorCadList)){ floorCadList.stream().forEach(str->{ String substring = str.substring(str.lastIndexOf(File.separator) + 1); String[] arr = substring.split("floor"); String[] arr2 = arr[1].split("\\."); //上传到用户编辑目录 uploadMap.put(str, String.format(cadPicPathFormat, arr2[0], arr2[1])); }); } //上传深时场景生成obj需要的文件 String localReconstruction = dataSource + "/caches/reconstruction/"; String ossReconstruction = ossResultPath + "caches/reconstruction/"; if(FileUtil.exist(localReconstruction)){ cacheSize += FileUtil.size(new File(localReconstruction)); fYunFileServiceInterface.uploadFileByCommand(localReconstruction, ossReconstruction); } String localDepthmap = dataSource + "/caches/depthmap/"; String ossDepthmap = ossResultPath + "caches/depthmap/"; if(FileUtil.exist(localDepthmap)){ cacheSize += FileUtil.size(new File(localDepthmap)); fYunFileServiceInterface.uploadFileByCommand(localDepthmap, ossDepthmap); } String localDepthmapAsc = dataSource + "/caches/depthmap_csc/"; String ossDepthmapAsc = ossResultPath + "caches/depthmap_csc/"; if(FileUtil.exist(localDepthmapAsc)){ cacheSize += FileUtil.size(new File(localDepthmapAsc)); fYunFileServiceInterface.uploadFileByCommand(localDepthmapAsc, ossDepthmapAsc); } String localPanoramaJson = dataSource + "/caches/panorama.json"; String ossPanoramaJson = ossResultPath + "caches/panorama.json"; if(FileUtil.exist(localPanoramaJson)){ cacheSize += FileUtil.size(new File(localPanoramaJson)); fYunFileServiceInterface.uploadFile(localPanoramaJson, ossPanoramaJson); } // String localLaserPly = dataSource + "/results/laserData/laser.ply"; // String ossLaserPly = ossResultPath + "results/laserData/laser.ply"; // if(FileUtil.exist(localLaserPly)){ // fYunFileServiceInterface.uploadFile(localLaserPly, ossLaserPly); // } String localFloorGroupFixJson = dataSource + "/caches/floor_group_fix.json"; String ossFloorGroupFixJson = ossResultPath + "caches/floor_group_fix.json"; if(FileUtil.exist(localFloorGroupFixJson)){ cacheSize += FileUtil.size(new File(localFloorGroupFixJson)); fYunFileServiceInterface.uploadFile(localFloorGroupFixJson, ossFloorGroupFixJson); } String localDepthmapVis = dataSource + "/caches/depthmap_vis"; String ossDepthmapVis = ossResultPath + "caches/depthmap_vis"; if(FileUtil.exist(localDepthmapVis)){ cacheSize += FileUtil.size(new File(localDepthmapVis)); fYunFileServiceInterface.uploadFileByCommand(localDepthmapVis, ossDepthmapVis); } String localIntensity = dataSource + "/caches/intensity"; String ossIntensity = ossResultPath + "caches/intensity"; if(FileUtil.exist(localIntensity)){ cacheSize += FileUtil.size(new File(localIntensity)); fYunFileServiceInterface.uploadFileByCommand(localIntensity, ossIntensity); } //开始上传 fYunFileServiceInterface.uploadMulFiles(uploadMap); return cacheSize; } @Override public void uploadFloorplanJson(String num, String dataSource) throws Exception{ String floorPlanCardFilePath = dataSource + File.separator + "results/floorplan_cad.json"; if (!new File(floorPlanCardFilePath).exists()) { log.warn("floorplan_cad.json 文件不存在,文件路径:{}", floorPlanCardFilePath); return; } JSONObject json = FloorPlanUserUtil.createFloorPlanUserJson(floorPlanCardFilePath); if(Objects.isNull(json)){ log.warn("生成floorplan.json失败,cadPath:{}", floorPlanCardFilePath); return; } String floorplanJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "floorplan.json"; fYunFileServiceInterface.uploadFile(json.toJSONString().getBytes(), floorplanJsonPath); } @Override public void initUserEditData(String num, Set bizs, Map> params) { if(StrUtil.isEmpty(num) || CollUtil.isEmpty(bizs)){ return; } for (String biz : bizs) { UserEditDataHandler handler = UserEditDataHandlerFactory.getHandler(biz); handler.init(num, CollUtil.isEmpty(params) ? null : params.get(biz)); } } @Override public void sendEmail(String num) { try { Long userId = null; String websize = ""; String title = ""; ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num); if(Objects.isNull(scenePlus)){ ScenePro scenePro = sceneProService.getByNum(num); if(Objects.isNull(scenePro)){ return; } userId = scenePro.getUserId(); websize = scenePro.getWebSite(); title = scenePro.getSceneName(); }else{ userId = scenePlus.getUserId(); ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId()); websize = scenePlusExt.getWebSite(); title = scenePlus.getTitle(); } if(Objects.isNull(userId)){ return; } User user = userService.getById(userId); if(Objects.isNull(user) || StrUtil.isEmpty(user.getUserName())){ return; } String toEmail = user.getUserName(); String helpLink = helpLinkEn; String lang = "en"; if("gn".equals(env)){ lang = "zh"; helpLink = helpLinkZh; } websize += "&lang=" + lang; MailTemplate mailTemplate = mailTemplateService.getOne(new LambdaQueryWrapper().eq(MailTemplate::getRemark, "计算完成").eq(MailTemplate::getLang, lang)); String content = mailTemplate.getMsg().replaceAll("scene_name", title).replaceAll("scene_link", websize).replaceAll("help_link", helpLink); SendMailAcceUtils.sendMail(mailTemplate.getSendMail(), mailTemplate.getSendPassword(), mailTemplate.getSendHost(), toEmail, mailTemplate.getSubject(), content, null); }catch (Exception e){ log.error("发送邮件失败,num:" + num, e); } } public String getOssOrignPath(String path) { String ossPath = ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, ""); if (!ossPath.endsWith("/")) { ossPath = ossPath.concat("/"); } return ossPath; } @Override public int getPayStatus(Long cameraId, Long space){ //查询权益统计容量的方式 String unit = SpaceType.GB.code(); CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId); if(StrUtil.isNotEmpty(cameraDetail.getUnit())){ unit = cameraDetail.getUnit(); } Long limit = this.getSpaceLimit(cameraDetail); if(SpaceType.SP.code().equals(unit)){//如果按空间方式统计,则space置为1 space = 1L; } //更新相机使用用量 cameraDetailService.updateCameraDetailByCameraIdAndSpace(cameraId, space); if(limit == -1){ return PayStatus.PAY.code(); } Long usedSpace = cameraService.getUsedSpace(cameraId, unit); if(usedSpace + space > limit){ return PayStatus.NO_CAPACITY.code(); } return PayStatus.PAY.code(); } @Override public void uploadStatusJson(String num, Integer sceneStatus, String webSite, String thumb, Integer payStatus, String uploadPath) { String localDataPath = String.format(ConstantFilePath.DATABUFFER_FORMAT, num); JSONObject statusJson = new JSONObject(); statusJson.put("status", sceneStatus); statusJson.put("webSite", webSite); statusJson.put("sceneNum", num); statusJson.put("thumb", thumb); statusJson.put("payStatus", PayStatus.NOT_PAY.code()); statusJson.put("recStatus", 'A'); FileUtils.writeFile(localDataPath + "status.json", statusJson.toString()); fYunFileServiceInterface.uploadFile(localDataPath + "status.json", uploadPath + "status.json"); } @Override public Long getSpaceLimit(CameraDetail cameraDetail){ Long limit = cameraDetail.getTotalSpace(); UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId()); if(Objects.nonNull(userIncrement) && userIncrement.getIsExpired() == CommonStatus.NO.code().intValue()){ IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId()); switch (SpaceType.get(cameraDetail.getUnit())){ case GB: limit = incrementType.getCameraCapacity(); if(limit != -1){ limit = limit * 1024 * 1024 * 1024; } break; case SP: limit = incrementType.getCameraSpace(); break; } } return limit; } }