SceneEditServiceImpl.java 12 KB

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