CommonServiceImpl.java 13 KB

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