package com.doumee.service.business.impl.hksync; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; 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.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; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 设备信息表Service实现 * @author 江蹄蹄 * @date 2023/11/30 15:33 */ @Service public class HkSyncDeviceServiceImpl extends HkSyncBaseServiceImpl { @Autowired private DeviceMapper deviceMapper; /** * 同步海康门禁设备数据 * @param param * @return */ @Override // @Async public String syncHkDevices(AcsDeviceListRequest param){ if(Constants.DEALING_HK_SYNCDEVICE){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ; } Constants.DEALING_HK_SYNCDEVICE =true; try { List deleteList = new ArrayList<>(); List addList = new ArrayList<>(); List editList = new ArrayList<>(); Date date = new Date(); //查询全部门禁设备数据 List allList = deviceMapper.selectList(null); List allDoors = getAllDoorList(); List allHkList = getAllDeViceList(); /** * 获取增删改数据集合 */ getDataChangeList(allList,allDoors,allHkList,addList,editList,deleteList,date); if(deleteList.size()>0){ //逻辑删除 for(Device d : deleteList){ deviceMapper.updateById(d); } } if(addList.size()>0){ deviceMapper.insertBatchSomeColumn(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; } } @Override // @Async public void getAscDeviceStatus(){ if(Constants.DEALING_HK_SYNCDEVICE_STATUS){ return; } Constants.DEALING_HK_SYNCDEVICE_STATUS =true; try { List editList = new ArrayList<>(); Date date = new Date(); //查询全部门禁设备数据 List allHkList = getAllDeviceStatusList(); if(allHkList.size()>0){ //逻辑删除 for(AscDeviceStatusInfoResponse d : allHkList){ deviceMapper.update(null,new UpdateWrapper().lambda() .set(Device::getOnline,d.getOnline()) .set(Device::getIp,d.getIp()) .set(Device::getPort,d.getPort()) .set(Device::getManufature,d.getManufacturer()) .set(Device::getEditDate,date) .eq(Device::getHkId,d.getIndexCode())); } } }catch (Exception e){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步设备状态失败!"); }finally { Constants.DEALING_HK_SYNCDEVICE_STATUS =false; } } private List getAllDeViceList() { AcsDeviceListRequest param = new AcsDeviceListRequest(); List allHkList = new ArrayList<>(); boolean hasNext = true; int curTotal = 0; int curPage = 1; while (hasNext){ //分页遍历循环查询所有门禁设备数据 param = new AcsDeviceListRequest(); param.setPageNo(curPage); param.setPageSize(100); BaseResponse response = HKService.acsDeviceList(param); if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~"); } AcsDeviceListResponse r = response.getData(); curTotal += 100; if(curTotal >= r.getTotal()){ hasNext = false; } if(r.getList() == null || r.getList().size()==0){ hasNext =false; }else{ allHkList.addAll(r.getList()); } curPage++; } return allHkList; } public List getAllDoorList(){ List allDoorList = new ArrayList<>(); Date date = new Date(); boolean hasNext = true; int curTotal = 0; int curPage = 1; while (hasNext){ //分页遍历循环查询所有门禁设备数据 DoorsListRequest param = new DoorsListRequest(); param.setPageNo(curPage); param.setPageSize(100); BaseResponse response = HKService.doorSearch(param); if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~"); } DoorsListResponse 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 getAllDeviceStatusList(){ List allDoorList = new ArrayList<>(); Date date = new Date(); boolean hasNext = true; int curTotal = 0; int curPage = 1; while (hasNext){ //分页遍历循环查询所有门禁设备数据 AcsDeviceStatusListRequest param = new AcsDeviceStatusListRequest(); param.setPageNo(curPage); param.setPageSize(100); BaseResponse> response = HKService.acsDeviceStatus(param); if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~"); } BaseListPageResponse 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; } private void getDataChangeList(List allList, List hkDoorList , List allHkList, List addList, List editList,List deleteList, Date date) { if(hkDoorList!=null && hkDoorList.size()>0){ //获取海康全部门禁组数据 for(DoorsInfoResponse device : hkDoorList){ Device model = getExistedDoor(device,allList); if(model !=null){ //如果已存在,则更新数据 model = initDoorDataByHkData(model,device,date,allHkList); editList.add(model); }else{ //如果不存在,则新增数据 model = new Device(); model = initDoorDataByHkData(model,device,date,allHkList); model.setIsEntrance(Constants.ZERO); addList.add(model); } } } //判断获取删除的门禁设备,逻辑删除 getDeleteDoorList(allList,hkDoorList,deleteList,date); } private Device initDoorDataByHkData(Device model, DoorsInfoResponse door, Date date, List allHkList) { AcsDeviceInfoResponse device = getDeviceByDoorid(door.getParentIndexCode(),allHkList); model.setIsdeleted(Constants.ZERO); model.setIp(device.getIp()); model.setHkDate(date); model.setCreateDate(DateUtil.getISO8601DateByStr(device.getCreateTime())); model.setEditDate(DateUtil.getISO8601DateByStr(device.getUpdateTime())); model.setNo(device.getDevSerialNum()); model.setHkId(device.getIndexCode()); model.setName(device.getName()); model.setHkStatus(Constants.ONE); model.setManufature(device.getManufacturer()); model.setType(Constants.ZERO); // model.setNo(device.getDevSerialNum()); // model.setChannelNo(getChannelNo(device.getIndexCode(),hkDoorList,model)); model.setChannelNo(door.getChannelNo()); model.setChannelInfo(door.getName()); model.setResourceType(device.getResourceType()); model.setDevTypeCode(device.getDevTypeCode()); model.setDevTypeDesc(device.getDevTypeDesc()); model.setManufature(device.getManufacturer()); model.setPort(device.getPort()); model.setRemark(JSONObject.toJSONString(device)); model.setDoorId(door.getIndexCode()); model.setDoorName(door.getName()); model.setRegionName(door.getRegionName()); model.setRegionPathName(door.getRegionPathName()); model.setDoorNo(door.getDoorNo()); return model; } private AcsDeviceInfoResponse getDeviceByDoorid(String indexCode, List allHkList ) { if(allHkList!=null && allHkList.size()>0){ for(AcsDeviceInfoResponse info : allHkList){ if(StringUtils.equals(indexCode,info.getIndexCode())){ return info; } } } return new AcsDeviceInfoResponse(); } private void getDataChangeList(List allList, List allHkList, List addList, List editList,List deleteList, Date date) { if(allHkList!=null && allHkList.size()>0){ //获取海康全部门禁组数据 List hkDoorList = getAllDoorList(); for(AcsDeviceInfoResponse device : allHkList){ Device model = getExistedDevice(device,allList); if(model !=null){ //如果已存在,则更新数据 model = initDataByHkData(model,device,date,hkDoorList); editList.add(model); }else{ //如果不存在,则新增数据 model = new Device(); model = initDataByHkData(model,device,date,hkDoorList); addList.add(model); } } } //判断获取删除的门禁设备,逻辑删除 getDeleteList(allList,allHkList,deleteList,date); } /** * 初始化海康入库数据 * @param model * @param device * @param date * @return */ private Device initDataByHkData(Device model, AcsDeviceInfoResponse device,Date date, List hkDoorList) { model.setIsdeleted(Constants.ZERO); model.setIp(device.getIp()); model.setHkDate(date); model.setCreateDate(DateUtil.getISO8601DateByStr(device.getCreateTime())); model.setEditDate(DateUtil.getISO8601DateByStr(device.getUpdateTime())); model.setNo(device.getDevSerialNum()); model.setHkId(device.getIndexCode()); model.setName(device.getName()); model.setHkStatus(Constants.ONE); model.setManufature(device.getManufacturer()); model.setType(Constants.ZERO); // model.setNo(device.getDevSerialNum()); model.setChannelNo(getChannelNo(device.getIndexCode(),hkDoorList,model)); model.setResourceType(device.getResourceType()); model.setDevTypeCode(device.getDevTypeCode()); model.setDevTypeDesc(device.getDevTypeDesc()); model.setManufature(device.getManufacturer()); model.setPort(device.getPort()); model.setRemark(JSONObject.toJSONString(device)); return model; } private String getChannelNo(String indexCode, List hkDoorList,Device model ) { String str = ""; String names = ""; if(hkDoorList!=null && hkDoorList.size()>0){ for(DoorsInfoResponse info : hkDoorList){ if(StringUtils.equals(indexCode,info.getParentIndexCode())){ if(StringUtils.isBlank(info.getChannelNo())){ continue; } if(StringUtils.isNotBlank(str)){ str += ","; names += ","; } str+=info.getChannelNo(); names+=info.getName(); } } } model.setChannelNo(str); model.setChannelInfo(names); return str; } private Device getExistedDoor(DoorsInfoResponse device, List allList) { if(allList.size()>0){ for(Device r : allList){ if(StringUtils.equals(r.getDoorId(), device.getIndexCode())){ //表示未删除 return r; } } } return null; } private Device getExistedDevice(AcsDeviceInfoResponse device, List allList) { if(allList.size()>0){ for(Device r : allList){ if(StringUtils.equals(r.getHkId(), device.getIndexCode())){ //表示未删除 return r; } } } return null; } private void getDeleteList(List allList, List allHkList,List deleteList ,Date date) { if(allList!=null && allList.size()>0){ for(Device device : allList){ if(isDeletedDevice(device,allHkList)){ device.setIsdeleted(Constants.ONE); device.setEditDate(date); deleteList.add(device); } } } } private void getDeleteDoorList(List allList, List allHkList,List deleteList ,Date date) { if(allList!=null && allList.size()>0){ for(Device device : allList){ if(isDeletedDoorDevice(device,allHkList)){ device.setIsdeleted(Constants.ONE); device.setEditDate(date); deleteList.add(device); } } } } private boolean isDeletedDoorDevice(Device device, List allHkList) { if(allHkList.size()>0){ for(DoorsInfoResponse r : allHkList){ if(StringUtils.equals(device.getDoorId(), r.getIndexCode())){ //表示未删除 return false; } } } return true; } private boolean isDeletedDevice(Device device, List allHkList) { if(allHkList.size()>0){ for(AcsDeviceInfoResponse r : allHkList){ if(StringUtils.equals(device.getHkId(), r.getIndexCode())){ //表示未删除 return false; } } } return true; } }