CommonServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package com.fdkankan.contro.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.common.util.FileUtils;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.fdkankan.common.constant.CommonStatus;
  10. import com.fdkankan.common.constant.PayStatus;
  11. import com.fdkankan.common.constant.SceneStatus;
  12. import com.fdkankan.common.constant.SpaceType;
  13. import com.fdkankan.common.util.FileUtils;
  14. import com.fdkankan.contro.entity.*;
  15. import com.fdkankan.contro.factory.UserEditData.UserEditDataHandler;
  16. import com.fdkankan.contro.factory.UserEditData.UserEditDataHandlerFactory;
  17. import com.fdkankan.contro.service.ICommonService;
  18. import com.fdkankan.contro.service.*;
  19. import com.fdkankan.contro.vo.ScenePlusVO;
  20. import com.fdkankan.fyun.config.FYunFileConfig;
  21. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  22. import com.fdkankan.model.constants.ConstantFilePath;
  23. import com.fdkankan.model.constants.UploadFilePath;
  24. import com.fdkankan.model.utils.FloorPlanUserUtil;
  25. import com.fdkankan.sms.SendMailAcceUtils;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.cloud.context.config.annotation.RefreshScope;
  30. import org.springframework.stereotype.Service;
  31. import java.io.File;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Objects;
  36. import java.util.Set;
  37. import java.util.Objects;
  38. import java.util.*;
  39. @Slf4j
  40. @Service
  41. @RefreshScope
  42. public class CommonServiceImpl implements ICommonService {
  43. @Value("${main.url}")
  44. private String mainUrl;
  45. @Value("${env:gn}")
  46. private String env;
  47. @Value("${helpLink.zh-cn:https://docs.4dkankan.com/#/product/4dkk/zh-cn/README}")
  48. private String helpLinkZh;
  49. @Value("${helpLink.en-us:https://docs.4dkankan.com/#/product/4dkk/en-us/README}")
  50. private String helpLinkEn;
  51. @Autowired
  52. private IScenePlusService scenePlusService;
  53. @Autowired
  54. private IScenePlusExtService scenePlusExtService;
  55. @Autowired
  56. private IMailTemplateService mailTemplateService;
  57. @Autowired
  58. private IUserService userService;
  59. @Autowired
  60. private ISceneProService sceneProService;
  61. @Autowired
  62. private ICameraDetailService cameraDetailService;
  63. @Autowired
  64. private IUserIncrementService userIncrementService;
  65. @Autowired
  66. private IIncrementTypeService incrementTypeService;
  67. @Autowired
  68. private ICameraService cameraService;
  69. @Autowired
  70. private FYunFileServiceInterface fYunFileService;
  71. @Autowired
  72. private FYunFileConfig fYunFileConfig;
  73. @Override
  74. public void uploadBuildResultData(String num, String dataSource, String version) {
  75. Map<String, String> uploadMap = new HashMap<>();
  76. String ossResultPath = String.format(UploadFilePath.scene_result_data_path, num);
  77. //删除旧的文件目录
  78. fYunFileService.deleteFolder(ossResultPath);
  79. //上传caches/images
  80. String localCachesImagePath = dataSource + "/caches/images/";
  81. String ossCachesImagePath = ossResultPath + "caches/images/";
  82. //先清除旧的全景图
  83. if(FileUtil.exist(localCachesImagePath)){
  84. fYunFileService.uploadFileByCommand(localCachesImagePath, ossCachesImagePath);
  85. }
  86. //上传project.json
  87. String localProjectJsonPath = dataSource + "/project.json";
  88. String ossProjectJsonPath = ossResultPath + "project.json";
  89. if(FileUtil.exist(localProjectJsonPath)){
  90. uploadMap.put(localProjectJsonPath, ossProjectJsonPath);
  91. }
  92. //上传data.json
  93. String localDataJsonPath = dataSource + "/data.json";
  94. String ossDataJsonPath = ossResultPath + "data.json";
  95. if(FileUtil.exist(localDataJsonPath)){
  96. uploadMap.put(localDataJsonPath, ossDataJsonPath);
  97. }
  98. //户型图上传
  99. String floorplanCadPath = ossResultPath + "floorplan_cad";
  100. String cadPicPathFormat = floorplanCadPath + "/floor-cad-%s.%s";
  101. String floorCadPath = dataSource + "/results/floorplan_cad";
  102. //清除原有旧的cad图
  103. fYunFileService.deleteFolder(floorplanCadPath);
  104. List<String> floorCadList = FileUtils.getFileList(floorCadPath);
  105. if(CollUtil.isNotEmpty(floorCadList)){
  106. floorCadList.stream().forEach(str->{
  107. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  108. String[] arr = substring.split("floor");
  109. String[] arr2 = arr[1].split("\\.");
  110. //上传到用户编辑目录
  111. uploadMap.put(str, String.format(cadPicPathFormat, arr2[0], arr2[1]));
  112. });
  113. }
  114. //上传深时场景生成obj需要的文件
  115. String localReconstruction = dataSource + "/caches/reconstruction/";
  116. String ossReconstruction = ossResultPath + "caches/reconstruction/";
  117. if(FileUtil.exist(localReconstruction)){
  118. fYunFileService.uploadFileByCommand(localReconstruction, ossReconstruction);
  119. }
  120. String localDepthmap = dataSource + "/caches/depthmap/";
  121. String ossDepthmap = ossResultPath + "caches/depthmap/";
  122. if(FileUtil.exist(localDepthmap)){
  123. fYunFileService.uploadFileByCommand(localDepthmap, ossDepthmap);
  124. }
  125. String localDepthmapAsc = dataSource + "/caches/depthmap_csc/";
  126. String ossDepthmapAsc = ossResultPath + "caches/depthmap_csc/";
  127. if(FileUtil.exist(localDepthmapAsc)){
  128. fYunFileService.uploadFileByCommand(localDepthmapAsc, ossDepthmapAsc);
  129. }
  130. String localPanoramaJson = dataSource + "/caches/panorama.json";
  131. String ossPanoramaJson = ossResultPath + "caches/panorama.json";
  132. if(FileUtil.exist(localPanoramaJson)){
  133. fYunFileService.uploadFile(localPanoramaJson, ossPanoramaJson);
  134. }
  135. // String localLaserPly = dataSource + "/results/laserData/laser.ply";
  136. // String ossLaserPly = ossResultPath + "results/laserData/laser.ply";
  137. // if(FileUtil.exist(localLaserPly)){
  138. // fYunFileServiceInterface.uploadFile(localLaserPly, ossLaserPly);
  139. // }
  140. String localFloorGroupFixJson = dataSource + "/caches/floor_group_fix.json";
  141. String ossFloorGroupFixJson = ossResultPath + "caches/floor_group_fix.json";
  142. if(FileUtil.exist(localFloorGroupFixJson)){
  143. fYunFileService.uploadFile(localFloorGroupFixJson, ossFloorGroupFixJson);
  144. }
  145. String localDepthmapVis = dataSource + "/caches/depthmap_vis";
  146. String ossDepthmapVis = ossResultPath + "caches/depthmap_vis";
  147. if(FileUtil.exist(localDepthmapVis)){
  148. fYunFileService.uploadFileByCommand(localDepthmapVis, ossDepthmapVis);
  149. }
  150. String localIntensity = dataSource + "/caches/intensity";
  151. String ossIntensity = ossResultPath + "caches/intensity";
  152. if(FileUtil.exist(localIntensity)){
  153. fYunFileService.uploadFileByCommand(localIntensity, ossIntensity);
  154. }
  155. //上传点位校准相关文件
  156. String localExtras = dataSource + "/extras";
  157. String ossExtras = ossResultPath + "extras";
  158. if(FileUtil.exist(localExtras)){
  159. fYunFileService.uploadFileByCommand(localExtras, ossExtras);
  160. }
  161. //开始上传
  162. fYunFileService.uploadMulFiles(uploadMap);
  163. }
  164. @Override
  165. public void uploadFloorplanJson(String num, String dataSource) throws Exception{
  166. String floorPlanCardFilePath = dataSource + File.separator + "results/floorplan_cad.json";
  167. if (!new File(floorPlanCardFilePath).exists()) {
  168. log.warn("floorplan_cad.json 文件不存在,文件路径:{}", floorPlanCardFilePath);
  169. return;
  170. }
  171. JSONObject json = FloorPlanUserUtil.createFloorPlanUserJson(floorPlanCardFilePath);
  172. if(Objects.isNull(json)){
  173. log.warn("生成floorplan.json失败,cadPath:{}", floorPlanCardFilePath);
  174. return;
  175. }
  176. String floorplanJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "floorplan.json";
  177. fYunFileService.uploadFile(json.toJSONString().getBytes(), floorplanJsonPath);
  178. }
  179. @Override
  180. public void initUserEditData(String num, Set<String> bizs, Map<String, Map<String ,Object>> params) {
  181. if(StrUtil.isEmpty(num) || CollUtil.isEmpty(bizs)){
  182. return;
  183. }
  184. for (String biz : bizs) {
  185. UserEditDataHandler handler = UserEditDataHandlerFactory.getHandler(biz);
  186. handler.init(num, CollUtil.isEmpty(params) ? null : params.get(biz));
  187. }
  188. }
  189. @Override
  190. public void sendEmail(String num) {
  191. try {
  192. Long userId = null;
  193. String websize = "";
  194. String title = "";
  195. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  196. if(Objects.isNull(scenePlus)){
  197. ScenePro scenePro = sceneProService.getByNum(num);
  198. if(Objects.isNull(scenePro)){
  199. return;
  200. }
  201. userId = scenePro.getUserId();
  202. websize = scenePro.getWebSite();
  203. title = scenePro.getSceneName();
  204. }else{
  205. userId = scenePlus.getUserId();
  206. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  207. websize = scenePlusExt.getWebSite();
  208. title = scenePlus.getTitle();
  209. }
  210. if(Objects.isNull(userId)){
  211. return;
  212. }
  213. User user = userService.getById(userId);
  214. if(Objects.isNull(user) || StrUtil.isEmpty(user.getUserName())){
  215. return;
  216. }
  217. String toEmail = user.getUserName();
  218. String helpLink = helpLinkEn;
  219. String lang = "en";
  220. if("gn".equals(env)){
  221. lang = "zh";
  222. helpLink = helpLinkZh;
  223. }
  224. websize += "&lang=" + lang;
  225. MailTemplate mailTemplate = mailTemplateService.getOne(new LambdaQueryWrapper<MailTemplate>().eq(MailTemplate::getRemark, "计算完成").eq(MailTemplate::getLang, lang));
  226. String content = mailTemplate.getMsg().replaceAll("scene_name", title).replaceAll("scene_link", websize).replaceAll("help_link", helpLink);
  227. SendMailAcceUtils.sendMail(mailTemplate.getSendMail(), mailTemplate.getSendPassword(), mailTemplate.getSendHost(), toEmail, mailTemplate.getSubject(), content, null);
  228. }catch (Exception e){
  229. log.error("发送邮件失败,num:" + num, e);
  230. }
  231. }
  232. public String getOssOrignPath(String path) {
  233. String ossPath = ConstantFilePath.OSS_PREFIX
  234. + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "")
  235. .replace(ConstantFilePath.BUILD_MODEL_LASER_PATH, "");
  236. if (!ossPath.endsWith("/")) {
  237. ossPath = ossPath.concat("/");
  238. }
  239. return ossPath;
  240. }
  241. @Override
  242. public int getPayStatus(Long cameraId, Long space){
  243. //查询权益统计容量的方式
  244. // String unit = SpaceType.GB.code();
  245. // CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
  246. // if(StrUtil.isNotEmpty(cameraDetail.getUnit())){
  247. // unit = cameraDetail.getUnit();
  248. // }
  249. // Long limit = this.getSpaceLimit(cameraDetail);
  250. //
  251. // if(SpaceType.SP.code().equals(unit)){//如果按空间方式统计,则space置为1
  252. // space = 1L;
  253. // }
  254. //
  255. // //更新相机使用用量
  256. // cameraDetailService.updateCameraDetailByCameraIdAndSpace(cameraId, space);
  257. //
  258. // if(limit == -1){
  259. // return PayStatus.PAY.code();
  260. // }
  261. //
  262. // Long usedSpace = cameraService.getUsedSpace(cameraId, unit);
  263. // if(usedSpace + space > limit){
  264. // return PayStatus.NO_CAPACITY.code();
  265. // }
  266. return PayStatus.PAY.code();
  267. }
  268. @Override
  269. public void uploadStatusJson(String num, Integer sceneStatus, String webSite, String thumb, Integer payStatus, String uploadPath) {
  270. String localDataPath = String.format(ConstantFilePath.DATABUFFER_FORMAT, num);
  271. JSONObject statusJson = new JSONObject();
  272. statusJson.put("status", sceneStatus);
  273. statusJson.put("webSite", webSite);
  274. statusJson.put("sceneNum", num);
  275. statusJson.put("thumb", thumb);
  276. statusJson.put("payStatus", PayStatus.NOT_PAY.code());
  277. statusJson.put("recStatus", 'A');
  278. FileUtils.writeFile(localDataPath + "status.json", statusJson.toString());
  279. fYunFileService.uploadFile(localDataPath + "status.json", uploadPath + "status.json");
  280. }
  281. @Override
  282. public Long getSpaceLimit(CameraDetail cameraDetail){
  283. Long limit = cameraDetail.getTotalSpace();
  284. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  285. if(Objects.nonNull(userIncrement) && userIncrement.getIsExpired() == CommonStatus.NO.code().intValue()){
  286. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  287. switch (SpaceType.get(cameraDetail.getUnit())){
  288. case GB:
  289. limit = incrementType.getCameraCapacity();
  290. if(limit != -1){
  291. limit = limit * 1024 * 1024 * 1024;
  292. }
  293. break;
  294. case SP:
  295. limit = incrementType.getCameraSpace();
  296. break;
  297. }
  298. }
  299. return limit;
  300. }
  301. @Override
  302. public Long getSpace(String num){
  303. Long space = fYunFileService.getSpace(fYunFileConfig.getBucket(), String.format(UploadFilePath.DATA_VIEW_PATH, num));
  304. space += fYunFileService.getSpace(fYunFileConfig.getBucket(), String.format(UploadFilePath.IMG_VIEW_PATH, num));
  305. space += fYunFileService.getSpace(fYunFileConfig.getBucket(), String.format(UploadFilePath.VIDEOS_VIEW_PATH, num));
  306. space += fYunFileService.getSpace(fYunFileConfig.getBucket(), String.format(UploadFilePath.VOICE_VIEW_PATH, num));
  307. space += fYunFileService.getSpace(fYunFileConfig.getBucket(), String.format(UploadFilePath.scene_result_data_path, num).concat("caches"));
  308. return space;
  309. }
  310. }