a7d4b3e21c10_create_contract_center_tables.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """create contract center tables
  2. Revision ID: a7d4b3e21c10
  3. Revises: 984d88f91c0d
  4. Create Date: 2026-03-28 00:00:00.000000
  5. """
  6. from typing import Sequence, Union
  7. from alembic import op
  8. import sqlalchemy as sa
  9. from sqlalchemy.dialects import mysql
  10. revision: str = "a7d4b3e21c10"
  11. down_revision: Union[str, Sequence[str], None] = "984d88f91c0d"
  12. branch_labels: Union[str, Sequence[str], None] = None
  13. depends_on: Union[str, Sequence[str], None] = None
  14. def upgrade() -> None:
  15. op.execute("DROP TABLE IF EXISTS contract_event")
  16. op.execute("DROP TABLE IF EXISTS contract_document")
  17. op.execute("DROP TABLE IF EXISTS contract_bundle")
  18. op.create_table(
  19. "contract_bundle",
  20. sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False, comment="合同包唯一标识"),
  21. sa.Column("subject_type", sa.String(length=20), nullable=False, comment="签约主体类型(store/lawyer)"),
  22. sa.Column("subject_id", sa.BigInteger(), nullable=False, comment="签约主体ID,关联门店/律师业务主键"),
  23. sa.Column("subject_name", sa.String(length=120), nullable=False, comment="签约主体名称"),
  24. sa.Column("business_segment", sa.String(length=100), nullable=False, comment="业务板块"),
  25. sa.Column("contact_name", sa.String(length=100), nullable=False, comment="联系人姓名"),
  26. sa.Column("contact_phone", sa.String(length=20), nullable=False, comment="联系人手机号"),
  27. sa.Column("ord_id", sa.String(length=40), nullable=False, comment="组织标识(统一社会信用代码)"),
  28. sa.Column("bundle_type", sa.String(length=50), nullable=False, comment="合同包类型(STORE_STANDARD/LAWYER_STANDARD)"),
  29. sa.Column("status", sa.String(length=20), nullable=False, server_default="未签署", comment="整体状态: 未签署/审核中/已签署"),
  30. sa.Column("primary_document_id", sa.BigInteger(), nullable=True, comment="主合同文档ID"),
  31. sa.Column("created_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="创建时间"),
  32. sa.Column("updated_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="更新时间"),
  33. sa.Column("delete_flag", sa.Integer(), server_default="0", nullable=False, comment="逻辑删除(0未删/1已删)"),
  34. sa.PrimaryKeyConstraint("id"),
  35. )
  36. op.create_index("idx_contract_bundle_subject", "contract_bundle", ["subject_type", "subject_id"], unique=False)
  37. op.create_index("idx_contract_bundle_status", "contract_bundle", ["status"], unique=False)
  38. op.create_table(
  39. "contract_document",
  40. sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False, comment="合同文档唯一标识"),
  41. sa.Column("bundle_id", sa.BigInteger(), nullable=False, comment="所属合同包ID"),
  42. sa.Column("contract_type", sa.String(length=50), nullable=False, comment="合同类型编码(store_agreement/alipay_auth等)"),
  43. sa.Column("contract_name", sa.String(length=100), nullable=False, comment="合同展示名称"),
  44. sa.Column("is_primary", sa.Integer(), nullable=False, server_default="0", comment="是否主合同(1是/0否)"),
  45. sa.Column("status", sa.Integer(), nullable=False, server_default="0", comment="签署状态(0未签署/1已签署)"),
  46. sa.Column("sign_flow_id", sa.String(length=64), nullable=False, comment="e签宝签署流程ID"),
  47. sa.Column("file_id", sa.String(length=64), nullable=False, comment="e签宝文件ID"),
  48. sa.Column("template_url", mysql.LONGTEXT(), nullable=False, comment="合同模板预览URL"),
  49. sa.Column("sign_url", mysql.LONGTEXT(), nullable=False, comment="签署链接URL"),
  50. sa.Column("download_url", mysql.LONGTEXT(), nullable=False, comment="签署完成后的下载URL"),
  51. sa.Column("signing_time", sa.DateTime(), nullable=True, comment="签署时间"),
  52. sa.Column("effective_time", sa.DateTime(), nullable=True, comment="合同生效时间"),
  53. sa.Column("expiry_time", sa.DateTime(), nullable=True, comment="合同到期时间"),
  54. sa.Column("created_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="创建时间"),
  55. sa.Column("updated_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="更新时间"),
  56. sa.Column("delete_flag", sa.Integer(), server_default="0", nullable=False, comment="逻辑删除(0未删/1已删)"),
  57. sa.ForeignKeyConstraint(["bundle_id"], ["contract_bundle.id"]),
  58. sa.PrimaryKeyConstraint("id"),
  59. )
  60. op.create_index("idx_contract_document_bundle_id", "contract_document", ["bundle_id"], unique=False)
  61. op.create_index("idx_contract_document_sign_flow_id", "contract_document", ["sign_flow_id"], unique=True)
  62. op.create_index("idx_contract_document_bundle_type_unique", "contract_document", ["bundle_id", "contract_type"], unique=True)
  63. op.create_table(
  64. "contract_event",
  65. sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False, comment="事件唯一标识"),
  66. sa.Column("bundle_id", sa.BigInteger(), nullable=True, comment="关联合同包ID"),
  67. sa.Column("document_id", sa.BigInteger(), nullable=True, comment="关联合同文档ID"),
  68. sa.Column("sign_flow_id", sa.String(length=64), nullable=False, comment="e签宝签署流程ID"),
  69. sa.Column("event_type", sa.String(length=50), nullable=False, comment="事件类型(esign_callback:{action})"),
  70. sa.Column("payload_json", mysql.LONGTEXT(), nullable=False, comment="e签宝回调原始JSON报文"),
  71. sa.Column("created_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="创建时间"),
  72. sa.Column("updated_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False, comment="更新时间"),
  73. sa.Column("delete_flag", sa.Integer(), server_default="0", nullable=False, comment="逻辑删除(0未删/1已删)"),
  74. sa.PrimaryKeyConstraint("id"),
  75. )
  76. op.create_index("idx_contract_event_sign_flow_id", "contract_event", ["sign_flow_id"], unique=False)
  77. def downgrade() -> None:
  78. op.drop_index("idx_contract_event_sign_flow_id", table_name="contract_event")
  79. op.drop_table("contract_event")
  80. op.drop_index("idx_contract_document_bundle_type_unique", table_name="contract_document")
  81. op.drop_index("idx_contract_document_sign_flow_id", table_name="contract_document")
  82. op.drop_index("idx_contract_document_bundle_id", table_name="contract_document")
  83. op.drop_table("contract_document")
  84. op.drop_index("idx_contract_bundle_status", table_name="contract_bundle")
  85. op.drop_index("idx_contract_bundle_subject", table_name="contract_bundle")
  86. op.drop_table("contract_bundle")