系统所在建筑
1. 系统下有设备
2. 系统下的设备有所属建筑的关系
Eq2Bd
1. 通过 系统 --> 设备 --> 建筑 的间接关系, 获取系统所在建筑的关系
2. 结果存储在 r_sy_in_bd 表中
-- 系统所在建筑
create or replace function public.rel_sy2bd(project_id character varying) returns boolean
as
$$
try:
# 将下面对数据库的操作作为一个事务, 出异常则自动rollback
with plpy.subtransaction():
delete_plan = plpy.prepare("delete from r_sy_in_bd where project_id = $1 and sign = 2", ["text"])
delete_plan.execute([project_id])
join_plan = plpy.prepare("select sy.sys_id, eq.building_id from r_sy_eq sy inner join equipment eq on sy.equip_id = eq.id where eq.project_id = $1 and sy.project_id = $1 and eq.building_id is not null", ["text"])
rel = join_plan.execute([project_id])
sy2bd = dict()
for row in rel:
sys = row['sys_id']
building = row['building_id']
if sys not in sy2bd:
sy2bd[sys] = set()
sys_set = sy2bd[sys]
sys_set.add(building)
for sid, sys_set in sy2bd.items():
for building in sys_set:
plan = plpy.prepare("insert into r_sy_in_bd(sys_id, building_id, project_id, sign) values($1, $2, $3, 2)", ["text", "text", "text"])
plan.execute([sid, building, project_id])
except Exception as e:
plpy.warning(e)
return False
else:
return True
$$
LANGUAGE 'plpython3u' VOLATILE;
1. 项目id
select public.rel_sy2bd('Pj1101010015');