from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.identification_record_service import Identification_recordService
from module_admin.entity.vo.identification_record_vo import DeleteIdentification_recordModel, Identification_recordModel, Identification_recordPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


identification_recordController = APIRouter(prefix='/system/identification_record', dependencies=[Depends(LoginService.get_current_user)])


@identification_recordController.get(
    '/list', response_model=PageResponseModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:list'))]
)
async def get_system_identification_record_list(
    request: Request,
identification_record_page_query: Identification_recordPageQueryModel = Depends(Identification_recordPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    identification_record_page_query_result = await Identification_recordService.get_identification_record_list_services(query_db, identification_record_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(data=identification_record_page_query_result)


@identification_recordController.post('', dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:add'))])
@ValidateFields(validate_model='add_identification_record')
@Log(title='识别记录', business_type=BusinessType.INSERT)
async def add_system_identification_record(
    request: Request,
    add_identification_record: Identification_recordModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_identification_record.create_time = datetime.now()
    add_identification_record.create_by = current_user.user.user_name
    add_identification_record_result = await Identification_recordService.add_identification_record_services(query_db, add_identification_record)
    logger.info(add_identification_record_result.message)

    return ResponseUtil.success(msg=add_identification_record_result.message)


# @identification_recordController.put('', dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:edit'))])
# @ValidateFields(validate_model='edit_identification_record')
# @Log(title='识别记录', business_type=BusinessType.UPDATE)
# async def edit_system_identification_record(
#     request: Request,
#     edit_identification_record: Identification_recordModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     edit_identification_record.update_by = current_user.user.user_name
#     edit_identification_record.update_time = datetime.now()
#     edit_identification_record_result = await Identification_recordService.edit_identification_record_services(query_db, edit_identification_record)
#     logger.info(edit_identification_record_result.message)

#     return ResponseUtil.success(msg=edit_identification_record_result.message)


# @identification_recordController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:remove'))])
# @Log(title='识别记录', business_type=BusinessType.DELETE)
# async def delete_system_identification_record(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
#     delete_identification_record = DeleteIdentification_recordModel(ids=ids)
#     delete_identification_record_result = await Identification_recordService.delete_identification_record_services(query_db, delete_identification_record)
#     logger.info(delete_identification_record_result.message)

#     return ResponseUtil.success(msg=delete_identification_record_result.message)


# @identification_recordController.get(
#     '/{id}', response_model=Identification_recordModel, dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:query'))]
# )
# async def query_detail_system_identification_record(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
#     identification_record_detail_result = await Identification_recordService.identification_record_detail_services(query_db, id)
#     logger.info(f'获取id为{id}的信息成功')

#     return ResponseUtil.success(data=identification_record_detail_result)


@identification_recordController.post('/export'
                                    #   , dependencies=[Depends(CheckUserInterfaceAuth('system:identification_record:export'))]
                                      )
@Log(title='识别记录', business_type=BusinessType.EXPORT)
async def export_system_identification_record_list(
    request: Request,
    identification_record_page_query: Identification_recordPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    identification_record_query_result = await Identification_recordService.get_identification_record_list_services(query_db, identification_record_page_query, is_page=False)
    identification_record_export_result = await Identification_recordService.export_identification_record_list_services(identification_record_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(identification_record_export_result))
