AppSceneService.java 13 KB

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