123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- package com.fdkankan.scene.service.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.fdkankan.common.constant.CommonStatus;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.model.constants.UploadFilePath;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.scene.httpclient.MyClient;
- import com.fdkankan.web.response.ResultData;
- import com.fdkankan.common.util.DateExtUtil;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.scene.entity.Camera;
- import com.fdkankan.scene.entity.CameraDetail;
- import com.fdkankan.scene.entity.SceneCooperation;
- import com.fdkankan.scene.entity.ScenePlus;
- import com.fdkankan.scene.entity.ScenePlusExt;
- import com.fdkankan.scene.entity.SceneResource;
- import com.fdkankan.scene.entity.User;
- import com.fdkankan.scene.entity.UserIncrement;
- import com.fdkankan.scene.service.ICameraDetailService;
- import com.fdkankan.scene.service.ICameraService;
- import com.fdkankan.scene.service.ISceneCooperationService;
- import com.fdkankan.scene.service.ISceneEditInfoExtService;
- import com.fdkankan.scene.service.ISceneEditService;
- import com.fdkankan.scene.service.IScenePlusExtService;
- import com.fdkankan.scene.service.IScenePlusService;
- import com.fdkankan.scene.service.ISceneProService;
- import com.fdkankan.scene.service.ISceneResourceService;
- import com.fdkankan.scene.service.ISceneUploadService;
- import com.fdkankan.scene.service.IUserIncrementService;
- import com.fdkankan.scene.service.IUserService;
- import com.fdkankan.scene.vo.BaseDataParamVO;
- import com.fdkankan.scene.vo.BaseSceneParamVO;
- import com.fdkankan.scene.vo.DeleteFileParamVO;
- import com.fdkankan.scene.vo.LocalesParamVO;
- import com.fdkankan.scene.vo.SceneAuthVO;
- import com.fdkankan.web.user.SSOUser;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Objects;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.stereotype.Service;
- import org.springframework.util.ObjectUtils;
- /**
- * <p>
- * TODO
- * </p>
- *
- * @author dengsixing
- * @since 2022/3/11
- **/
- @Slf4j
- @Service
- @RefreshScope
- public class SceneEditServiceImpl implements ISceneEditService {
- @Value("${4dkk.manageService.basePath}")
- private String manageServiceUrl;
- @Value("${4dkk.manageService.api.getServiceUpTip}")
- private String getServiceUpTipApi;
- @Autowired
- ISceneProService sceneProService;
- @Autowired
- IScenePlusService scenePlusService;
- @Autowired
- private FYunFileServiceInterface fYunFileService;
- @Autowired
- private ISceneUploadService sceneUploadService;
- @Autowired
- private ISceneEditInfoExtService sceneEditInfoExtService;
- @Autowired
- private ICameraDetailService cameraDetailService;
- @Autowired
- private ICameraService cameraService;
- @Autowired
- private IUserIncrementService userIncrementService;
- @Value("${expected.time}")
- private String expectedTime;
- @Autowired
- private IUserService userService;
- @Autowired
- private ISceneCooperationService sceneCooperationService;
- @Autowired
- private ISceneResourceService sceneResourceService;
- @Autowired
- private IScenePlusExtService scenePlusExtService;
- @Value("${scene.expired.month:#{null}}")
- private Integer expiredMonth;
- @Autowired
- private MyClient myClient;
- @Override
- public SceneAuthVO getAuth(String num, SSOUser ssoUser){
- // 解密获得username,用于和数据库进行对比
- String username = ssoUser.getUserName();
- ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
- if(Objects.isNull(scenePlus)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
- }
- ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
- SceneAuthVO sceneAuthVO = new SceneAuthVO();
- List<SceneResource> exclude = new ArrayList<>();
- SceneResource excludeEntity = new SceneResource();
- excludeEntity.setKeyWord("data");
- exclude.add(excludeEntity);
- Camera camera = cameraService.findByChildName(username);
- sceneAuthVO.setExclude(exclude);
- sceneAuthVO.setInclude(new ArrayList<>());
- sceneAuthVO.setCompany(null);
- //判断data.json是否存在,如果存在,则证明计算在未被定时任务删除前上传过oss
- boolean exist = fYunFileService.fileExist(String.format(UploadFilePath.scene_result_data_path, num).concat("data.json"));
- if(!exist){
- sceneAuthVO.setSourceExpired(true);
- }
- //判断该场景是否属于增值权益
- boolean isVip = false;
- //获取该相机是否有权益
- boolean isExpired = false;
- Camera sceneCamera = cameraService.getById(scenePlus.getCameraId());
- if(sceneCamera != null){
- UserIncrement userIncrementEntity = userIncrementService.findByCameraId(sceneCamera.getId());
- if(userIncrementEntity != null){
- if( userIncrementEntity.getIsExpired().intValue() == 0){
- isVip = true;
- }
- if(userIncrementEntity.getIsExpired().intValue() == 1){
- isExpired = true;
- }
- }else{
- try {
- Date date = DateUtil.parse("2021-09-09 00:00:00", DateExtUtil.dateStyle);
- //非07批次的放开
- String pc = sceneCamera.getSnCode().substring(0,2);
- if(!pc.equals("07") ){
- if(sceneCamera.getCreateTime()!=null && date.after(sceneCamera.getCreateTime())){
- isVip = true;
- isExpired = false;
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- sceneAuthVO.setIsExpired(isExpired);
- sceneAuthVO.setIsVip(isVip);
- //判断过期时间
- Date date = DateUtil.parse(expectedTime, DateExtUtil.dateStyle);
- if(camera != null){
- if(!scenePlus.getCameraId().equals(camera.getId())){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- CameraDetail cameraDetailEntity = cameraDetailService.findByCameraId(camera.getId());
- if(cameraDetailEntity.getCompanyId() != null){
- if(cameraDetailEntity.getCompanyId().longValue() == 1 || cameraDetailEntity.getCompanyId().longValue() == 14){
- sceneAuthVO.setExclude(new ArrayList<>());
- sceneAuthVO.setCompany(cameraDetailEntity.getCompanyId());
- }
- }
- return sceneAuthVO;
- }
- User user = userService.findByUserName(username);
- if("18750226207".equals(username)){
- log.info("18750226207该账号默认超级管理员,可以操作所有场景");
- }
- else if(user == null || scenePlus.getUserId() == null || user.getId().longValue() != scenePlus.getUserId().longValue()){
- log.info("user:" + user.getId() + ",scene:" + scenePlus.getUserId());
- List<SceneCooperation> list =
- sceneCooperationService.list(
- new LambdaQueryWrapper<SceneCooperation>()
- .eq(SceneCooperation::getSceneNum, num));
- if(list != null && list.size() > 0){
- if(list.get(0).getUserId().longValue() != user.getId().longValue()){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- }else {
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- }
- if(scenePlus.getCameraId() != null){
- camera = cameraService.getById(scenePlus.getCameraId());
- if(camera != null){
- CameraDetail cameraDetail = cameraDetailService.findByCameraId(camera.getId());
- if(cameraDetail.getCompanyId() != null){
- if(cameraDetail.getCompanyId().longValue() == 1 || cameraDetail.getCompanyId().longValue() == 14){
- sceneAuthVO.setExclude(new ArrayList<>());
- sceneAuthVO.setCompany(cameraDetail.getCompanyId());
- }
- }
- }
- }
- //权限控制完后判断若是协作账号,获取协作权限
- SceneCooperation sceneCooperation = sceneCooperationService.getByNum(num);
- //若数据为空表示可操作全部资源
- if(sceneCooperation == null || sceneCooperation.getUserId().longValue() != ssoUser.getId().longValue()){
- return sceneAuthVO;
- }
- if(scenePlus.getUserId()!= null && sceneCooperation.getUserId()!= null &&
- scenePlus.getUserId().equals(sceneCooperation.getUserId())){
- return sceneAuthVO;
- }
- sceneAuthVO.setInclude(sceneResourceService.findByCooperationId(sceneCooperation.getId()));
- return sceneAuthVO;
- }
- @Override
- public ResultData locales(LocalesParamVO param) throws Exception {
- String key = "v4/www/locales/" + param.getFile();
- fYunFileService.uploadFile(JSON.toJSONBytes(param.getData()), key);
- return ResultData.ok();
- }
- @Override
- public ResultData saveTour(BaseDataParamVO param) throws IOException {
- ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
- if(Objects.isNull(scenePlus)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
- }
- ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
- String key = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + "tour.json";
- fYunFileService.uploadFile(scenePlusExt.getYunFileBucket(), param.getData().getBytes(StandardCharsets.UTF_8), key);
- sceneEditInfoExtService.updateToursByNum(
- param.getNum(), Integer.valueOf(CommonStatus.YES.code()));
- return ResultData.ok();
- }
- @Override
- public ResultData deleteTour(BaseSceneParamVO param) throws Exception {
- ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
- if(Objects.isNull(scenePlus)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
- }
- ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
- String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
- String tourJsonPath = userEditPath + "tour.json";
- String tourJson = fYunFileService.getFileContent(scenePlusExt.getYunFileBucket(), tourJsonPath);
- JSONArray tours = JSON.parseArray(tourJson);
- if(CollUtil.isNotEmpty(tours)){
- List<String> fileNames = new ArrayList<>();
- for (Object o : tours) {
- JSONObject tour = (JSONObject)o;
- JSONObject enter = tour.getJSONObject("enter");
- if(Objects.nonNull(enter)){
- String enterOver = enter.getString("cover");
- if(StrUtil.isNotEmpty(enterOver)){
- fileNames.add(enterOver);
- }
- }
- JSONObject exit = tour.getJSONObject("exit");
- if(Objects.nonNull(exit)){
- String exitOver = exit.getString("cover");
- if(StrUtil.isNotEmpty(exitOver)){
- fileNames.add(exitOver);
- }
- }
- String music = tour.getString("music");
- if(StrUtil.isNotEmpty(music)){
- fileNames.add( music);
- }
- }
- //批量删除资源文件
- if(CollUtil.isNotEmpty(fileNames)){
- sceneUploadService.delete(
- DeleteFileParamVO.builder()
- .num(param.getNum()).bizType("tour").fileNames(fileNames)
- .build());
- }
- }
- //删除tour.json文件
- fYunFileService.deleteFile(scenePlusExt.getYunFileBucket(), tourJsonPath);
- sceneEditInfoExtService.updateToursByNum(
- param.getNum(), Integer.valueOf(CommonStatus.NO.code()));
- return ResultData.ok();
- }
- @Override
- public ResultData getServiceUpTip() {
- try {
- String url = this.manageServiceUrl.concat(this.getServiceUpTipApi);
- String params = "type=" + 0;
- ResultData resultData = myClient.get(url, params);
- return ResultData.ok(resultData.getData());
- }catch (Exception e){
- return ResultData.ok();
- }
- }
- }
|