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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
"""
自动化相关 Protocol 接口定义

包含:IAutoTokenAcquisitionService, IAutoLoginService, ITokenService, IBrowserService,
      ICaptchaService, IAutomationService, ICourtSMSService, ICourtDocumentService,
      ICourtDocumentRecognitionService, IPreservationQuoteService, IDocumentProcessingService,
      IAutoNamerService, IPerformanceMonitorService
"""

from typing import TYPE_CHECKING, Any, Optional, Protocol

from apps.core.dto import AccountCredentialDTO

if TYPE_CHECKING:
    from apps.automation.dtos import CourtTokenDTO
    from apps.core.dto import CourtPleadingSignalsDTO


class IAutoTokenAcquisitionService(Protocol):
    """
    自动Token获取服务接口

    定义自动Token获取的核心方法
    """

    async def acquire_token_if_needed(self, site_name: str, credential_id: int | None = None) -> str:
        """
        如果需要则自动获取token

        Args:
            site_name: 网站名称
            credential_id: 指定的凭证ID(可选)

        Returns:
            有效的token字符串

        Raises:
            AutoTokenAcquisitionError: Token获取失败
        """
        ...


class IAutoLoginService(Protocol):
    """
    自动登录服务接口

    定义自动登录的核心方法
    """

    async def login_and_get_token(self, credential: AccountCredentialDTO) -> str:
        """
        执行自动登录并返回token

        Args:
            credential: 账号凭证DTO

        Returns:
            登录成功后的token字符串

        Raises:
            LoginFailedError: 登录失败
            NetworkError: 网络错误
            TokenError: Token获取失败
        """
        ...


class ITokenService(Protocol):
    """
    Token 服务接口

    定义 Token 管理的核心方法
    """

    async def get_token(self, site_name: str) -> str | None:
        """
        获取指定站点的 Token

        Args:
            site_name: 站点名称

        Returns:
            Token 字符串,不存在或已过期时返回 None
        """
        ...


class ICourtTokenStoreService(Protocol):
    def get_latest_valid_token_internal(
        self,
        *,
        site_name: str,
        account: str | None = None,
        token_prefix: str | None = None,
    ) -> Optional["CourtTokenDTO"]: ...

    def save_token_internal(
        self,
        *,
        site_name: str,
        account: str,
        token: str,
        expires_in: int,
        token_type: str = "Bearer",
        credential_id: int | None = None,
    ) -> None: ...

    async def save_token(self, site_name: str, token: str, expires_in: int) -> None:
        """
        保存 Token

        Args:
            site_name: 站点名称
            token: Token 字符串
            expires_in: 过期时间(秒)
        """
        ...

    async def delete_token(self, site_name: str) -> None:
        """
        删除 Token

        Args:
            site_name: 站点名称
        """
        ...


class ICourtPleadingSignalsService(Protocol):
    def get_signals_internal(self, case_id: int) -> "CourtPleadingSignalsDTO": ...


class IBaoquanTokenService(Protocol):
    async def get_valid_baoquan_token(self, credential_id: int | None = None) -> str: ...


class IBrowserService(Protocol):
    """
    浏览器服务接口

    定义浏览器管理的核心方法
    """

    async def get_browser(self) -> Any:
        """
        获取浏览器实例

        Returns:
            浏览器实例对象
        """
        ...

    async def close_browser(self) -> None:
        """
        关闭浏览器
        """
        ...


class ICaptchaService(Protocol):
    """
    验证码服务接口

    定义验证码识别的核心方法
    """

    def recognize(self, image_data: bytes) -> str:
        """
        识别验证码

        Args:
            image_data: 验证码图片的二进制数据

        Returns:
            识别出的验证码文本

        Raises:
            CaptchaRecognitionError: 验证码识别失败
        """
        ...


class IOcrService(Protocol):
    def extract_text(self, image_bytes: bytes) -> Any: ...


class IAutomationService(Protocol):
    """
    自动化服务接口

    定义自动化模块对外提供的核心方法
    """

    def create_token_acquisition_history_internal(self, history_data: dict[str, Any]) -> Any:
        """
        创建Token获取历史记录(内部方法)

        Args:
            history_data: 历史记录数据

        Returns:
            创建的历史记录对象
        """
        ...


class ICourtSMSService(Protocol):
    """
    法院短信处理服务接口

    定义法院短信处理的核心方法
    """

    def submit_sms(self, content: str, received_at: Any | None = None, sender: str | None = None) -> Any:
        """
        提交短信内容

        Args:
            content: 短信内容
            received_at: 收到时间(可选,默认当前时间)
            sender: 发送方号码(可选)

        Returns:
            创建的 CourtSMS 记录

        Raises:
            ValidationException: 短信内容为空
        """
        ...

    def get_sms_detail(self, sms_id: int) -> Any:
        """
        获取短信处理详情

        Args:
            sms_id: 短信记录ID

        Returns:
            短信记录对象

        Raises:
            NotFoundError: 短信记录不存在
        """
        ...

    def list_sms(
        self,
        status: str | None = None,
        sms_type: str | None = None,
        has_case: bool | None = None,
        date_from: Any | None = None,
        date_to: Any | None = None,
    ) -> list[Any]:
        """
        查询短信列表

        Args:
            status: 状态筛选(可选)
            sms_type: 短信类型筛选(可选)
            has_case: 是否关联案件筛选(可选)
            date_from: 开始日期筛选(可选)
            date_to: 结束日期筛选(可选)

        Returns:
            短信记录列表
        """
        ...

    def assign_case(self, sms_id: int, case_id: int) -> Any:
        """
        手动指定案件

        Args:
            sms_id: 短信记录ID
            case_id: 案件ID

        Returns:
            更新后的短信记录

        Raises:
            NotFoundError: 短信记录或案件不存在
        """
        ...

    def retry_processing(self, sms_id: int) -> Any:
        """
        重新处理短信

        Args:
            sms_id: 短信记录ID

        Returns:
            重置后的短信记录

        Raises:
            NotFoundError: 短信记录不存在
        """
        ...


class ICourtDocumentService(Protocol):
    """
    法院文书服务接口

    定义法院文书管理的核心方法
    """

    def create_document_from_api_data(
        self, scraper_task_id: int, api_data: dict[str, Any], case_id: int | None = None
    ) -> Any:
        """
        从API数据创建文书记录

        Args:
            scraper_task_id: 爬虫任务ID
            api_data: API返回的文书数据
            case_id: 关联案件ID(可选)

        Returns:
            创建的文书记录

        Raises:
            ValidationException: 数据验证失败
            NotFoundError: 爬虫任务不存在
        """
        ...

    def update_download_status(
        self,
        document_id: int,
        status: str,
        local_file_path: str | None = None,
        file_size: int | None = None,
        error_message: str | None = None,
    ) -> Any:
        """
        更新文书下载状态

        Args:
            document_id: 文书记录ID
            status: 下载状态
            local_file_path: 本地文件路径(可选)
            file_size: 文件大小(可选)
            error_message: 错误信息(可选)

        Returns:
            更新后的文书记录

        Raises:
            NotFoundError: 文书记录不存在
            ValidationException: 状态值无效
        """
        ...

    def get_documents_by_task(self, scraper_task_id: int) -> list[Any]:
        """
        获取任务的所有文书记录

        Args:
            scraper_task_id: 爬虫任务ID

        Returns:
            文书记录列表
        """
        ...

    def get_document_by_id(self, document_id: int) -> Any | None:
        """
        根据ID获取文书记录

        Args:
            document_id: 文书记录ID

        Returns:
            文书记录,不存在时返回 None
        """
        ...


class ICourtDocumentRecognitionService(Protocol):
    """
    法院文书智能识别服务接口

    定义法院文书识别的核心方法,支持传票、执行裁定书等文书的
    类型识别、关键信息提取和案件绑定.

    Requirements: 7.1, 7.2, 7.3
    """

    def recognize_document(self, file_path: str, user: Any | None = None) -> Any:
        """
        识别文书并绑定案件

        完整的识别流程:
        1. 从文件提取文本(PDF直接提取或OCR)
        2. 使用 AI 分类文书类型
        3. 提取关键信息(案号、开庭时间等)
        4. 匹配案件并创建日志

        Args:
            file_path: 文书文件路径(支持 PDF、JPG、PNG)
            user: 当前用户(可选,用于日志记录)

        Returns:
            RecognitionResponse 对象,包含识别结果和绑定结果

        Raises:
            ValidationException: 文件格式不支持
            ServiceUnavailableError: AI 服务不可用
            TimeoutError: 识别超时
        """
        ...

    def recognize_document_from_text(self, text: str) -> Any:
        """
        从已提取的文本识别文书

        供其他服务调用(如短信下载服务),跳过文本提取步骤,
        直接进行文书分类和信息提取.

        Args:
            text: 已提取的文书文本内容

        Returns:
            RecognitionResult 对象,包含文书类型、案号、关键时间等

        Raises:
            ValidationException: 文本内容为空
            ServiceUnavailableError: AI 服务不可用
            TimeoutError: 识别超时
        """
        ...


class IPreservationQuoteService(Protocol):
    """
    财产保全询价服务接口

    定义财产保全询价的核心方法
    """

    def create_quote(
        self,
        case_name: str,
        target_amount: Any,  # Decimal
        applicant_name: str,
        respondent_name: str,
        court_name: str,
        case_type: str = "财产保全",
        **kwargs: Any,
    ) -> Any:
        """
        创建询价任务

        Args:
            case_name: 案件名称
            target_amount: 保全金额
            applicant_name: 申请人姓名
            respondent_name: 被申请人姓名
            court_name: 法院名称
            case_type: 案件类型
            **kwargs: 其他参数

        Returns:
            创建的询价记录

        Raises:
            ValidationException: 数据验证失败
        """
        ...

    def execute_quote(self, quote_id: int, force_refresh_token: bool = False) -> dict[str, Any]:
        """
        执行询价任务

        Args:
            quote_id: 询价记录ID
            force_refresh_token: 是否强制刷新Token

        Returns:
            询价结果字典

        Raises:
            NotFoundError: 询价记录不存在
            BusinessException: 询价执行失败
        """
        ...

    def get_quote_by_id(self, quote_id: int) -> Any | None:
        """
        根据ID获取询价记录

        Args:
            quote_id: 询价记录ID

        Returns:
            询价记录,不存在时返回 None
        """
        ...

    def list_quotes(self, status: str | None = None, limit: int = 20, offset: int = 0) -> dict[str, Any]:
        """
        获取询价记录列表

        Args:
            status: 状态筛选(可选)
            limit: 限制数量
            offset: 偏移量

        Returns:
            包含记录列表和总数的字典
        """
        ...


class IDocumentProcessingService(Protocol):
    """
    文档处理服务接口

    定义文档处理的核心方法
    """

    def extract_text_from_pdf(
        self, file_path: str, limit: int | None = None, preview_page: int | None = None
    ) -> dict[str, Any]:
        """
        从PDF文件提取文本

        Args:
            file_path: PDF文件路径
            limit: 文本长度限制
            preview_page: 预览页码

        Returns:
            包含文本和预览图的字典

        Raises:
            ValidationException: 文件格式不支持
            FileNotFoundError: 文件不存在
        """
        ...

    def extract_text_from_docx(self, file_path: str, limit: int | None = None) -> str:
        """
        从DOCX文件提取文本

        Args:
            file_path: DOCX文件路径
            limit: 文本长度限制

        Returns:
            提取的文本内容

        Raises:
            ValidationException: 文件格式不支持
            FileNotFoundError: 文件不存在
        """
        ...

    def extract_text_from_image(self, file_path: str, limit: int | None = None) -> str:
        """
        从图片文件提取文本(OCR)

        Args:
            file_path: 图片文件路径
            limit: 文本长度限制

        Returns:
            OCR识别的文本内容

        Raises:
            ValidationException: 文件格式不支持
            FileNotFoundError: 文件不存在
        """
        ...

    def process_uploaded_document(
        self, uploaded_file: Any, limit: int | None = None, preview_page: int | None = None
    ) -> dict[str, Any]:
        """
        处理上传的文档

        Args:
            uploaded_file: 上传的文件对象
            limit: 文本长度限制
            preview_page: 预览页码

        Returns:
            包含文本和预览信息的字典

        Raises:
            ValidationException: 文件格式不支持
        """
        ...


class IAutoNamerService(Protocol):
    """
    自动命名服务接口

    定义自动命名的核心方法
    """

    def generate_filename(self, document_content: str, prompt: str | None = None, model: str = "qwen3:0.6b") -> str:
        """
        根据文档内容生成文件名

        Args:
            document_content: 文档文本内容
            prompt: 自定义提示词(可选)
            model: 使用的AI模型

        Returns:
            生成的文件名建议

        Raises:
            ValidationException: 内容为空或无效
            BusinessException: AI服务调用失败
        """
        ...

    def process_document_for_naming(
        self,
        uploaded_file: Any,
        prompt: str | None = None,
        model: str = "qwen3:0.6b",
        limit: int | None = None,
        preview_page: int | None = None,
    ) -> dict[str, Any]:
        """
        处理文档并生成命名建议

        Args:
            uploaded_file: 上传的文件对象
            prompt: 自定义提示词(可选)
            model: 使用的AI模型
            limit: 文本长度限制
            preview_page: 预览页码

        Returns:
            包含文本内容、命名建议等信息的字典

        Raises:
            ValidationException: 文件格式不支持
            BusinessException: 处理失败
        """
        ...


class IPerformanceMonitorService(Protocol):
    """
    性能监控服务接口

    定义性能监控的核心方法
    """

    def get_system_metrics(self) -> dict[str, Any]:
        """
        获取系统性能指标

        Returns:
            系统性能指标字典,包含CPU、内存、磁盘等信息
        """
        ...

    def get_token_acquisition_metrics(self, hours: int = 24) -> dict[str, Any]:
        """
        获取Token获取性能指标

        Args:
            hours: 统计最近多少小时的数据

        Returns:
            Token获取性能指标字典
        """
        ...

    def get_api_performance_metrics(self, api_name: str | None = None, hours: int = 24) -> dict[str, Any]:
        """
        获取API性能指标

        Args:
            api_name: API名称(可选,为空时返回所有API)
            hours: 统计最近多少小时的数据

        Returns:
            API性能指标字典
        """
        ...

    def record_performance_metric(self, metric_name: str, value: float, tags: dict[str, str] | None = None) -> None:
        """
        记录性能指标

        Args:
            metric_name: 指标名称
            value: 指标值
            tags: 标签字典(可选)
        """
        ...