SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS admin_role_permissions;
DROP TABLE IF EXISTS admin_permissions;
DROP TABLE IF EXISTS admin_role_relations;
DROP TABLE IF EXISTS admin_roles;
DROP TABLE IF EXISTS admin_api_tokens;
DROP TABLE IF EXISTS enterprise_webhook_deliveries;
DROP TABLE IF EXISTS enterprise_order_events;
DROP TABLE IF EXISTS enterprise_customer_order_refs;
DROP TABLE IF EXISTS enterprise_api_nonces;
DROP TABLE IF EXISTS enterprise_customer_apps;
DROP TABLE IF EXISTS enterprise_customers;
DROP TABLE IF EXISTS kuaidi100_express_company_catalog;
DROP TABLE IF EXISTS express_companies;
DROP TABLE IF EXISTS shipping_warehouses;
DROP TABLE IF EXISTS user_api_tokens;
DROP TABLE IF EXISTS sms_code_logs;
DROP TABLE IF EXISTS operation_logs;
DROP TABLE IF EXISTS material_tag_scan_logs;
DROP TABLE IF EXISTS material_batch_download_logs;
DROP TABLE IF EXISTS material_tag_codes;
DROP TABLE IF EXISTS material_batches;
DROP TABLE IF EXISTS system_configs;
DROP TABLE IF EXISTS service_packages;
DROP TABLE IF EXISTS admin_users;
DROP TABLE IF EXISTS user_messages;
DROP TABLE IF EXISTS message_logs;
DROP TABLE IF EXISTS message_rules;
DROP TABLE IF EXISTS message_templates;
DROP TABLE IF EXISTS help_articles;
DROP TABLE IF EXISTS ticket_messages;
DROP TABLE IF EXISTS tickets;
DROP TABLE IF EXISTS report_logs;
DROP TABLE IF EXISTS report_verify_logs;
DROP TABLE IF EXISTS report_verifies;
DROP TABLE IF EXISTS report_files;
DROP TABLE IF EXISTS report_contents;
DROP TABLE IF EXISTS reports;
DROP TABLE IF EXISTS appraisal_task_logs;
DROP TABLE IF EXISTS appraisal_task_reviews;
DROP TABLE IF EXISTS appraisal_task_key_points;
DROP TABLE IF EXISTS appraisal_task_results;
DROP TABLE IF EXISTS appraisal_tasks;
DROP TABLE IF EXISTS order_abnormals;
DROP TABLE IF EXISTS order_transfer_flow_logs;
DROP TABLE IF EXISTS order_transfer_flows;
DROP TABLE IF EXISTS internal_transfer_tags;
DROP TABLE IF EXISTS internal_transfer_tag_batches;
DROP TABLE IF EXISTS order_logistics_syncs;
DROP TABLE IF EXISTS order_logistics_nodes;
DROP TABLE IF EXISTS order_logistics;
DROP TABLE IF EXISTS order_supplement_task_items;
DROP TABLE IF EXISTS order_supplement_tasks;
DROP TABLE IF EXISTS order_assignments;
DROP TABLE IF EXISTS order_timelines;
DROP TABLE IF EXISTS order_upload_files;
DROP TABLE IF EXISTS order_upload_items;
DROP TABLE IF EXISTS order_return_addresses;
DROP TABLE IF EXISTS order_shipping_targets;
DROP TABLE IF EXISTS order_extras;
DROP TABLE IF EXISTS order_products;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS appraisal_draft_upload_files;
DROP TABLE IF EXISTS appraisal_draft_uploads;
DROP TABLE IF EXISTS appraisal_draft_extras;
DROP TABLE IF EXISTS appraisal_draft_products;
DROP TABLE IF EXISTS appraisal_drafts;
DROP TABLE IF EXISTS appraisal_template_key_points;
DROP TABLE IF EXISTS appraisal_templates;
DROP TABLE IF EXISTS upload_template_items;
DROP TABLE IF EXISTS upload_templates;
DROP TABLE IF EXISTS catalog_attribute_scopes;
DROP TABLE IF EXISTS catalog_attribute_fields;
DROP TABLE IF EXISTS catalog_models;
DROP TABLE IF EXISTS catalog_series;
DROP TABLE IF EXISTS catalog_brand_categories;
DROP TABLE IF EXISTS catalog_brands;
DROP TABLE IF EXISTS catalog_categories;
DROP TABLE IF EXISTS user_addresses;
DROP TABLE IF EXISTS user_auths;
DROP TABLE IF EXISTS users;

CREATE TABLE users (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  nickname VARCHAR(64) NOT NULL DEFAULT '',
  avatar VARCHAR(255) NOT NULL DEFAULT '',
  mobile VARCHAR(32) NOT NULL DEFAULT '',
  password VARCHAR(255) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  last_login_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  deleted_at DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_users_mobile (mobile),
  KEY idx_users_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户主表';

CREATE TABLE user_auths (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  auth_type VARCHAR(32) NOT NULL,
  auth_open_id VARCHAR(128) NOT NULL DEFAULT '',
  auth_union_id VARCHAR(128) NOT NULL DEFAULT '',
  auth_key VARCHAR(128) NOT NULL DEFAULT '',
  auth_extra JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_auths_type_key (auth_type, auth_key),
  KEY idx_user_auths_user_id (user_id),
  KEY idx_user_auths_auth_union_id (auth_type, auth_union_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户认证映射';

CREATE TABLE user_api_tokens (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  token_hash VARCHAR(64) NOT NULL,
  auth_type VARCHAR(32) NOT NULL DEFAULT 'password',
  expire_time DATETIME NOT NULL,
  last_active_at DATETIME NULL DEFAULT NULL,
  last_ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_api_tokens_token_hash (token_hash),
  KEY idx_user_api_tokens_user_id (user_id),
  KEY idx_user_api_tokens_expire_time (expire_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户登录Token';

CREATE TABLE sms_code_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  mobile VARCHAR(32) NOT NULL,
  scene VARCHAR(32) NOT NULL DEFAULT 'login',
  code_hash VARCHAR(64) NOT NULL,
  send_status VARCHAR(32) NOT NULL DEFAULT 'success',
  provider VARCHAR(32) NOT NULL DEFAULT 'aliyun_sms',
  template_code VARCHAR(64) NOT NULL DEFAULT '',
  request_id VARCHAR(128) NOT NULL DEFAULT '',
  biz_id VARCHAR(128) NOT NULL DEFAULT '',
  failed_reason VARCHAR(255) NOT NULL DEFAULT '',
  expire_time DATETIME NOT NULL,
  used_at DATETIME NULL DEFAULT NULL,
  send_ip VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_sms_code_logs_mobile_scene (mobile, scene),
  KEY idx_sms_code_logs_expire_time (expire_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='短信验证码发送记录';

CREATE TABLE enterprise_customers (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_code VARCHAR(64) NOT NULL,
  customer_name VARCHAR(128) NOT NULL DEFAULT '',
  contact_name VARCHAR(64) NOT NULL DEFAULT '',
  contact_mobile VARCHAR(32) NOT NULL DEFAULT '',
  contact_email VARCHAR(128) NOT NULL DEFAULT '',
  settlement_type VARCHAR(32) NOT NULL DEFAULT 'monthly',
  user_id BIGINT UNSIGNED NULL DEFAULT NULL,
  webhook_url VARCHAR(500) NOT NULL DEFAULT '',
  webhook_enabled TINYINT(1) NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  remark VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_enterprise_customers_code (customer_code),
  KEY idx_enterprise_customers_status (status),
  KEY idx_enterprise_customers_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='大客户资料';

CREATE TABLE enterprise_customer_apps (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id BIGINT UNSIGNED NOT NULL,
  app_name VARCHAR(128) NOT NULL DEFAULT '',
  app_key VARCHAR(64) NOT NULL,
  app_secret_cipher TEXT NULL,
  secret_last4 VARCHAR(8) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  last_used_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_enterprise_customer_apps_key (app_key),
  KEY idx_enterprise_customer_apps_customer_id (customer_id),
  KEY idx_enterprise_customer_apps_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='大客户开放接口应用';

CREATE TABLE enterprise_api_nonces (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  app_key VARCHAR(64) NOT NULL,
  nonce VARCHAR(128) NOT NULL,
  request_timestamp BIGINT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_enterprise_api_nonces_key_nonce (app_key, nonce),
  KEY idx_enterprise_api_nonces_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='开放接口防重放Nonce';

CREATE TABLE enterprise_customer_order_refs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id BIGINT UNSIGNED NOT NULL,
  external_order_no VARCHAR(128) NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  order_no VARCHAR(64) NOT NULL DEFAULT '',
  appraisal_no VARCHAR(64) NOT NULL DEFAULT '',
  payload_hash VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_enterprise_customer_order_refs_external (customer_id, external_order_no),
  UNIQUE KEY uk_enterprise_customer_order_refs_order_id (order_id),
  KEY idx_enterprise_customer_order_refs_order_no (order_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='大客户外部订单映射';

CREATE TABLE enterprise_order_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  external_order_no VARCHAR(128) NOT NULL DEFAULT '',
  event_code VARCHAR(64) NOT NULL,
  event_text VARCHAR(128) NOT NULL DEFAULT '',
  status_code VARCHAR(64) NOT NULL DEFAULT '',
  status_text VARCHAR(128) NOT NULL DEFAULT '',
  payload_json JSON NULL,
  occurred_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_enterprise_order_events_customer_id (customer_id),
  KEY idx_enterprise_order_events_order_id (order_id),
  KEY idx_enterprise_order_events_event_code (event_code),
  KEY idx_enterprise_order_events_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='大客户订单事件';

CREATE TABLE enterprise_webhook_deliveries (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  event_id BIGINT UNSIGNED NOT NULL,
  customer_id BIGINT UNSIGNED NOT NULL,
  webhook_url VARCHAR(500) NOT NULL DEFAULT '',
  app_key VARCHAR(64) NOT NULL DEFAULT '',
  attempt_no INT NOT NULL DEFAULT 1,
  delivery_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  http_status INT NOT NULL DEFAULT 0,
  response_body TEXT NULL,
  error_message VARCHAR(500) NOT NULL DEFAULT '',
  is_manual TINYINT(1) NOT NULL DEFAULT 0,
  sent_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_enterprise_webhook_deliveries_event_id (event_id),
  KEY idx_enterprise_webhook_deliveries_customer_id (customer_id),
  KEY idx_enterprise_webhook_deliveries_status (delivery_status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='大客户Webhook推送记录';

CREATE TABLE user_addresses (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  consignee VARCHAR(64) NOT NULL DEFAULT '',
  mobile VARCHAR(32) NOT NULL DEFAULT '',
  province VARCHAR(64) NOT NULL DEFAULT '',
  city VARCHAR(64) NOT NULL DEFAULT '',
  district VARCHAR(64) NOT NULL DEFAULT '',
  detail_address VARCHAR(255) NOT NULL DEFAULT '',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_user_addresses_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户地址';

CREATE TABLE shipping_warehouses (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  warehouse_name VARCHAR(128) NOT NULL DEFAULT '',
  warehouse_code VARCHAR(64) NOT NULL DEFAULT '',
  warehouse_type VARCHAR(32) NOT NULL DEFAULT 'detection_center',
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  receiver_name VARCHAR(64) NOT NULL DEFAULT '',
  receiver_mobile VARCHAR(32) NOT NULL DEFAULT '',
  province VARCHAR(64) NOT NULL DEFAULT '',
  city VARCHAR(64) NOT NULL DEFAULT '',
  district VARCHAR(64) NOT NULL DEFAULT '',
  detail_address VARCHAR(255) NOT NULL DEFAULT '',
  service_time VARCHAR(128) NOT NULL DEFAULT '',
  notice VARCHAR(500) NOT NULL DEFAULT '',
  supported_category_ids_json JSON NULL,
  service_area_provinces_json JSON NULL,
  service_area_cities_json JSON NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_shipping_warehouses_code (warehouse_code),
  KEY idx_shipping_warehouses_service_provider (service_provider),
  KEY idx_shipping_warehouses_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收货仓库 / 检测中心';

CREATE TABLE catalog_categories (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL,
  code VARCHAR(64) NOT NULL,
  icon VARCHAR(255) NOT NULL DEFAULT '',
  sort_order INT NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  need_shipping TINYINT(1) NOT NULL DEFAULT 1,
  supported_service_types JSON NULL,
  default_upload_template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  default_appraisal_template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_catalog_categories_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='品类';

CREATE TABLE catalog_brands (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(128) NOT NULL,
  en_name VARCHAR(128) NOT NULL DEFAULT '',
  code VARCHAR(64) NOT NULL,
  logo VARCHAR(255) NOT NULL DEFAULT '',
  sort_order INT NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  supported_service_types JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_catalog_brands_code (code),
  KEY idx_catalog_brands_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='品牌';

CREATE TABLE catalog_brand_categories (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  brand_id BIGINT UNSIGNED NOT NULL,
  category_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_catalog_brand_categories (brand_id, category_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='品牌品类关联';

CREATE TABLE catalog_attribute_fields (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL,
  code VARCHAR(64) NOT NULL,
  field_type VARCHAR(32) NOT NULL,
  options_json JSON NULL,
  is_required TINYINT(1) NOT NULL DEFAULT 0,
  is_front_visible TINYINT(1) NOT NULL DEFAULT 1,
  is_report_visible TINYINT(1) NOT NULL DEFAULT 0,
  is_admin_visible TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_catalog_attribute_fields_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='动态字段定义';

CREATE TABLE catalog_attribute_scopes (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  field_id BIGINT UNSIGNED NOT NULL,
  category_id BIGINT UNSIGNED NULL DEFAULT NULL,
  brand_id BIGINT UNSIGNED NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_catalog_attribute_scopes_field_id (field_id),
  KEY idx_catalog_attribute_scopes_scope (category_id, brand_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='动态字段适用范围';

CREATE TABLE upload_templates (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(128) NOT NULL,
  code VARCHAR(64) NOT NULL,
  scope_type VARCHAR(32) NOT NULL,
  scope_id BIGINT UNSIGNED NOT NULL,
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_upload_templates_code (code),
  KEY idx_upload_templates_scope (scope_type, scope_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='上传模板';

CREATE TABLE upload_template_items (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  template_id BIGINT UNSIGNED NOT NULL,
  item_code VARCHAR(64) NOT NULL,
  item_name VARCHAR(128) NOT NULL,
  is_required TINYINT(1) NOT NULL DEFAULT 1,
  guide_text VARCHAR(255) NOT NULL DEFAULT '',
  sample_image_url VARCHAR(255) NOT NULL DEFAULT '',
  max_upload_count INT NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_upload_template_items_template_id (template_id),
  KEY idx_upload_template_items_item_code (item_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='上传模板项';

CREATE TABLE appraisal_templates (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(128) NOT NULL,
  code VARCHAR(64) NOT NULL,
  scope_type VARCHAR(32) NOT NULL,
  scope_id BIGINT UNSIGNED NOT NULL,
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  result_options_json JSON NULL,
  condition_rule_json JSON NULL,
  valuation_rule_json JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_appraisal_templates_code (code),
  KEY idx_appraisal_templates_scope (scope_type, scope_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定模板';

CREATE TABLE appraisal_template_key_points (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  template_id BIGINT UNSIGNED NOT NULL,
  point_code VARCHAR(64) NOT NULL,
  point_name VARCHAR(128) NOT NULL,
  point_type VARCHAR(32) NOT NULL DEFAULT 'text',
  options_json JSON NULL,
  sort_order INT NOT NULL DEFAULT 0,
  is_required TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_template_key_points_template_id (template_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定模板关键点';

CREATE TABLE appraisal_drafts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  service_mode VARCHAR(32) NOT NULL DEFAULT 'physical',
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  current_step INT NOT NULL DEFAULT 1,
  status VARCHAR(32) NOT NULL DEFAULT 'draft',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_drafts_user_id (user_id),
  KEY idx_appraisal_drafts_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定草稿';

CREATE TABLE appraisal_draft_products (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  draft_id BIGINT UNSIGNED NOT NULL,
  category_id BIGINT UNSIGNED NULL DEFAULT NULL,
  brand_id BIGINT UNSIGNED NULL DEFAULT NULL,
  brand_name VARCHAR(128) NOT NULL DEFAULT '',
  color VARCHAR(64) NOT NULL DEFAULT '',
  size_spec VARCHAR(64) NOT NULL DEFAULT '',
  serial_no VARCHAR(128) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_appraisal_draft_products_draft_id (draft_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草稿商品信息';

CREATE TABLE appraisal_draft_extras (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  draft_id BIGINT UNSIGNED NOT NULL,
  purchase_channel VARCHAR(64) NOT NULL DEFAULT '',
  purchase_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  purchase_date DATE NULL DEFAULT NULL,
  usage_status VARCHAR(32) NOT NULL DEFAULT '',
  condition_desc VARCHAR(255) NOT NULL DEFAULT '',
  has_accessories TINYINT(1) NOT NULL DEFAULT 0,
  accessories_json JSON NULL,
  remark VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_appraisal_draft_extras_draft_id (draft_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草稿补充信息';

CREATE TABLE appraisal_draft_uploads (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  draft_id BIGINT UNSIGNED NOT NULL,
  template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  item_code VARCHAR(64) NOT NULL,
  item_name VARCHAR(128) NOT NULL,
  is_required TINYINT(1) NOT NULL DEFAULT 1,
  quality_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  quality_message VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_draft_uploads_draft_id (draft_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草稿上传任务项';

CREATE TABLE appraisal_draft_upload_files (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  draft_upload_id BIGINT UNSIGNED NOT NULL,
  file_id VARCHAR(64) NOT NULL DEFAULT '',
  file_url VARCHAR(255) NOT NULL DEFAULT '',
  thumbnail_url VARCHAR(255) NOT NULL DEFAULT '',
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_draft_upload_files_draft_upload_id (draft_upload_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='草稿上传文件';

CREATE TABLE orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_no VARCHAR(64) NOT NULL,
  appraisal_no VARCHAR(64) NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  service_mode VARCHAR(32) NOT NULL DEFAULT 'physical',
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  payment_status VARCHAR(32) NOT NULL DEFAULT 'unpaid',
  order_status VARCHAR(32) NOT NULL DEFAULT 'pending_payment',
  display_status VARCHAR(64) NOT NULL DEFAULT '待支付',
  estimated_finish_time DATETIME NULL DEFAULT NULL,
  source_channel VARCHAR(32) NOT NULL DEFAULT 'mini_program',
  source_customer_id VARCHAR(64) NOT NULL DEFAULT '',
  pay_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  paid_at DATETIME NULL DEFAULT NULL,
  cancelled_at DATETIME NULL DEFAULT NULL,
  completed_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_orders_order_no (order_no),
  UNIQUE KEY uk_orders_appraisal_no (appraisal_no),
  KEY idx_orders_user_id (user_id),
  KEY idx_orders_order_status (order_status),
  KEY idx_orders_service_provider (service_provider),
  KEY idx_orders_source_channel (source_channel),
  KEY idx_orders_source_customer_id (source_customer_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单主表';

CREATE TABLE order_products (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  category_id BIGINT UNSIGNED NULL DEFAULT NULL,
  category_name VARCHAR(64) NOT NULL DEFAULT '',
  brand_id BIGINT UNSIGNED NULL DEFAULT NULL,
  brand_name VARCHAR(128) NOT NULL DEFAULT '',
  color VARCHAR(64) NOT NULL DEFAULT '',
  size_spec VARCHAR(64) NOT NULL DEFAULT '',
  serial_no VARCHAR(128) NOT NULL DEFAULT '',
  product_name VARCHAR(255) NOT NULL DEFAULT '',
  product_cover VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_products_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单商品快照';

CREATE TABLE order_extras (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  purchase_channel VARCHAR(64) NOT NULL DEFAULT '',
  purchase_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  purchase_date DATE NULL DEFAULT NULL,
  usage_status VARCHAR(32) NOT NULL DEFAULT '',
  condition_desc VARCHAR(255) NOT NULL DEFAULT '',
  has_accessories TINYINT(1) NOT NULL DEFAULT 0,
  accessories_json JSON NULL,
  remark VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_extras_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单补充信息';

CREATE TABLE order_shipping_targets (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  warehouse_id BIGINT UNSIGNED NULL DEFAULT NULL,
  warehouse_name VARCHAR(128) NOT NULL DEFAULT '',
  warehouse_code VARCHAR(64) NOT NULL DEFAULT '',
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  receiver_name VARCHAR(64) NOT NULL DEFAULT '',
  receiver_mobile VARCHAR(32) NOT NULL DEFAULT '',
  province VARCHAR(64) NOT NULL DEFAULT '',
  city VARCHAR(64) NOT NULL DEFAULT '',
  district VARCHAR(64) NOT NULL DEFAULT '',
  detail_address VARCHAR(255) NOT NULL DEFAULT '',
  service_time VARCHAR(128) NOT NULL DEFAULT '',
  notice VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_shipping_targets_order_id (order_id),
  KEY idx_order_shipping_targets_warehouse_id (warehouse_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单锁定仓库快照';

CREATE TABLE order_return_addresses (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  user_address_id BIGINT UNSIGNED NULL DEFAULT NULL,
  consignee VARCHAR(64) NOT NULL DEFAULT '',
  mobile VARCHAR(32) NOT NULL DEFAULT '',
  province VARCHAR(64) NOT NULL DEFAULT '',
  city VARCHAR(64) NOT NULL DEFAULT '',
  district VARCHAR(64) NOT NULL DEFAULT '',
  detail_address VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_return_addresses_order_id (order_id),
  KEY idx_order_return_addresses_user_address_id (user_address_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单寄回地址快照';

CREATE TABLE order_upload_items (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  item_code VARCHAR(64) NOT NULL,
  item_name VARCHAR(128) NOT NULL,
  is_required TINYINT(1) NOT NULL DEFAULT 1,
  source_type VARCHAR(32) NOT NULL DEFAULT 'initial',
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_upload_items_order_id (order_id),
  KEY idx_order_upload_items_item_code (item_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单资料任务项';

CREATE TABLE order_upload_files (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_upload_item_id BIGINT UNSIGNED NOT NULL,
  file_id VARCHAR(64) NOT NULL DEFAULT '',
  file_url VARCHAR(255) NOT NULL DEFAULT '',
  thumbnail_url VARCHAR(255) NOT NULL DEFAULT '',
  quality_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  quality_message VARCHAR(255) NOT NULL DEFAULT '',
  uploaded_by_user_id BIGINT UNSIGNED NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_upload_files_item_id (order_upload_item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单资料文件';

CREATE TABLE order_timelines (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  node_code VARCHAR(64) NOT NULL,
  node_text VARCHAR(128) NOT NULL,
  node_desc VARCHAR(255) NOT NULL DEFAULT '',
  operator_type VARCHAR(32) NOT NULL DEFAULT 'system',
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  occurred_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_timelines_order_id (order_id),
  KEY idx_order_timelines_node_code (node_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单时间轴';

CREATE TABLE order_assignments (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  task_stage VARCHAR(32) NOT NULL,
  assignee_id BIGINT UNSIGNED NOT NULL,
  assignee_name VARCHAR(64) NOT NULL DEFAULT '',
  assigned_by BIGINT UNSIGNED NULL DEFAULT NULL,
  assigned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_assignments_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单分配记录';

CREATE TABLE order_supplement_tasks (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  reason VARCHAR(255) NOT NULL DEFAULT '',
  deadline DATETIME NULL DEFAULT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  created_by BIGINT UNSIGNED NULL DEFAULT NULL,
  submitted_at DATETIME NULL DEFAULT NULL,
  approved_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_supplement_tasks_order_id (order_id),
  KEY idx_order_supplement_tasks_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='补图任务';

CREATE TABLE order_supplement_task_items (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  task_id BIGINT UNSIGNED NOT NULL,
  item_code VARCHAR(64) NOT NULL,
  item_name VARCHAR(128) NOT NULL,
  guide_text VARCHAR(255) NOT NULL DEFAULT '',
  sample_image_url VARCHAR(255) NOT NULL DEFAULT '',
  is_required TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_supplement_task_items_task_id (task_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='补图任务项';

CREATE TABLE order_logistics (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  logistics_type VARCHAR(32) NOT NULL DEFAULT 'send_to_center',
  express_company VARCHAR(64) NOT NULL DEFAULT '',
  tracking_no VARCHAR(64) NOT NULL DEFAULT '',
  tracking_status VARCHAR(32) NOT NULL DEFAULT '',
  latest_desc VARCHAR(255) NOT NULL DEFAULT '',
  latest_time DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_logistics_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物流主表';

CREATE TABLE order_logistics_nodes (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  logistics_id BIGINT UNSIGNED NOT NULL,
  node_time DATETIME NOT NULL,
  node_desc VARCHAR(255) NOT NULL DEFAULT '',
  node_location VARCHAR(128) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_logistics_nodes_logistics_id (logistics_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物流节点';

CREATE TABLE order_logistics_syncs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  logistics_id BIGINT UNSIGNED NOT NULL,
  provider VARCHAR(32) NOT NULL DEFAULT 'kuaidi100',
  provider_com VARCHAR(64) NOT NULL DEFAULT '',
  subscription_status VARCHAR(32) NOT NULL DEFAULT '',
  provider_state VARCHAR(32) NOT NULL DEFAULT '',
  provider_status_text VARCHAR(64) NOT NULL DEFAULT '',
  last_query_at DATETIME NULL DEFAULT NULL,
  last_push_at DATETIME NULL DEFAULT NULL,
  last_error VARCHAR(500) NOT NULL DEFAULT '',
  raw_status VARCHAR(32) NOT NULL DEFAULT '',
  raw_summary LONGTEXT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_logistics_syncs_provider (logistics_id, provider),
  KEY idx_order_logistics_syncs_provider_com (provider, provider_com),
  KEY idx_order_logistics_syncs_last_query (last_query_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单物流第三方同步状态';

CREATE TABLE express_companies (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  company_name VARCHAR(64) NOT NULL DEFAULT '',
  company_code VARCHAR(64) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_express_companies_name (company_name),
  UNIQUE KEY uk_express_companies_code (company_code),
  KEY idx_express_companies_status (status),
  KEY idx_express_companies_default (is_default)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='快递公司字典';

CREATE TABLE kuaidi100_express_company_catalog (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  company_name VARCHAR(64) NOT NULL DEFAULT '',
  company_code VARCHAR(64) NOT NULL DEFAULT '',
  company_type VARCHAR(64) NOT NULL DEFAULT '',
  sort_order INT NOT NULL DEFAULT 0,
  synced_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_kuaidi100_express_company_catalog_code (company_code),
  KEY idx_kuaidi100_express_company_catalog_name (company_name),
  KEY idx_kuaidi100_express_company_catalog_type (company_type),
  KEY idx_kuaidi100_express_company_catalog_synced_at (synced_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='快递100官方公司码表';

CREATE TABLE order_abnormals (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  abnormal_type VARCHAR(64) NOT NULL,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  owner_id BIGINT UNSIGNED NULL DEFAULT NULL,
  resolved_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_abnormals_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='异常订单';

CREATE TABLE internal_transfer_tag_batches (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  batch_no VARCHAR(64) NOT NULL,
  total_count INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  remark VARCHAR(500) NOT NULL DEFAULT '',
  created_by BIGINT UNSIGNED NULL DEFAULT NULL,
  created_by_name VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_internal_transfer_tag_batches_no (batch_no),
  KEY idx_internal_transfer_tag_batches_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='内部流转挂牌批次';

CREATE TABLE internal_transfer_tags (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  batch_id BIGINT UNSIGNED NULL DEFAULT NULL,
  tag_no VARCHAR(80) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  bind_status VARCHAR(32) NOT NULL DEFAULT 'free',
  current_order_id BIGINT UNSIGNED NULL DEFAULT NULL,
  current_flow_id BIGINT UNSIGNED NULL DEFAULT NULL,
  current_stage VARCHAR(64) NOT NULL DEFAULT 'idle',
  current_location VARCHAR(64) NOT NULL DEFAULT 'warehouse',
  bound_by BIGINT UNSIGNED NULL DEFAULT NULL,
  bound_by_name VARCHAR(64) NOT NULL DEFAULT '',
  bound_at DATETIME NULL DEFAULT NULL,
  released_by BIGINT UNSIGNED NULL DEFAULT NULL,
  released_by_name VARCHAR(64) NOT NULL DEFAULT '',
  released_at DATETIME NULL DEFAULT NULL,
  created_by BIGINT UNSIGNED NULL DEFAULT NULL,
  created_by_name VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_internal_transfer_tags_no (tag_no),
  KEY idx_internal_transfer_tags_batch_id (batch_id),
  KEY idx_internal_transfer_tags_bind_status (bind_status),
  KEY idx_internal_transfer_tags_current_order_id (current_order_id),
  KEY idx_internal_transfer_tags_current_stage (current_stage)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='内部流转挂牌';

CREATE TABLE order_transfer_flows (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  internal_tag_id BIGINT UNSIGNED NOT NULL,
  internal_tag_no VARCHAR(80) NOT NULL,
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  flow_status VARCHAR(32) NOT NULL DEFAULT 'active',
  current_stage VARCHAR(64) NOT NULL DEFAULT 'warehouse_received',
  current_location VARCHAR(64) NOT NULL DEFAULT 'warehouse_pending_inspection',
  inbound_by BIGINT UNSIGNED NULL DEFAULT NULL,
  inbound_by_name VARCHAR(64) NOT NULL DEFAULT '',
  inbound_at DATETIME NULL DEFAULT NULL,
  zhongjian_outbound_by BIGINT UNSIGNED NULL DEFAULT NULL,
  zhongjian_outbound_by_name VARCHAR(64) NOT NULL DEFAULT '',
  zhongjian_outbound_at DATETIME NULL DEFAULT NULL,
  zhongjian_inbound_by BIGINT UNSIGNED NULL DEFAULT NULL,
  zhongjian_inbound_by_name VARCHAR(64) NOT NULL DEFAULT '',
  zhongjian_inbound_at DATETIME NULL DEFAULT NULL,
  appraisal_started_by BIGINT UNSIGNED NULL DEFAULT NULL,
  appraisal_started_by_name VARCHAR(64) NOT NULL DEFAULT '',
  appraisal_started_at DATETIME NULL DEFAULT NULL,
  report_published_by BIGINT UNSIGNED NULL DEFAULT NULL,
  report_published_by_name VARCHAR(64) NOT NULL DEFAULT '',
  report_published_at DATETIME NULL DEFAULT NULL,
  return_confirmed_by BIGINT UNSIGNED NULL DEFAULT NULL,
  return_confirmed_by_name VARCHAR(64) NOT NULL DEFAULT '',
  return_confirmed_at DATETIME NULL DEFAULT NULL,
  return_shipped_by BIGINT UNSIGNED NULL DEFAULT NULL,
  return_shipped_by_name VARCHAR(64) NOT NULL DEFAULT '',
  return_shipped_at DATETIME NULL DEFAULT NULL,
  ended_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_transfer_flows_order_id (order_id),
  KEY idx_order_transfer_flows_tag_id (internal_tag_id),
  KEY idx_order_transfer_flows_tag_no (internal_tag_no),
  KEY idx_order_transfer_flows_status_stage (flow_status, current_stage)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单内部流转';

CREATE TABLE order_transfer_flow_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  flow_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  internal_tag_id BIGINT UNSIGNED NOT NULL,
  internal_tag_no VARCHAR(80) NOT NULL,
  action_code VARCHAR(64) NOT NULL,
  action_text VARCHAR(128) NOT NULL DEFAULT '',
  before_stage VARCHAR(64) NOT NULL DEFAULT '',
  before_location VARCHAR(64) NOT NULL DEFAULT '',
  after_stage VARCHAR(64) NOT NULL DEFAULT '',
  after_location VARCHAR(64) NOT NULL DEFAULT '',
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  remark VARCHAR(500) NOT NULL DEFAULT '',
  payload_json JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_order_transfer_flow_logs_flow_id (flow_id),
  KEY idx_order_transfer_flow_logs_order_id (order_id),
  KEY idx_order_transfer_flow_logs_tag_no (internal_tag_no),
  KEY idx_order_transfer_flow_logs_action_code (action_code),
  KEY idx_order_transfer_flow_logs_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单内部流转日志';

CREATE TABLE appraisal_tasks (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  task_stage VARCHAR(32) NOT NULL,
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  assignee_id BIGINT UNSIGNED NULL DEFAULT NULL,
  assignee_name VARCHAR(64) NOT NULL DEFAULT '',
  started_at DATETIME NULL DEFAULT NULL,
  submitted_at DATETIME NULL DEFAULT NULL,
  sla_deadline DATETIME NULL DEFAULT NULL,
  is_overtime TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_tasks_order_id (order_id),
  KEY idx_appraisal_tasks_stage_status (task_stage, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定任务';

CREATE TABLE appraisal_task_results (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  task_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  result_status VARCHAR(32) NOT NULL DEFAULT '',
  result_text VARCHAR(64) NOT NULL DEFAULT '',
  result_desc VARCHAR(255) NOT NULL DEFAULT '',
  condition_grade VARCHAR(16) NOT NULL DEFAULT '',
  condition_desc VARCHAR(255) NOT NULL DEFAULT '',
  valuation_min DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  valuation_max DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  valuation_desc VARCHAR(255) NOT NULL DEFAULT '',
  attachments_json JSON NULL,
  internal_remark TEXT NULL,
  external_remark TEXT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_appraisal_task_results_task_id (task_id),
  KEY idx_appraisal_task_results_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定结果';

CREATE TABLE appraisal_task_key_points (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  task_result_id BIGINT UNSIGNED NOT NULL,
  point_code VARCHAR(64) NOT NULL,
  point_name VARCHAR(128) NOT NULL,
  point_value VARCHAR(255) NOT NULL DEFAULT '',
  point_remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_task_key_points_result_id (task_result_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='鉴定关键点记录';

CREATE TABLE appraisal_task_reviews (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  task_id BIGINT UNSIGNED NOT NULL,
  reviewer_id BIGINT UNSIGNED NOT NULL,
  reviewer_name VARCHAR(64) NOT NULL DEFAULT '',
  review_action VARCHAR(32) NOT NULL,
  review_opinion VARCHAR(255) NOT NULL DEFAULT '',
  reviewed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_task_reviews_task_id (task_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='复核记录';

CREATE TABLE appraisal_task_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  task_id BIGINT UNSIGNED NOT NULL,
  action VARCHAR(64) NOT NULL,
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  before_data JSON NULL,
  after_data JSON NULL,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_appraisal_task_logs_task_id (task_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='作业日志';

CREATE TABLE reports (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_no VARCHAR(64) NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  appraisal_no VARCHAR(64) NOT NULL,
  report_type VARCHAR(32) NOT NULL DEFAULT 'appraisal',
  service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan',
  institution_name VARCHAR(128) NOT NULL DEFAULT '',
  report_title VARCHAR(128) NOT NULL DEFAULT '',
  report_status VARCHAR(32) NOT NULL DEFAULT 'draft',
  report_version INT NOT NULL DEFAULT 1,
  source_report_id BIGINT UNSIGNED NULL DEFAULT NULL,
  publish_time DATETIME NULL DEFAULT NULL,
  zhongjian_report_no VARCHAR(128) NOT NULL DEFAULT '',
  report_entry_admin_id BIGINT UNSIGNED NULL DEFAULT NULL,
  report_entry_admin_name VARCHAR(64) NOT NULL DEFAULT '',
  report_entered_at DATETIME NULL DEFAULT NULL,
  trace_info_visible TINYINT(1) NOT NULL DEFAULT 0,
  invalid_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_reports_report_no (report_no),
  KEY idx_reports_order_id (order_id),
  KEY idx_reports_report_type (report_type),
  KEY idx_reports_report_status (report_status),
  KEY idx_reports_zhongjian_report_no (zhongjian_report_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告主表';

CREATE TABLE report_contents (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_id BIGINT UNSIGNED NOT NULL,
  product_snapshot_json JSON NULL,
  result_snapshot_json JSON NULL,
  appraisal_snapshot_json JSON NULL,
  valuation_snapshot_json JSON NULL,
  evidence_attachments_json JSON NULL,
  zhongjian_report_files_json JSON NULL,
  risk_notice_text TEXT NULL,
  page_template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  pdf_template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  verify_template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_report_contents_report_id (report_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告内容快照';

CREATE TABLE report_files (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_id BIGINT UNSIGNED NOT NULL,
  file_type VARCHAR(32) NOT NULL DEFAULT 'pdf',
  file_url VARCHAR(255) NOT NULL DEFAULT '',
  file_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_report_files_report_id (report_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告文件';

CREATE TABLE report_verifies (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_id BIGINT UNSIGNED NOT NULL,
  report_no VARCHAR(64) NOT NULL,
  verify_token VARCHAR(128) NOT NULL,
  verify_qrcode_url VARCHAR(255) NOT NULL DEFAULT '',
  verify_url VARCHAR(255) NOT NULL DEFAULT '',
  verify_status VARCHAR(32) NOT NULL DEFAULT 'valid',
  last_verified_at DATETIME NULL DEFAULT NULL,
  verify_count INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_report_verifies_report_id (report_id),
  UNIQUE KEY uk_report_verifies_report_no (report_no),
  UNIQUE KEY uk_report_verifies_verify_token (verify_token)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='验真主表';

CREATE TABLE report_verify_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_verify_id BIGINT UNSIGNED NOT NULL,
  verify_type VARCHAR(32) NOT NULL DEFAULT 'input',
  ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(500) NOT NULL DEFAULT '',
  verified_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_report_verify_logs_report_verify_id (report_verify_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='验真日志';

CREATE TABLE report_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_id BIGINT UNSIGNED NOT NULL,
  action VARCHAR(64) NOT NULL,
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  before_data JSON NULL,
  after_data JSON NULL,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_report_logs_report_id (report_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告操作日志';

CREATE TABLE material_batches (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  batch_no VARCHAR(64) NOT NULL,
  total_count INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  invalidated_at DATETIME NULL DEFAULT NULL,
  invalidated_by BIGINT UNSIGNED NULL DEFAULT NULL,
  invalidated_by_name VARCHAR(64) NOT NULL DEFAULT '',
  invalid_reason VARCHAR(500) NOT NULL DEFAULT '',
  package_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  package_path VARCHAR(255) NOT NULL DEFAULT '',
  package_url VARCHAR(500) NOT NULL DEFAULT '',
  package_error VARCHAR(500) NOT NULL DEFAULT '',
  package_requested_at DATETIME NULL DEFAULT NULL,
  package_generated_at DATETIME NULL DEFAULT NULL,
  package_purged_at DATETIME NULL DEFAULT NULL,
  remark VARCHAR(500) NOT NULL DEFAULT '',
  download_count INT NOT NULL DEFAULT 0,
  last_downloaded_at DATETIME NULL DEFAULT NULL,
  created_by BIGINT UNSIGNED NULL DEFAULT NULL,
  created_by_name VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_material_batches_batch_no (batch_no),
  KEY idx_material_batches_status (status),
  KEY idx_material_batches_package_status (package_status),
  KEY idx_material_batches_package_generated_at (package_generated_at),
  KEY idx_material_batches_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物料二维码批次';

CREATE TABLE material_tag_codes (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  batch_id BIGINT UNSIGNED NOT NULL,
  qr_token VARCHAR(80) NOT NULL,
  qr_url VARCHAR(500) NOT NULL,
  qr_image_url VARCHAR(500) NOT NULL DEFAULT '',
  qr_image_path VARCHAR(255) NOT NULL DEFAULT '',
  qr_image_status VARCHAR(32) NOT NULL DEFAULT 'pending',
  qr_image_error VARCHAR(500) NOT NULL DEFAULT '',
  qr_image_generated_at DATETIME NULL DEFAULT NULL,
  verify_code VARCHAR(16) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  invalidated_at DATETIME NULL DEFAULT NULL,
  invalidated_by BIGINT UNSIGNED NULL DEFAULT NULL,
  invalidated_by_name VARCHAR(64) NOT NULL DEFAULT '',
  invalid_reason VARCHAR(500) NOT NULL DEFAULT '',
  bind_status VARCHAR(32) NOT NULL DEFAULT 'unbound',
  report_id BIGINT UNSIGNED NULL DEFAULT NULL,
  report_no VARCHAR(64) NOT NULL DEFAULT '',
  bound_task_id BIGINT UNSIGNED NULL DEFAULT NULL,
  bound_order_id BIGINT UNSIGNED NULL DEFAULT NULL,
  bound_by BIGINT UNSIGNED NULL DEFAULT NULL,
  bound_by_name VARCHAR(64) NOT NULL DEFAULT '',
  bound_at DATETIME NULL DEFAULT NULL,
  scan_count INT NOT NULL DEFAULT 0,
  verify_count INT NOT NULL DEFAULT 0,
  last_scanned_at DATETIME NULL DEFAULT NULL,
  last_verified_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_material_tag_codes_qr_token (qr_token),
  UNIQUE KEY uk_material_tag_codes_qr_url (qr_url),
  UNIQUE KEY uk_material_tag_codes_report_id (report_id),
  KEY idx_material_tag_codes_batch_id (batch_id),
  KEY idx_material_tag_codes_verify_code (verify_code),
  KEY idx_material_tag_codes_report_no (report_no),
  KEY idx_material_tag_codes_bind_status (bind_status),
  KEY idx_material_tag_codes_status (status),
  KEY idx_material_tag_codes_qr_image_status (qr_image_status),
  KEY idx_material_tag_codes_qr_image_generated_at (qr_image_generated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物料吊牌二维码';

CREATE TABLE material_batch_download_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  batch_id BIGINT UNSIGNED NOT NULL,
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(500) NOT NULL DEFAULT '',
  downloaded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_material_batch_download_logs_batch_id (batch_id),
  KEY idx_material_batch_download_logs_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物料批次下载日志';

CREATE TABLE material_tag_scan_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  tag_code_id BIGINT UNSIGNED NOT NULL,
  batch_id BIGINT UNSIGNED NOT NULL,
  report_id BIGINT UNSIGNED NULL DEFAULT NULL,
  report_no VARCHAR(64) NOT NULL DEFAULT '',
  verify_type VARCHAR(32) NOT NULL DEFAULT 'scan',
  verify_code_input VARCHAR(16) NOT NULL DEFAULT '',
  verify_passed TINYINT(1) NOT NULL DEFAULT 0,
  ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(500) NOT NULL DEFAULT '',
  scanned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_material_tag_scan_logs_tag_code_id (tag_code_id),
  KEY idx_material_tag_scan_logs_batch_id (batch_id),
  KEY idx_material_tag_scan_logs_report_id (report_id),
  KEY idx_material_tag_scan_logs_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='物料吊牌扫码与验真日志';

CREATE TABLE tickets (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  ticket_no VARCHAR(64) NOT NULL,
  ticket_type VARCHAR(32) NOT NULL,
  biz_type VARCHAR(32) NOT NULL DEFAULT '',
  biz_id BIGINT UNSIGNED NULL DEFAULT NULL,
  order_id BIGINT UNSIGNED NULL DEFAULT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  priority VARCHAR(32) NOT NULL DEFAULT 'normal',
  assignee_id BIGINT UNSIGNED NULL DEFAULT NULL,
  title VARCHAR(128) NOT NULL DEFAULT '',
  content TEXT NULL,
  closed_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_tickets_ticket_no (ticket_no),
  KEY idx_tickets_order_id (order_id),
  KEY idx_tickets_user_id (user_id),
  KEY idx_tickets_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='工单主表';

CREATE TABLE ticket_messages (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  ticket_id BIGINT UNSIGNED NOT NULL,
  sender_type VARCHAR(32) NOT NULL,
  sender_id BIGINT UNSIGNED NULL DEFAULT NULL,
  content TEXT NULL,
  attachments_json JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_ticket_messages_ticket_id (ticket_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='工单消息';

CREATE TABLE message_templates (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  template_name VARCHAR(128) NOT NULL,
  template_code VARCHAR(64) NOT NULL,
  channel VARCHAR(32) NOT NULL,
  event_code VARCHAR(64) NOT NULL,
  title VARCHAR(128) NOT NULL DEFAULT '',
  content TEXT NULL,
  jump_type VARCHAR(32) NOT NULL DEFAULT '',
  jump_value VARCHAR(255) NOT NULL DEFAULT '',
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_message_templates_code (template_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息模板';

CREATE TABLE message_rules (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  event_code VARCHAR(64) NOT NULL,
  channel VARCHAR(32) NOT NULL,
  template_id BIGINT UNSIGNED NOT NULL,
  delay_seconds INT NOT NULL DEFAULT 0,
  dedupe_window INT NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_message_rules_event_code (event_code),
  KEY idx_message_rules_template_id (template_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息规则';

CREATE TABLE message_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NULL DEFAULT NULL,
  template_id BIGINT UNSIGNED NULL DEFAULT NULL,
  biz_type VARCHAR(32) NOT NULL DEFAULT '',
  biz_id BIGINT UNSIGNED NULL DEFAULT NULL,
  channel VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  fail_reason VARCHAR(255) NOT NULL DEFAULT '',
  sent_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_message_logs_user_id (user_id),
  KEY idx_message_logs_template_id (template_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息发送日志';

CREATE TABLE user_messages (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL,
  title VARCHAR(128) NOT NULL DEFAULT '',
  content TEXT NULL,
  biz_type VARCHAR(32) NOT NULL DEFAULT '',
  biz_id BIGINT UNSIGNED NULL DEFAULT NULL,
  is_read TINYINT(1) NOT NULL DEFAULT 0,
  read_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_user_messages_user_id (user_id),
  KEY idx_user_messages_is_read (is_read)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='站内消息';

CREATE TABLE admin_users (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL,
  mobile VARCHAR(32) NOT NULL DEFAULT '',
  email VARCHAR(128) NOT NULL DEFAULT '',
  password VARCHAR(255) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  last_login_at DATETIME NULL DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_users_mobile (mobile)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台用户';

CREATE TABLE admin_api_tokens (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  admin_user_id BIGINT UNSIGNED NOT NULL,
  token_hash VARCHAR(64) NOT NULL,
  expire_time DATETIME NOT NULL,
  last_active_at DATETIME NULL DEFAULT NULL,
  last_ip VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_api_tokens_token_hash (token_hash),
  KEY idx_admin_api_tokens_admin_user_id (admin_user_id),
  KEY idx_admin_api_tokens_expire_time (expire_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台登录Token';

CREATE TABLE admin_roles (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL,
  code VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'enabled',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_roles_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台角色';

CREATE TABLE admin_role_relations (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  admin_user_id BIGINT UNSIGNED NOT NULL,
  role_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_role_relations (admin_user_id, role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台用户角色关联';

CREATE TABLE admin_permissions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(128) NOT NULL,
  code VARCHAR(64) NOT NULL,
  module VARCHAR(64) NOT NULL DEFAULT '',
  action VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_permissions_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='权限点';

CREATE TABLE admin_role_permissions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  role_id BIGINT UNSIGNED NOT NULL,
  permission_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_admin_role_permissions (role_id, permission_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色权限关联';

CREATE TABLE system_configs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  config_group VARCHAR(64) NOT NULL,
  config_key VARCHAR(128) NOT NULL,
  config_value LONGTEXT NULL,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_system_configs_group_key (config_group, config_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统配置';

CREATE TABLE help_articles (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  category VARCHAR(32) NOT NULL DEFAULT 'service',
  title VARCHAR(255) NOT NULL DEFAULT '',
  summary VARCHAR(500) NOT NULL DEFAULT '',
  keywords_json LONGTEXT NULL,
  content_html LONGTEXT NULL,
  content_blocks_json LONGTEXT NULL,
  is_recommended TINYINT(1) NOT NULL DEFAULT 0,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_help_articles_category (category),
  KEY idx_help_articles_enabled (is_enabled),
  KEY idx_help_articles_sort (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='帮助中心文章';

CREATE TABLE operation_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  module VARCHAR(64) NOT NULL,
  biz_id BIGINT UNSIGNED NULL DEFAULT NULL,
  action VARCHAR(64) NOT NULL,
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  before_data JSON NULL,
  after_data JSON NULL,
  ip VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_operation_logs_module_biz_id (module, biz_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='通用操作日志';

SET FOREIGN_KEY_CHECKS = 1;
