File size: 31,286 Bytes
f8b03c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
"""
文书模板 Admin 配置

Requirements: 6.1, 2.9, 2.10
"""

import json
import logging
from pathlib import Path
from typing import Any, cast

from django import forms
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from apps.core.interfaces import ServiceLocator
from apps.core.models.enums import LegalStatus
from apps.documents.models import (
    DocumentCaseFileSubType,
    DocumentCaseStage,
    DocumentCaseType,
    DocumentContractSubType,
    DocumentContractType,
    DocumentTemplate,
    DocumentTemplateFolderBinding,
    DocumentTemplateType,
    LegalStatusMatchMode,
)
from apps.documents.storage import (
    PRIVATE_DOCX_ROOT_SETTING,
    get_configured_private_docx_templates_root,
    get_docx_templates_root,
    get_docx_templates_source,
    list_docx_templates_files,
)

logger = logging.getLogger(__name__)


def _get_template_service() -> Any:
    """工厂函数获取模板服务"""
    from apps.documents.services.template.template_service import DocumentTemplateService

    return DocumentTemplateService()


def _get_admin_service() -> Any:
    """工厂函数获取Admin服务"""
    from apps.documents.services.template.document_template.admin_service import DocumentTemplateAdminService

    return DocumentTemplateAdminService()


def _to_django_relative_path(path: Path) -> str:
    """将绝对路径尽量转换为相对 Django 服务目录(backend)的路径。"""
    backend_root = Path(str(getattr(settings, "BASE_DIR", "."))).resolve().parent
    target = path.resolve()
    try:
        return target.relative_to(backend_root).as_posix()
    except ValueError:
        return Path(str(target)).as_posix()


def _normalize_private_docx_root(raw_value: str) -> str:
    """标准化私有模板根目录输入(支持绝对路径或相对 backend 路径)。"""
    normalized = raw_value.strip()
    if not normalized:
        return ""

    candidate = Path(normalized).expanduser()
    if not candidate.is_absolute():
        backend_root = Path(str(getattr(settings, "BASE_DIR", "."))).resolve().parent
        candidate = (backend_root / candidate).resolve()
    else:
        candidate = candidate.resolve()

    if not candidate.exists() or not candidate.is_dir():
        raise ValueError(str(_("模板根目录不存在或不是文件夹")))

    return str(candidate)


def _get_system_config_service() -> Any:
    return ServiceLocator.get_system_config_service()


class DocumentTemplateFolderBindingInline(admin.TabularInline):
    """文书模板文件夹绑定内联"""

    model = DocumentTemplateFolderBinding
    extra = 1
    fields = ("folder_template", "folder_node_id", "folder_node_path", "is_active")
    readonly_fields = ("folder_node_path",)
    autocomplete_fields = ["folder_template"]

    class Media:
        css = {LegalStatusMatchMode.ALL: ("documents/css/folder_binding_inline.css",)}
        js = ("admin/js/jquery.init.js", "documents/js/folder_binding_inline.js")


class DocumentTemplateForm(forms.ModelForm):
    """文书模板表单,包含模板类型和适用范围选择(与文件夹模板保持一致)"""

    # 模板类型单选(必选)
    template_type = forms.ChoiceField(
        choices=DocumentTemplateType.choices,
        widget=forms.RadioSelect,
        label=_("模板类型"),
        help_text=_("必须二选一:合同文书模板或案件文书模板"),
    )

    # 合同子类型单选(仅合同模板时显示)
    contract_sub_type = forms.ChoiceField(
        choices=[("", "请选择")] + [(c.value, c.label) for c in DocumentContractSubType],
        widget=forms.RadioSelect,
        required=False,
        label=_("合同子类型"),
        help_text=_("仅在选择'合同文书模板'时有效,必须选择合同模板或补充协议模板"),
    )

    case_sub_type = forms.ChoiceField(
        choices=[("", "请选择")] + [(c.value, c.label) for c in DocumentCaseFileSubType],
        widget=forms.RadioSelect,
        required=False,
        label=_("案件文件子类型"),
        help_text=_("仅在选择'案件文书模板'时有效,可选择诉状材料、证据材料、授权委托材料等"),
    )

    # 合同类型多选(仅合同模板时显示)
    contract_types_field = forms.MultipleChoiceField(
        choices=[(c.value, c.label) for c in DocumentContractType],
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label=_("合同类型"),
        help_text=_("仅在选择'合同文书模板'时有效,可多选"),
    )

    # 案件类型多选(仅案件模板时显示)
    case_types_field = forms.MultipleChoiceField(
        choices=[(c.value, c.label) for c in DocumentCaseType],
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label=_("案件类型"),
        help_text=_("仅在选择'案件文书模板'时有效,可多选"),
    )

    # 案件阶段单选(仅案件模板时显示)
    case_stage_field = forms.ChoiceField(
        choices=[("", "请选择")] + [(c.value, c.label) for c in DocumentCaseStage],
        widget=forms.Select,
        required=False,
        label=_("案件阶段"),
        help_text=_("仅在选择'案件文书模板'时有效,单选"),
    )

    # 我方诉讼地位多选(仅案件模板时显示)
    legal_statuses_field = forms.MultipleChoiceField(
        choices=LegalStatus.choices,
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label=_("我方诉讼地位"),
        help_text=_("可单选或多选;不选表示匹配任意诉讼地位"),
    )

    # 诉讼地位匹配模式单选(仅案件模板时显示)
    legal_status_match_mode = forms.ChoiceField(
        choices=LegalStatusMatchMode.choices,
        widget=forms.RadioSelect,
        required=False,
        initial=LegalStatusMatchMode.ANY,
        label=_("诉讼地位匹配模式"),
    )

    # 适用机构(案件模板时显示)
    applicable_institutions_field = forms.CharField(
        required=False,
        label=_("适用机构"),
        help_text=_("输入机构名称后回车添加,支持搜索法院名称"),
        widget=forms.Textarea(
            attrs={
                "id": "id_applicable_institutions_field",
                "style": "display:none;",
                "rows": "1",
            }
        ),
    )

    # 从已有文件中选择(新增)
    existing_file = forms.ChoiceField(
        choices=[],
        required=False,
        label=_("从模板库选择"),
        help_text=_("从 docx_templates 目录中选择已有的模板文件(不会复制文件)"),
    )

    class Meta:
        model = DocumentTemplate
        fields = [
            "name",
            "template_type",
            "contract_sub_type",
            "case_sub_type",
            "file",
            "file_path",
            "is_active",
        ]

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)

        # 动态加载已有文件列表
        existing_files = list_docx_templates_files()
        existing_file_field = cast(forms.ChoiceField, self.fields["existing_file"])
        existing_file_field.choices = [("", "-- 不选择 / 上传新文件 --")] + existing_files

        # 从实例加载已选值
        if self.instance and self.instance.pk:
            admin_service = _get_admin_service()
            initial_values = admin_service.get_form_initial_values(self.instance, existing_files)

            self.fields["template_type"].initial = initial_values["template_type"]
            self.fields["contract_sub_type"].initial = initial_values["contract_sub_type"]
            self.fields["case_sub_type"].initial = initial_values["case_sub_type"]
            self.fields["contract_types_field"].initial = initial_values["contract_types_field"]
            self.fields["case_types_field"].initial = initial_values["case_types_field"]
            self.fields["case_stage_field"].initial = initial_values["case_stage_field"]
            self.fields["legal_statuses_field"].initial = initial_values["legal_statuses_field"]
            self.fields["legal_status_match_mode"].initial = initial_values["legal_status_match_mode"]
            self.fields["existing_file"].initial = initial_values["existing_file"]

            # 加载适用机构
            institutions = self.instance.applicable_institutions or []
            self.fields["applicable_institutions_field"].initial = json.dumps(institutions, ensure_ascii=False)

            if initial_values["file_path"] == "":
                self.initial["file_path"] = ""

    def clean(self) -> Any:
        """验证文件选择逻辑和模板类型逻辑"""
        cleaned_data = super().clean() or {}
        existing_file = cleaned_data.get("existing_file")
        uploaded_file = cleaned_data.get("file")
        file_path = cleaned_data.get("file_path")
        template_type = cleaned_data.get("template_type")
        contract_sub_type = cleaned_data.get("contract_sub_type")
        case_sub_type = cleaned_data.get("case_sub_type")
        case_stage_field = cleaned_data.get("case_stage_field")

        admin_service = _get_admin_service()
        is_editing = self.instance and self.instance.pk
        original_template_type = self.instance.template_type if is_editing else None

        # 验证文件来源
        file_result = admin_service.validate_file_sources(
            existing_file, uploaded_file, file_path, self.instance, is_editing
        )

        if not file_result["is_valid"]:
            raise forms.ValidationError(file_result["error"])

        # 如果跳过文件验证(编辑模式且未修改文件)
        if file_result.get("skip_file_validation"):
            # 仅验证模板类型
            type_result = admin_service.validate_template_type(
                template_type=template_type,
                contract_sub_type=contract_sub_type,
                case_sub_type=case_sub_type,
                is_editing=is_editing,
                original_template_type=original_template_type,
            )
            if not type_result["is_valid"]:
                raise forms.ValidationError(type_result["errors"])
            cleaned_data["contract_sub_type"] = type_result["contract_sub_type"]
            cleaned_data["case_sub_type"] = type_result["case_sub_type"]
            return cleaned_data

        # 更新cleaned_data
        cleaned_data["file_path"] = file_result["cleaned_data"]["file_path"]
        cleaned_data["file"] = file_result["cleaned_data"]["file"]
        cleaned_data["existing_file"] = file_result["cleaned_data"]["existing_file"]

        # 验证模板类型
        type_result = admin_service.validate_template_type(
            template_type=template_type,
            contract_sub_type=contract_sub_type,
            case_sub_type=case_sub_type,
            is_editing=is_editing,
            original_template_type=original_template_type,
        )
        if not type_result["is_valid"]:
            raise forms.ValidationError(type_result["errors"])
        cleaned_data["contract_sub_type"] = type_result["contract_sub_type"]
        cleaned_data["case_sub_type"] = type_result["case_sub_type"]

        if template_type == DocumentTemplateType.CASE and not case_stage_field:
            self.add_error("case_stage_field", _("请选择案件阶段"))

        return cleaned_data

    def save(self, commit: bool = True) -> Any:
        """保存时将多选字段值写入JSON字段,根据模板类型处理相应字段"""
        instance = super().save(commit=False)

        admin_service = _get_admin_service()
        save_data = admin_service.prepare_save_data(
            template_type=self.cleaned_data.get("template_type"),
            contract_sub_type=self.cleaned_data.get("contract_sub_type"),
            case_sub_type=self.cleaned_data.get("case_sub_type"),
            contract_types_field=self.cleaned_data.get("contract_types_field", []),
            case_types_field=self.cleaned_data.get("case_types_field", []),
            case_stage_field=self.cleaned_data.get("case_stage_field", ""),
            legal_statuses_field=self.cleaned_data.get("legal_statuses_field", []),
            legal_status_match_mode=self.cleaned_data.get("legal_status_match_mode", LegalStatusMatchMode.ANY),
            file=self.cleaned_data.get("file"),
            file_path=self.cleaned_data.get("file_path"),
        )

        instance.template_type = save_data["template_type"]
        instance.contract_sub_type = save_data["contract_sub_type"]
        instance.case_sub_type = save_data["case_sub_type"]
        instance.contract_types = save_data["contract_types"]
        instance.case_types = save_data["case_types"]
        instance.case_stages = save_data["case_stages"]
        instance.legal_statuses = save_data["legal_statuses"]
        instance.legal_status_match_mode = save_data["legal_status_match_mode"]

        # 保存适用机构
        raw = self.cleaned_data.get("applicable_institutions_field", "")
        try:
            institutions = json.loads(raw) if raw else []
        except (json.JSONDecodeError, TypeError):
            institutions = []
        instance.applicable_institutions = institutions if isinstance(institutions, list) else []

        # 确保file和file_path互斥
        if save_data["file"]:
            instance.file_path = ""
        elif save_data["file_path"]:
            instance.file = ""

        if commit:
            instance.save()
        return instance


@admin.register(DocumentTemplate)
class DocumentTemplateAdmin(admin.ModelAdmin[DocumentTemplate]):
    """
    文书模板管理

    提供文书模板的 CRUD 操作,显示占位符列表并高亮未定义的占位符.
    """

    form = DocumentTemplateForm

    list_display = (
        "id",
        "name",
        "template_type_display",
        "file_location_display",
        "is_active",
        "updated_at",
    )

    list_filter = (
        "template_type",
        "is_active",
    )

    search_fields = (
        "name",
        "description",
    )

    ordering = ("-id",)

    readonly_fields = (
        "current_file_display",
        "placeholders_display",
        "undefined_placeholders_display",
    )

    fieldsets = (
        (None, {"fields": ("name",)}),
        (
            _("模板类型"),
            {
                "fields": ("template_type", "contract_sub_type", "case_sub_type"),
                "description": _("先选择合同文书模板或案件文书模板,再选择对应的子类型"),
            },
        ),
        (
            _("适用范围"),
            {
                "fields": (
                    "contract_types_field",
                    "case_types_field",
                    "case_stage_field",
                    "legal_statuses_field",
                    "legal_status_match_mode",
                    "applicable_institutions_field",
                ),
                "description": _("根据模板类型选择相应的适用范围"),
            },
        ),
        (
            _("文件"),
            {
                "fields": ("current_file_display", "existing_file", "file", "file_path"),
                "description": _(
                    "三选一:从模板库选择已有文件(不复制)、上传新文件(复制到用户自定义模板目录)、或手动输入路径"
                ),
            },
        ),
        (_("状态"), {"fields": ("is_active",)}),
        (
            _("占位符信息"),
            {
                "fields": ("placeholders_display", "undefined_placeholders_display"),
                "classes": ("collapse",),
                "description": _("模板中使用的占位符列表"),
            },
        ),
    )

    inlines = [DocumentTemplateFolderBindingInline]

    change_list_template = "admin/documents/documenttemplate/change_list.html"

    actions = [
        "activate_templates",
        "deactivate_templates",
        "refresh_placeholders",
        "duplicate_templates",
    ]

    class Media:
        css = {
            LegalStatusMatchMode.ALL: (
                "documents/css/multi_select.css",
                "cases/css/autocomplete.css",
                "documents/css/institution_tags.css",
            )
        }
        js = (
            "cases/js/autocomplete.js",
            "documents/js/template_type_toggle.js",
            "documents/js/institution_tags.js",
        )

    def get_search_results(self, request: Any, queryset: Any, search_term: str) -> tuple[Any, bool]:
        queryset, use_distinct = super().get_search_results(request, queryset, search_term)
        if request.GET.get("field_name") == "export_template":
            queryset = queryset.filter(
                is_active=True,
                template_type=DocumentTemplateType.CASE,
                case_sub_type=DocumentCaseFileSubType.EVIDENCE_MATERIALS,
            )
        return queryset, use_distinct

    def get_urls(self) -> list[Any]:
        """添加自定义URL"""
        from django.urls import path

        urls = super().get_urls()
        custom_urls = [
            path(
                "<int:pk>/download/",
                self.admin_site.admin_view(self.download_view),
                name="documents_documenttemplate_download",
            ),
            path(
                "initialize-defaults/",
                self.admin_site.admin_view(self.initialize_defaults_view),
                name="documents_documenttemplate_initialize",
            ),
            path(
                "set-docx-root/",
                self.admin_site.admin_view(self.set_docx_root_view),
                name="documents_documenttemplate_set_docx_root",
            ),
        ]
        return custom_urls + urls

    def download_view(self, request: Any, pk: int) -> Any:
        """下载文件视图"""
        from django.http import FileResponse, Http404

        from apps.core.exceptions import NotFoundError

        obj = self.get_object(request, str(pk))
        if not obj:
            raise Http404("模板不存在")

        try:
            service = _get_admin_service()
            file_path, filename = service.download_file(obj)
        except NotFoundError:
            raise Http404("文件不存在")

        response: Any = FileResponse(Path(file_path).open("rb"), as_attachment=True, filename=filename)
        return response

    def initialize_defaults_view(self, request: Any) -> Any:
        """初始化默认文件模板视图"""
        from django.contrib import messages
        from django.http import HttpResponseRedirect
        from django.urls import reverse

        from apps.documents.services.document_template.init_service import DocumentTemplateInitService

        try:
            init_service = DocumentTemplateInitService()
            result = init_service.initialize_default_templates()
        except Exception as exc:
            logger.exception("初始化默认模板失败")
            messages.error(request, _("初始化失败:%(error)s") % {"error": str(exc)})
            return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

        if not result.get("success", True):
            missing_files = result.get("missing_files", [])
            preview_files = "、".join(str(item) for item in missing_files[:5])
            if len(missing_files) > 5:
                preview_files = f"{preview_files} ..."

            messages.error(
                request,
                _(
                    "初始化失败:缺少 %(count)s 个模板文件,请先补齐当前模板根目录下对应 docx 文件。当前目录:%(root)s。缺失示例:%(files)s"
                )
                % {"count": len(missing_files), "root": get_docx_templates_root(), "files": preview_files or "-"},
            )
            return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

        msg_parts = []
        if result["folder_created"] > 0:
            msg_parts.append(f"文件夹模板 {result['folder_created']} 个")
        if result["doc_created"] > 0:
            msg_parts.append(f"文件模板 {result['doc_created']} 个")
        if result["binding_created"] > 0:
            msg_parts.append(f"绑定关系 {result['binding_created']} 个")

        if msg_parts:
            messages.success(request, f"✅ 初始化成功!创建了:{' | '.join(msg_parts)}")
        else:
            messages.info(request, "ℹ️ 所有数据已存在,无需初始化")

        return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

    def set_docx_root_view(self, request: Any) -> HttpResponseRedirect:
        """在线设置私有模板根目录(为空则切回公用目录)。"""
        from django.contrib import messages
        from django.urls import reverse

        if request.method != "POST":
            messages.error(request, _("仅支持 POST 请求"))
            return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

        raw_value = str(request.POST.get("private_docx_root", "") or "")

        try:
            normalized = _normalize_private_docx_root(raw_value)
            _get_system_config_service().set_value(
                key=PRIVATE_DOCX_ROOT_SETTING,
                value=normalized,
                category="general",
                description="文书模板私有根目录,留空表示使用公用目录",
                is_secret=False,
            )
        except ValueError as exc:
            messages.error(request, str(exc))
            return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))
        except Exception as exc:
            logger.exception("更新模板根目录失败")
            messages.error(request, _("更新失败:%(error)s") % {"error": str(exc)})
            return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

        if normalized:
            messages.success(request, _("模板根目录已更新为:%(path)s") % {"path": normalized})
        else:
            messages.success(request, _("已切换为公用模板目录"))

        return HttpResponseRedirect(reverse("admin:documents_documenttemplate_changelist"))

    def _get_docx_root_extra_context(self) -> dict[str, str]:
        from django.urls import reverse

        source = get_docx_templates_source()
        source_label = _("私有模板目录") if source == "private" else _("公用模板目录")
        private_root_input = get_configured_private_docx_templates_root()
        return {
            "docx_templates_source": source,
            "docx_templates_source_label": str(source_label),
            "docx_templates_root": _to_django_relative_path(get_docx_templates_root()),
            "docx_templates_set_root_url": reverse("admin:documents_documenttemplate_set_docx_root"),
            "docx_templates_private_root_input": private_root_input,
        }

    def changelist_view(self, request: Any, extra_context: Any = None) -> Any:
        """重写changelist视图,添加初始化按钮"""
        from django.urls import reverse

        extra_context = extra_context or {}
        extra_context["initialize_url"] = reverse("admin:documents_documenttemplate_initialize")
        extra_context.update(self._get_docx_root_extra_context())
        return super().changelist_view(request, extra_context=extra_context)

    def changeform_view(
        self,
        request: Any,
        object_id: str | None = None,
        form_url: str = "",
        extra_context: dict[str, Any] | None = None,
    ) -> Any:
        extra_context = extra_context or {}
        extra_context.update(self._get_docx_root_extra_context())
        return super().changeform_view(request, object_id=object_id, form_url=form_url, extra_context=extra_context)

    @admin.display(description=_("模板类型"))
    def template_type_display(self, obj: DocumentTemplate) -> str:
        """显示模板类型"""
        return obj.template_type_display  # type: ignore[no-any-return]

    @admin.display(description=_("合同类型"))
    def contract_types_display(self, obj: DocumentTemplate) -> str:
        """显示合同类型"""
        return obj.contract_types_display  # type: ignore[no-any-return]

    @admin.display(description=_("案件类型"))
    def case_types_display(self, obj: DocumentTemplate) -> str:
        """显示案件类型"""
        return obj.case_types_display  # type: ignore[no-any-return]

    @admin.display(description=_("案件阶段"))
    def case_stage_display(self, obj: DocumentTemplate) -> str:
        """显示案件阶段"""
        stages = obj.case_stages or []
        if not stages:
            return "-"
        stage_label = dict(DocumentCaseStage.choices).get(stages[0], stages[0])
        return str(stage_label)

    @admin.display(description=_("当前文件"))
    def current_file_display(self, obj: DocumentTemplate) -> Any:
        """显示当前文件(只读,不可点击)"""
        if not obj.pk:
            return _("新建模板,请上传文件")
        if obj.file:
            absolute_path = obj.file.path if hasattr(obj.file, "path") else str(obj.file)
            return format_html('<span style="color: #2e7d32;" title="{}">📄 {}</span>', absolute_path, obj.file.name)
        elif obj.file_path:
            return format_html(
                '<span style="color: #1565c0;" title="{}">📁 {}</span>', obj.absolute_file_path, obj.file_path
            )
        return format_html('<span style="color: #c62828;">{}</span>', "⚠️ 未设置文件")

    @admin.display(description=_("文件位置"))
    def file_location_display(self, obj: DocumentTemplate) -> Any:
        """显示文件位置,可点击下载"""
        from django.urls import reverse

        if obj.file:
            download_url = reverse("admin:documents_documenttemplate_download", args=[obj.pk])
            absolute_path = obj.file.path if hasattr(obj.file, "path") else str(obj.file)
            return format_html(
                '<a href="{}" title="点击下载 | 绝对路径: {}" target="_blank">📄 {}</a>',
                download_url,
                absolute_path,
                obj.file.name,
            )
        elif obj.file_path:
            download_url = reverse("admin:documents_documenttemplate_download", args=[obj.pk])
            return format_html(
                '<a href="{}" title="点击下载 | 绝对路径: {}" target="_blank">📁 {}</a>',
                download_url,
                obj.absolute_file_path,
                obj.file_path,
            )
        return format_html('<span style="color: #999;">{}</span>', "未设置")

    @admin.display(description=_("占位符"))
    def placeholder_count_display(self, obj: DocumentTemplate) -> Any:
        """显示占位符数量"""
        try:
            service = _get_template_service()
            placeholders = service.extract_placeholders(obj)
            undefined = service.get_undefined_placeholders(obj)

            all_placeholders_text = ", ".join(placeholders) if placeholders else "无占位符"

            if undefined:
                undefined_names = ", ".join(undefined[:3])
                if len(undefined) > 3:
                    undefined_names += f" 等{len(undefined)}个"

                return format_html(
                    '<span style="color: #e65100;" title="所有占位符: {} | 未定义占位符: {}">'
                    "{} ({}个未定义: {})</span>",
                    all_placeholders_text,
                    ", ".join(undefined),
                    len(placeholders),
                    len(undefined),
                    undefined_names,
                )
            else:
                return format_html('<span title="所有占位符: {}">{}</span>', all_placeholders_text, len(placeholders))
        except Exception as e:
            logger.error("提取占位符失败 - 模板ID: %s, 错误: %s", obj.id, e, exc_info=True)
            return format_html('<span style="color: #c62828;" title="{}">错误</span>', str(e))

    @admin.display(description=_("占位符列表"))
    def placeholders_display(self, obj: DocumentTemplate) -> Any:
        """显示占位符列表"""
        if not obj.pk:
            return _("保存后可查看占位符")

        try:
            service = _get_template_service()
            placeholders = service.extract_placeholders(obj)
            undefined = set(service.get_undefined_placeholders(obj))

            admin_service = _get_admin_service()
            return admin_service.render_placeholders_table(placeholders, undefined)
        except Exception as e:
            logger.exception("操作失败")
            return format_html('<span style="color: #c62828;">提取失败: {}</span>', str(e))

    @admin.display(description=_("未定义占位符"))
    def undefined_placeholders_display(self, obj: DocumentTemplate) -> Any:
        """显示未定义的占位符(高亮警告)"""
        if not obj.pk:
            return _("保存后可查看")

        try:
            service = _get_template_service()
            undefined = service.get_undefined_placeholders(obj)

            admin_service = _get_admin_service()
            return admin_service.render_undefined_placeholders_warning(undefined)
        except Exception as e:
            logger.exception("操作失败")
            return format_html('<span style="color: #c62828;">检查失败: {}</span>', str(e))

    def activate_templates(self, request: Any, queryset: Any) -> None:
        """批量启用模板"""
        service = _get_admin_service()
        updated: int = service.batch_activate(queryset)
        self.message_user(request, _("已启用 %(count)d 个模板") % {"count": updated})

    def deactivate_templates(self, request: Any, queryset: Any) -> None:
        """批量禁用模板"""
        service = _get_admin_service()
        updated: int = service.batch_deactivate(queryset)
        self.message_user(request, _("已禁用 %(count)d 个模板") % {"count": updated})

    @admin.action(description=_("刷新占位符信息"))
    def refresh_placeholders(self, request: Any, queryset: Any) -> None:
        """刷新占位符信息(触发重新解析)"""
        count = queryset.count()
        self.message_user(request, _("已刷新 %(count)d 个模板的占位符信息") % {"count": count})

    @admin.action(description=_("复制选中的模板"))
    def duplicate_templates(self, request: Any, queryset: Any) -> None:
        """批量复制文书模板"""
        admin_service = _get_admin_service()
        count = admin_service.batch_duplicate_templates(queryset)
        self.message_user(request, _("已复制 %(count)d 个模板") % {"count": count})

    def save_model(self, request: Any, obj: DocumentTemplate, form: Any, change: bool) -> None:
        """保存模型时的额外处理"""
        super().save_model(request, obj, form, change)