2024-10-24 09:23:39 +08:00

96 lines
3.2 KiB
Python

# -*- coding:utf-8 -*-
"""
@Author : xuxingchen
@Contact : xuxingchen@sinochem.com
@Desc : 子系统映射表 增&改&查
"""
import csv
import os
from models.houses import HousesTable
from utils.database import BaseTable
from utils.misc import now_datetime_second, sql_export_xls
class SpacesTable(BaseTable):
@staticmethod
def get_sub_system_name_by_room_id(table_handler: BaseTable, room_id: str):
table_handler.query(
"""
SELECT sub_space_name
FROM subsystem
WHERE room_id = ?
""", (room_id,)
)
res = table_handler.cursor.fetchall()
if res:
return res[0][0]
else:
return None
@staticmethod
def get_space_info_by_sub_space_name(table_handler: BaseTable, sub_space_name: str):
"""根据第三方ID获取对应的房产信息"""
table_handler.query(
"""
SELECT subsystem.room_id, house_name, project_id, sub_system_id
FROM subsystem
LEFT JOIN houses ON subsystem.room_id = houses.room_id
WHERE sub_space_name = ?
""", (sub_space_name,)
)
res = table_handler.cursor.fetchall()
if res:
return {
"room_id": res[0][0],
"house_name": res[0][1],
"project_id": res[0][2],
"sub_system_id": res[0][3]
}
else:
return None
@staticmethod
def get_aiot_platform_data(table_handler: BaseTable):
query = """
SELECT project_id, houses.room_id, house_name, COALESCE(subsystem_id, ''), COALESCE(sub_space_name, '')
FROM houses
LEFT JOIN subsystem ON subsystem.room_id = houses.room_id
"""
file_path = os.path.join(f"data/AIOT平台空间数据{now_datetime_second().replace(':', '_')}.xls")
sql_export_xls(query, table_handler.connection, file_path,
"房屋子系统映射数据表",
["项目id", "房屋id", "房屋名称", "子系统id", "子系统空间名称"])
return file_path
@staticmethod
def get_sub_system_data(table_handler: BaseTable):
query = "SELECT subsystem_id, sub_space_name FROM subsystem"
file_path = os.path.join(f"data/子系统空间数据{now_datetime_second().replace(':', '_')}.xls")
sql_export_xls(query, table_handler.connection, file_path,
"子系统数据表",
["子系统id", "子系统空间名称"])
return file_path
@staticmethod
def update(table_handler: BaseTable, objs: list):
data = []
for obj in objs:
data.append((obj[0], now_datetime_second(), obj[1], obj[2]))
table_handler.executemany(
f"""
UPDATE subsystem
SET room_id = ?, update_datetime = ?
WHERE subsystem_id = ? and sub_space_name = ?
""", data
)
@staticmethod
def exits_room_id(table_handler: BaseTable, room_id: str):
table_handler.query("SELECT room_id FROM spaces WHERE room_id = ?", (room_id,))
if table_handler.cursor.fetchone() is not None:
return True
else:
return False