from datetime import datetime from typing import Annotated from fastapi import Path, Query, Request, Response from sqlalchemy.ext.asyncio import AsyncSession from common.annotation.log_annotation import Log from common.aspect.db_seesion import DBSessionDependency from common.aspect.interface_auth import UserInterfaceAuthDependency from common.aspect.pre_auth import CurrentUserDependency, PreAuthDependency from common.enums import BusinessType from common.router import APIRouterPro from common.vo import DataResponseModel, PageResponseModel, ResponseBaseModel from module_admin.entity.vo.user_vo import CurrentUserModel from module_business.dependency.test_batch_dependency import TestBatchScopeDependency from module_business.domain.test_batch_scope import TestBatchScope from module_business.entity.vo.master_summary_vo import CustomerDetailModel, CustomerListItemModel from module_business.entity.vo.master_vo import CustomerModel, CustomerPageQueryModel, DeleteCustomerModel from module_business.service.master_service import MasterService from module_business.service.master_summary_service import MasterSummaryService from utils.response_util import ResponseUtil customer_controller = APIRouterPro( prefix='/master/customer', tags=['基础资料-客户管理'], dependencies=[PreAuthDependency()] ) @customer_controller.get( '/list', summary='获取客户分页列表接口', description='用于获取客户分页列表', response_model=PageResponseModel[CustomerListItemModel], dependencies=[UserInterfaceAuthDependency('master:customer:list')], ) async def get_master_customer_list( request: Request, customer_page_query: Annotated[CustomerPageQueryModel, Query()], query_db: Annotated[AsyncSession, DBSessionDependency()], scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: customer_page_query_result = await MasterService.list_customer(query_db, customer_page_query, scope, is_page=True) customer_page_query_result = await MasterSummaryService.enrich_page( query_db, 'customer', customer_page_query_result, scope ) return ResponseUtil.success(model_content=customer_page_query_result) @customer_controller.get( '/{customer_id}', summary='获取客户详情接口', description='用于获取指定客户的详细信息', response_model=DataResponseModel[CustomerDetailModel], dependencies=[UserInterfaceAuthDependency('master:customer:query')], ) async def query_detail_master_customer( request: Request, customer_id: Annotated[int, Path(gt=0, description='客户ID')], query_db: Annotated[AsyncSession, DBSessionDependency()], scope: Annotated[TestBatchScope, TestBatchScopeDependency()], ) -> Response: customer_detail_result = await MasterService.detail_customer(query_db, customer_id, scope) customer_detail_result = await MasterSummaryService.customer_detail(query_db, customer_detail_result, scope) return ResponseUtil.success(data=customer_detail_result) @customer_controller.post( '', summary='新增客户接口', description='用于新增客户', response_model=ResponseBaseModel, dependencies=[UserInterfaceAuthDependency('master:customer:add')], ) @Log(title='客户管理', business_type=BusinessType.INSERT) async def add_master_customer( request: Request, add_customer: CustomerModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], scope: Annotated[TestBatchScope, TestBatchScopeDependency(write=True)], ) -> Response: current_time = datetime.now() add_customer.create_by = current_user.user.user_name add_customer.create_time = current_time add_customer.update_by = current_user.user.user_name add_customer.update_time = current_time add_customer_result = await MasterService.add_customer(query_db, add_customer, current_user, scope) return ResponseUtil.success(msg=add_customer_result.message) @customer_controller.put( '', summary='编辑客户接口', description='用于编辑客户', response_model=ResponseBaseModel, dependencies=[UserInterfaceAuthDependency('master:customer:edit')], ) @Log(title='客户管理', business_type=BusinessType.UPDATE) async def edit_master_customer( request: Request, edit_customer: CustomerModel, query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], scope: Annotated[TestBatchScope, TestBatchScopeDependency(write=True)], ) -> Response: edit_customer.update_by = current_user.user.user_name edit_customer.update_time = datetime.now() edit_customer_result = await MasterService.edit_customer(query_db, edit_customer, current_user, scope) return ResponseUtil.success(msg=edit_customer_result.message) @customer_controller.delete( '/{customer_ids}', summary='兼容旧接口,固定拒绝,请使用测试数据批次清理', description='兼容旧接口,固定拒绝单条物理删除,请使用测试数据批次清理', response_model=ResponseBaseModel, dependencies=[UserInterfaceAuthDependency('master:customer:list')], ) @Log(title='客户管理', business_type=BusinessType.DELETE) async def delete_master_customer( request: Request, customer_ids: Annotated[ str, Path(pattern=r'^[1-9]\d*(,[1-9]\d*)*$', description='需要删除的客户ID'), ], query_db: Annotated[AsyncSession, DBSessionDependency()], current_user: Annotated[CurrentUserModel, CurrentUserDependency()], ) -> Response: delete_customer = DeleteCustomerModel(customerIds=[int(customer_id) for customer_id in customer_ids.split(',')]) delete_customer_result = await MasterService.remove_customer(query_db, delete_customer) return ResponseUtil.success(msg=delete_customer_result.message)