CameraServiceImpl.java 19 KB

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