from fastapi import APIRouter, BackgroundTasks, UploadFile, File, Form from services.token_service import TokenService from services.file_service import FileService from services.statement_service import StatementService from utils.error_simulator import ErrorSimulator from typing import List, Optional # 创建路由器 router = APIRouter() # 初始化服务实例 token_service = TokenService() file_service = FileService() statement_service = StatementService() # ==================== 接口1:获取Token ==================== @router.post("/account/common/getToken") async def get_token( projectNo: str = Form(..., description="项目编号,格式:902000_当前时间戳"), entityName: str = Form(..., description="项目名称"), userId: str = Form(..., description="操作人员编号,固定值"), userName: str = Form(..., description="操作人员姓名,固定值"), appId: str = Form("remote_app", description="应用ID,固定值"), appSecretCode: str = Form(..., description="安全码"), role: str = Form("VIEWER", description="角色,固定值"), orgCode: str = Form(..., description="行社机构号,固定值"), entityId: Optional[str] = Form(None, description="企业统信码或个人身份证号"), xdRelatedPersons: Optional[str] = Form(None, description="信贷关联人信息"), jzDataDateId: str = Form("0", description="拉取指定日期推送过来的金综链流水"), innerBSStartDateId: str = Form("0", description="拉取行内流水开始日期"), innerBSEndDateId: str = Form("0", description="拉取行内流水结束日期"), analysisType: str = Form("-1", description="分析类型,固定值"), departmentCode: str = Form(..., description="客户经理所属营业部/分理处的机构编码"), ): """创建项目并获取访问Token 如果 projectNo 包含 error_XXXX 标记,将返回对应的错误响应 """ # 检测错误标记 error_code = ErrorSimulator.detect_error_marker(projectNo) if error_code: return ErrorSimulator.build_error_response(error_code) # 构建请求数据字典 request_data = { "projectNo": projectNo, "entityName": entityName, "userId": userId, "userName": userName, "appId": appId, "appSecretCode": appSecretCode, "role": role, "orgCode": orgCode, "entityId": entityId, "xdRelatedPersons": xdRelatedPersons, "jzDataDateId": jzDataDateId, "innerBSStartDateId": innerBSStartDateId, "innerBSEndDateId": innerBSEndDateId, "analysisType": analysisType, "departmentCode": departmentCode, } # 正常流程 return token_service.create_token(request_data) # ==================== 接口2:上传文件 ==================== @router.post("/watson/api/project/remoteUploadSplitFile") async def upload_file( background_tasks: BackgroundTasks, groupId: int = Form(..., description="项目ID"), file: UploadFile = File(..., description="流水文件"), ): """上传流水文件 文件将立即返回,并在后台延迟4秒完成解析 """ return await file_service.upload_file(groupId, file, background_tasks) # ==================== 接口3:拉取行内流水 ==================== @router.post("/watson/api/project/getJZFileOrZjrcuFile") async def fetch_inner_flow( groupId: int = Form(..., description="项目id"), customerNo: str = Form(..., description="客户身份证号"), dataChannelCode: str = Form(..., description="校验码"), requestDateId: int = Form(..., description="发起请求的时间"), dataStartDateId: int = Form(..., description="拉取开始日期"), dataEndDateId: int = Form(..., description="拉取结束日期"), uploadUserId: int = Form(..., description="柜员号"), ): """拉取行内流水 如果 customerNo 包含 error_XXXX 标记,将返回对应的错误响应 """ # 检测错误标记 error_code = ErrorSimulator.detect_error_marker(customerNo) if error_code: return ErrorSimulator.build_error_response(error_code) # 构建请求字典 request_data = { "groupId": groupId, "customerNo": customerNo, "dataChannelCode": dataChannelCode, "requestDateId": requestDateId, "dataStartDateId": dataStartDateId, "dataEndDateId": dataEndDateId, "uploadUserId": uploadUserId, } # 正常流程 return file_service.fetch_inner_flow(request_data) # ==================== 接口4:检查文件解析状态 ==================== @router.post("/watson/api/project/upload/getpendings") async def check_parse_status( groupId: int = Form(..., description="项目id"), inprogressList: str = Form(..., description="文件id列表,逗号分隔"), ): """检查文件解析状态 返回文件是否还在解析中(parsing字段) """ return file_service.check_parse_status(groupId, inprogressList) # ==================== 接口5:删除文件 ==================== @router.post("/watson/api/project/batchDeleteUploadFile") async def delete_files( groupId: int = Form(..., description="项目id"), logIds: str = Form(..., description="文件id数组,逗号分隔,如: 10001,10002"), userId: int = Form(..., description="用户柜员号"), ): """批量删除上传的文件 根据logIds列表删除对应的文件记录 """ # 将逗号分隔的字符串转换为整数列表 log_id_list = [int(id.strip()) for id in logIds.split(",")] return file_service.delete_files(groupId, log_id_list, userId) # ==================== 接口6:获取银行流水 ==================== @router.post("/watson/api/project/getBSByLogId") async def get_bank_statement( groupId: int = Form(..., description="项目id"), logId: int = Form(..., description="文件id"), pageNow: int = Form(..., description="当前页码"), pageSize: int = Form(..., description="查询条数"), ): """获取银行流水列表 支持分页查询(pageNow, pageSize) """ # 构建请求字典 request_data = { "groupId": groupId, "logId": logId, "pageNow": pageNow, "pageSize": pageSize, } return statement_service.get_bank_statement(request_data)