Files
ShiErFeng/shierfeng-fastapi-backend/module_business/controller/customer_controller.py
T

124 lines
4.8 KiB
Python

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.entity.vo.master_vo import CustomerModel, CustomerPageQueryModel, DeleteCustomerModel
from module_business.service.master_service import MasterService
from utils.response_util import ResponseUtil
customer_controller = APIRouterPro(
prefix='/master/customer', tags=['基础资料-客户管理'], dependencies=[PreAuthDependency()]
)
@customer_controller.get(
'/list',
summary='获取客户分页列表接口',
description='用于获取客户分页列表',
response_model=PageResponseModel[CustomerModel],
dependencies=[UserInterfaceAuthDependency('master:customer:list')],
)
async def get_master_customer_list(
request: Request,
customer_page_query: Annotated[CustomerPageQueryModel, Query()],
query_db: Annotated[AsyncSession, DBSessionDependency()],
) -> Response:
customer_page_query_result = await MasterService.list_customer(query_db, customer_page_query, is_page=True)
return ResponseUtil.success(model_content=customer_page_query_result)
@customer_controller.get(
'/{customer_id}',
summary='获取客户详情接口',
description='用于获取指定客户的详细信息',
response_model=DataResponseModel[CustomerModel],
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()],
) -> Response:
customer_detail_result = await MasterService.detail_customer(query_db, customer_id)
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()],
) -> 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)
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()],
) -> 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)
return ResponseUtil.success(msg=edit_customer_result.message)
@customer_controller.delete(
'/{customer_ids}',
summary='删除客户接口',
description='用于删除客户',
response_model=ResponseBaseModel,
dependencies=[UserInterfaceAuthDependency('master:customer:remove')],
)
@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)