|  |  | 
 |  |  | package com.doumee.service.business.impl.hksync; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.haikang.model.HKConstants; | 
 |  |  | import com.doumee.core.haikang.model.param.BaseListPageResponse; | 
 |  |  | import com.doumee.core.haikang.model.param.BaseResponse; | 
 |  |  | import com.doumee.core.haikang.model.param.request.AcsDeviceListRequest; | 
 |  |  | import com.doumee.core.haikang.model.param.request.AcsDeviceStatusListRequest; | 
 |  |  | import com.doumee.core.haikang.model.param.request.DoorsListRequest; | 
 |  |  | import com.doumee.core.haikang.model.param.request.ParkListRequest; | 
 |  |  | import com.doumee.core.haikang.model.param.request.*; | 
 |  |  | import com.doumee.core.haikang.model.param.respose.*; | 
 |  |  | import com.doumee.core.haikang.service.HKService; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.core.utils.DateUtil; | 
 |  |  | import com.doumee.dao.business.DeviceMapper; | 
 |  |  | import com.doumee.dao.business.ParksMapper; | 
 |  |  | import com.doumee.dao.business.model.Device; | 
 |  |  | import com.doumee.dao.business.model.Parks; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | 
 |  |  |  */ | 
 |  |  | @Service | 
 |  |  | public class HkSyncDeviceServiceImpl extends HkSyncBaseServiceImpl { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private DeviceMapper deviceMapper; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 同步海康门禁设备数据 | 
 |  |  |      * @param param | 
 |  |  | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  | //    @Async | 
 |  |  |     public String syncHkDevices(AcsDeviceListRequest param){ | 
 |  |  |     public String syncHkDevices(Device param){ | 
 |  |  |         if(Constants.DEALING_HK_SYNCDEVICE){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ; | 
 |  |  |         } | 
 |  |  | 
 |  |  |             List<Device> editList = new ArrayList<>(); | 
 |  |  |             Date date = new Date(); | 
 |  |  |             //查询全部门禁设备数据 | 
 |  |  |             List<Device> allList = deviceMapper.selectList(null); | 
 |  |  |             List<Device> allList = deviceMapper.selectList(new QueryWrapper<Device>().lambda() | 
 |  |  |                     .eq(Device::getType,Constants.ZERO)); | 
 |  |  |             List<DoorsInfoResponse> allDoors = getAllDoorList(); | 
 |  |  |             List<AcsDeviceInfoResponse> allHkList = getAllDeViceList(); | 
 |  |  |             /** | 
 |  |  |              * 获取增删改数据集合 | 
 |  |  |              */ | 
 |  |  |             getDataChangeList(allList,allDoors,allHkList,addList,editList,deleteList,date); | 
 |  |  |             getDataChangeList(allList,allDoors,allHkList,addList,editList,deleteList,date ); | 
 |  |  |             if(deleteList.size()>0){ | 
 |  |  |                 //逻辑删除 | 
 |  |  |                 for(Device d : deleteList){ | 
 |  |  | 
 |  |  |             Constants.DEALING_HK_SYNCDEVICE =false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 同步海康门禁设备数据 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  | //    @Async | 
 |  |  |     public String syncHkLed(Device param){ | 
 |  |  |         if(Constants.DEALING_HK_SYNCDEVICE){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ; | 
 |  |  |         } | 
 |  |  |         Constants.DEALING_HK_SYNCDEVICE =true; | 
 |  |  |         try { | 
 |  |  |             List<Device> deleteList = new ArrayList<>(); | 
 |  |  |             List<Device> addList = new ArrayList<>(); | 
 |  |  |             List<Device> editList = new ArrayList<>(); | 
 |  |  |             Date date = new Date(); | 
 |  |  |             //查询全部门禁设备数据 | 
 |  |  |             List<Device> allList = deviceMapper.selectList(new QueryWrapper<Device>().lambda() | 
 |  |  |                     .eq(Device::getType,Constants.TWO)); | 
 |  |  |             List<LedRecordsInfoResponse> allHkList = getAllLedList(); | 
 |  |  |             /** | 
 |  |  |              * 获取增删改数据集合 | 
 |  |  |              */ | 
 |  |  |             getLedDataChangeList(allList,allHkList,addList,editList,deleteList,date); | 
 |  |  |             if(deleteList.size()>0){ | 
 |  |  |                 //逻辑删除 | 
 |  |  |                 for(Device d : deleteList){ | 
 |  |  |                     deviceMapper.updateById(d); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             if(addList.size()>0){ | 
 |  |  |                 deviceMapper.insert(addList); | 
 |  |  |             } | 
 |  |  |             if(editList.size()>0){ | 
 |  |  |                 for(Device d : editList){ | 
 |  |  |                     deviceMapper.updateById(d); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条"; | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步失败!"); | 
 |  |  |         }finally { | 
 |  |  |             Constants.DEALING_HK_SYNCDEVICE =false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void getLedDataChangeList(List<Device> allList, List<LedRecordsInfoResponse> allHkList, List<Device> addList, List<Device> editList, List<Device> deleteList, Date date) { | 
 |  |  |         if(allHkList!=null && allHkList.size()>0){ | 
 |  |  |             //获取海康全部门禁组数据 | 
 |  |  |             for(LedRecordsInfoResponse device : allHkList){ | 
 |  |  |                 Device model = getExistedLed(device,allList); | 
 |  |  |                 if(model !=null){ | 
 |  |  |                     //如果已存在,则更新数据 | 
 |  |  |                     model =  initLedDataByHkData(model,device,date); | 
 |  |  |                     editList.add(model); | 
 |  |  |                 }else{ | 
 |  |  |                     //如果不存在,则新增数据 | 
 |  |  |                     model = new Device(); | 
 |  |  |                     model =  initLedDataByHkData(model,device,date); | 
 |  |  |                     model.setIsEntrance(Constants.ZERO); | 
 |  |  |                     addList.add(model); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //判断获取删除的门禁设备,逻辑删除 | 
 |  |  |         getDeleteLedList(allList,allHkList,deleteList,date); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 同步海康门禁设备数据 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  | //    @Async | 
 |  |  |     public String syncHkBroadcast(Device param){ | 
 |  |  |         if(Constants.DEALING_HK_SYNCDEVICE){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ; | 
 |  |  |         } | 
 |  |  |         Constants.DEALING_HK_SYNCDEVICE =true; | 
 |  |  |         try { | 
 |  |  |             List<Device> deleteList = new ArrayList<>(); | 
 |  |  |             List<Device> addList = new ArrayList<>(); | 
 |  |  |             List<Device> editList = new ArrayList<>(); | 
 |  |  |             Date date = new Date(); | 
 |  |  |             //查询全部广播点设备数据 | 
 |  |  |             List<Device> allList = deviceMapper.selectList(new QueryWrapper<Device>().lambda() | 
 |  |  |                     .eq(Device::getType,Constants.THREE)); | 
 |  |  |             List<FetchAudioChannelInfoResponse> allHkList = getAllBroadcastList(); | 
 |  |  |             /** | 
 |  |  |              * 获取增删改数据集合 | 
 |  |  |              */ | 
 |  |  |             getBroadcastDataChangeList(allList,allHkList,addList,editList,deleteList,date); | 
 |  |  |             if(deleteList.size()>0){ | 
 |  |  |                 //逻辑删除 | 
 |  |  |                 for(Device d : deleteList){ | 
 |  |  |                     deviceMapper.updateById(d); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             if(addList.size()>0){ | 
 |  |  |                 deviceMapper.insert(addList); | 
 |  |  |             } | 
 |  |  |             if(editList.size()>0){ | 
 |  |  |                 for(Device d : editList){ | 
 |  |  |                     deviceMapper.updateById(d); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条"; | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步失败!"); | 
 |  |  |         }finally { | 
 |  |  |             Constants.DEALING_HK_SYNCDEVICE =false; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void getBroadcastDataChangeList(List<Device> allList, List<FetchAudioChannelInfoResponse> allHkList, List<Device> addList, List<Device> editList, List<Device> deleteList, Date date) { | 
 |  |  |         if(allHkList!=null && allHkList.size()>0){ | 
 |  |  |             //获取海康全部门禁组数据 | 
 |  |  |             for(FetchAudioChannelInfoResponse device : allHkList){ | 
 |  |  |                 Device model = getExistedBroadcast(device,allList); | 
 |  |  |                 if(model !=null){ | 
 |  |  |                     //如果已存在,则更新数据 | 
 |  |  |                     model =  initBroadcastDataByHkData(model,device,date); | 
 |  |  |                     editList.add(model); | 
 |  |  |                 }else{ | 
 |  |  |                     //如果不存在,则新增数据 | 
 |  |  |                     model = new Device(); | 
 |  |  |                     model =  initBroadcastDataByHkData(model,device,date); | 
 |  |  |                     model.setIsEntrance(Constants.ZERO); | 
 |  |  |                     addList.add(model); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //判断获取删除的门禁设备,逻辑删除 | 
 |  |  |         getDeleteBroadcastList(allList,allHkList,deleteList,date); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | //    @Async | 
 |  |  |     public void getAscDeviceStatus(){ | 
 |  |  | 
 |  |  |         } | 
 |  |  |         return  allDoorList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取全部广播点数据 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public  List<FetchAudioChannelInfoResponse>  getAllBroadcastList(){ | 
 |  |  |         List<FetchAudioChannelInfoResponse> allDoorList = new ArrayList<>(); | 
 |  |  |         Date date = new Date(); | 
 |  |  |         boolean hasNext = true; | 
 |  |  |         int curTotal = 0; | 
 |  |  |         int curPage = 1; | 
 |  |  |         while (hasNext){ | 
 |  |  |             //分页遍历循环查询所有门禁设备数据 | 
 |  |  |             FetchAudioChannelRequest param = new FetchAudioChannelRequest(); | 
 |  |  |             param.setPageNo(curPage); | 
 |  |  |             param.setPageSize(100); | 
 |  |  |             BaseResponse<BaseListPageResponse<FetchAudioChannelInfoResponse>>  response = HKService.fetchAudioChannel(param); | 
 |  |  |             if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~"); | 
 |  |  |             } | 
 |  |  |             BaseListPageResponse<FetchAudioChannelInfoResponse> r = response.getData(); | 
 |  |  |             curTotal += 100; | 
 |  |  |             if(curTotal >= r.getTotal()){ | 
 |  |  |                 hasNext = false; | 
 |  |  |             } | 
 |  |  |             if(r.getList() == null || r.getList().size()==0){ | 
 |  |  |                 hasNext =false; | 
 |  |  |             }else{ | 
 |  |  |                 allDoorList.addAll(r.getList()); | 
 |  |  |             } | 
 |  |  |             curPage++; | 
 |  |  |         } | 
 |  |  |         return  allDoorList; | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 获取全部LED数据 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public  List<LedRecordsInfoResponse>  getAllLedList(){ | 
 |  |  |         List<LedRecordsInfoResponse> allDoorList = new ArrayList<>(); | 
 |  |  |         Date date = new Date(); | 
 |  |  |         boolean hasNext = true; | 
 |  |  |         int curTotal = 0; | 
 |  |  |         int curPage = 1; | 
 |  |  |         while (hasNext){ | 
 |  |  |             //分页遍历循环查询所有门禁设备数据 | 
 |  |  |             LedRecordsRequest param = new LedRecordsRequest(); | 
 |  |  |             param.setPageNo(curPage); | 
 |  |  |             param.setPageSize(100); | 
 |  |  |             BaseResponse<BaseListPageResponse<LedRecordsInfoResponse>>    response = HKService.ledRecords(param); | 
 |  |  |             if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~"); | 
 |  |  |             } | 
 |  |  |             BaseListPageResponse<LedRecordsInfoResponse> r = response.getData(); | 
 |  |  |             curTotal += 100; | 
 |  |  |             if(curTotal >= r.getTotal()){ | 
 |  |  |                 hasNext = false; | 
 |  |  |             } | 
 |  |  |             if(r.getList() == null || r.getList().size()==0){ | 
 |  |  |                 hasNext =false; | 
 |  |  |             }else{ | 
 |  |  |                 allDoorList.addAll(r.getList()); | 
 |  |  |             } | 
 |  |  |             curPage++; | 
 |  |  |         } | 
 |  |  |         return  allDoorList; | 
 |  |  |     } | 
 |  |  |     public  List<AscDeviceStatusInfoResponse>  getAllDeviceStatusList(){ | 
 |  |  |         List<AscDeviceStatusInfoResponse> allDoorList = new ArrayList<>(); | 
 |  |  |         Date date = new Date(); | 
 |  |  | 
 |  |  |         return  allDoorList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void getDataChangeList(List<Device> allList, List<DoorsInfoResponse> hkDoorList , List<AcsDeviceInfoResponse> allHkList, List<Device> addList, List<Device> editList,List<Device> deleteList, Date date) { | 
 |  |  |     private void getDataChangeList(List<Device> allList, | 
 |  |  |                                    List<DoorsInfoResponse> hkDoorList , | 
 |  |  |                                    List<AcsDeviceInfoResponse> allHkList, | 
 |  |  |                                    List<Device> addList, | 
 |  |  |                                    List<Device> editList, | 
 |  |  |                                    List<Device> deleteList, | 
 |  |  |                                    Date date ) { | 
 |  |  |         if(hkDoorList!=null && hkDoorList.size()>0){ | 
 |  |  |             //获取海康全部门禁组数据 | 
 |  |  |             for(DoorsInfoResponse device : hkDoorList){ | 
 |  |  | 
 |  |  |         getDeleteDoorList(allList,hkDoorList,deleteList,date); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private Device initLedDataByHkData(Device model, LedRecordsInfoResponse door, Date date) { | 
 |  |  |         model.setIsdeleted(Constants.ZERO); | 
 |  |  |         model.setHkId(door.getModel_data_id()); | 
 |  |  |         model.setHkDate(date); | 
 |  |  |         model.setHkStatus(Constants.ONE); | 
 |  |  |         model.setNo(door.getIndex_code()); | 
 |  |  |         model.setType(Constants.TWO); | 
 |  |  |         model.setManufature(door.getManufacturer()); | 
 |  |  |         model.setPort(door.getPort()); | 
 |  |  |         model.setIp(door.getAddr()); | 
 |  |  |         model.setResourceType(door.getDevice_type()); | 
 |  |  |         model.setName(door.getScreen_name()); | 
 |  |  |         model.setRemark(JSONObject.toJSONString(door)); | 
 |  |  |         return  model; | 
 |  |  |     } | 
 |  |  |     private Device initBroadcastDataByHkData(Device model, FetchAudioChannelInfoResponse device, Date date) { | 
 |  |  |         model.setIsdeleted(Constants.ZERO); | 
 |  |  |         model.setHkDate(date); | 
 |  |  |         model.setHkId(device.getAudioChannelIndexCode()); | 
 |  |  |         model.setName(device.getAudioChannelName()); | 
 |  |  |         model.setHkStatus(Constants.ONE); | 
 |  |  |         model.setType(Constants.THREE); | 
 |  |  |         model.setChannelNo(device.getChannelNo()); | 
 |  |  |         model.setChannelInfo(device.getChannelType()); | 
 |  |  |         model.setResourceType(device.getAudioChannelType()); | 
 |  |  |         model.setStatus(device.getState()); | 
 |  |  |         model.setRegionName(device.getRegionName()); | 
 |  |  |         return  model; | 
 |  |  |     } | 
 |  |  |     private Device initDoorDataByHkData(Device model, DoorsInfoResponse door, Date date, List<AcsDeviceInfoResponse> allHkList) { | 
 |  |  |         AcsDeviceInfoResponse device = getDeviceByDoorid(door.getParentIndexCode(),allHkList); | 
 |  |  |         model.setIsdeleted(Constants.ZERO); | 
 |  |  | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } | 
 |  |  |     private Device getExistedLed(LedRecordsInfoResponse device, List<Device> allList) { | 
 |  |  |         if(allList.size()>0){ | 
 |  |  |             for(Device r : allList){ | 
 |  |  |                 if(StringUtils.equals(r.getHkId(), device.getModel_data_id())){ | 
 |  |  |                     //表示未删除 | 
 |  |  |                     return  r; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } | 
 |  |  |     private Device getExistedBroadcast(FetchAudioChannelInfoResponse device, List<Device> allList) { | 
 |  |  |         if(allList.size()>0){ | 
 |  |  |             for(Device r : allList){ | 
 |  |  |                 if(StringUtils.equals(r.getHkId(), device.getAudioChannelIndexCode())){ | 
 |  |  |                     //表示未删除 | 
 |  |  |                     return  r; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } | 
 |  |  |     private Device getExistedDevice(AcsDeviceInfoResponse device, List<Device> allList) { | 
 |  |  |         if(allList.size()>0){ | 
 |  |  |             for(Device r : allList){ | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     private void getDeleteLedList(List<Device> allList,  List<LedRecordsInfoResponse> allHkList,List<Device> deleteList ,Date date) { | 
 |  |  |         if(allList!=null && allList.size()>0){ | 
 |  |  |             for(Device device : allList){ | 
 |  |  |                 if(isDeletedLedDevice(device,allHkList)){ | 
 |  |  |                     device.setIsdeleted(Constants.ONE); | 
 |  |  |                     device.setEditDate(date); | 
 |  |  |                     deleteList.add(device); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     private void getDeleteBroadcastList(List<Device> allList,  List<FetchAudioChannelInfoResponse> allHkList,List<Device> deleteList ,Date date) { | 
 |  |  |         if(allList!=null && allList.size()>0){ | 
 |  |  |             for(Device device : allList){ | 
 |  |  |                 if(isDeletedBroadcastDevice(device,allHkList)){ | 
 |  |  |                     device.setIsdeleted(Constants.ONE); | 
 |  |  |                     device.setEditDate(date); | 
 |  |  |                     deleteList.add(device); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private boolean isDeletedDoorDevice(Device device, List<DoorsInfoResponse> allHkList) { | 
 |  |  |         if(allHkList.size()>0){ | 
 |  |  | 
 |  |  |         return  true; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     private boolean isDeletedLedDevice(Device device, List<LedRecordsInfoResponse> allHkList) { | 
 |  |  |         if(allHkList.size()>0){ | 
 |  |  |             for(LedRecordsInfoResponse r : allHkList){ | 
 |  |  |                 if(StringUtils.equals(device.getHkId(), r.getModel_data_id())){ | 
 |  |  |                     //表示未删除 | 
 |  |  |                     return  false; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  true; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     private boolean isDeletedBroadcastDevice(Device device, List<FetchAudioChannelInfoResponse> allHkList) { | 
 |  |  |         if(allHkList.size()>0){ | 
 |  |  |             for(FetchAudioChannelInfoResponse r : allHkList){ | 
 |  |  |                 if(StringUtils.equals(device.getHkId(), r.getAudioChannelIndexCode())){ | 
 |  |  |                     //表示未删除 | 
 |  |  |                     return  false; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  true; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     private boolean isDeletedDevice(Device device, List<AcsDeviceInfoResponse> allHkList) { | 
 |  |  |         if(allHkList.size()>0){ | 
 |  |  |             for(AcsDeviceInfoResponse r : allHkList){ |