kangda-robot-backend/ruoyi-fastapi-backend/module_admin/dao/identification_statistics_dao.py

201 lines
7.5 KiB
Python

from datetime import datetime, time
from sqlalchemy import delete, select, update, func
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.identification_statistics_do import IdentificationStatistics
from module_admin.entity.vo.identification_statistics_vo import Identification_statisticsModel, Identification_statisticsPageQueryModel
from utils.page_util import PageUtil
class Identification_statisticsDao:
"""
识别统计模块数据库操作层
"""
@classmethod
async def get_today_identification_statistics(cls, query_db: AsyncSession):
stmt = (
select(IdentificationStatistics.employ, IdentificationStatistics.stranger, IdentificationStatistics.visitor, IdentificationStatistics.create_time)
.where(IdentificationStatistics.create_time == datetime.now().date())
)
result = await query_db.execute(stmt)
return result.mappings().all()
@classmethod
async def get_total_identification_statistics(cls, query_db: AsyncSession):
"""
获取总的识别统计数据 - 返回各个字段的总和
:param query_db: orm对象
:return: 总和统计数据
"""
stmt = select(
func.sum(IdentificationStatistics.employ).label('employ'),
func.sum(IdentificationStatistics.visitor).label('visitor'),
func.sum(IdentificationStatistics.stranger).label('stranger')
)
result = await query_db.execute(stmt)
return result.mappings().first()
@classmethod
async def get_identification_statistics_detail_by_id(cls, db: AsyncSession, id: int):
"""
根据主键 自增获取识别统计详细信息
:param db: orm对象
:param id: 主键 自增
:return: 识别统计信息对象
"""
identification_statistics_info = (
(
await db.execute(
select(IdentificationStatistics)
.where(
IdentificationStatistics.id == id
)
)
)
.scalars()
.first()
)
return identification_statistics_info
@classmethod
async def get_identification_statistics_detail_by_info(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
"""
根据识别统计参数获取识别统计信息
:param db: orm对象
:param identification_statistics: 识别统计参数对象
:return: 识别统计信息对象
"""
identification_statistics_info = (
(
await db.execute(
select(IdentificationStatistics).where(
)
)
)
.scalars()
.first()
)
return identification_statistics_info
@classmethod
async def get_identification_statistics_list(cls, db: AsyncSession, query_object: Identification_statisticsPageQueryModel, is_page: bool = False):
"""
根据查询参数获取识别统计列表信息
:param db: orm对象
:param query_object: 查询参数对象
:param is_page: 是否开启分页
:return: 识别统计列表信息对象
"""
query = (
select(IdentificationStatistics)
.where(
IdentificationStatistics.create_time.between(
datetime.combine(datetime.strptime(query_object.begin_create_time, '%Y-%m-%d'), time(00, 00, 00)),
datetime.combine(datetime.strptime(query_object.end_create_time, '%Y-%m-%d'), time(23, 59, 59)),
)
if query_object.begin_create_time and query_object.end_create_time
else True,
)
.order_by(IdentificationStatistics.id)
.distinct()
)
identification_statistics_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
return identification_statistics_list
@classmethod
async def add_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
"""
新增识别统计数据库操作
:param db: orm对象
:param identification_statistics: 识别统计对象
:return:
"""
db_identification_statistics = IdentificationStatistics(**identification_statistics.model_dump(exclude={}))
db.add(db_identification_statistics)
await db.flush()
return db_identification_statistics
@classmethod
async def edit_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: dict):
"""
编辑识别统计数据库操作
:param db: orm对象
:param identification_statistics: 需要更新的识别统计字典
:return:
"""
await db.execute(update(IdentificationStatistics), [identification_statistics])
@classmethod
async def delete_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
"""
删除识别统计数据库操作
:param db: orm对象
:param identification_statistics: 识别统计对象
:return:
"""
await db.execute(delete(IdentificationStatistics).where(IdentificationStatistics.id.in_([identification_statistics.id])))
@classmethod
async def update_identification_statistics_dao(cls, db: AsyncSession, person_status: str):
"""
更新今日识别统计数据库操作 - 根据person_status增加相应字段的计数
:param db: orm对象
:param person_status: 人员状态 (0 陌生人, 1 员工, 2 访客)
:return:
"""
today = datetime.now().date()
# 查询今日是否已有统计记录
stmt = select(IdentificationStatistics).where(
func.date(IdentificationStatistics.create_time) == today
)
result = await db.execute(stmt)
today_statistics = result.scalars().first()
# 如果没有今日记录,创建新记录
if not today_statistics:
new_statistics = IdentificationStatistics(
employ=1 if person_status == "1" else 0,
visitor=1 if person_status == "2" else 0,
stranger=1 if person_status == "0" else 0,
create_time=datetime.now(),
create_by="system"
)
db.add(new_statistics)
await db.flush()
else:
# 根据person_status更新相应字段
if person_status == "0": # 陌生人
await db.execute(
update(IdentificationStatistics)
.where(IdentificationStatistics.id == today_statistics.id)
.values(stranger=IdentificationStatistics.stranger + 1)
)
elif person_status == "1": # 员工
await db.execute(
update(IdentificationStatistics)
.where(IdentificationStatistics.id == today_statistics.id)
.values(employ=IdentificationStatistics.employ + 1)
)
elif person_status == "2": # 访客
await db.execute(
update(IdentificationStatistics)
.where(IdentificationStatistics.id == today_statistics.id)
.values(visitor=IdentificationStatistics.visitor + 1)
)