You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.2 KiB
Python

from website.handler import BaseHandler
from sqlalchemy import text
from typing import Any
from website.db_mysql import get_session, get_async_session, to_json
import json
# 获取企业模型数量
def get_enterprise_model_count(id: int) -> int:
return 0
# 获取企业设备数量
def get_enterprise_device_count(id: int) -> int:
return 0
# 获取所有企业实体数量
def get_enterprise_entity_count(engine: Any) -> int:
with engine.connect() as conn:
count_sql_text = "select count(id) from enterprise "
count = conn.execute(text(count_sql_text)).fetchone()
if count:
return count[0]
return 0
# 获取所有企业模型数量
def get_enterprise_model_count(entity_id: int = 0, entity_suid: str = "") -> int:
with get_session() as session:
sql = "select base_models from enterprise_busi_model where "
param = {}
if entity_id:
sql += "entity_id = :entity_id"
param["entity_id"] = entity_id
elif entity_suid:
sql += "entity_suid = :entity_suid"
param["entity_suid"] = entity_suid
cur = session.execute(text(sql), param)
res = to_json(cur)
if res:
base_model_list = json.loads(res)
return len(base_model_list)
return 0
# 获取所有企业设备数量
def get_enterprise_device_count(entity_id: int = 0, entity_suid: str = "") -> int:
with get_session() as session:
sql = "select count(id) as device_count from enterprise_device "
param = {}
if entity_id:
sql += "where entity_id = :entity_id"
param = {"entity_id": entity_id}
elif entity_suid:
sql += "where entity_suid = :entity_suid"
param = {"entity_suid": entity_suid}
cur = session.execute(text(sql), param)
res = to_json(cur)
if res:
return res["device_count"]
return 0
async def get_enterprise_model_and_device_count(
entity_id: int = 0, entity_suid: str = ""
) -> (int, int):
model_count = 0
device_count = 0
async with get_async_session() as session:
# sql_model = "select base_models from enterprise_busi_model where "
# param_model = {}
# if entity_id:
# sql_model += "entity_id = :entity_id"
# param_model["entity_id"] = entity_id
# elif entity_suid:
# sql_model += "entity_suid = :entity_suid"
# param_model["entity_suid"] = entity_suid
# cur_model = await session.execute(text(sql_model), param_model)
# res_model = to_json(cur_model)
# if res_model:
# base_model_list = json.loads(res_model)
# model_count = len(base_model_list)
# sql_device = "select count(id) as device_count from enterprise_device where "
# param_device = {}
# if entity_id:
# sql_device += "entity_id = :entity_id"
# param_device["entity_id"] = entity_id
# elif entity_suid:
# sql_device += "where entity_suid = :entity_suid"
# param_device = {"entity_suid": entity_suid}
# cur_device = await session.execute(text(sql_device), param_device)
# res_device = to_json(cur_device)
# if res_device:
# device_count = res_device["device_count"]
sql = """
SELECT
(SELECT base_models FROM enterprise_busi_model WHERE {where_clause}) as base_models,
(SELECT COUNT(*) FROM enterprise_device WHERE {where_clause}) AS device_count
"""
where_clause = ""
params = {}
if entity_id:
where_clause = "entity_id = :entity_id"
params["entity_id"] = entity_id
elif entity_suid:
where_clause = "entity_suid = :entity_suid"
params["entity_suid"] = entity_suid
sql = sql.format(where_clause=where_clause)
result = await session.execute(text(sql), params)
# row = result.fetchone()
row = to_json(result)
base_models, device_count = row["base_models"], row["device_count"]
if base_models:
model_count = len(json.loads(base_models))
return model_count, device_count