CameraServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.common.constant.Constant;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.exception.BusinessException;
  9. import com.fdkankan.common.response.PageInfo;
  10. import com.fdkankan.common.util.DateUtil;
  11. import com.fdkankan.manage.common.CameraTypeEnum;
  12. import com.fdkankan.manage.constant.CameraConstant;
  13. import com.fdkankan.manage.entity.Camera;
  14. import com.fdkankan.manage.entity.CameraDetail;
  15. import com.fdkankan.manage.entity.Company;
  16. import com.fdkankan.manage.mapper.ICameraMapper;
  17. import com.fdkankan.manage.service.*;
  18. import com.fdkankan.manage.util.Dateutils;
  19. import com.fdkankan.manage.vo.request.CameraInOutParam;
  20. import com.fdkankan.manage.vo.request.CameraParam;
  21. import com.fdkankan.manage.vo.response.CameraDetailVo;
  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. public class CameraServiceImpl extends ServiceImpl<ICameraMapper, Camera> implements ICameraService {
  38. @Autowired
  39. ICameraDetailService cameraDetailService;
  40. @Autowired
  41. ISceneService sceneService;
  42. @Autowired
  43. ISceneProService sceneProService;
  44. @Autowired
  45. IUserIncrementService userIncrementService;
  46. @Autowired
  47. ISceneResourceCameraService sceneResourceCameraService;
  48. @Autowired
  49. IScenePlusService scenePlusService;
  50. @Autowired
  51. ICompanyService companyService;
  52. @Value("${upload.type}")
  53. private String ossType;
  54. @Override
  55. public List<Camera> getListByCameraIdList(List<Long> cameraIdList) {
  56. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  57. wrapper.in(Camera::getId,cameraIdList);
  58. return this.list(wrapper);
  59. }
  60. @Override
  61. public Camera getBySnCode(String snCode) {
  62. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  63. wrapper.eq(Camera::getSnCode,snCode);
  64. List<Camera> list = this.list(wrapper);
  65. if(list == null || list.size() <=0 ){
  66. return null;
  67. }
  68. return list.get(0);
  69. }
  70. private List<Camera> getBySnCodes(List<String> snCodeList) {
  71. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  72. wrapper.in(Camera::getSnCode,snCodeList);
  73. return this.list(wrapper);
  74. }
  75. @Override
  76. public void unbindCamera(Long cameraId) {
  77. cameraDetailService.unbindCamera(cameraId); //取消相机用户关联
  78. userIncrementService.unbindCamera(cameraId); //取消关联用户权益
  79. sceneService.unbindCamera(cameraId); //取消关联场景
  80. sceneProService.unbindCamera(cameraId); //取消关联场景
  81. scenePlusService.unbindCamera(cameraId); //取消关联场景
  82. sceneResourceCameraService.unbindCamera(cameraId); //删除协作相机
  83. }
  84. @Override
  85. public PageInfo pageList(CameraParam param) {
  86. Page<CameraDetailVo> voPage = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  87. return PageInfo.PageInfo(voPage);
  88. }
  89. @Override
  90. public void in(String wifiName) {
  91. if(StringUtils.isEmpty(wifiName) ){
  92. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  93. }
  94. if( !wifiName.contains("_") || !wifiName.contains("4D")){
  95. throw new BusinessException(-1,"wifi名称格式错误");
  96. }
  97. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  98. wrapper.eq(Camera::getWifiName,wifiName);
  99. long count = this.count(wrapper);
  100. if(count > 0){
  101. throw new BusinessException(-1,"设备wifi名称重复");
  102. }
  103. saveBatchCamera(Collections.singletonList(wifiName));
  104. }
  105. private Integer saveBatchCamera(List<String> wifiNameList){
  106. HashSet<String> wifiNameSet = new HashSet<>(wifiNameList);
  107. List<Camera> cameraList = new ArrayList<>();
  108. for (String wifiName : wifiNameSet) {
  109. String[] res = wifiName.split("_");
  110. Camera camera = new Camera();
  111. camera.setWifiName(wifiName);
  112. camera.setSnCode(res[1]);
  113. camera.setChildName(res[1]);
  114. camera.setWifiPassword(CameraConstant.WIFI_PASSWORD_VALUE);
  115. camera.setChildPassword(CameraConstant.CHILD_PASSWORD_VALUE);
  116. camera.setActivatedTime(DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  117. cameraList.add(camera);
  118. }
  119. this.saveBatch(cameraList);
  120. List<CameraDetail> cameraDetailList = new ArrayList<>();
  121. for (Camera camera : cameraList) {
  122. String wifiName = camera.getWifiName();
  123. Integer type = CameraTypeEnum.getTypeByWifiNamePrefix(wifiName.substring(0,wifiName.lastIndexOf("_") +1));
  124. if(type == null){
  125. throw new BusinessException(-1,"wifi名称前缀错误!");
  126. }
  127. CameraDetail cameraDetail = new CameraDetail();
  128. cameraDetail.setAgency(CameraConstant.DEFAULT_AGENT);
  129. cameraDetail.setCameraId(camera.getId());
  130. cameraDetail.setCountry(0);//默认中国
  131. if("aws".equals(ossType)){
  132. cameraDetail.setCountry(1);//1-国外
  133. }
  134. cameraDetail.setType(type);
  135. if (1 == cameraDetail.getType() || 9 == cameraDetail.getType() || 10 == cameraDetail.getType()){
  136. cameraDetail.setTotalSpace(Long.valueOf(Constant.CAMERA_BASE_SPACE_VALUE));
  137. cameraDetail.setUsedSpace(0L);
  138. }
  139. if(type != 9 && type != 10){
  140. type = type == 0 ? 1: 4;
  141. }
  142. cameraDetail.setGoodsId(Long.valueOf(type));
  143. cameraDetailList.add(cameraDetail);
  144. }
  145. return cameraDetailService.saveBatch(cameraDetailList) ? cameraList.size() : 0;
  146. }
  147. @Override
  148. public Integer ins(List<String> wifiNameList){
  149. List<Integer> errorRow = getErrorRow(wifiNameList);
  150. getResultIn(errorRow);
  151. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  152. wrapper.in(Camera::getWifiName,wifiNameList);
  153. List<Camera> list = this.list(wrapper);
  154. if(list.size() >0){
  155. List<String> newList = list.parallelStream().map(Camera::getWifiName).collect(Collectors.toList());
  156. List<Integer> errorRow2 = getErrorRow(wifiNameList, newList);
  157. getResultIn(errorRow2);
  158. }
  159. return saveBatchCamera(wifiNameList);
  160. }
  161. private void getResultIn(List<Integer> errorList){
  162. if(errorList.size() <=0){
  163. return;
  164. }
  165. StringBuilder res = new StringBuilder("第");
  166. for (Integer index : errorList) {
  167. res.append(index ).append(",");
  168. }
  169. res.deleteCharAt(res.lastIndexOf(","));
  170. res.append("行存在错误");
  171. throw new BusinessException(-1, res.toString());
  172. }
  173. private List<Integer> getErrorRow(List<String> wifiNameList){
  174. List<Integer> errorIndexList = new ArrayList<>();
  175. Set<String> wifiNameSet = new HashSet<>();
  176. Integer index = 3;
  177. for (String wifiName : wifiNameList) {
  178. index ++;
  179. if(StringUtils.isBlank(wifiName)){
  180. errorIndexList.add(index);
  181. continue;
  182. }
  183. if(wifiNameSet.contains(wifiName)){
  184. errorIndexList.add(index);
  185. continue;
  186. }
  187. if( !wifiName.contains("_") || !wifiName.contains("4D")
  188. || !CameraTypeEnum.typePrefixMap.containsKey(wifiName.split("_")[0]+"_")){
  189. errorIndexList.add(index);
  190. }
  191. wifiNameSet.add(wifiName);
  192. }
  193. return errorIndexList;
  194. }
  195. private List<Integer> getErrorRow(List<String> wifiNameList,List<String> dbList){
  196. List<Integer> errorIndexList = new ArrayList<>();
  197. if(dbList.size() <=0){
  198. return errorIndexList;
  199. }
  200. Integer index = 3;
  201. for (String wifiName : wifiNameList) {
  202. index ++;
  203. if(dbList.contains(wifiName)){
  204. errorIndexList.add(index);
  205. }
  206. }
  207. return errorIndexList;
  208. }
  209. @Override
  210. public void out(CameraInOutParam param) {
  211. if(param.getOutType() == null || param.getCompanyName() == null || param.getId() == null){
  212. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  213. }
  214. Company company = companyService.getCompanyByName(param.getCompanyName());
  215. if(company == null){
  216. throw new BusinessException(-1,"客户名称不存在");
  217. }
  218. param.setCompanyId(company.getId());
  219. Camera camera = this.getById(param.getId());
  220. if(camera == null){
  221. throw new BusinessException(-1,"相机不存在");
  222. }
  223. CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
  224. if(detail ==null){
  225. throw new BusinessException(-1,"该相机未入库");
  226. }
  227. HashMap<Long,CameraDetail> detailMap = new HashMap<>();
  228. detailMap.put(detail.getCameraId(),detail);
  229. this.saveBatchDetail(detailMap, Collections.singletonList(param));
  230. }
  231. @Override
  232. public Integer outs(List<CameraInOutParam> params){
  233. setCompanyIdByDb(params);
  234. HashMap<String, Object> resultMap = getResultMap(params);
  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 Integer saveBatchDetail(HashMap<Long, CameraDetail> detailMap, List<CameraInOutParam> params){
  245. List<CameraDetail> cameraDetails = new ArrayList<>();
  246. for (CameraInOutParam param : params) {
  247. CameraDetail cameraDetail = detailMap.get(param.getId());
  248. if(cameraDetail == null){
  249. throw new BusinessException(-1,"相机未入库");
  250. }
  251. cameraDetail.setOrderSn(param.getOrderSn());
  252. cameraDetail.setOwn(param.getOutType());
  253. cameraDetail.setCompanyId(param.getCompanyId());
  254. cameraDetail.setOutTime(Dateutils.getDate(new Date()));
  255. cameraDetails.add(cameraDetail);
  256. }
  257. return cameraDetailService.saveOrUpdateBatch(cameraDetails) ? cameraDetails.size() : 0;
  258. }
  259. private void checkSnCode(List<String> snCodeList,List<String> dbSnCode) {
  260. HashMap<String,Integer> map = new HashMap<>();
  261. for (String snCode : snCodeList) {
  262. if(StringUtils.isBlank(snCode)){
  263. map.put(snCode,2);
  264. }
  265. if(map.get(snCode) == null){
  266. map.put(snCode,1);
  267. }else {
  268. map.put(snCode,2);
  269. }
  270. }
  271. List<String> errorSnCode = new ArrayList<>();
  272. for (String snCode : map.keySet()) {
  273. if(map.get(snCode) > 1){
  274. errorSnCode.add(snCode);
  275. }
  276. }
  277. if(errorSnCode.size() >0){
  278. throw new BusinessException(-1,"存在重复的snCode:"+errorSnCode);
  279. }
  280. List<String> errorSnCode2 = new ArrayList<>();
  281. for (String snCode : snCodeList) {
  282. if(!dbSnCode.contains(snCode)){
  283. errorSnCode2.add(snCode);
  284. }
  285. }
  286. if(errorSnCode2.size() >0){
  287. throw new BusinessException(-1,"snCode:"+errorSnCode2+"不存在");
  288. }
  289. }
  290. @Override
  291. public void updateCamera(CameraInOutParam param) {
  292. if(param.getId() == null|| param.getOutType() == null){
  293. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  294. }
  295. CameraDetail cameraDetail = cameraDetailService.getByCameraId(param.getId());
  296. if(cameraDetail == null){
  297. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  298. }
  299. if(StringUtils.isNotBlank(param.getCompanyName())){
  300. Company company = companyService.getCompanyByName(param.getCompanyName());
  301. if(company !=null){
  302. cameraDetail.setCompanyId(company.getId());
  303. }
  304. }
  305. cameraDetail.setOwn(param.getOutType());
  306. cameraDetail.setOrderSn(param.getOrderSn());
  307. cameraDetailService.updateById(cameraDetail);
  308. }
  309. @Override
  310. public void deleteCamera(Long id) {
  311. if(id == null){
  312. throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
  313. }
  314. CameraDetail cameraDetail = cameraDetailService.getByCameraId(id);
  315. if(cameraDetail !=null && cameraDetail.getUserId()!=null){
  316. throw new BusinessException(-1,"相机已绑定,请先解绑后删除");
  317. }
  318. this.removeById(id);
  319. cameraDetailService.deleteByCameraId(id);
  320. }
  321. @Override
  322. public Integer updateCompany(List<CameraInOutParam> companyParams) {
  323. setCompanyIdByDb(companyParams);
  324. HashMap<String, Object> resultMap = getResultMap(companyParams);
  325. HashMap<Long, CameraDetail> detailMap = (HashMap<Long, CameraDetail>) resultMap.get("detailMap");
  326. HashMap<String, Camera> snCodeMap = (HashMap<String, Camera>) resultMap.get("snCodeMap");
  327. List<CameraDetail> updateDetailList = new ArrayList<>();
  328. for (CameraInOutParam companyParam : companyParams) {
  329. Camera camera = snCodeMap.get(companyParam.getSnCode());
  330. CameraDetail cameraDetail = detailMap.get(camera.getId());
  331. cameraDetail.setCompanyId(companyParam.getCompanyId());
  332. updateDetailList.add(cameraDetail);
  333. }
  334. return cameraDetailService.updateBatchById(updateDetailList) ? updateDetailList.size() : 0;
  335. }
  336. private void setCompanyIdByDb(List<CameraInOutParam> companyParams){
  337. List<String> companyNames = companyParams.parallelStream().map(CameraInOutParam::getCompanyName).collect(Collectors.toList());
  338. List<Company> companyList = companyService.getCompanyByNames(companyNames);
  339. HashMap<String,Long> companyNameMap = new HashMap<>();
  340. for (Company company : companyList) {
  341. companyNameMap.put(company.getCompanyName(),company.getId());
  342. }
  343. for (CameraInOutParam companyParam : companyParams) {
  344. if(StringUtils.isNotBlank(companyParam.getCompanyName())){
  345. if(companyNameMap.get(companyParam.getCompanyName()) == null){
  346. throw new BusinessException(-1,"客户名称"+ companyParam.getCompanyName()+"-不存在");
  347. }
  348. companyParam.setCompanyId(companyNameMap.get(companyParam.getCompanyName()));
  349. }
  350. }
  351. }
  352. private HashMap<String, Object> getResultMap(List<CameraInOutParam> params){
  353. HashMap<String,Object> resultMap = new HashMap<>();
  354. List<String> snCodeList = params.parallelStream().map(CameraInOutParam::getSnCode).collect(Collectors.toList());
  355. if(snCodeList.size() <=0){
  356. throw new BusinessException(-1,"snCode为空");
  357. }
  358. List<Camera> cameraList = this.getBySnCodes(snCodeList);
  359. checkSnCode(snCodeList,cameraList.stream().map(Camera::getSnCode).collect(Collectors.toList()));
  360. List<Long> cameraIds = cameraList.parallelStream().map(Camera::getId).collect(Collectors.toList());
  361. List<CameraDetail> cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
  362. HashMap<Long,CameraDetail> detailMap = new HashMap<>();
  363. for (CameraDetail cameraDetail : cameraDetails) {
  364. detailMap.put(cameraDetail.getCameraId(),cameraDetail);
  365. }
  366. HashMap<String,Camera> snCodeMap = new HashMap<>();
  367. for (Camera camera : cameraList) {
  368. snCodeMap.put(camera.getSnCode(),camera);
  369. }
  370. List<Integer> errorList = new ArrayList<>();
  371. for (String snCode : snCodeList) {
  372. Camera camera = snCodeMap.get(snCode);
  373. if(camera == null || detailMap.get(camera.getId()) ==null){
  374. errorList.add(snCodeList.indexOf(snCode) +3);
  375. }
  376. }
  377. getResultIn(errorList);
  378. resultMap.put("detailMap",detailMap);
  379. resultMap.put("snCodeMap",snCodeMap);
  380. return resultMap;
  381. }
  382. }