CommonServiceImpl.java 14 KB

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