CameraDetailServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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,null);
  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. if(cameraIds.isEmpty()){
  178. return;
  179. }
  180. wrapper.in(CameraDetail::getCameraId,cameraIds);
  181. List<CameraDetail> list = this.list(wrapper);
  182. for (CameraDetail cameraDetail : list) {
  183. //解封封存场景
  184. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId());
  185. this.initSpace(cameraDetail);
  186. }
  187. }
  188. @Override
  189. public void addUsedSpace(CameraDetail cameraDetail,Long space) {
  190. if("SP".equals(cameraDetail.getUnit())){
  191. space = 1L;
  192. }
  193. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  194. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  195. wrapper.setSql("used_space = used_space + " + space);
  196. this.update(wrapper);
  197. }
  198. public void subUsedSpace(CameraDetail cameraDetail,Long space) {
  199. if("SP".equals(cameraDetail.getUnit())){
  200. space = 1L;
  201. }
  202. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  203. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  204. wrapper.setSql("used_space = used_space - " + space);
  205. this.update(wrapper);
  206. }
  207. @Override
  208. public List<CameraAppVo> getListByUserAndType(Long userId, Integer cameraType) {
  209. return this.getBaseMapper().getListByUserAndType(userId,cameraType);
  210. }
  211. @Override
  212. public List<CameraAppVo> getListByUserIdsAndType(List<Long> userId, Integer cameraType) {
  213. return this.getBaseMapper().getListByUserIdsAndType(userId,cameraType);
  214. }
  215. @Override
  216. public boolean updateCameraBalance(Long cameraId, int body, Integer points) {
  217. CameraDetail detailEntity = this.getByCameraId(cameraId);
  218. int sub = Integer.parseInt(detailEntity.getBalance());
  219. if (0 == body){
  220. sub += points;
  221. }else if (-2 == body){
  222. sub -= points;
  223. }
  224. detailEntity.setBalance(String.valueOf(sub));
  225. return this.updateById(detailEntity);
  226. }
  227. @Override
  228. public void uploadUserCameraInfo(Long id, String cameraVersion, String appVersion) {
  229. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  230. wrapper.eq(CameraDetail::getCameraId,id)
  231. .set(CameraDetail::getUserCameraVersion,cameraVersion)
  232. .set(CameraDetail::getAppVersion,appVersion)
  233. .set(CameraDetail::getLastRequestTime,new Date());
  234. this.update(wrapper);
  235. }
  236. @Override
  237. public List<CameraExcelVo> getListByUserAndTypeEx(Long userId) {
  238. return this.getBaseMapper().getListByUserAndTypeEx(userId);
  239. }
  240. @Override
  241. public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
  242. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  243. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  244. return -1L;
  245. }
  246. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  247. if(userIncrement != null && userIncrement.getIsExpired() == 0){
  248. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  249. return getTotalSpace(cameraDetail,incrementType);
  250. }
  251. return getTotalSpace(cameraDetail,null);
  252. }
  253. @Override
  254. public Boolean checkSpace(Long cameraId, CameraType cameraType) {
  255. CameraDetail detailEntity = this.getByCameraId(cameraId);
  256. if(detailEntity == null){
  257. return false;
  258. }
  259. return checkSpace(detailEntity,cameraType);
  260. }
  261. @Override
  262. public Boolean checkSpace(CameraDetail detailEntity, CameraType cameraType) {
  263. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  264. return true;
  265. }
  266. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  267. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  268. return checkSpace(detailEntity,null,cameraType,true);
  269. }
  270. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  271. return checkSpace(detailEntity,incrementType,cameraType,true);
  272. }
  273. @Override
  274. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space,CameraType cameraType) {
  275. Long totalSpace = 0L;
  276. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  277. return true;
  278. }
  279. if("SP".equals(detailEntity.getUnit())){
  280. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  281. return true;
  282. }
  283. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  284. Long proCount = sceneProService.getCountByCameraId(detailEntity.getCameraId());
  285. Long plusCount = scenePlusService.getCountByCameraId(detailEntity.getCameraId());
  286. return proCount + plusCount + 1 <= totalSpace;
  287. }
  288. if("GB".equals(detailEntity.getUnit())){
  289. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  290. return true;
  291. }
  292. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  293. return detailEntity.getUsedSpace() + space <= totalSpace;
  294. }
  295. return false;
  296. }
  297. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType,CameraType cameraType,Boolean falg) {
  298. Long totalSpace = 0L;
  299. if (!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1) {
  300. return true;
  301. }
  302. if("SP".equals(detailEntity.getUnit())){
  303. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  304. return true;
  305. }
  306. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  307. Long proCount = sceneProService.getCountByCameraId(detailEntity.getCameraId());
  308. Long plusCount = scenePlusService.getCountByCameraId(detailEntity.getCameraId());
  309. return proCount + plusCount <= totalSpace;
  310. }
  311. if("GB".equals(detailEntity.getUnit())){
  312. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  313. return true;
  314. }
  315. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  316. return detailEntity.getUsedSpace() <= totalSpace;
  317. }
  318. return false;
  319. }
  320. public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
  321. if("SP".equals(detailEntity.getUnit())){
  322. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  323. return -1L;
  324. }
  325. return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  326. }
  327. if("GB".equals(detailEntity.getUnit())){
  328. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  329. return -1L;
  330. }
  331. return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L : detailEntity.getTotalSpace();
  332. }
  333. return 0L;
  334. }
  335. @Override
  336. public void initSpace(Long cameraId) {
  337. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  338. if(cameraDetail != null){
  339. initSpace(cameraDetail);
  340. }
  341. }
  342. @Override
  343. public void initSpace(CameraDetail cameraDetail) {
  344. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  345. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  346. if("GB".equals(cameraDetail.getUnit())){
  347. Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  348. Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  349. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
  350. }
  351. if("SP".equals(cameraDetail.getUnit())){
  352. Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId(),true);
  353. Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId(),true);
  354. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
  355. }
  356. this.update(wrapper);
  357. }
  358. }