CameraServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package com.fdkankan.manage.service.impl;
  2. import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.fdkankan.common.constant.Constant;
  8. import com.fdkankan.common.util.MD5;
  9. import com.fdkankan.manage.common.*;
  10. import com.fdkankan.manage.entity.*;
  11. import com.fdkankan.manage.exception.BusinessException;
  12. import com.fdkankan.common.util.DateUtil;
  13. import com.fdkankan.manage.constant.CameraConstant;
  14. import com.fdkankan.manage.mapper.ICameraMapper;
  15. import com.fdkankan.manage.service.*;
  16. import com.fdkankan.manage.util.Dateutils;
  17. import com.fdkankan.manage.vo.request.CameraInOutParam;
  18. import com.fdkankan.manage.vo.request.CameraParam;
  19. import com.fdkankan.manage.vo.response.CameraDataVo;
  20. import com.fdkankan.manage.vo.response.CameraDetailVo;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. /**
  28. * <p>
  29. * 相机主表 服务实现类
  30. * </p>
  31. *
  32. * @author
  33. * @since 2022-06-16
  34. */
  35. @Service
  36. public class CameraServiceImpl extends ServiceImpl<ICameraMapper, Camera> implements ICameraService {
  37. @Autowired
  38. ICameraDetailService cameraDetailService;
  39. @Autowired
  40. ISceneService sceneService;
  41. @Autowired
  42. ISceneProService sceneProService;
  43. @Autowired
  44. IUserIncrementService userIncrementService;
  45. @Autowired
  46. ISceneResourceCameraService sceneResourceCameraService;
  47. @Autowired
  48. IScenePlusService scenePlusService;
  49. @Autowired
  50. ICompanyService companyService;
  51. @Autowired
  52. IExcelService excelService;
  53. @Value("${fyun.type}")
  54. private String ossType;
  55. @Override
  56. public List<Camera> getListByCameraIdList(List<Long> cameraIdList) {
  57. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  58. wrapper.in(Camera::getId,cameraIdList);
  59. return this.list(wrapper);
  60. }
  61. @Override
  62. public Camera getBySnCode(String snCode) {
  63. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  64. wrapper.eq(Camera::getSnCode,snCode);
  65. List<Camera> list = this.list(wrapper);
  66. if(list == null || list.size() <=0 ){
  67. return null;
  68. }
  69. return list.get(0);
  70. }
  71. private List<Camera> getBySnCodes(List<String> snCodeList) {
  72. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  73. wrapper.in(Camera::getSnCode,snCodeList);
  74. return this.list(wrapper);
  75. }
  76. private List<Camera> getByOutSnCodes(List<String> snCodeList) {
  77. return this.getBaseMapper().getByOutSnCodes(snCodeList);
  78. }
  79. @Override
  80. public void unbindCamera(Long cameraId) {
  81. userIncrementService.unbindCamera(cameraId); //取消关联用户权益
  82. cameraDetailService.unbindCamera(cameraId); //取消相机用户关联
  83. sceneService.unbindCamera(cameraId); //取消关联场景
  84. sceneProService.unbindCamera(cameraId); //取消关联场景
  85. scenePlusService.unbindCamera(cameraId); //取消关联场景
  86. sceneResourceCameraService.unbindCamera(cameraId); //删除协作相机
  87. }
  88. @Override
  89. public PageInfo pageList(CameraParam param) {
  90. Page<CameraDetailVo> voPage = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  91. return PageInfo.PageInfo(voPage);
  92. }
  93. @Override
  94. public void in(String wifiName) {
  95. if(StringUtils.isEmpty(wifiName) ){
  96. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  97. }
  98. if( !wifiName.contains("_") || !wifiName.contains("4D")){
  99. throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
  100. }
  101. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  102. wrapper.eq(Camera::getWifiName,wifiName);
  103. long count = this.count(wrapper);
  104. if(count > 0){
  105. throw new BusinessException(ResultCode.WIFI_NAME_REPEAT);
  106. }
  107. saveBatchCamera(Collections.singletonList(wifiName));
  108. }
  109. private Integer saveBatchCamera(List<String> wifiNameList){
  110. HashSet<String> wifiNameSet = new HashSet<>(wifiNameList);
  111. List<Camera> cameraList = new ArrayList<>();
  112. for (String wifiName : wifiNameSet) {
  113. String[] res = wifiName.split("_");
  114. if(res.length !=2 || StringUtils.isBlank(res[1])){
  115. throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
  116. }
  117. Camera camera = new Camera();
  118. camera.setWifiName(wifiName);
  119. camera.setSnCode(res[1]);
  120. camera.setChildName(res[1]);
  121. camera.setWifiPassword(CameraConstant.WIFI_PASSWORD_VALUE);
  122. camera.setChildPassword(CameraConstant.CHILD_PASSWORD_VALUE);
  123. camera.setActivatedTime(DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  124. cameraList.add(camera);
  125. }
  126. this.saveBatch(cameraList);
  127. List<CameraDetail> cameraDetailList = new ArrayList<>();
  128. for (Camera camera : cameraList) {
  129. String wifiName = camera.getWifiName();
  130. Integer type = CameraTypeEnum.getTypeByWifiNamePrefix(wifiName.substring(0,wifiName.lastIndexOf("_") +1));
  131. CameraDetail cameraDetail = new CameraDetail();
  132. cameraDetail.setAgency(CameraConstant.DEFAULT_AGENT);
  133. cameraDetail.setCameraId(camera.getId());
  134. cameraDetail.setCountry(0);//默认中国
  135. cameraDetail.setType(type);
  136. if (1 == cameraDetail.getType() || 9 == cameraDetail.getType() || 10 == cameraDetail.getType() ||11 ==cameraDetail.getType() ){
  137. cameraDetail.setTotalSpace(Long.valueOf(Constant.CAMERA_BASE_SPACE_VALUE));
  138. cameraDetail.setUsedSpace(0L);
  139. }
  140. if(type != 9 && type != 10){
  141. type = type == 0 ? 1: 4;
  142. }
  143. if("aws".equals(CacheUtil.uploadType)){
  144. cameraDetail.setCountry(1);//1-国外
  145. }
  146. cameraDetail.setGoodsId(Long.valueOf(type));
  147. if("local".equals(CacheUtil.uploadType)) { //本地版本 ,入库即出库,并且添加无限期会员权限
  148. cameraDetail.setCountry(2);
  149. cameraDetail.setOwn(2);
  150. cameraDetail.setOutTime(Dateutils.getDate(new Date()));
  151. }
  152. cameraDetailList.add(cameraDetail);
  153. }
  154. return cameraDetailService.saveBatch(cameraDetailList) ? cameraList.size() : 0;
  155. }
  156. @Override
  157. public Integer ins(List<String> wifiNameList){
  158. List<Integer> errorRow = getErrorRow(wifiNameList);
  159. excelService.toExcelError(errorRow);
  160. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  161. wrapper.in(Camera::getWifiName,wifiNameList);
  162. List<Camera> list = this.list(wrapper);
  163. if(list.size() >0){
  164. List<String> newList = list.parallelStream().map(Camera::getWifiName).collect(Collectors.toList());
  165. List<Integer> errorRow2 = getErrorRow(wifiNameList, newList);
  166. excelService.toExcelError(errorRow2);
  167. }
  168. return saveBatchCamera(wifiNameList);
  169. }
  170. private List<Integer> getErrorRow(List<String> wifiNameList){
  171. List<Integer> errorIndexList = new ArrayList<>();
  172. Set<String> wifiNameSet = new HashSet<>();
  173. Integer index = 0;
  174. for (String wifiName : wifiNameList) {
  175. index ++;
  176. if(StringUtils.isBlank(wifiName)){
  177. errorIndexList.add(index);
  178. continue;
  179. }
  180. if(wifiNameSet.contains(wifiName)){
  181. errorIndexList.add(index);
  182. continue;
  183. }
  184. if( !wifiName.contains("_") || !wifiName.contains("4D")
  185. || !CameraTypeEnum.typePrefixMap.containsKey(wifiName.split("_")[0]+"_")){
  186. errorIndexList.add(index);
  187. }
  188. wifiNameSet.add(wifiName);
  189. }
  190. return errorIndexList;
  191. }
  192. private List<Integer> getErrorRow(List<String> wifiNameList,List<String> dbList){
  193. List<Integer> errorIndexList = new ArrayList<>();
  194. if(dbList.size() <=0){
  195. return errorIndexList;
  196. }
  197. Integer index = 0;
  198. for (String wifiName : wifiNameList) {
  199. index ++;
  200. if(dbList.contains(wifiName)){
  201. errorIndexList.add(index);
  202. }
  203. }
  204. return errorIndexList;
  205. }
  206. @Override
  207. public void out(CameraInOutParam param) {
  208. if(param.getOutType() == null || param.getId() == null){
  209. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  210. }
  211. if(StringUtils.isNotBlank(param.getCompanyName())){
  212. Company company = companyService.getCompanyByName(param.getCompanyName());
  213. if(company == null){
  214. throw new BusinessException(ResultCode.COMPANY_NAME_NOT_EXIST);
  215. }
  216. param.setCompanyId(company.getId());
  217. }
  218. Camera camera = this.getById(param.getId());
  219. if(camera == null){
  220. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  221. }
  222. CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
  223. if(detail ==null){
  224. throw new BusinessException(ResultCode.CAMERA_NOT_IN);
  225. }
  226. HashMap<Long,CameraDetail> detailMap = new HashMap<>();
  227. detailMap.put(detail.getCameraId(),detail);
  228. this.saveBatchDetail(detailMap, Collections.singletonList(param));
  229. }
  230. @Override
  231. public Integer outs(List<CameraInOutParam> params){
  232. setCompanyIdByDb(params);
  233. checkOrderSn(params);
  234. HashMap<String, Object> resultMap = getResultMap(params,1);
  235. HashMap<Long,CameraDetail> detailMap = (HashMap<Long, CameraDetail>) resultMap.get("detailMap");
  236. HashMap<String, Camera> snCodeMap = (HashMap<String, Camera>) resultMap.get("snCodeMap");
  237. for (CameraInOutParam param : params) {
  238. if(param.getId() == null){
  239. param.setId(snCodeMap.get(param.getSnCode()).getId());
  240. }
  241. }
  242. return this.saveBatchDetail(detailMap,params);
  243. }
  244. private void checkOrderSn(List<CameraInOutParam> params) {
  245. List<String> orderSnList = params.stream().map(CameraInOutParam::getOrderSn).collect(Collectors.toList());
  246. if(orderSnList.size() >0){
  247. List<Order> orders = orderService.getByOrderSnList(orderSnList);
  248. List<String> dbOrderSn = orders.stream().map(Order::getOrderSn).collect(Collectors.toList());
  249. List<Integer> errorList = new ArrayList<>();
  250. Integer index = 0;
  251. for (String sn : orderSnList) {
  252. index ++;
  253. if(StringUtils.isBlank(sn)){
  254. continue;
  255. }
  256. if(!dbOrderSn.contains(sn)){
  257. errorList.add(index);
  258. }
  259. }
  260. excelService.toExcelError(errorList);
  261. }
  262. }
  263. private Integer saveBatchDetail(HashMap<Long, CameraDetail> detailMap, List<CameraInOutParam> params){
  264. List<CameraDetail> cameraDetails = new ArrayList<>();
  265. for (CameraInOutParam param : params) {
  266. CameraDetail cameraDetail = detailMap.get(param.getId());
  267. if(cameraDetail == null){
  268. throw new BusinessException(ResultCode.CAMERA_NOT_IN);
  269. }
  270. if(param.getAgentId() != null){
  271. cameraDetail.setAgentId(param.getAgentId());
  272. }
  273. cameraDetail.setOrderSn(param.getOrderSn());
  274. cameraDetail.setOwn(param.getOutType());
  275. cameraDetail.setCompanyId(param.getCompanyId());
  276. cameraDetail.setOutTime(Dateutils.getDate(new Date()));
  277. cameraDetails.add(cameraDetail);
  278. }
  279. return cameraDetailService.saveOrUpdateBatch(cameraDetails) ? cameraDetails.size() : 0;
  280. }
  281. private void checkSnCode(List<String> snCodeList,List<String> dbSnCode) {
  282. HashMap<String,Integer> map = new HashMap<>();
  283. List<Integer> errorList = new ArrayList<>();
  284. Integer index = 0;
  285. for (String snCode : snCodeList) {
  286. index ++;
  287. if(StringUtils.isBlank(snCode)){
  288. errorList.add(index);
  289. }
  290. if(map.get(snCode) == null){
  291. map.put(snCode,1);
  292. }else {
  293. map.put(snCode,2);
  294. errorList.add(index);
  295. }
  296. }
  297. excelService.toExcelError(errorList);
  298. Integer index2 = 0;
  299. for (String snCode : snCodeList) {
  300. index2 ++;
  301. if(!dbSnCode.contains(snCode)){
  302. errorList.add(index2);
  303. }
  304. }
  305. excelService.toExcelError(errorList);
  306. }
  307. @Autowired
  308. IOrderService orderService;
  309. @Override
  310. public void updateCamera(CameraInOutParam param) {
  311. if(param.getId() == null|| param.getOutType() == null){
  312. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  313. }
  314. CameraDetail cameraDetail = cameraDetailService.getByCameraId(param.getId());
  315. if(cameraDetail == null){
  316. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  317. }
  318. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  319. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  320. if(StringUtils.isNotBlank(param.getCompanyName())){
  321. Company company = companyService.getCompanyByName(param.getCompanyName());
  322. if(company == null){
  323. throw new BusinessException(ResultCode.COMPANY_NAME_NOT_EXIST);
  324. }
  325. wrapper.set(CameraDetail::getCompanyId,company.getId());
  326. }else {
  327. wrapper.set(CameraDetail::getCompanyId,null);
  328. }
  329. if(StringUtils.isNotBlank(param.getOrderSn())){
  330. Order orderSn = orderService.getByOrderSn(param.getOrderSn());
  331. if(orderSn == null ){
  332. throw new BusinessException(ResultCode.ORDER_SN_ERROR);
  333. }
  334. wrapper.set(CameraDetail::getOrderSn,orderSn.getOrderSn());
  335. }else {
  336. wrapper.set(CameraDetail::getOrderSn,null);
  337. }
  338. if(param.getAgentId() != null){
  339. wrapper.set(CameraDetail::getAgentId,param.getAgentId());
  340. }else {
  341. wrapper.set(CameraDetail::getAgentId,null);
  342. }
  343. wrapper.set(CameraDetail::getOwn,param.getOutType());
  344. cameraDetailService.update(wrapper);
  345. }
  346. @Override
  347. public void deleteCamera(Long id) {
  348. if(id == null){
  349. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  350. }
  351. CameraDetail cameraDetail = cameraDetailService.getByCameraId(id);
  352. if(cameraDetail !=null && cameraDetail.getUserId()!=null){
  353. throw new BusinessException(ResultCode.CAMERA_DEL_ERROR);
  354. }
  355. this.removeById(id);
  356. cameraDetailService.deleteByCameraId(id);
  357. }
  358. @Override
  359. public Integer updateCompany(List<CameraInOutParam> companyParams) {
  360. setCompanyIdByDb(companyParams);
  361. HashMap<String, Object> resultMap = getResultMap(companyParams,2);
  362. HashMap<Long, CameraDetail> detailMap = (HashMap<Long, CameraDetail>) resultMap.get("detailMap");
  363. HashMap<String, Camera> snCodeMap = (HashMap<String, Camera>) resultMap.get("snCodeMap");
  364. List<CameraDetail> updateDetailList = new ArrayList<>();
  365. for (CameraInOutParam companyParam : companyParams) {
  366. Camera camera = snCodeMap.get(companyParam.getSnCode());
  367. CameraDetail cameraDetail = detailMap.get(camera.getId());
  368. cameraDetail.setCompanyId(companyParam.getCompanyId());
  369. updateDetailList.add(cameraDetail);
  370. }
  371. return cameraDetailService.updateBatchById(updateDetailList) ? updateDetailList.size() : 0;
  372. }
  373. private void setCompanyIdByDb(List<CameraInOutParam> companyParams){
  374. List<String> companyNames = companyParams.parallelStream().map(CameraInOutParam::getCompanyName).collect(Collectors.toList());
  375. List<Company> companyList = companyService.getCompanyByNames(companyNames);
  376. HashMap<String,Long> companyNameMap = new HashMap<>();
  377. for (Company company : companyList) {
  378. companyNameMap.put(company.getCompanyName(),company.getId());
  379. }
  380. List<Integer> errorList = new ArrayList<>();
  381. Integer index = 0;
  382. for (CameraInOutParam companyParam : companyParams) {
  383. index ++;
  384. if(StringUtils.isNotBlank(companyParam.getCompanyName())){
  385. if(companyNameMap.get(companyParam.getCompanyName()) == null){
  386. errorList.add(index);
  387. }
  388. companyParam.setCompanyId(companyNameMap.get(companyParam.getCompanyName()));
  389. }
  390. }
  391. excelService.toExcelError(errorList);
  392. }
  393. private HashMap<String, Object> getResultMap(List<CameraInOutParam> params,Integer type){
  394. HashMap<String,Object> resultMap = new HashMap<>();
  395. List<String> snCodeList = params.parallelStream().map(CameraInOutParam::getSnCode).collect(Collectors.toList());
  396. List<Camera> cameraList = new ArrayList<>();
  397. if(type == 1){
  398. cameraList = this.getBySnCodes(snCodeList);
  399. }
  400. if(type == 2){
  401. cameraList = this.getByOutSnCodes(snCodeList);
  402. }
  403. checkSnCode(snCodeList,cameraList.stream().map(Camera::getSnCode).collect(Collectors.toList()));
  404. List<Long> cameraIds = cameraList.parallelStream().map(Camera::getId).collect(Collectors.toList());
  405. List<CameraDetail> cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
  406. HashMap<Long,CameraDetail> detailMap = new HashMap<>();
  407. for (CameraDetail cameraDetail : cameraDetails) {
  408. detailMap.put(cameraDetail.getCameraId(),cameraDetail);
  409. }
  410. HashMap<String,Camera> snCodeMap = new HashMap<>();
  411. for (Camera camera : cameraList) {
  412. snCodeMap.put(camera.getSnCode(),camera);
  413. }
  414. List<Integer> errorList = new ArrayList<>();
  415. for (String snCode : snCodeList) {
  416. Camera camera = snCodeMap.get(snCode);
  417. if(camera == null || detailMap.get(camera.getId()) ==null){
  418. errorList.add(snCodeList.indexOf(snCode) );
  419. }
  420. }
  421. excelService.toExcelError(errorList);
  422. resultMap.put("detailMap",detailMap);
  423. resultMap.put("snCodeMap",snCodeMap);
  424. return resultMap;
  425. }
  426. }