def get_adj_dic(orgcode): gantry_adj = pd.read_csv(ws + f'/data/{orgcode}_gantry_hard_lon_lat.csv') gantry_adj = gantry_adj[['o_name', 'd_name']].values.tolist() adj_dic = {} for row in gantry_adj: if row[0] in adj_dic.keys(): adj_dic[row[0]].append(row[1]) else: adj_dic[row[0]] = [row[1]] return adj_dic
时间: 2024-04-06 14:28:58 浏览: 14
这段代码实现了一个函数get_adj_dic,其输入参数是orgcode,输出结果是一个邻接表形式的字典adj_dic。具体操作是:首先读取一个名为orgcode_gantry_hard_lon_lat.csv的csv文件,该文件有两列,分别为o_name和d_name,表示起点和终点的名称;然后将o_name和d_name转换为一个列表,即gantry_adj;接着遍历gantry_adj列表,将其中的元素按照起点进行分组,得到邻接表adj_dic,其中每个起点对应的终点为一个列表。最后返回adj_dic。
相关问题
//获取用户id String userId = UserAgent.get().getUserId(); //根据id拿到用户组织 Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid(); String orgCode = userMapByUid.get(userId).getOrgCode(); //所有的行权限 列权限 List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); List<SchemaRowRule> userRowRuleList = new ArrayList<>(); DataPermissionSchema build = DataPermissionSchema.builder().build(); //设置行权限 根据用户id和用户组织去获取 for (SchemaRowRuleEo schemaRowRuleEo : schemaRowRuleList) { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); if(userlist.contains(userId)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } } } build.setSchemaRowRuleList(userRowRuleList);代码优化及原因
可以将循环中的if语句提取出来,避免重复代码。同时,使用stream流简化代码,并提高代码可读性。代码优化后如下:
```
String userId = UserAgent.get().getUserId();
Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid();
String orgCode = userMapByUid.get(userId).getOrgCode();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userId) || orgList.contains(orgCode);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = SchemaRowRule.builder().build();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
DataPermissionSchema build = DataPermissionSchema.builder().build();
build.setSchemaRowRuleList(userRowRuleList);
```
优化后的代码更加简洁易读,使用stream流可以避免重复代码,同时也可以提高代码的执行效率。
@Query("SELECT new com.cmict.iot.server.dao.model.sql.DeviceTransferInfoEntity(dts,dp.deptName,d.deviceName,d.name,a.booleanValue) " + "FROM DeviceTransferEntity dts " + "LEFT JOIN DeptEntity dp on dp.orgCode = dts.newOrgCode " + "LEFT JOIN DeviceEntity d on d.id = dts.deviceId " + "LEFT JOIN AttributeKvEntity a on a.id.entityId = d.id AND a.id.entityType = 'DEVICE' AND a.id.attributeType = 'SERVER_SCOPE' AND a.id.attributeKey = 'active' " + "WHERE dts.tenantId= :tenantId and dts.oldOrgCode= :orgCode ") Page<DeviceTransferInfoEntity> getDeviceTransferInfoByTenantIdAndOrgCode(@Param("tenantId") UUID tenantId, @Param("orgCode") String orgCode, Pageable pageable); 帮我修改sql,目前deviceName,deviceProfileName,active值为null
可以尝试修改以下的 SQL 查询语句,使用 `LEFT JOIN` 连接 DeviceProfileEntity 表,并将 `AttributeKvEntity` 表的连接方式修改为 `LEFT JOIN`,同时在查询结果中将 `DeviceProfileEntity` 表和 `AttributeKvEntity` 表的相关字段设置为 `COALESCE` 函数,以防止出现 `NULL` 值:
```
SELECT new com.cmict.iot.server.dao.model.sql.DeviceTransferInfoEntity(dts,dp.deptName,COALESCE(d.deviceName, ''),COALESCE(dp.name, ''), COALESCE(a.booleanValue, false))
FROM DeviceTransferEntity dts
LEFT JOIN DeptEntity dp on dp.orgCode = dts.newOrgCode
LEFT JOIN DeviceEntity d on d.id = dts.deviceId
LEFT JOIN DeviceProfileEntity dpf on dpf.id = d.deviceProfileId
LEFT JOIN AttributeKvEntity a on a.id.entityId = d.id AND a.id.entityType = 'DEVICE' AND a.id.attributeType = 'SERVER_SCOPE' AND a.id.attributeKey = 'active'
LEFT JOIN AttributeKvEntity ap on ap.id.entityId = dpf.id AND ap.id.entityType = 'DEVICE_PROFILE' AND ap.id.attributeType = 'SERVER_SCOPE' AND ap.id.attributeKey = 'active'
WHERE dts.tenantId = :tenantId and dts.oldOrgCode = :orgCode
```
这样可以在查询结果中将 `deviceName` 和 `deviceProfileName` 的 `NULL` 值替换为 `''` 空字符串,将 `active` 的 `NULL` 值替换为 `false` 值。