CommonServiceImpl.java 14 KB

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