1. 系统下有设备
2. 系统下的设备有所在业务空间关系
Eq2Sp
1. 根据 系统 --> 设备 --> 业务空间 的间接关系, 获取系统和业务空间的关系
2. 计算结果存储在r_sy_in_sp中 (先删除以前的自动计算结果)
源码
-- 系统所在楼层
create or replace function public.rel_sy2sp(project_id character varying) returns boolean
as
$$
CREATE OR REPLACE FUNCTION "public"."rel_sy2sp"("project_id" varchar)
RETURNS "pg_catalog"."bool" AS $BODY$
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']
input_tables = ['relationship.r_eq2sp']
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']
output_tables = ['relationship.r_sy2sp']
# 将下面对数据库的操作作为一个事务, 出异常则自动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 and type = 'sy2sp'".format(output_tables[i]), ["text"])
delete_plan.execute([project_id])
join_plan = plpy.prepare("select sy.sys_id, rel.sp_id space_id, zone_type from r_sy_eq sy inner join {0} rel on sy.equip_id = rel.eq_id where rel.project_id = $1 and sy.project_id = $1 and rel.type = 'eq2sp_in'".format(input_tables[i]), ["text"])
rel = join_plan.execute([project_id])
sy2sp = dict()
zone_type_map = dict()
for row in rel:
sys = row['sys_id']
space = row['space_id']
space_zone = row['zone_type']
zone_type_map[space] = space_zone
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}(sy_id, sp_id, project_id, type, zone_type, sign) values($1, $2, $3, 'sy2sp', '{1}', 2)".format(output_tables[i], zone_type_map[space]), ["text", "text", "text"])
plan.execute([sid, space, project_id])
except Exception as e:
plpy.warning(e)
return False
else:
return True
$BODY$
LANGUAGE plpython3u VOLATILE
COST 100
1. 项目id
select public.rel_sy2sp('Pj1101010015');