k94314517
2023-12-26 c1e829fcf97594aaa30ffb531f213b703e0eb21f
server/dmvisit_service/src/main/java/com/doumee/service/business/impl/hksync/HkSyncPrivilegeServiceImpl.java
@@ -5,12 +5,16 @@
import com.doumee.core.exception.BusinessException;
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.PrivilegeGroupRequest;
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.PrivilegeGroupInfoResponse;
import com.doumee.core.haikang.model.param.respose.PrivilegeGroupListResponse;
import com.doumee.core.haikang.service.HKService;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.DeviceRoleMapper;
import com.doumee.dao.business.model.Device;
import com.doumee.dao.business.model.DeviceRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,59 +42,71 @@
    @Override
//    @Async
    public String syncPrivilege(PrivilegeGroupRequest param){
        List<DeviceRole> deleteList = new ArrayList<>();
        List<DeviceRole> addList = new ArrayList<>();
        List<DeviceRole> editList = new ArrayList<>();
        List<PrivilegeGroupInfoResponse> allHkList = new ArrayList<>();
        Date date = new Date();
        //查询全部现有数据,只管理同步 访客组
        List<Integer> types = new ArrayList<>();
        types.add(Constants.DOOR_ROLE_TYPE.fk);
        types.add(Constants.DOOR_ROLE_TYPE.lw);
        List<DeviceRole> allList = DeviceRoleMapper.selectList(new QueryWrapper<DeviceRole>().lambda().in(DeviceRole::getType,types));
        boolean hasNext = true;
        int curTotal = 0;
        int curPage = 1;
        while (hasNext){
            //分页遍历循环查询所有门禁设备数据
            param = new PrivilegeGroupRequest();
            param.setPageNo(curPage);
            param.setPageSize(10000);
            BaseResponse<PrivilegeGroupListResponse> response = HKService.privilegeGroup(param);
            if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
            }
            PrivilegeGroupListResponse r = response.getData();
            curTotal += 10000;
            if(curTotal >= r.getTotal()){
                hasNext = false;
            }
            if(r.getList() == null || r.getList().size()==0){
                hasNext =false;
            }else{
                allHkList.addAll(r.getList());
            }
            curPage ++;
        if(Constants.DEALING_HK_SYNCDEVICE){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ;
        }
        /**
         * 获取增删改数据集合
         */
        getDataChangeList(allList,allHkList,addList,editList,deleteList,date);
        if(deleteList.size()>0){
            //逻辑删除
            for(DeviceRole d : deleteList){
                DeviceRoleMapper.updateById(d);
        Constants.DEALING_HK_SYNCDEVICE =true;
        try {
            List<DeviceRole> deleteList = new ArrayList<>();
            List<DeviceRole> addList = new ArrayList<>();
            List<DeviceRole> editList = new ArrayList<>();
            List<PrivilegeGroupInfoResponse> allHkList = new ArrayList<>();
            Date date = new Date();
            //查询全部现有数据,只管理同步 访客组
            List<Integer> types = new ArrayList<>();
            types.add(Constants.DOOR_ROLE_TYPE.fk);
            types.add(Constants.DOOR_ROLE_TYPE.lw);
            List<DeviceRole> allList = DeviceRoleMapper.selectList(new QueryWrapper<DeviceRole>().lambda().in(DeviceRole::getType,types));
            boolean hasNext = true;
            int curTotal = 0;
            int curPage = 1;
            while (hasNext){
                //分页遍历循环查询所有门禁设备数据
                param = new PrivilegeGroupRequest();
                param.setPageNo(curPage);
                param.setPageSize(100);
                BaseResponse<PrivilegeGroupListResponse> response = HKService.privilegeGroup(param);
                if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                    throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
                }
                PrivilegeGroupListResponse 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 ++;
            }
        }
        if(addList.size()>0){
            DeviceRoleMapper.insertBatchSomeColumn(addList);
        }
        if(editList.size()>0){
            for(DeviceRole d : editList){
                DeviceRoleMapper.updateById(d);
            /**
             * 获取增删改数据集合
             */
            getDataChangeList(allList,allHkList,addList,editList,deleteList,date);
            if(deleteList.size()>0){
                //逻辑删除
                for(DeviceRole d : deleteList){
                    DeviceRoleMapper.updateById(d);
                }
            }
            if(addList.size()>0){
                DeviceRoleMapper.insertBatchSomeColumn(addList);
            }
            if(editList.size()>0){
                for(DeviceRole d : editList){
                    DeviceRoleMapper.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;
        }
        return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条";
    }
    private void getDataChangeList(List<DeviceRole> allList, List<PrivilegeGroupInfoResponse> allHkList, List<DeviceRole> addList, List<DeviceRole> editList,List<DeviceRole> deleteList, Date date) {
@@ -127,7 +143,7 @@
        model.setHkStatus(Constants.ONE);
        model.setIsdeleted(Constants.ONE);
        model.setIsDefault(device.getIsDefault());
        model.setType(Constants.ZERO);
        model.setType(String.valueOf(Constants.ZERO));
        return  model;
    }