CommonServiceImpl.java 14 KB

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