系统所在业务空间
1. 系统下有设备
2. 系统下的设备有所在业务空间关系
Eq2Sp
1. 根据 系统 --> 设备 --> 业务空间 的间接关系, 获取系统和业务空间的关系
2. 计算结果存储在r_sy_in_sp_*中
-- 系统所在楼层
create or replace function public.rel_sy2sp(project_id character varying) returns boolean
as
$$
try:
input_tables = ['r_eq_in_sp_zone_air_conditioning', 'r_eq_in_sp_zone_clean', 'r_eq_in_sp_zone_domestic_water_supply', 'r_eq_in_sp_zone_fire', 'r_eq_in_sp_zone_function',
'r_eq_in_sp_zone_general', 'r_eq_in_sp_zone_heating', 'r_eq_in_sp_zone_lighting', 'r_eq_in_sp_zone_network', 'r_eq_in_sp_zone_power_supply', 'r_eq_in_sp_zone_security', 'r_eq_in_sp_zone_tenant']
output_tables = ['r_sy_in_sp_zone_air_conditioning', 'r_sy_in_sp_zone_clean', 'r_sy_in_sp_zone_domestic_water_supply',
'r_sy_in_sp_zone_fire', 'r_sy_in_sp_zone_function', 'r_sy_in_sp_zone_general', 'r_sy_in_sp_zone_heating', 'r_sy_in_sp_zone_lighting',
'r_sy_in_sp_zone_network', 'r_sy_in_sp_zone_power_supply', 'r_sy_in_sp_zone_security', 'r_sy_in_sp_zone_tenant']
# 将下面对数据库的操作作为一个事务, 出异常则自动rollback
with plpy.subtransaction():
for i in range(len(output_tables)):
delete_plan = plpy.prepare("delete from {0} where project_id = $1 and sign = 2".format(output_tables[i]), ["text"])
delete_plan.execute([project_id])
join_plan = plpy.prepare("select sy.sys_id, rel.space_id from r_sy_eq sy inner join {0} rel on sy.equip_id = rel.equip_id where rel.project_id = $1 and sy.project_id = $1".format(input_tables[i]), ["text"])
rel = join_plan.execute([project_id])
sy2sp = dict()
for row in rel:
sys = row['sys_id']
space = row['space_id']
if sys not in sy2sp:
sy2sp[sys] = set()
space_set = sy2sp[sys]
space_set.add(space)
for sid, space_set in sy2sp.items():
for space in space_set:
plan = plpy.prepare("insert into {0}(sys_id, space_id, project_id, sign) values($1, $2, $3, 2)".format(output_tables[i]), ["text", "text", "text"])
plan.execute([sid, space, project_id])
except Exception as e:
plpy.warning(e)
return False
else:
return True
$$
LANGUAGE 'plpython3u' VOLATILE;
1. 项目id
select public.rel_sy2sp('Pj1101010015');