AppSceneService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.fdkankan.common.util.JwtUtil;
  4. import com.fdkankan.ucenter.common.constants.ConstantFilePath;
  5. import com.fdkankan.common.constant.SceneConstant;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.ucenter.common.PageInfo;
  8. import com.fdkankan.common.util.FileUtils;
  9. import com.fdkankan.redis.util.RedisUtil;
  10. import com.fdkankan.ucenter.common.OssPath;
  11. import com.fdkankan.ucenter.constant.LoginConstant;
  12. import com.fdkankan.ucenter.entity.*;
  13. import com.fdkankan.ucenter.httpClient.service.LaserService;
  14. import com.fdkankan.ucenter.mapper.IScenePlusMapper;
  15. import com.fdkankan.ucenter.service.*;
  16. import com.fdkankan.ucenter.vo.request.AppSceneParam;
  17. import com.fdkankan.ucenter.vo.response.AppSceneVo;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.util.ObjectUtils;
  24. import javax.annotation.Resource;
  25. import java.io.File;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. @Service
  29. @Slf4j
  30. public class AppSceneService {
  31. @Autowired
  32. IUserService userService;
  33. @Autowired
  34. ICameraService cameraService;
  35. @Autowired
  36. ICameraDetailService cameraDetailService;
  37. @Autowired
  38. ISceneProService sceneProService;
  39. @Autowired
  40. IScenePlusService scenePlusService;
  41. @Autowired
  42. IScenePlusExtService scenePlusExtService;
  43. @Autowired
  44. ISceneEditInfoService sceneEditInfoService;
  45. @Autowired
  46. ISceneProEditService sceneProEditService;
  47. @Autowired
  48. IUserIncrementService userIncrementService;
  49. @Autowired
  50. RedisUtil redisUtil;
  51. @Autowired
  52. ISceneCooperationService sceneCooperationService;
  53. @Autowired
  54. LaserService laserService;
  55. @Autowired
  56. ISceneCopyLogService sceneCopyLogService;
  57. @Autowired
  58. IScenePlusMapper scenePlusMapper;
  59. @Value("${fyun.host}")
  60. private String ossHost;
  61. public PageInfo pageList(AppSceneParam param) {
  62. if(StringUtils.isEmpty(param.getAppUserName()) && StringUtils.isEmpty(param.getPhoneNum()) && param.getCameraId() == null){
  63. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  64. }
  65. if(!StringUtils.isEmpty(param.getAppUserName()) && !StringUtils.isEmpty(param.getAppPassword())){
  66. Camera camera = cameraService.getBySnCodeAndPassword(param.getAppUserName(), param.getAppPassword());
  67. if(camera == null){
  68. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  69. }
  70. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  71. if(cameraDetail == null){
  72. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  73. }
  74. param.setSnCode(camera.getSnCode());
  75. param.setCameraId(camera.getId());
  76. }
  77. List<String> cooperationNumList = new ArrayList<>();
  78. if(StringUtils.isNotBlank(param.getPhoneNum())){
  79. User user = userService.getByUserName(param.getPhoneNum());
  80. if(user == null){
  81. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  82. }
  83. cooperationNumList = sceneCooperationService.getNumByUserIds(Arrays.asList(user.getId()));
  84. if(cooperationNumList.size() >0){
  85. param.setCooperationNumList(cooperationNumList );
  86. }
  87. param.setUserId(user.getId());
  88. }
  89. //云端场景不展示复制场景
  90. Set<String> copyNumSet = sceneCopyLogService.getCopyNumSet();
  91. param.setCopyNumSet(copyNumSet);
  92. Page<AppSceneVo> page = scenePlusMapper.pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  93. Set<Long> cameraIdSet = page.getRecords().stream().map(AppSceneVo::getCameraId).collect(Collectors.toSet());
  94. List<Long> cameraIds = new ArrayList<>(cameraIdSet);
  95. HashMap<Long, Camera> cameraHashMap = cameraService.getByIds(cameraIds);
  96. HashMap<Long, CameraDetail> detailHashMap = cameraDetailService.getByCameraIds(cameraIds);
  97. for (AppSceneVo record : page.getRecords()) {
  98. record.setChildName(record.getSnCode());
  99. if (record.getStatus() == -1) {
  100. record.setStatus(0);
  101. } else if (record.getStatus() == 500) {
  102. record.setStatus(-1);
  103. }
  104. if(record.getCameraId() == null){
  105. record.setSceneSourceType(record.getUserId().longValue() == param.getUserId() ? 1 : 2);
  106. }else {
  107. record.setSceneSourceType(0);
  108. Camera camera = cameraHashMap.get(record.getCameraId());
  109. if(camera == null){
  110. continue;
  111. }
  112. CameraDetail cameraDetail = detailHashMap.get(camera.getId());
  113. if(cameraDetail == null){
  114. continue;
  115. }
  116. record.setChildName(camera.getChildName());
  117. record.setSnCode(camera.getSnCode());
  118. if(cooperationNumList.contains(record.getNum())){
  119. record.setSceneSourceType(2);
  120. continue ;
  121. }
  122. //相机的userId为空,表示相机的场景
  123. if (cameraDetail.getUserId() == null || param.getUserId() == null ) {
  124. record.setSceneSourceType(0);
  125. continue;
  126. }
  127. if (cameraDetail.getUserId().equals(param.getUserId())) {
  128. //相机用户id等于该用户id,既为用户的场景
  129. record.setSceneSourceType(1);
  130. continue;
  131. }
  132. if (param.getCameraId()!=null && cameraDetail.getCameraId().equals(param.getCameraId())) {
  133. if (cameraDetail.getCooperationUser() == null) {
  134. //场景相机id等于该相机id
  135. record.setSceneSourceType(0);
  136. } else if (cameraDetail.getCooperationUser().equals(param.getUserId())) {
  137. record.setSceneSourceType(2);
  138. }
  139. }
  140. }
  141. }
  142. return PageInfo.PageInfo(page);
  143. }
  144. public HashMap<String, Object> getSceneStatusByUnicode(String appUserName, String appPassword, String unicode) {
  145. if(StringUtils.isEmpty(appPassword) || StringUtils.isEmpty(appPassword) || StringUtils.isEmpty(unicode)){
  146. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  147. }
  148. Camera camera = cameraService.getBySnCodeAndPassword(appUserName, appPassword);
  149. if(camera == null){
  150. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  151. }
  152. ScenePro scenePro = sceneProService.getByUnicode(unicode);
  153. ScenePlusExt plusExt = scenePlusExtService.getByUnicode(unicode);
  154. ScenePlus scenePlus = null;
  155. if(plusExt !=null){
  156. scenePlus = scenePlusService.getById(plusExt.getPlusId());
  157. }
  158. if(scenePro == null && scenePlus == null){
  159. throw new BusinessException(SceneConstant.FAILURE_CODE_5009, SceneConstant.FAILURE_MSG_5009);
  160. }
  161. Integer payStatus = scenePro == null ? scenePlus.getPayStatus() : scenePro.getPayStatus();
  162. // 当计算时,返回给前端的状态为计算中
  163. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  164. if(status == -1){
  165. status = 0;
  166. }else if(status == 500){
  167. status = -1;
  168. }
  169. String webSite = scenePro == null ? plusExt.getWebSite() : scenePro.getWebSite();
  170. String sceneNum = scenePro == null ? scenePlus.getNum() : scenePro.getNum();
  171. String thumb = scenePro == null ? plusExt.getThumb() : scenePro.getThumb();
  172. String dataSource = scenePro == null ? plusExt.getDataSource() : scenePro.getDataSource();
  173. String sceneCallPath = scenePro == null ? String.format(OssPath.v4_statusPath,sceneNum) : String.format(OssPath.v3_statusPath,sceneNum);
  174. HashMap<String,Object> map = new HashMap<>();
  175. map.put("status",status == -2 ? 1:status);
  176. map.put("webSite",webSite);
  177. map.put("sceneNum",sceneNum);
  178. map.put("thumb",thumb);
  179. map.put("dataSource",dataSource);
  180. map.put("payStatus",payStatus);
  181. map.put("recStatus","A");
  182. map.put("sceneCallPath",ossHost+sceneCallPath);
  183. return map;
  184. }
  185. public void getScreencapVoice(String screencapMusic, String originalFileName, String sceneNum) throws Exception {
  186. if(StringUtils.isEmpty(screencapMusic) || StringUtils.isEmpty(originalFileName) || StringUtils.isEmpty(sceneNum)){
  187. throw new BusinessException(LoginConstant.FAILURE_CODE_3001,LoginConstant.FAILURE_MSG_3001);
  188. }
  189. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  190. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  191. if(scenePro == null && scenePlus == null){
  192. throw new BusinessException(SceneConstant.FAILURE_CODE_5005,SceneConstant.FAILURE_MSG_5005);
  193. }
  194. long time = System.currentTimeMillis();
  195. Map<String, Object> map = new HashMap<>();
  196. map.put("screencapVoiceSoundsyncFileName", screencapMusic + ".mp3");
  197. map.put("screencapVoiceSoundsync", originalFileName + "?t=" + time);
  198. log.info("微信上传的音频路径:" + originalFileName + "?t=" + time);
  199. map.put("screencapVoiceType", "soundsync");
  200. SceneProEdit sceneProEdit= null;
  201. SceneEditInfo editInfo = null;
  202. if(scenePro !=null){
  203. sceneProEdit = sceneProEditService.getByProId(scenePro.getId());
  204. Integer version = sceneProEdit.getVersion() + 1;
  205. sceneProEdit.setVersion(version);
  206. sceneProEdit.setScreencapVoiceSoundsync(originalFileName + "?t=" + time);
  207. sceneProEdit.setScreencapVoiceType("soundsync");
  208. map.put("version", version);
  209. }else {
  210. editInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  211. Integer version = editInfo.getVersion() + 1;
  212. editInfo.setVersion(version);
  213. //editInfo.setScreencapVoiceSoundsync(originalFileName + "?t=" + time);
  214. //editInfo.setScreencapVoiceType("soundsync");
  215. map.put("version", version);
  216. }
  217. StringBuilder dataBuffer = new StringBuilder(ConstantFilePath.SCENE_PATH)
  218. .append("data").append(File.separator)
  219. .append("data").append(sceneNum);
  220. FileUtils.writeJsonFile(dataBuffer.append(File.separator).append("scene.json").toString(), map);
  221. FileUtils.downLoadFromUrl(originalFileName + "?t=" + time, screencapMusic + ".mp3",
  222. ConstantFilePath.SCENE_PATH+ "voice" + File.separator + "voice" + sceneNum);
  223. if(sceneProEdit!=null){
  224. sceneProEditService.updateById(sceneProEdit);
  225. }
  226. if(editInfo !=null){
  227. sceneEditInfoService.updateById(editInfo);
  228. }
  229. }
  230. public void deleteForCameraName(Long cameraId, String sceneNum,Long userId) {
  231. if( StringUtils.isEmpty(sceneNum)){
  232. throw new BusinessException(LoginConstant.FAILURE_CODE_3001,LoginConstant.FAILURE_MSG_3001);
  233. }
  234. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  235. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  236. if(scenePro == null && scenePlus == null){
  237. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  238. }
  239. if(scenePro !=null && scenePro.getStatus() == 0){
  240. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  241. }
  242. if(scenePlus != null && scenePlus.getSceneStatus() == 0){
  243. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  244. }
  245. if(cameraId != null && scenePro!=null && !scenePro.getCameraId().equals(cameraId)){
  246. throw new BusinessException(SceneConstant.FAILURE_CODE_5010, SceneConstant.FAILURE_MSG_5010);
  247. }
  248. if(cameraId != null && scenePlus!=null && !scenePlus.getCameraId().equals(cameraId)){
  249. throw new BusinessException(SceneConstant.FAILURE_CODE_5010, SceneConstant.FAILURE_MSG_5010);
  250. }
  251. Integer sceneSource = null;
  252. List<ScenePro> proList = new ArrayList<>();
  253. if(scenePro!=null){
  254. proList.add(scenePro);
  255. sceneSource = scenePro.getSceneSource();
  256. }
  257. List<ScenePlus> plusList = new ArrayList<>();
  258. if(scenePlus!=null){
  259. plusList.add(scenePlus);
  260. sceneSource = scenePlus.getSceneSource();
  261. }
  262. sceneProService.deleteByList(proList,plusList,userId);
  263. if(sceneSource != null && sceneSource == 4){
  264. laserService.delete(sceneNum);
  265. }
  266. }
  267. }