CommonServiceImpl.java 12 KB

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