AppSceneService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. IScenePlusMapper scenePlusMapper;
  57. @Value("${fyun.host}")
  58. private String ossHost;
  59. public PageInfo pageList(AppSceneParam param) {
  60. if(StringUtils.isEmpty(param.getAppUserName()) && StringUtils.isEmpty(param.getPhoneNum()) && param.getCameraId() == null){
  61. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  62. }
  63. if(!StringUtils.isEmpty(param.getAppUserName()) && !StringUtils.isEmpty(param.getAppPassword())){
  64. Camera camera = cameraService.getBySnCodeAndPassword(param.getAppUserName(), param.getAppPassword());
  65. if(camera == null){
  66. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  67. }
  68. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  69. if(cameraDetail == null){
  70. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  71. }
  72. param.setSnCode(camera.getSnCode());
  73. param.setCameraId(camera.getId());
  74. }
  75. List<String> cooperationNumList = new ArrayList<>();
  76. if(StringUtils.isNotBlank(param.getPhoneNum())){
  77. User user = userService.getByUserName(param.getPhoneNum());
  78. if(user == null){
  79. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  80. }
  81. cooperationNumList = sceneCooperationService.getNumByUserIds(Arrays.asList(user.getId()));
  82. if(cooperationNumList.size() >0){
  83. param.setCooperationNumList(cooperationNumList );
  84. }
  85. param.setUserId(user.getId());
  86. }
  87. Page<AppSceneVo> page = scenePlusMapper.pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  88. Set<Long> cameraIdSet = page.getRecords().stream().map(AppSceneVo::getCameraId).collect(Collectors.toSet());
  89. List<Long> cameraIds = new ArrayList<>(cameraIdSet);
  90. HashMap<Long, Camera> cameraHashMap = cameraService.getByIds(cameraIds);
  91. HashMap<Long, CameraDetail> detailHashMap = cameraDetailService.getByCameraIds(cameraIds);
  92. for (AppSceneVo record : page.getRecords()) {
  93. record.setChildName(record.getSnCode());
  94. if (record.getStatus() == -1) {
  95. record.setStatus(0);
  96. } else if (record.getStatus() == 500) {
  97. record.setStatus(-1);
  98. }
  99. if(record.getCameraId() == null){
  100. record.setSceneSourceType(record.getUserId().longValue() == param.getUserId() ? 1 : 2);
  101. }else {
  102. record.setSceneSourceType(0);
  103. Camera camera = cameraHashMap.get(record.getCameraId());
  104. if(camera == null){
  105. continue;
  106. }
  107. CameraDetail cameraDetail = detailHashMap.get(camera.getId());
  108. if(cameraDetail == null){
  109. continue;
  110. }
  111. record.setChildName(camera.getChildName());
  112. record.setSnCode(camera.getSnCode());
  113. if(cooperationNumList.contains(record.getNum())){
  114. record.setSceneSourceType(2);
  115. continue ;
  116. }
  117. //相机的userId为空,表示相机的场景
  118. if (cameraDetail.getUserId() == null || param.getUserId() == null ) {
  119. record.setSceneSourceType(0);
  120. continue;
  121. }
  122. if (cameraDetail.getUserId().equals(param.getUserId())) {
  123. //相机用户id等于该用户id,既为用户的场景
  124. record.setSceneSourceType(1);
  125. continue;
  126. }
  127. if (param.getCameraId()!=null && cameraDetail.getCameraId().equals(param.getCameraId())) {
  128. if (cameraDetail.getCooperationUser() == null) {
  129. //场景相机id等于该相机id
  130. record.setSceneSourceType(0);
  131. } else if (cameraDetail.getCooperationUser().equals(param.getUserId())) {
  132. record.setSceneSourceType(2);
  133. }
  134. }
  135. }
  136. }
  137. return PageInfo.PageInfo(page);
  138. }
  139. public HashMap<String, Object> getSceneStatusByUnicode(String appUserName, String appPassword, String unicode) {
  140. if(StringUtils.isEmpty(appPassword) || StringUtils.isEmpty(appPassword) || StringUtils.isEmpty(unicode)){
  141. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  142. }
  143. Camera camera = cameraService.getBySnCodeAndPassword(appUserName, appPassword);
  144. if(camera == null){
  145. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  146. }
  147. ScenePro scenePro = sceneProService.getByUnicode(unicode);
  148. ScenePlusExt plusExt = scenePlusExtService.getByUnicode(unicode);
  149. ScenePlus scenePlus = null;
  150. if(plusExt !=null){
  151. scenePlus = scenePlusService.getById(plusExt.getPlusId());
  152. }
  153. if(scenePro == null && scenePlus == null){
  154. throw new BusinessException(SceneConstant.FAILURE_CODE_5009, SceneConstant.FAILURE_MSG_5009);
  155. }
  156. Integer payStatus = scenePro == null ? scenePlus.getPayStatus() : scenePro.getPayStatus();
  157. // 当计算时,返回给前端的状态为计算中
  158. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  159. if(status == -1){
  160. status = 0;
  161. }else if(status == 500){
  162. status = -1;
  163. }
  164. String webSite = scenePro == null ? plusExt.getWebSite() : scenePro.getWebSite();
  165. String sceneNum = scenePro == null ? scenePlus.getNum() : scenePro.getNum();
  166. String thumb = scenePro == null ? plusExt.getThumb() : scenePro.getThumb();
  167. String dataSource = scenePro == null ? plusExt.getDataSource() : scenePro.getDataSource();
  168. String sceneCallPath = scenePro == null ? String.format(OssPath.v4_statusPath,sceneNum) : String.format(OssPath.v3_statusPath,sceneNum);
  169. HashMap<String,Object> map = new HashMap<>();
  170. map.put("status",status == -2 ? 1:status);
  171. map.put("webSite",webSite);
  172. map.put("sceneNum",sceneNum);
  173. map.put("thumb",thumb);
  174. map.put("dataSource",dataSource);
  175. map.put("payStatus",payStatus);
  176. map.put("recStatus","A");
  177. map.put("sceneCallPath",ossHost+sceneCallPath);
  178. return map;
  179. }
  180. public void getScreencapVoice(String screencapMusic, String originalFileName, String sceneNum) throws Exception {
  181. if(StringUtils.isEmpty(screencapMusic) || StringUtils.isEmpty(originalFileName) || StringUtils.isEmpty(sceneNum)){
  182. throw new BusinessException(LoginConstant.FAILURE_CODE_3001,LoginConstant.FAILURE_MSG_3001);
  183. }
  184. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  185. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  186. if(scenePro == null && scenePlus == null){
  187. throw new BusinessException(SceneConstant.FAILURE_CODE_5005,SceneConstant.FAILURE_MSG_5005);
  188. }
  189. long time = System.currentTimeMillis();
  190. Map<String, Object> map = new HashMap<>();
  191. map.put("screencapVoiceSoundsyncFileName", screencapMusic + ".mp3");
  192. map.put("screencapVoiceSoundsync", originalFileName + "?t=" + time);
  193. log.info("微信上传的音频路径:" + originalFileName + "?t=" + time);
  194. map.put("screencapVoiceType", "soundsync");
  195. SceneProEdit sceneProEdit= null;
  196. SceneEditInfo editInfo = null;
  197. if(scenePro !=null){
  198. sceneProEdit = sceneProEditService.getByProId(scenePro.getId());
  199. Integer version = sceneProEdit.getVersion() + 1;
  200. sceneProEdit.setVersion(version);
  201. sceneProEdit.setScreencapVoiceSoundsync(originalFileName + "?t=" + time);
  202. sceneProEdit.setScreencapVoiceType("soundsync");
  203. map.put("version", version);
  204. }else {
  205. editInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  206. Integer version = editInfo.getVersion() + 1;
  207. editInfo.setVersion(version);
  208. //editInfo.setScreencapVoiceSoundsync(originalFileName + "?t=" + time);
  209. //editInfo.setScreencapVoiceType("soundsync");
  210. map.put("version", version);
  211. }
  212. StringBuilder dataBuffer = new StringBuilder(ConstantFilePath.SCENE_PATH)
  213. .append("data").append(File.separator)
  214. .append("data").append(sceneNum);
  215. FileUtils.writeJsonFile(dataBuffer.append(File.separator).append("scene.json").toString(), map);
  216. FileUtils.downLoadFromUrl(originalFileName + "?t=" + time, screencapMusic + ".mp3",
  217. ConstantFilePath.SCENE_PATH+ "voice" + File.separator + "voice" + sceneNum);
  218. if(sceneProEdit!=null){
  219. sceneProEditService.updateById(sceneProEdit);
  220. }
  221. if(editInfo !=null){
  222. sceneEditInfoService.updateById(editInfo);
  223. }
  224. }
  225. public void deleteForCameraName(Long cameraId, String sceneNum,Long userId) {
  226. if( StringUtils.isEmpty(sceneNum)){
  227. throw new BusinessException(LoginConstant.FAILURE_CODE_3001,LoginConstant.FAILURE_MSG_3001);
  228. }
  229. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  230. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  231. if(scenePro == null && scenePlus == null){
  232. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  233. }
  234. if(scenePro !=null && scenePro.getStatus() == 0){
  235. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  236. }
  237. if(scenePlus != null && scenePlus.getSceneStatus() == 0){
  238. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  239. }
  240. if(cameraId != null && scenePro!=null && !scenePro.getCameraId().equals(cameraId)){
  241. throw new BusinessException(SceneConstant.FAILURE_CODE_5010, SceneConstant.FAILURE_MSG_5010);
  242. }
  243. if(cameraId != null && scenePlus!=null && !scenePlus.getCameraId().equals(cameraId)){
  244. throw new BusinessException(SceneConstant.FAILURE_CODE_5010, SceneConstant.FAILURE_MSG_5010);
  245. }
  246. Integer sceneSource = null;
  247. List<ScenePro> proList = new ArrayList<>();
  248. if(scenePro!=null){
  249. proList.add(scenePro);
  250. sceneSource = scenePro.getSceneSource();
  251. }
  252. List<ScenePlus> plusList = new ArrayList<>();
  253. if(scenePlus!=null){
  254. plusList.add(scenePlus);
  255. sceneSource = scenePlus.getSceneSource();
  256. }
  257. sceneProService.deleteByList(proList,plusList,userId);
  258. if(sceneSource != null && sceneSource == 4){
  259. laserService.delete(sceneNum);
  260. }
  261. }
  262. }