SceneEditServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  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.ErrorCode;
  11. import com.fdkankan.model.constants.UploadFilePath;
  12. import com.fdkankan.common.exception.BusinessException;
  13. import com.fdkankan.scene.httpclient.MyClient;
  14. import com.fdkankan.web.response.ResultData;
  15. import com.fdkankan.common.util.DateExtUtil;
  16. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  17. import com.fdkankan.scene.entity.Camera;
  18. import com.fdkankan.scene.entity.CameraDetail;
  19. import com.fdkankan.scene.entity.SceneCooperation;
  20. import com.fdkankan.scene.entity.ScenePlus;
  21. import com.fdkankan.scene.entity.ScenePlusExt;
  22. import com.fdkankan.scene.entity.SceneResource;
  23. import com.fdkankan.scene.entity.User;
  24. import com.fdkankan.scene.entity.UserIncrement;
  25. import com.fdkankan.scene.service.ICameraDetailService;
  26. import com.fdkankan.scene.service.ICameraService;
  27. import com.fdkankan.scene.service.ISceneCooperationService;
  28. import com.fdkankan.scene.service.ISceneEditInfoExtService;
  29. import com.fdkankan.scene.service.ISceneEditService;
  30. import com.fdkankan.scene.service.IScenePlusExtService;
  31. import com.fdkankan.scene.service.IScenePlusService;
  32. import com.fdkankan.scene.service.ISceneProService;
  33. import com.fdkankan.scene.service.ISceneResourceService;
  34. import com.fdkankan.scene.service.ISceneUploadService;
  35. import com.fdkankan.scene.service.IUserIncrementService;
  36. import com.fdkankan.scene.service.IUserService;
  37. import com.fdkankan.scene.vo.BaseDataParamVO;
  38. import com.fdkankan.scene.vo.BaseSceneParamVO;
  39. import com.fdkankan.scene.vo.DeleteFileParamVO;
  40. import com.fdkankan.scene.vo.LocalesParamVO;
  41. import com.fdkankan.scene.vo.SceneAuthVO;
  42. import com.fdkankan.web.user.SSOUser;
  43. import java.io.IOException;
  44. import java.nio.charset.StandardCharsets;
  45. import java.util.ArrayList;
  46. import java.util.Calendar;
  47. import java.util.Date;
  48. import java.util.List;
  49. import java.util.Objects;
  50. import lombok.extern.slf4j.Slf4j;
  51. import org.springframework.beans.factory.annotation.Autowired;
  52. import org.springframework.beans.factory.annotation.Value;
  53. import org.springframework.cloud.context.config.annotation.RefreshScope;
  54. import org.springframework.stereotype.Service;
  55. import org.springframework.util.ObjectUtils;
  56. /**
  57. * <p>
  58. * TODO
  59. * </p>
  60. *
  61. * @author dengsixing
  62. * @since 2022/3/11
  63. **/
  64. @Slf4j
  65. @Service
  66. @RefreshScope
  67. public class SceneEditServiceImpl implements ISceneEditService {
  68. @Value("${4dkk.manageService.basePath:#{null}}")
  69. private String manageServiceUrl;
  70. @Value("${4dkk.manageService.api.getServiceUpTip:#{null}}")
  71. private String getServiceUpTipApi;
  72. @Autowired
  73. ISceneProService sceneProService;
  74. @Autowired
  75. IScenePlusService scenePlusService;
  76. @Autowired
  77. private FYunFileServiceInterface fYunFileService;
  78. @Autowired
  79. private ISceneUploadService sceneUploadService;
  80. @Autowired
  81. private ISceneEditInfoExtService sceneEditInfoExtService;
  82. @Autowired
  83. private ICameraDetailService cameraDetailService;
  84. @Autowired
  85. private ICameraService cameraService;
  86. @Autowired
  87. private IUserIncrementService userIncrementService;
  88. @Value("${expected.time}")
  89. private String expectedTime;
  90. @Autowired
  91. private IUserService userService;
  92. @Autowired
  93. private ISceneCooperationService sceneCooperationService;
  94. @Autowired
  95. private ISceneResourceService sceneResourceService;
  96. @Autowired
  97. private IScenePlusExtService scenePlusExtService;
  98. @Value("${scene.expired.month:#{null}}")
  99. private Integer expiredMonth;
  100. @Autowired
  101. private MyClient myClient;
  102. @Override
  103. public SceneAuthVO getAuth(String num, SSOUser ssoUser){
  104. // 解密获得username,用于和数据库进行对比
  105. String username = ssoUser.getUserName();
  106. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  107. if(Objects.isNull(scenePlus)){
  108. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  109. }
  110. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  111. SceneAuthVO sceneAuthVO = new SceneAuthVO();
  112. List<SceneResource> exclude = new ArrayList<>();
  113. SceneResource excludeEntity = new SceneResource();
  114. excludeEntity.setKeyWord("data");
  115. exclude.add(excludeEntity);
  116. Camera camera = cameraService.findByChildName(username);
  117. sceneAuthVO.setExclude(exclude);
  118. sceneAuthVO.setInclude(new ArrayList<>());
  119. sceneAuthVO.setCompany(null);
  120. //判断data.json是否存在,如果存在,则证明计算在未被定时任务删除前上传过oss
  121. boolean exist = fYunFileService.fileExist(String.format(UploadFilePath.scene_result_data_path, num).concat("data.json"));
  122. if(!exist){
  123. sceneAuthVO.setSourceExpired(true);
  124. }
  125. //判断该场景是否属于增值权益
  126. boolean isVip = false;
  127. //获取该相机是否有权益
  128. boolean isExpired = false;
  129. // Camera sceneCamera = cameraService.getById(scenePlus.getCameraId());
  130. // if(sceneCamera != null){
  131. // UserIncrement userIncrementEntity = userIncrementService.findByCameraId(sceneCamera.getId());
  132. //
  133. // if(userIncrementEntity != null){
  134. // if( userIncrementEntity.getIsExpired().intValue() == 0){
  135. // isVip = true;
  136. // }
  137. // if(userIncrementEntity.getIsExpired().intValue() == 1){
  138. // isExpired = true;
  139. // }
  140. // }else{
  141. // try {
  142. // Date date = DateUtil.parse("2021-09-09 00:00:00", DateExtUtil.dateStyle);
  143. //
  144. // //非07批次的放开
  145. // String pc = sceneCamera.getSnCode().substring(0,2);
  146. // if(!pc.equals("07") ){
  147. // if(sceneCamera.getCreateTime()!=null && date.after(sceneCamera.getCreateTime())){
  148. // isVip = true;
  149. // isExpired = false;
  150. // }
  151. // }
  152. // }catch (Exception e){
  153. // e.printStackTrace();
  154. // }
  155. // }
  156. // }
  157. sceneAuthVO.setIsExpired(isExpired);
  158. sceneAuthVO.setIsVip(isVip);
  159. //判断过期时间
  160. // Date date = DateUtil.parse(expectedTime, DateExtUtil.dateStyle);
  161. // if(camera != null){
  162. // if(!scenePlus.getCameraId().equals(camera.getId())){
  163. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  164. // }
  165. //
  166. // CameraDetail cameraDetailEntity = cameraDetailService.findByCameraId(camera.getId());
  167. // if(cameraDetailEntity.getCompanyId() != null){
  168. // if(cameraDetailEntity.getCompanyId().longValue() == 1 || cameraDetailEntity.getCompanyId().longValue() == 14){
  169. // sceneAuthVO.setExclude(new ArrayList<>());
  170. // sceneAuthVO.setCompany(cameraDetailEntity.getCompanyId());
  171. // }
  172. // }
  173. // return sceneAuthVO;
  174. // }
  175. // User user = userService.findByUserName(username);
  176. // if("18750226207".equals(username)){
  177. // log.info("18750226207该账号默认超级管理员,可以操作所有场景");
  178. // }
  179. // else if(user == null || scenePlus.getUserId() == null || user.getId().longValue() != scenePlus.getUserId().longValue()){
  180. // log.info("user:" + user.getId() + ",scene:" + scenePlus.getUserId());
  181. //
  182. // List<SceneCooperation> list =
  183. // sceneCooperationService.list(
  184. // new LambdaQueryWrapper<SceneCooperation>()
  185. // .eq(SceneCooperation::getSceneNum, num));
  186. // if(list != null && list.size() > 0){
  187. // if(list.get(0).getUserId().longValue() != user.getId().longValue()){
  188. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  189. // }
  190. // }else {
  191. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  192. // }
  193. // }
  194. // if(scenePlus.getCameraId() != null){
  195. // camera = cameraService.getById(scenePlus.getCameraId());
  196. // if(camera != null){
  197. // CameraDetail cameraDetail = cameraDetailService.findByCameraId(camera.getId());
  198. // if(cameraDetail.getCompanyId() != null){
  199. // if(cameraDetail.getCompanyId().longValue() == 1 || cameraDetail.getCompanyId().longValue() == 14){
  200. // sceneAuthVO.setExclude(new ArrayList<>());
  201. // sceneAuthVO.setCompany(cameraDetail.getCompanyId());
  202. // }
  203. // }
  204. // }
  205. // }
  206. //权限控制完后判断若是协作账号,获取协作权限
  207. // SceneCooperation sceneCooperation = sceneCooperationService.getByNum(num);
  208. // //若数据为空表示可操作全部资源
  209. // if(sceneCooperation == null || sceneCooperation.getUserId().longValue() != ssoUser.getId().longValue()){
  210. // return sceneAuthVO;
  211. // }
  212. //
  213. // if(scenePlus.getUserId()!= null && sceneCooperation.getUserId()!= null &&
  214. // scenePlus.getUserId().equals(sceneCooperation.getUserId())){
  215. // return sceneAuthVO;
  216. // }
  217. //
  218. // sceneAuthVO.setInclude(sceneResourceService.findByCooperationId(sceneCooperation.getId()));
  219. return sceneAuthVO;
  220. }
  221. @Override
  222. public ResultData locales(LocalesParamVO param) throws Exception {
  223. String key = "v4/www/locales/" + param.getFile();
  224. fYunFileService.uploadFile(JSON.toJSONBytes(param.getData()), key);
  225. return ResultData.ok();
  226. }
  227. @Override
  228. public ResultData saveTour(BaseDataParamVO param) throws IOException {
  229. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  230. if(Objects.isNull(scenePlus)){
  231. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  232. }
  233. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  234. String key = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + "tour.json";
  235. fYunFileService.uploadFile(scenePlusExt.getYunFileBucket(), param.getData().getBytes(StandardCharsets.UTF_8), key);
  236. sceneEditInfoExtService.updateToursByNum(
  237. param.getNum(), Integer.valueOf(CommonStatus.YES.code()));
  238. return ResultData.ok();
  239. }
  240. @Override
  241. public ResultData deleteTour(BaseSceneParamVO param) throws Exception {
  242. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  243. if(Objects.isNull(scenePlus)){
  244. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  245. }
  246. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  247. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
  248. String tourJsonPath = userEditPath + "tour.json";
  249. String tourJson = fYunFileService.getFileContent(scenePlusExt.getYunFileBucket(), tourJsonPath);
  250. JSONArray tours = JSON.parseArray(tourJson);
  251. if(CollUtil.isNotEmpty(tours)){
  252. List<String> fileNames = new ArrayList<>();
  253. for (Object o : tours) {
  254. JSONObject tour = (JSONObject)o;
  255. JSONObject enter = tour.getJSONObject("enter");
  256. if(Objects.nonNull(enter)){
  257. String enterOver = enter.getString("cover");
  258. if(StrUtil.isNotEmpty(enterOver)){
  259. fileNames.add(enterOver);
  260. }
  261. }
  262. JSONObject exit = tour.getJSONObject("exit");
  263. if(Objects.nonNull(exit)){
  264. String exitOver = exit.getString("cover");
  265. if(StrUtil.isNotEmpty(exitOver)){
  266. fileNames.add(exitOver);
  267. }
  268. }
  269. String music = tour.getString("music");
  270. if(StrUtil.isNotEmpty(music)){
  271. fileNames.add( music);
  272. }
  273. }
  274. //批量删除资源文件
  275. if(CollUtil.isNotEmpty(fileNames)){
  276. sceneUploadService.delete(
  277. DeleteFileParamVO.builder()
  278. .num(param.getNum()).bizType("tour").fileNames(fileNames)
  279. .build());
  280. }
  281. }
  282. //删除tour.json文件
  283. fYunFileService.deleteFile(scenePlusExt.getYunFileBucket(), tourJsonPath);
  284. sceneEditInfoExtService.updateToursByNum(
  285. param.getNum(), Integer.valueOf(CommonStatus.NO.code()));
  286. return ResultData.ok();
  287. }
  288. @Override
  289. public ResultData getServiceUpTip() {
  290. try {
  291. String url = this.manageServiceUrl.concat(this.getServiceUpTipApi);
  292. String params = "type=" + 0;
  293. ResultData resultData = myClient.get(url, params);
  294. return ResultData.ok(resultData.getData());
  295. }catch (Exception e){
  296. return ResultData.ok();
  297. }
  298. }
  299. }