资产所在业务空间 ## 前置条件 1. 资产有绑定的设备或部件 2. 绑定的设备或部件有所在业务空间关系 ## 依赖函数 Eq2Sp ## 计算流程 根据 资产 --> 设备/部件 --> 业务空间 的间接关系, 来获取资产所在业务空间的关系 ## 函数 ``` -- 资产所在业务空间 create or replace function public.rel_pe2sp(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_pe_in_sp_zone_air_conditioning', 'r_pe_in_sp_zone_clean', 'r_pe_in_sp_zone_domestic_water_supply', 'r_pe_in_sp_zone_fire', 'r_pe_in_sp_zone_function', 'r_pe_in_sp_zone_general', 'r_pe_in_sp_zone_heating', 'r_pe_in_sp_zone_lighting', 'r_pe_in_sp_zone_network', 'r_pe_in_sp_zone_power_supply', 'r_pe_in_sp_zone_security', 'r_pe_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("insert into {1}(equip_id, space_id, project_id, sign) select pe.id, rel.space_id, pe.project_id, 2 from property pe inner join {0} rel on pe.equip_id = rel.equip_id where pe.project_id = $1 and rel.project_id = $1".format(input_tables[i], output_tables[i]), ["text"]) rel = join_plan.execute([project_id]) except Exception as e: plpy.warning(e) return False else: return True $$ LANGUAGE 'plpython3u' VOLATILE; select public.rel_pe2sp('Pj1101010015'); ``` ## 入参 1. 项目id ## 例子 select public.rel_pe2sp('Pj1101010015');