CameraDetailServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.common.constant.Constant;
  6. import com.fdkankan.ucenter.common.CameraTypeEnum;
  7. import com.fdkankan.ucenter.common.constants.NacosProperty;
  8. import com.fdkankan.ucenter.entity.*;
  9. import com.fdkankan.ucenter.httpClient.service.LaserService;
  10. import com.fdkankan.ucenter.mapper.ICameraDetailMapper;
  11. import com.fdkankan.ucenter.service.*;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.fdkankan.ucenter.vo.response.CameraAppVo;
  14. import com.fdkankan.ucenter.vo.response.CameraExcelVo;
  15. import com.fdkankan.ucenter.vo.response.CameraSpaceVo;
  16. import org.joda.time.DateTime;
  17. import org.joda.time.Days;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. /**
  23. * <p>
  24. * 相机子表 服务实现类
  25. * </p>
  26. *
  27. * @author
  28. * @since 2022-07-04
  29. */
  30. @Service
  31. public class CameraDetailServiceImpl extends ServiceImpl<ICameraDetailMapper, CameraDetail> implements ICameraDetailService {
  32. @Autowired
  33. IUserService userService;
  34. @Autowired
  35. ICameraSpaceService cameraSpaceService;
  36. @Autowired
  37. LaserService fdkkLaserService;
  38. @Autowired
  39. ICameraService cameraService;
  40. @Autowired
  41. ISceneCooperationService sceneCooperationService;
  42. @Autowired
  43. ISceneProService sceneProService;
  44. @Autowired
  45. IScenePlusService scenePlusService;
  46. @Autowired
  47. IUserIncrementService userIncrementService;
  48. @Autowired
  49. IIncrementTypeService incrementTypeService;
  50. @Autowired
  51. IFolderSceneService folderSceneService;
  52. @Autowired
  53. ICameraTypeService cameraTypeService;
  54. @Override
  55. public CameraDetail getByCameraId(Long cameraId) {
  56. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  57. wrapper.eq(CameraDetail::getCameraId,cameraId);
  58. List<CameraDetail> list = this.list(wrapper);
  59. if(list!=null && list.size() >0){
  60. return list.get(0);
  61. }
  62. return null;
  63. }
  64. @Override
  65. public HashMap<Long, CameraDetail> getByCameraIds(List<Long> cameraIds) {
  66. HashMap<Long, CameraDetail> map = new HashMap<>();
  67. if(cameraIds.size() >0){
  68. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  69. wrapper.in(CameraDetail::getCameraId,cameraIds);
  70. List<CameraDetail> list = this.list(wrapper);
  71. list.forEach(entity -> map.put(entity.getCameraId(),entity));
  72. }
  73. return map;
  74. }
  75. @Override
  76. public Long getCountByUserId(Long userId,Integer type) {
  77. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  78. wrapper.eq(CameraDetail::getUserId,userId);
  79. if(type != null){
  80. wrapper.eq(CameraDetail::getType,type);
  81. }
  82. return this.count(wrapper);
  83. }
  84. @Override
  85. public Integer deadlineNumber(String username) {
  86. User user = userService.getByUserName(username);
  87. QueryWrapper<CameraDetail> queryWrapper = new QueryWrapper<>();
  88. queryWrapper.lambda().eq(CameraDetail::getUserId,user.getId());
  89. List<CameraDetail> list = this.list(queryWrapper);
  90. Integer number = 0;
  91. List<CameraSpaceVo> cameraList = null;
  92. for (CameraDetail cameraDetailEntity : list) {
  93. cameraList = cameraSpaceService.getVoListByCameraId(cameraDetailEntity.getCameraId());
  94. if(cameraList != null && cameraList.size() > 0){
  95. if(Days.daysBetween(new DateTime(), new DateTime(cameraList.get(0).getSpaceEndTime())).getDays() < 30){
  96. number++;
  97. }
  98. }
  99. }
  100. return number;
  101. }
  102. @Override
  103. public void unbindCamera(List<Long> cameraIds) {
  104. HashMap<Long, CameraDetail> detailMap = this.getByCameraIds(cameraIds);
  105. HashMap<Long, Camera> cameraMap = cameraService.getByIds(cameraIds);
  106. for (CameraDetail cameraDetail : detailMap.values()) {
  107. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  108. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  109. wrapper.set(CameraDetail::getUserId,null);
  110. wrapper.set(CameraDetail::getCooperationUser,null);
  111. this.update(wrapper);
  112. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  113. //国内服解绑
  114. if(!"aws".equals(NacosProperty.uploadType) && !"local".equals(NacosProperty.uploadType) && cameraType.getIsLaser() ==0){
  115. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId()); //封存场景
  116. }
  117. //国际服解绑
  118. if("aws".equals(NacosProperty.uploadType) ){
  119. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId()); //封存场景
  120. }
  121. if (cameraType.getIsLaser() == 1) {
  122. fdkkLaserService.toBind(false,cameraMap.get(cameraDetail.getCameraId()).getSnCode(),null,null);
  123. }
  124. }
  125. //恢复10G基本容量
  126. //删除场景协作信息
  127. List<ScenePro> sceneProList = sceneProService.getListByCameraIds(cameraIds);
  128. List<ScenePlus> scenePlusList = scenePlusService.getListByCameraIds(cameraIds);
  129. sceneCooperationService.deleteCooperationList(sceneProList,scenePlusList);
  130. fdkkLaserService.disableCooperation(detailMap, cameraMap);
  131. //解绑删除相机。文件夹与场景绑定关系
  132. List<Long > sceneIds = new ArrayList<>();
  133. if(sceneProList.size() >0){
  134. List<Long> proIds = sceneProList.stream().map(ScenePro::getId).collect(Collectors.toList());
  135. if(proIds.size() >0){
  136. sceneIds.addAll(proIds);
  137. }
  138. }
  139. if(scenePlusList.size() >0){
  140. List<Long> plusIds = scenePlusList.stream().map(ScenePlus::getId).collect(Collectors.toList());
  141. if(plusIds.size() >0){
  142. sceneIds.addAll(plusIds);
  143. }
  144. }
  145. if(sceneIds.size() >0){
  146. folderSceneService.delBySceneId(sceneIds);
  147. }
  148. }
  149. @Override
  150. public void bindCamera(List<Long> cameraIds,User user) {
  151. if(cameraIds.size() >0){
  152. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  153. wrapper.in(CameraDetail::getCameraId,cameraIds)
  154. .set(CameraDetail::getUserId,user.getId());
  155. this.update(wrapper);
  156. HashMap<Long, CameraDetail> detailMap = this.getByCameraIds(cameraIds);
  157. HashMap<Long, Camera> cameraMap = cameraService.getByIds(cameraIds);
  158. for (CameraDetail cameraDetail : detailMap.values()) {
  159. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  160. if (cameraType.getIsLaser() == 1) {
  161. fdkkLaserService.toBind(true,cameraMap.get(cameraDetail.getCameraId()).getSnCode(),user.getUserName(),user.getId());
  162. }
  163. }
  164. }
  165. }
  166. @Override
  167. public void updateCooperationByIds(List<Long> cameraIds, Long userId) {
  168. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  169. wrapper.in(CameraDetail::getCameraId,cameraIds)
  170. .set(CameraDetail::getCooperationUser,userId);
  171. this.update(wrapper);
  172. }
  173. @Override
  174. public void addUsedSpace(HashMap<Long, Long> cameraMap) {
  175. Set<Long> cameraIds = cameraMap.keySet();
  176. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  177. wrapper.in(CameraDetail::getCameraId,cameraIds);
  178. List<CameraDetail> list = this.list(wrapper);
  179. for (CameraDetail cameraDetail : list) {
  180. //解封封存场景
  181. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId());
  182. this.initSpace(cameraDetail);
  183. }
  184. }
  185. @Override
  186. public void addUsedSpace(CameraDetail cameraDetail,Long space) {
  187. if("SP".equals(cameraDetail.getUnit())){
  188. space = 1L;
  189. }
  190. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  191. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  192. wrapper.setSql("used_space = used_space + " + space);
  193. this.update(wrapper);
  194. }
  195. public void subUsedSpace(CameraDetail cameraDetail,Long space) {
  196. if("SP".equals(cameraDetail.getUnit())){
  197. space = 1L;
  198. }
  199. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  200. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  201. wrapper.setSql("used_space = used_space - " + space);
  202. this.update(wrapper);
  203. }
  204. @Override
  205. public List<CameraAppVo> getListByUserAndType(Long userId, Integer cameraType) {
  206. return this.getBaseMapper().getListByUserAndType(userId,cameraType);
  207. }
  208. @Override
  209. public List<CameraAppVo> getListByUserIdsAndType(List<Long> userId, Integer cameraType) {
  210. return this.getBaseMapper().getListByUserIdsAndType(userId,cameraType);
  211. }
  212. @Override
  213. public boolean updateCameraBalance(Long cameraId, int body, Integer points) {
  214. CameraDetail detailEntity = this.getByCameraId(cameraId);
  215. int sub = Integer.parseInt(detailEntity.getBalance());
  216. if (0 == body){
  217. sub += points;
  218. }else if (-2 == body){
  219. sub -= points;
  220. }
  221. detailEntity.setBalance(String.valueOf(sub));
  222. return this.updateById(detailEntity);
  223. }
  224. @Override
  225. public void uploadUserCameraInfo(Long id, String cameraVersion, String appVersion) {
  226. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  227. wrapper.eq(CameraDetail::getCameraId,id)
  228. .set(CameraDetail::getUserCameraVersion,cameraVersion)
  229. .set(CameraDetail::getAppVersion,appVersion)
  230. .set(CameraDetail::getLastRequestTime,new Date());
  231. this.update(wrapper);
  232. }
  233. @Override
  234. public List<CameraExcelVo> getListByUserAndTypeEx(Long userId) {
  235. return this.getBaseMapper().getListByUserAndTypeEx(userId);
  236. }
  237. @Override
  238. public void updateSpaceByCameraId(Long cameraId, Long cameraDetailSpace) {
  239. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  240. wrapper.eq(CameraDetail::getCameraId,cameraId)
  241. .set(CameraDetail::getUsedSpace,cameraDetailSpace);
  242. this.update(wrapper);
  243. }
  244. @Override
  245. public Long getTotalSpaceByCameraId(Long cameraId) {
  246. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  247. if(cameraDetail == null){
  248. return 0L;
  249. }
  250. return getTotalSpaceByCameraId(cameraDetail);
  251. }
  252. @Override
  253. public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
  254. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  255. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  256. return -1L;
  257. }
  258. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  259. if(userIncrement != null && userIncrement.getIsExpired() == 0){
  260. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  261. return getTotalSpace(cameraDetail,incrementType);
  262. }
  263. return getTotalSpace(cameraDetail,null);
  264. }
  265. @Override
  266. public Boolean checkSpace(Long cameraId, Long space) {
  267. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  268. if(cameraDetail == null){
  269. return false;
  270. }
  271. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  272. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  273. return true;
  274. }
  275. return checkSpace(cameraDetail,space, cameraType);
  276. }
  277. @Override
  278. public Boolean checkSpace(Long cameraId, CameraType cameraType) {
  279. CameraDetail detailEntity = this.getByCameraId(cameraId);
  280. if(detailEntity == null){
  281. return false;
  282. }
  283. return checkSpace(detailEntity,cameraType);
  284. }
  285. @Override
  286. public Boolean checkSpace(CameraDetail detailEntity, CameraType cameraType) {
  287. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  288. return true;
  289. }
  290. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  291. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  292. return checkSpace(detailEntity,null,cameraType,true);
  293. }
  294. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  295. return checkSpace(detailEntity,incrementType,cameraType,true);
  296. }
  297. @Override
  298. public Boolean checkSpace(CameraDetail detailEntity, Long space,CameraType cameraType) {
  299. if(detailEntity == null){
  300. return false;
  301. }
  302. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  303. return true;
  304. }
  305. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  306. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  307. return checkSpace(detailEntity,null,space,cameraType);
  308. }
  309. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  310. return checkSpace(detailEntity,incrementType,space,cameraType);
  311. }
  312. @Override
  313. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space,CameraType cameraType) {
  314. Long totalSpace = 0L;
  315. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  316. return true;
  317. }
  318. if("SP".equals(detailEntity.getUnit())){
  319. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  320. return true;
  321. }
  322. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  323. Long proCount = sceneProService.getCountByCameraId(detailEntity.getCameraId());
  324. Long plusCount = scenePlusService.getCountByCameraId(detailEntity.getCameraId());
  325. return proCount + plusCount + 1 <= totalSpace;
  326. }
  327. if("GB".equals(detailEntity.getUnit())){
  328. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  329. return true;
  330. }
  331. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  332. return detailEntity.getUsedSpace() + space <= totalSpace;
  333. }
  334. return false;
  335. }
  336. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType,CameraType cameraType,Boolean falg) {
  337. Long totalSpace = 0L;
  338. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  339. return true;
  340. }
  341. if("SP".equals(detailEntity.getUnit())){
  342. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  343. return true;
  344. }
  345. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  346. Long proCount = sceneProService.getCountByCameraId(detailEntity.getCameraId());
  347. Long plusCount = scenePlusService.getCountByCameraId(detailEntity.getCameraId());
  348. return proCount + plusCount <= totalSpace;
  349. }
  350. if("GB".equals(detailEntity.getUnit())){
  351. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  352. return true;
  353. }
  354. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  355. return detailEntity.getUsedSpace() <= totalSpace;
  356. }
  357. return false;
  358. }
  359. public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
  360. if("SP".equals(detailEntity.getUnit())){
  361. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  362. return -1L;
  363. }
  364. return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  365. }
  366. if("GB".equals(detailEntity.getUnit())){
  367. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  368. return -1L;
  369. }
  370. return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L : detailEntity.getTotalSpace();
  371. }
  372. return 0L;
  373. }
  374. @Override
  375. public void initSpace(Long cameraId) {
  376. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  377. if(cameraDetail != null){
  378. initSpace(cameraDetail);
  379. }
  380. }
  381. @Override
  382. public void initSpace(CameraDetail cameraDetail) {
  383. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  384. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  385. if("GB".equals(cameraDetail.getUnit())){
  386. Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  387. Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  388. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
  389. }
  390. if("SP".equals(cameraDetail.getUnit())){
  391. Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId(),true);
  392. Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId(),true);
  393. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
  394. }
  395. this.update(wrapper);
  396. }
  397. }