| package com.doumee.service.business.impl.hksync; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| 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.BaseResponse; | 
| 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.PrivilegeGroupInfoResponse; | 
| import com.doumee.core.haikang.model.param.respose.PrivilegeGroupListResponse; | 
| import com.doumee.core.utils.Constants; | 
| import com.doumee.core.utils.DateUtil; | 
| 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; | 
| 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 HkSyncPrivilegeServiceImpl extends HkSyncBaseServiceImpl { | 
|   | 
|     @Autowired | 
|     private DeviceRoleMapper DeviceRoleMapper; | 
|     /** | 
|      * 同步海康访客组权限数据 | 
|      * @param param | 
|      * @return | 
|      */ | 
|     @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 ++; | 
|         } | 
|         /** | 
|          * 获取增删改数据集合 | 
|          */ | 
|         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()+"】条"; | 
|     } | 
|   | 
|     private void getDataChangeList(List<DeviceRole> allList, List<PrivilegeGroupInfoResponse> allHkList, List<DeviceRole> addList, List<DeviceRole> editList,List<DeviceRole> deleteList, Date date) { | 
|         if(allHkList!=null && allHkList.size()>0){ | 
|             for(PrivilegeGroupInfoResponse device : allHkList){ | 
|                 DeviceRole model = getExistedData(device,allList); | 
|                 if(model !=null){ | 
|                     //如果已存在,则更新数据 | 
|                     model =  initDataByHkData(model,device,date); | 
|                     editList.add(model); | 
|                 }else{ | 
|                     //如果不存在,则新增数据 | 
|                     model = new DeviceRole(); | 
|                     model =  initDataByHkData(model,device,date); | 
|                     addList.add(model); | 
|                 } | 
|             } | 
|         } | 
|         //判断获取删除的门禁设备,逻辑删除 | 
|         getDeleteList(allList,allHkList,deleteList,date); | 
|     } | 
|   | 
|     /** | 
|      * 初始化海康入库数据 | 
|      * @param model | 
|      * @param device | 
|      * @param date | 
|      * @return | 
|      */ | 
|     private DeviceRole initDataByHkData(DeviceRole model, PrivilegeGroupInfoResponse device,Date date) { | 
|         model.setIsdeleted(Constants.ZERO); | 
|         model.setHkId(device.getPrivilegeGroupId()); | 
|         model.setName(device.getPrivilegeGroupName()); | 
|         model.setHkStatus(Constants.ONE); | 
|         model.setIsdeleted(Constants.ONE); | 
|         model.setIsDefault(device.getIsDefault()); | 
|         model.setType(Constants.ZERO); | 
|         return  model; | 
|     } | 
|   | 
|     private DeviceRole getExistedData(PrivilegeGroupInfoResponse device, List<DeviceRole> allList) { | 
|         if(allList.size()>0){ | 
|             for(DeviceRole r : allList){ | 
|                 if(StringUtils.equals(r.getHkId(), device.getPrivilegeGroupId())){ | 
|                     //表示未删除 | 
|                     return  r; | 
|                 } | 
|             } | 
|         } | 
|         return  null; | 
|     } | 
|   | 
|     private void getDeleteList(List<DeviceRole> allList, List<PrivilegeGroupInfoResponse> allHkList,List<DeviceRole> deleteList ,Date date) { | 
|         if(allList!=null && allList.size()>0){ | 
|             for(DeviceRole device : allList){ | 
|                 if(isDeletedData(device,allHkList)){ | 
|                     device.setIsdeleted(Constants.ONE); | 
|                     device.setEditDate(date); | 
|                     deleteList.add(device); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     private boolean isDeletedData(DeviceRole device, List<PrivilegeGroupInfoResponse> allHkList) { | 
|         if(allHkList.size()>0){ | 
|             for(PrivilegeGroupInfoResponse r : allHkList){ | 
|                 if(StringUtils.equals(device.getHkId(), r.getPrivilegeGroupId())){ | 
|                     //表示未删除 | 
|                     return  false; | 
|                 } | 
|             } | 
|         } | 
|         return  true; | 
|   | 
|     } | 
|      | 
| } |