jiangping
2023-12-20 4b139bcb2b3c6bf1082aca29913caf5166ec8167
server/dmvisit_service/src/main/java/com/doumee/service/business/impl/hksync/HkSyncDeviceServiceImpl.java
@@ -7,10 +7,9 @@
import com.doumee.core.haikang.model.HKConstants;
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.DoorsListRequest;
import com.doumee.core.haikang.model.param.request.ParkListRequest;
import com.doumee.core.haikang.model.param.respose.AcsDeviceInfoResponse;
import com.doumee.core.haikang.model.param.respose.AcsDeviceListResponse;
import com.doumee.core.haikang.model.param.respose.ParkListResponse;
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;
@@ -45,6 +44,11 @@
    @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<Device> deleteList = new ArrayList<>();
        List<Device> addList = new ArrayList<>();
        List<Device> editList = new ArrayList<>();
@@ -95,20 +99,58 @@
            }
        }
        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;
        }
    }
    public  List<DoorsInfoResponse>  getAllDoorList(){
        List<DoorsInfoResponse> 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<DoorsListResponse> 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;
    }
    private void getDataChangeList(List<Device> allList, List<AcsDeviceInfoResponse> allHkList, List<Device> addList, List<Device> editList,List<Device> deleteList, Date date) {
        if(allHkList!=null && allHkList.size()>0){
            //获取海康全部门禁组数据
            List<DoorsInfoResponse> hkDoorList = getAllDoorList();
            for(AcsDeviceInfoResponse device : allHkList){
                Device model = getExistedDevice(device,allList);
                if(model !=null){
                    //如果已存在,则更新数据
                    model =  initDataByHkData(model,device,date);
                    model =  initDataByHkData(model,device,date,hkDoorList);
                    editList.add(model);
                }else{
                    //如果不存在,则新增数据
                    model = new Device();
                    model =  initDataByHkData(model,device,date);
                    model =  initDataByHkData(model,device,date,hkDoorList);
                    addList.add(model);
                }
            }
@@ -124,7 +166,7 @@
     * @param date
     * @return
     */
    private Device initDataByHkData(Device model, AcsDeviceInfoResponse device,Date date) {
    private Device initDataByHkData(Device model, AcsDeviceInfoResponse device,Date date, List<DoorsInfoResponse> hkDoorList) {
        model.setIsdeleted(Constants.ZERO);
        model.setIp(device.getIp());
        model.setHkDate(date);
@@ -137,7 +179,7 @@
        model.setManufature(device.getManufacturer());
        model.setType(Constants.ZERO);
//        model.setNo(device.getDevSerialNum());
//        model.setChannelNo(device);
        model.setChannelNo(getChannelNo(device.getIndexCode(),hkDoorList));
        model.setResourceType(device.getResourceType());
        model.setDevTypeCode(device.getDevTypeCode());
        model.setManufature(device.getManufacturer());
@@ -146,6 +188,24 @@
        return  model;
    }
    private String getChannelNo(String indexCode, List<DoorsInfoResponse> hkDoorList) {
        String str = "";
        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 += ",";
                    }
                    str+=info.getChannelNo();
                }
            }
        }
        return str;
    }
    private Device getExistedDevice(AcsDeviceInfoResponse device, List<Device> allList) {
        if(allList.size()>0){
            for(Device r : allList){