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}")
  69. private String manageServiceUrl;
  70. @Value("${4dkk.manageService.api.getServiceUpTip}")
  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. if(userIncrementEntity != null){
  133. if( userIncrementEntity.getIsExpired().intValue() == 0){
  134. isVip = true;
  135. }
  136. if(userIncrementEntity.getIsExpired().intValue() == 1){
  137. isExpired = true;
  138. }
  139. }else{
  140. try {
  141. Date date = DateUtil.parse("2021-09-09 00:00:00", DateExtUtil.dateStyle);
  142. //非07批次的放开
  143. String pc = sceneCamera.getSnCode().substring(0,2);
  144. if(!pc.equals("07") ){
  145. if(sceneCamera.getCreateTime()!=null && date.after(sceneCamera.getCreateTime())){
  146. isVip = true;
  147. isExpired = false;
  148. }
  149. }
  150. }catch (Exception e){
  151. e.printStackTrace();
  152. }
  153. }
  154. }
  155. sceneAuthVO.setIsExpired(isExpired);
  156. sceneAuthVO.setIsVip(isVip);
  157. //判断过期时间
  158. Date date = DateUtil.parse(expectedTime, DateExtUtil.dateStyle);
  159. if(camera != null){
  160. if(!scenePlus.getCameraId().equals(camera.getId())){
  161. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  162. }
  163. CameraDetail cameraDetailEntity = cameraDetailService.findByCameraId(camera.getId());
  164. if(cameraDetailEntity.getCompanyId() != null){
  165. if(cameraDetailEntity.getCompanyId().longValue() == 1 || cameraDetailEntity.getCompanyId().longValue() == 14){
  166. sceneAuthVO.setExclude(new ArrayList<>());
  167. sceneAuthVO.setCompany(cameraDetailEntity.getCompanyId());
  168. }
  169. }
  170. return sceneAuthVO;
  171. }
  172. User user = userService.findByUserName(username);
  173. if("18750226207".equals(username)){
  174. log.info("18750226207该账号默认超级管理员,可以操作所有场景");
  175. }
  176. else if(user == null || scenePlus.getUserId() == null || user.getId().longValue() != scenePlus.getUserId().longValue()){
  177. log.info("user:" + user.getId() + ",scene:" + scenePlus.getUserId());
  178. List<SceneCooperation> list =
  179. sceneCooperationService.list(
  180. new LambdaQueryWrapper<SceneCooperation>()
  181. .eq(SceneCooperation::getSceneNum, num));
  182. if(list != null && list.size() > 0){
  183. if(list.get(0).getUserId().longValue() != user.getId().longValue()){
  184. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  185. }
  186. }else {
  187. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  188. }
  189. }
  190. if(scenePlus.getCameraId() != null){
  191. camera = cameraService.getById(scenePlus.getCameraId());
  192. if(camera != null){
  193. CameraDetail cameraDetail = cameraDetailService.findByCameraId(camera.getId());
  194. if(cameraDetail.getCompanyId() != null){
  195. if(cameraDetail.getCompanyId().longValue() == 1 || cameraDetail.getCompanyId().longValue() == 14){
  196. sceneAuthVO.setExclude(new ArrayList<>());
  197. sceneAuthVO.setCompany(cameraDetail.getCompanyId());
  198. }
  199. }
  200. }
  201. }
  202. //权限控制完后判断若是协作账号,获取协作权限
  203. SceneCooperation sceneCooperation = sceneCooperationService.getByNum(num);
  204. //若数据为空表示可操作全部资源
  205. if(sceneCooperation == null || sceneCooperation.getUserId().longValue() != ssoUser.getId().longValue()){
  206. return sceneAuthVO;
  207. }
  208. if(scenePlus.getUserId()!= null && sceneCooperation.getUserId()!= null &&
  209. scenePlus.getUserId().equals(sceneCooperation.getUserId())){
  210. return sceneAuthVO;
  211. }
  212. sceneAuthVO.setInclude(sceneResourceService.findByCooperationId(sceneCooperation.getId()));
  213. return sceneAuthVO;
  214. }
  215. @Override
  216. public ResultData locales(LocalesParamVO param) throws Exception {
  217. String key = "v4/www/locales/" + param.getFile();
  218. fYunFileService.uploadFile(JSON.toJSONBytes(param.getData()), key);
  219. return ResultData.ok();
  220. }
  221. @Override
  222. public ResultData saveTour(BaseDataParamVO param) throws IOException {
  223. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  224. if(Objects.isNull(scenePlus)){
  225. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  226. }
  227. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  228. String key = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + "tour.json";
  229. fYunFileService.uploadFile(scenePlusExt.getYunFileBucket(), param.getData().getBytes(StandardCharsets.UTF_8), key);
  230. sceneEditInfoExtService.updateToursByNum(
  231. param.getNum(), Integer.valueOf(CommonStatus.YES.code()));
  232. return ResultData.ok();
  233. }
  234. @Override
  235. public ResultData deleteTour(BaseSceneParamVO param) throws Exception {
  236. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  237. if(Objects.isNull(scenePlus)){
  238. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  239. }
  240. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  241. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
  242. String tourJsonPath = userEditPath + "tour.json";
  243. String tourJson = fYunFileService.getFileContent(scenePlusExt.getYunFileBucket(), tourJsonPath);
  244. JSONArray tours = JSON.parseArray(tourJson);
  245. if(CollUtil.isNotEmpty(tours)){
  246. List<String> fileNames = new ArrayList<>();
  247. for (Object o : tours) {
  248. JSONObject tour = (JSONObject)o;
  249. JSONObject enter = tour.getJSONObject("enter");
  250. if(Objects.nonNull(enter)){
  251. String enterOver = enter.getString("cover");
  252. if(StrUtil.isNotEmpty(enterOver)){
  253. fileNames.add(enterOver);
  254. }
  255. }
  256. JSONObject exit = tour.getJSONObject("exit");
  257. if(Objects.nonNull(exit)){
  258. String exitOver = exit.getString("cover");
  259. if(StrUtil.isNotEmpty(exitOver)){
  260. fileNames.add(exitOver);
  261. }
  262. }
  263. String music = tour.getString("music");
  264. if(StrUtil.isNotEmpty(music)){
  265. fileNames.add( music);
  266. }
  267. }
  268. //批量删除资源文件
  269. if(CollUtil.isNotEmpty(fileNames)){
  270. sceneUploadService.delete(
  271. DeleteFileParamVO.builder()
  272. .num(param.getNum()).bizType("tour").fileNames(fileNames)
  273. .build());
  274. }
  275. }
  276. //删除tour.json文件
  277. fYunFileService.deleteFile(scenePlusExt.getYunFileBucket(), tourJsonPath);
  278. sceneEditInfoExtService.updateToursByNum(
  279. param.getNum(), Integer.valueOf(CommonStatus.NO.code()));
  280. return ResultData.ok();
  281. }
  282. @Override
  283. public ResultData getServiceUpTip() {
  284. try {
  285. String url = this.manageServiceUrl.concat(this.getServiceUpTipApi);
  286. String params = "type=" + 0;
  287. ResultData resultData = myClient.get(url, params);
  288. return ResultData.ok(resultData.getData());
  289. }catch (Exception e){
  290. return ResultData.ok();
  291. }
  292. }
  293. }