 ============================================================
-- 杜康好客 V3.1 数据库初始化脚本
-- MySQL 8.0+  utf8mb4_unicode_ci  InnoDB
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE DATABASE IF NOT EXISTS dukang_haoke
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_unicode_ci;

USE dukang_haoke;

-- ===================== COMMON =============================

DROP TABLE IF EXISTS common_wx_app_config;
CREATE TABLE common_wx_app_config (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  client_app    VARCHAR(32)  NOT NULL COMMENT 'USER_MINI|PARTNER_MINI|HQ_MINI|SHOP_H5',
  app_id        VARCHAR(64)  NOT NULL,
  app_secret    VARCHAR(128) NOT NULL,
  mch_id        VARCHAR(32)  DEFAULT NULL,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_wx_app_config_client (client_app)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='四端微信配置';

DROP TABLE IF EXISTS common_resource;
CREATE TABLE common_resource (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  owner_type    VARCHAR(32)  NOT NULL COMMENT 'PRODUCT|STORE|PARTNER|USER|ORDER|PROMO|HQ',
  owner_id      BIGINT UNSIGNED NOT NULL COMMENT '归属业务ID',
  biz_type      VARCHAR(32)  NOT NULL COMMENT 'COVER|ENV|CONTRACT|CAROUSEL|DETAIL|AVATAR|QRCODE|SIGN_PHOTO|VIDEO',
  media_type    VARCHAR(16)  NOT NULL DEFAULT 'IMAGE' COMMENT 'IMAGE|VIDEO|FILE',
  oss_bucket    VARCHAR(64)  NOT NULL COMMENT 'OSS Bucket',
  oss_key       VARCHAR(256) NOT NULL COMMENT 'OSS Object Key',
  url           VARCHAR(512) NOT NULL COMMENT 'CDN访问URL',
  file_name     VARCHAR(128) DEFAULT NULL,
  file_size     BIGINT UNSIGNED DEFAULT NULL COMMENT '字节',
  mime_type     VARCHAR(64)  DEFAULT NULL,
  sort_order    INT          NOT NULL DEFAULT 0,
  status        VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE' COMMENT 'ACTIVE|DELETED',
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_common_resource_owner (owner_type, owner_id, biz_type),
  KEY idx_common_resource_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统一资源表(OSS)';

DROP TABLE IF EXISTS common_event;
CREATE TABLE common_event (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  event_type    VARCHAR(32)  NOT NULL COMMENT 'STORE_AUDIT|ORDER_STATUS|BENEFIT_LEDGER|HQ_OPERATION|PROMO_TOUCH',
  ref_type      VARCHAR(32)  NOT NULL COMMENT 'ORDER|STORE|BENEFIT_COUPON|USER|PRODUCT|...',
  ref_id        BIGINT UNSIGNED NOT NULL,
  actor_type    VARCHAR(16)  DEFAULT NULL COMMENT 'USER|STORE|PARTNER|HQ|SYSTEM',
  actor_id      BIGINT UNSIGNED DEFAULT NULL,
  status        VARCHAR(32)  DEFAULT NULL COMMENT '事件子状态(如审核PENDING/APPROVED)',
  param1        VARCHAR(128) DEFAULT NULL,
  param1_desc   VARCHAR(64)  DEFAULT NULL,
  param2        VARCHAR(128) DEFAULT NULL,
  param2_desc   VARCHAR(64)  DEFAULT NULL,
  param3        VARCHAR(128) DEFAULT NULL,
  param3_desc   VARCHAR(64)  DEFAULT NULL,
  amount1       DECIMAL(10,2) DEFAULT NULL COMMENT '仅BENEFIT_LEDGER:变动额',
  amount2       DECIMAL(10,2) DEFAULT NULL COMMENT '仅BENEFIT_LEDGER:变动后余额',
  remark        VARCHAR(512) DEFAULT NULL,
  extra_json    JSON         DEFAULT NULL,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_common_event_ref (ref_type, ref_id, event_type),
  KEY idx_common_event_type_created (event_type, created_at),
  KEY idx_common_event_actor (actor_type, actor_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统一事件表';

DROP TABLE IF EXISTS common_ticket;
CREATE TABLE common_ticket (
  id              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  ticket_no       VARCHAR(32)  NOT NULL,
  ticket_type     VARCHAR(32)  NOT NULL COMMENT 'REFUND|RESHIPMENT|ALERT',
  status          VARCHAR(32)  NOT NULL DEFAULT 'PENDING',
  ref_type        VARCHAR(32)  NOT NULL COMMENT 'ORDER|STORE|PARTNER|...',
  ref_id          BIGINT UNSIGNED NOT NULL,
  operator_type   VARCHAR(16)  DEFAULT NULL COMMENT 'HQ|PARTNER|SYSTEM',
  operator_id     BIGINT UNSIGNED DEFAULT NULL,
  param1          VARCHAR(128) DEFAULT NULL,
  param1_desc     VARCHAR(64)  DEFAULT NULL,
  param2          VARCHAR(128) DEFAULT NULL,
  param2_desc     VARCHAR(64)  DEFAULT NULL,
  param3          VARCHAR(128) DEFAULT NULL,
  param3_desc     VARCHAR(64)  DEFAULT NULL,
  remark          VARCHAR(512) DEFAULT NULL,
  extra_json      JSON         DEFAULT NULL,
  created_at      DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  completed_at    DATETIME(3)  DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_ticket_no (ticket_no),
  KEY idx_common_ticket_ref (ref_type, ref_id),
  KEY idx_common_ticket_type_status (ticket_type, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='通用工单表';

DROP TABLE IF EXISTS common_support_ticket;
CREATE TABLE common_support_ticket (
  id              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  ticket_no       VARCHAR(32)  NOT NULL,
  ticket_type     VARCHAR(32)  NOT NULL COMMENT 'BUG|SUGGESTION|OTHER',
  status          VARCHAR(32)  NOT NULL DEFAULT 'PENDING_REVIEW' COMMENT 'PENDING_REVIEW|REJECTED|DEVELOPING|TESTING|PASSED',
  title           VARCHAR(128) NOT NULL,
  content         TEXT         DEFAULT NULL,
  reject_reason   VARCHAR(512) DEFAULT NULL,
  creator_id      BIGINT UNSIGNED NOT NULL,
  creator_name    VARCHAR(64)  NOT NULL,
  reviewer_id     BIGINT UNSIGNED DEFAULT NULL,
  reviewer_name   VARCHAR(64)  DEFAULT NULL,
  reviewed_at     DATETIME(3)  DEFAULT NULL,
  remark          VARCHAR(512) DEFAULT NULL,
  created_at      DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at      DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  completed_at    DATETIME(3)  DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_support_ticket_no (ticket_no),
  KEY idx_common_support_ticket_status (status, created_at),
  KEY idx_common_support_ticket_type_status (ticket_type, status),
  KEY idx_common_support_ticket_creator (creator_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='HQ技术支持工单';

DROP TABLE IF EXISTS common_product_item;
CREATE TABLE common_product_item (
  id                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  sku_code          VARCHAR(32)  NOT NULL COMMENT 'SKU编码',
  barcode_69        VARCHAR(32)  NOT NULL COMMENT '69码(商品条码)',
  name              VARCHAR(128) NOT NULL,
  subtitle          VARCHAR(256) DEFAULT NULL,
  aroma_type        VARCHAR(16)  NOT NULL COMMENT 'QINGXIANG|JIANGXIANG|NONGXIANG',
  spec              VARCHAR(128) NOT NULL,
  price             DECIMAL(10,2) NOT NULL,
  benefit_amount    DECIMAL(10,2) DEFAULT NULL COMMENT 'NULL=等同售价',
  status            VARCHAR(16)  NOT NULL DEFAULT 'DRAFT' COMMENT 'DRAFT|ON_SALE|OFF_SALE',
  sort_order        INT          NOT NULL DEFAULT 0,
  cover_resource_id BIGINT UNSIGNED DEFAULT NULL COMMENT '主图 common_resource.id',
  detail_content    JSON         DEFAULT NULL COMMENT '图文详情(纯文本结构)',
  created_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_product_item_sku (sku_code),
  UNIQUE KEY uk_common_product_item_barcode (barcode_69),
  KEY idx_common_product_item_status (status, aroma_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品SKU';

DROP TABLE IF EXISTS common_store_category;
CREATE TABLE common_store_category (
  id    BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  code  VARCHAR(32)  NOT NULL,
  name  VARCHAR(64)  NOT NULL,
  sort  INT          NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_store_category_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='门店餐饮分类';

DROP TABLE IF EXISTS common_promo_code;
CREATE TABLE common_promo_code (
  id                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  code               VARCHAR(32)  NOT NULL,
  name               VARCHAR(128) NOT NULL,
  scene              VARCHAR(32)  NOT NULL DEFAULT 'ONLINE_LINK',
  qrcode_id          VARCHAR(64)  NOT NULL COMMENT '二维码唯一识别码',
  status             VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  owner_user_id      BIGINT UNSIGNED DEFAULT NULL COMMENT '关联用户（统计/归因）',
  remark             VARCHAR(256) DEFAULT NULL,
  qrcode_resource_id BIGINT UNSIGNED DEFAULT NULL COMMENT '小程序码 common_resource.id',
  scan_count         INT          NOT NULL DEFAULT 0,
  order_count        INT          NOT NULL DEFAULT 0,
  created_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_promo_code_code (code),
  UNIQUE KEY uk_common_promo_code_qrcode_id (qrcode_id),
  KEY idx_common_promo_code_owner (owner_user_id),
  KEY idx_common_promo_code_scene_status (scene, status),
  CONSTRAINT fk_common_promo_code_owner_user FOREIGN KEY (owner_user_id) REFERENCES user_user(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推广码';

-- ===================== PARTNER（城市合伙人主账号 + 子账号） =============================

DROP TABLE IF EXISTS common_city;
CREATE TABLE common_city (
  id             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  code           VARCHAR(16)  NOT NULL,
  name           VARCHAR(64)  NOT NULL,
  province       VARCHAR(32)  NOT NULL,
  status         VARCHAR(16)  NOT NULL DEFAULT 'PENDING',
  local_min_qty  INT          NOT NULL DEFAULT 2,
  cross_min_qty  INT          NOT NULL DEFAULT 6,
  max_partner_commission_rate DECIMAL(5,4) NOT NULL DEFAULT 0.0500 COMMENT '订单+核销佣金合计上限',
  created_at     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_common_city_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='开城配置';

DROP TABLE IF EXISTS partner_account;
CREATE TABLE partner_account (
  id                     BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  phone                  VARCHAR(20)  NOT NULL,
  name                   VARCHAR(64)  NOT NULL,
  wx_open_id             VARCHAR(64)  DEFAULT NULL,
  wx_union_id            VARCHAR(64)  DEFAULT NULL,
  is_primary             TINYINT      NOT NULL DEFAULT 0,
  parent_account_id      BIGINT UNSIGNED DEFAULT NULL,
  staff_role             VARCHAR(16)  DEFAULT NULL COMMENT 'PARTNER|INTERNAL|PROMOTER',
  permissions            JSON         DEFAULT NULL,
  status                 VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  last_login_at          DATETIME(3)  DEFAULT NULL,
  city_id                BIGINT UNSIGNED DEFAULT NULL COMMENT '主账号绑定城市',
  scope_type             VARCHAR(16)  DEFAULT NULL COMMENT 'CITY_WIDE|DISTRICT',
  district_codes         JSON         DEFAULT NULL,
  order_commission_rate  DECIMAL(5,4) DEFAULT 0.0000,
  redeem_commission_rate DECIMAL(5,4) DEFAULT 0.0300,
  binding_status         VARCHAR(16)  DEFAULT 'ACTIVE',
  company_name           VARCHAR(128) DEFAULT NULL,
  address                VARCHAR(256) DEFAULT NULL,
  contact_phone          VARCHAR(20)  DEFAULT NULL,
  contract_no            VARCHAR(64)  DEFAULT NULL,
  contract_signed_at     DATETIME(3)  DEFAULT NULL,
  contract_expire_at     DATETIME(3)  DEFAULT NULL,
  bank_account_name      VARCHAR(64)  DEFAULT NULL,
  bank_account_no        VARCHAR(32)  DEFAULT NULL,
  bank_branch            VARCHAR(128) DEFAULT NULL,
  weekly_store_target    INT          DEFAULT 20,
  managed_warehouse_id   BIGINT UNSIGNED DEFAULT NULL,
  created_at             DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at             DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_partner_account_phone (phone),
  UNIQUE KEY uk_partner_account_managed_warehouse (managed_warehouse_id),
  KEY idx_partner_account_city_scope (city_id, scope_type),
  KEY idx_partner_account_city_primary (city_id, is_primary),
  KEY idx_partner_account_parent (parent_account_id),
  KEY idx_partner_account_contact_phone (contact_phone),
  CONSTRAINT fk_partner_account_city FOREIGN KEY (city_id) REFERENCES common_city(id) ON DELETE RESTRICT,
  CONSTRAINT fk_partner_account_parent FOREIGN KEY (parent_account_id) REFERENCES partner_account(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='城市合伙人账号(主账号=实体+城市绑定)';

DROP TABLE IF EXISTS common_city_warehouse;
CREATE TABLE common_city_warehouse (
  id                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  city_id            BIGINT UNSIGNED NOT NULL,
  name               VARCHAR(128) NOT NULL,
  address            VARCHAR(256) NOT NULL,
  contact_name       VARCHAR(64)  NOT NULL,
  contact_phone      VARCHAR(20)  NOT NULL,
  manager_type       VARCHAR(16)  NOT NULL COMMENT 'HQ|PARTNER',
  partner_account_id BIGINT UNSIGNED DEFAULT NULL,
  status             VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  created_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_city_warehouse_city (city_id),
  KEY idx_city_warehouse_partner_account (partner_account_id),
  CONSTRAINT fk_city_warehouse_city FOREIGN KEY (city_id) REFERENCES common_city(id) ON DELETE CASCADE,
  CONSTRAINT fk_city_warehouse_partner_account FOREIGN KEY (partner_account_id) REFERENCES partner_account(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='城市仓库';

ALTER TABLE partner_account
  ADD CONSTRAINT fk_partner_account_managed_warehouse FOREIGN KEY (managed_warehouse_id) REFERENCES common_city_warehouse(id) ON DELETE SET NULL;

DROP TABLE IF EXISTS partner_bill;
CREATE TABLE partner_bill (
  id                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  bill_no            VARCHAR(32)  NOT NULL,
  partner_account_id BIGINT UNSIGNED NOT NULL,
  period_start       DATETIME(3)  NOT NULL,
  period_end         DATETIME(3)  NOT NULL,
  order_commission   DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '下单佣金汇总',
  redeem_commission  DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '核销佣金汇总',
  total_amount       DECIMAL(10,2) NOT NULL,
  status             VARCHAR(16)  NOT NULL DEFAULT 'DRAFT',
  confirmed_at       DATETIME(3)  DEFAULT NULL,
  paid_at            DATETIME(3)  DEFAULT NULL,
  created_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_partner_bill_no (bill_no),
  KEY idx_partner_bill_account_status (partner_account_id, status),
  CONSTRAINT fk_partner_bill_account FOREIGN KEY (partner_account_id) REFERENCES partner_account(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合伙人T+30账单';

-- ===================== HQ =============================

DROP TABLE IF EXISTS hq_account;
CREATE TABLE hq_account (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  phone         VARCHAR(20)  NOT NULL,
  name          VARCHAR(64)  NOT NULL,
  admin_role    VARCHAR(32)  NOT NULL DEFAULT 'OPS',
  wx_open_id    VARCHAR(64)  DEFAULT NULL,
  wx_union_id   VARCHAR(64)  DEFAULT NULL,
  status        VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  last_login_at DATETIME(3)  DEFAULT NULL,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_hq_account_phone (phone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='总部账号';

-- ===================== USER =============================

DROP TABLE IF EXISTS user_user;
CREATE TABLE user_user (
  id                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_no           VARCHAR(20)  NOT NULL,
  device_key        VARCHAR(36)  DEFAULT NULL COMMENT '访客设备标识',
  phone             VARCHAR(20)  DEFAULT NULL COMMENT '验机后必填;访客可为NULL',
  phone_verified_at DATETIME(3)  DEFAULT NULL,
  merged_into_user_id BIGINT UNSIGNED DEFAULT NULL COMMENT '合并入主账号',
  wx_open_id        VARCHAR(64)  DEFAULT NULL,
  wx_union_id       VARCHAR(64)  DEFAULT NULL,
  nickname          VARCHAR(64)  DEFAULT NULL,
  avatar_resource_id BIGINT UNSIGNED DEFAULT NULL COMMENT '头像 common_resource.id',
  status            TINYINT      NOT NULL DEFAULT 1,
  source_type       VARCHAR(32)  NOT NULL DEFAULT 'ORGANIC' COMMENT 'ORGANIC|PROMO_CODE|SHARE_LINK|FRIEND_REFERRAL|OFFLINE_EVENT|OTHER',
  source_ref_id     BIGINT UNSIGNED DEFAULT NULL COMMENT 'promo_code_id 或 referrer_user_id',
  source_label      VARCHAR(128) DEFAULT NULL COMMENT '渠道名称快照',
  referrer_user_id  BIGINT UNSIGNED DEFAULT NULL COMMENT '好友推荐人 user_user.id',
  created_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_user_phone (phone),
  UNIQUE KEY uk_user_user_no (user_no),
  UNIQUE KEY uk_user_user_device_key (device_key),
  KEY idx_user_user_source (source_type, source_ref_id),
  KEY idx_user_user_referrer (referrer_user_id),
  KEY idx_user_user_merged (merged_into_user_id),
  KEY idx_user_user_wx_open (wx_open_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='C端用户';

DROP TABLE IF EXISTS user_address;
CREATE TABLE user_address (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id       BIGINT UNSIGNED NOT NULL,
  receiver_name VARCHAR(32)  NOT NULL,
  phone         VARCHAR(20)  NOT NULL,
  province      VARCHAR(32)  NOT NULL,
  city          VARCHAR(32)  NOT NULL,
  district      VARCHAR(32)  NOT NULL,
  detail        VARCHAR(256) NOT NULL,
  latitude      DECIMAL(10,7) DEFAULT NULL,
  longitude     DECIMAL(10,7) DEFAULT NULL,
  is_default    TINYINT      NOT NULL DEFAULT 0,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_user_address_user (user_id),
  CONSTRAINT fk_user_address_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户收货地址';

DROP TABLE IF EXISTS user_city_preference;
CREATE TABLE user_city_preference (
  id                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id            BIGINT UNSIGNED NOT NULL,
  selected_city_code VARCHAR(16)  DEFAULT NULL,
  selected_district  VARCHAR(32)  DEFAULT NULL,
  locate_city_code   VARCHAR(16)  DEFAULT NULL,
  locate_district    VARCHAR(32)  DEFAULT NULL,
  updated_at         DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_city_preference_user (user_id),
  CONSTRAINT fk_user_city_preference_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户城市偏好';

DROP TABLE IF EXISTS user_promo_attribution;
CREATE TABLE user_promo_attribution (
  id             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id        BIGINT UNSIGNED NOT NULL,
  promo_code_id  BIGINT UNSIGNED NOT NULL,
  channel_name   VARCHAR(128) NOT NULL,
  first_touch_at DATETIME(3)  NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_promo_attribution_user (user_id),
  KEY idx_user_promo_attribution_promo (promo_code_id),
  CONSTRAINT fk_user_promo_attribution_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE CASCADE,
  CONSTRAINT fk_user_promo_attribution_promo FOREIGN KEY (promo_code_id) REFERENCES common_promo_code(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='推广首次触达(统计)';

-- ===================== STORE =============================

DROP TABLE IF EXISTS store_store;
CREATE TABLE store_store (
  id                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  city_id           BIGINT UNSIGNED NOT NULL,
  partner_account_id BIGINT UNSIGNED NOT NULL,
  category_id       BIGINT UNSIGNED DEFAULT NULL,
  name              VARCHAR(128) NOT NULL,
  phone             VARCHAR(20)  NOT NULL,
  province          VARCHAR(32)  NOT NULL,
  city_name         VARCHAR(32)  NOT NULL COMMENT '市(冗余)',
  district          VARCHAR(32)  NOT NULL,
  address           VARCHAR(256) NOT NULL,
  latitude          DECIMAL(10,7) DEFAULT NULL,
  longitude         DECIMAL(10,7) DEFAULT NULL,
  intro             TEXT         DEFAULT NULL,
  cover_resource_id BIGINT UNSIGNED DEFAULT NULL COMMENT '门头图',
  avg_price         DECIMAL(10,2) DEFAULT NULL,
  rating            DECIMAL(3,2) DEFAULT NULL,
  tags              JSON         DEFAULT NULL,
  status            VARCHAR(16)  NOT NULL DEFAULT 'PAUSED',
  open_time         VARCHAR(8)   DEFAULT NULL,
  close_time        VARCHAR(8)   DEFAULT NULL,
  bank_account_name VARCHAR(64)  DEFAULT NULL,
  bank_account_no   VARCHAR(32)  DEFAULT NULL,
  bank_branch       VARCHAR(128) DEFAULT NULL,
  settlement_rate   DECIMAL(5,4) NOT NULL DEFAULT 0.6000 COMMENT '门店核销结算比例',
  created_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at        DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_store_store_city_status (city_id, status),
  KEY idx_store_store_partner_account (partner_account_id),
  CONSTRAINT fk_store_store_city FOREIGN KEY (city_id) REFERENCES common_city(id) ON DELETE RESTRICT,
  CONSTRAINT fk_store_store_partner_account FOREIGN KEY (partner_account_id) REFERENCES partner_account(id) ON DELETE RESTRICT,
  CONSTRAINT fk_store_store_category FOREIGN KEY (category_id) REFERENCES common_store_category(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='餐饮门店';

DROP TABLE IF EXISTS store_account;
CREATE TABLE store_account (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  store_id      BIGINT UNSIGNED NOT NULL,
  phone         VARCHAR(20)  NOT NULL,
  name          VARCHAR(64)  NOT NULL,
  wx_open_id    VARCHAR(64)  DEFAULT NULL,
  wx_union_id   VARCHAR(64)  DEFAULT NULL,
  status        VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  last_login_at DATETIME(3)  DEFAULT NULL,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_store_account_store (store_id),
  UNIQUE KEY uk_store_account_phone (phone),
  CONSTRAINT fk_store_account_store FOREIGN KEY (store_id) REFERENCES store_store(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='门店H5账号';

-- ===================== USER ORDER =============================

DROP TABLE IF EXISTS user_order_item;
DROP TABLE IF EXISTS user_order_delivery;
DROP TABLE IF EXISTS user_order;
CREATE TABLE user_order (
  id                  BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_no            VARCHAR(32)  NOT NULL,
  order_type          VARCHAR(16)  NOT NULL DEFAULT 'NORMAL' COMMENT 'NORMAL|RESHIPMENT',
  user_id             BIGINT UNSIGNED NOT NULL,
  city_id             BIGINT UNSIGNED NOT NULL,
  status              VARCHAR(32)  NOT NULL DEFAULT 'PENDING_PAY',
  pay_status          VARCHAR(16)  NOT NULL DEFAULT 'UNPAID' COMMENT 'UNPAID|PAYING|PAID|REFUNDING|REFUNDED',
  delivery_type       VARCHAR(16)  NOT NULL COMMENT 'LOCAL|CROSS_CITY',
  origin_order_id     BIGINT UNSIGNED DEFAULT NULL,
  promo_code_id       BIGINT UNSIGNED DEFAULT NULL,
  channel_source      VARCHAR(128) DEFAULT NULL,
  product_id          BIGINT UNSIGNED NOT NULL COMMENT '商品 common_product_item.id',
  barcode_69          VARCHAR(32)  NOT NULL COMMENT '69码快照',
  product_name        VARCHAR(128) NOT NULL COMMENT '商品名称快照',
  product_spec        VARCHAR(128) NOT NULL COMMENT '规格快照',
  image_resource_id   BIGINT UNSIGNED DEFAULT NULL COMMENT '商品图快照 common_resource.id',
  quantity            INT          NOT NULL COMMENT '购买数量',
  list_unit_price     DECIMAL(10,2) NOT NULL COMMENT '标价单价',
  list_amount         DECIMAL(10,2) NOT NULL COMMENT '标价总额',
  discount_amount     DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '优惠金额',
  product_amount      DECIMAL(10,2) NOT NULL COMMENT '商品应付',
  freight_amount      DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '运费',
  freight_pay_type    VARCHAR(8)   DEFAULT NULL COMMENT 'FREE|COD',
  pay_amount          DECIMAL(10,2) NOT NULL COMMENT '实付总额',
  benefit_amount      DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '本单发放权益',
  receiver_name       VARCHAR(32)  NOT NULL,
  receiver_phone      VARCHAR(20)  NOT NULL,
  receiver_address    TEXT         NOT NULL,
  receiver_province   VARCHAR(32)  NOT NULL,
  receiver_city       VARCHAR(32)  NOT NULL,
  receiver_district   VARCHAR(32)  NOT NULL,
  client_ip           VARCHAR(45)  DEFAULT NULL COMMENT '下单时客户端IP',
  ip_province         VARCHAR(32)  DEFAULT NULL COMMENT 'IP解析省',
  ip_city             VARCHAR(32)  DEFAULT NULL COMMENT 'IP解析市',
  ip_district         VARCHAR(32)  DEFAULT NULL COMMENT 'IP解析区县',
  gps_province        VARCHAR(32)  DEFAULT NULL COMMENT 'GPS解析省',
  gps_city            VARCHAR(32)  DEFAULT NULL COMMENT 'GPS解析市',
  gps_district        VARCHAR(32)  DEFAULT NULL COMMENT 'GPS解析区县',
  gps_latitude        DECIMAL(10,7) DEFAULT NULL,
  gps_longitude       DECIMAL(10,7) DEFAULT NULL,
  gps_address         VARCHAR(256) DEFAULT NULL COMMENT 'GPS逆地理地址',
  pay_external_no     VARCHAR(64)  DEFAULT NULL COMMENT '微信交易号(冗余)',
  paid_at             DATETIME(3)  DEFAULT NULL COMMENT '支付时间',
  shipped_at          DATETIME(3)  DEFAULT NULL COMMENT '发货时间(冗余=user_order_delivery.shipping_at)',
  completed_at        DATETIME(3)  DEFAULT NULL COMMENT '完成时间',
  cancelled_at        DATETIME(3)  DEFAULT NULL COMMENT '取消时间',
  pay_expire_at                DATETIME(3)  DEFAULT NULL COMMENT '待付款过期时间',
  partner_account_id_at_pay      BIGINT UNSIGNED DEFAULT NULL,
  order_commission_rate_at_pay DECIMAL(5,4) DEFAULT NULL,
  remark              VARCHAR(512) DEFAULT NULL,
  created_at          DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at          DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_order_no (order_no),
  KEY idx_user_order_user_status (user_id, status),
  KEY idx_user_order_city_created (city_id, created_at),
  KEY idx_user_order_product (product_id),
  KEY idx_user_order_barcode (barcode_69),
  KEY idx_user_order_pay_external (pay_external_no),
  KEY idx_user_order_ip_city (ip_city),
  KEY idx_user_order_gps_city (gps_city),
  CONSTRAINT fk_user_order_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE RESTRICT,
  CONSTRAINT fk_user_order_city FOREIGN KEY (city_id) REFERENCES common_city(id) ON DELETE RESTRICT,
  CONSTRAINT fk_user_order_origin FOREIGN KEY (origin_order_id) REFERENCES user_order(id) ON DELETE SET NULL,
  CONSTRAINT fk_user_order_promo FOREIGN KEY (promo_code_id) REFERENCES common_promo_code(id) ON DELETE SET NULL,
  CONSTRAINT fk_user_order_product FOREIGN KEY (product_id) REFERENCES common_product_item(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单(含商品快照,V1单SKU)';

DROP TABLE IF EXISTS user_order_delivery;
CREATE TABLE user_order_delivery (
  id                   BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id             BIGINT UNSIGNED NOT NULL,
  provider             VARCHAR(16)  NOT NULL COMMENT 'XFX|LOGISTICS|MANUAL',
  provider_order_no    VARCHAR(64)  DEFAULT NULL,
  tracking_no          VARCHAR(64)  DEFAULT NULL,
  out_warehouse_at     DATETIME(3)  DEFAULT NULL,
  shipping_at          DATETIME(3)  DEFAULT NULL,
  delivered_at         DATETIME(3)  DEFAULT NULL,
  sign_photo_resource_id BIGINT UNSIGNED DEFAULT NULL COMMENT '签收照片 common_resource.id',
  updated_at           DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_order_delivery_order (order_id),
  CONSTRAINT fk_user_order_delivery_order FOREIGN KEY (order_id) REFERENCES user_order(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单配送(与user_order 1:1)';

-- ===================== BENEFIT & REDEEM =============================

DROP TABLE IF EXISTS user_benefit_coupon;
CREATE TABLE user_benefit_coupon (
  id             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  coupon_no      VARCHAR(32)  NOT NULL,
  user_id        BIGINT UNSIGNED NOT NULL,
  order_id       BIGINT UNSIGNED NOT NULL,
  total_amount   DECIMAL(10,2) NOT NULL,
  used_amount    DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  balance        DECIMAL(10,2) NOT NULL,
  status         VARCHAR(16)  NOT NULL DEFAULT 'ACTIVE',
  source_product VARCHAR(128) NOT NULL,
  version        INT          NOT NULL DEFAULT 0 COMMENT '乐观锁',
  created_at     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_benefit_coupon_no (coupon_no),
  KEY idx_user_benefit_coupon_user (user_id, status),
  CONSTRAINT fk_user_benefit_coupon_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE RESTRICT,
  CONSTRAINT fk_user_benefit_coupon_order FOREIGN KEY (order_id) REFERENCES user_order(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='好客权益券';

DROP TABLE IF EXISTS user_redeem_record;
CREATE TABLE user_redeem_record (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  redeem_no     VARCHAR(32)  NOT NULL,
  user_id       BIGINT UNSIGNED NOT NULL,
  coupon_id     BIGINT UNSIGNED NOT NULL,
  store_id      BIGINT UNSIGNED NOT NULL,
  amount        DECIMAL(10,2) NOT NULL,
  settle_amount DECIMAL(10,2) NOT NULL,
  created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_redeem_record_no (redeem_no),
  KEY idx_user_redeem_record_store (store_id, created_at),
  CONSTRAINT fk_user_redeem_record_user FOREIGN KEY (user_id) REFERENCES user_user(id) ON DELETE RESTRICT,
  CONSTRAINT fk_user_redeem_record_coupon FOREIGN KEY (coupon_id) REFERENCES user_benefit_coupon(id) ON DELETE RESTRICT,
  CONSTRAINT fk_user_redeem_record_store FOREIGN KEY (store_id) REFERENCES store_store(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='核销记录';

DROP TABLE IF EXISTS user_store_rating;
CREATE TABLE user_store_rating (
  id               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  redeem_record_id BIGINT UNSIGNED NOT NULL,
  store_id         BIGINT UNSIGNED NOT NULL,
  service_score    TINYINT      NOT NULL,
  env_score        TINYINT      NOT NULL,
  created_at       DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_store_rating_redeem (redeem_record_id),
  CONSTRAINT fk_user_store_rating_redeem FOREIGN KEY (redeem_record_id) REFERENCES user_redeem_record(id) ON DELETE CASCADE,
  CONSTRAINT fk_user_store_rating_store FOREIGN KEY (store_id) REFERENCES store_store(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='核销评价';

DROP TABLE IF EXISTS store_payout;
CREATE TABLE store_payout (
  id               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  redeem_record_id BIGINT UNSIGNED NOT NULL,
  store_id         BIGINT UNSIGNED NOT NULL,
  redeem_amount    DECIMAL(10,2) NOT NULL,
  payout_amount    DECIMAL(10,2) NOT NULL,
  settlement_rate  DECIMAL(5,4) NOT NULL,
  status           VARCHAR(16)  NOT NULL DEFAULT 'PENDING',
  expected_pay_at  DATETIME(3)  NOT NULL,
  paid_at          DATETIME(3)  DEFAULT NULL,
  batch_no         VARCHAR(32)  DEFAULT NULL,
  created_at       DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  UNIQUE KEY uk_store_payout_redeem (redeem_record_id),
  KEY idx_store_payout_store_status (store_id, status),
  CONSTRAINT fk_store_payout_redeem FOREIGN KEY (redeem_record_id) REFERENCES user_redeem_record(id) ON DELETE RESTRICT,
  CONSTRAINT fk_store_payout_store FOREIGN KEY (store_id) REFERENCES store_store(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='门店T+1打款';

-- ===================== LOG =============================

DROP TABLE IF EXISTS log_third_party;
CREATE TABLE log_third_party (
  id             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  provider       VARCHAR(32)  NOT NULL COMMENT 'WECHAT_PAY|WECHAT_REFUND|WECHAT_AUTH|WECHAT_MAP|XFX|SMS|LOGISTICS',
  scene          VARCHAR(64)  NOT NULL COMMENT '业务场景',
  ref_type       VARCHAR(32)  DEFAULT NULL COMMENT 'ORDER|USER|STORE|...',
  ref_id         BIGINT UNSIGNED DEFAULT NULL,
  request_url    VARCHAR(512) DEFAULT NULL,
  request_body   JSON         DEFAULT NULL COMMENT '发送参数',
  response_body  JSON         DEFAULT NULL COMMENT '响应数据',
  external_no    VARCHAR(128) DEFAULT NULL COMMENT '第三方单号',
  amount         DECIMAL(10,2) DEFAULT NULL,
  status         VARCHAR(16)  NOT NULL DEFAULT 'PENDING' COMMENT 'PENDING|SUCCESS|FAILED',
  error_message  VARCHAR(512) DEFAULT NULL,
  created_at     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_log_third_party_ref (ref_type, ref_id),
  KEY idx_log_third_party_provider_scene (provider, scene, created_at),
  KEY idx_log_third_party_external (external_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='第三方交互记录';

DROP TABLE IF EXISTS log_user_analytics;
CREATE TABLE log_user_analytics (
  id              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id         BIGINT UNSIGNED DEFAULT NULL COMMENT '未登录可为NULL',
  session_id      VARCHAR(64)  DEFAULT NULL COMMENT '会话ID',
  event_name      VARCHAR(64)  NOT NULL COMMENT '见§4.1 event_name 清单',
  client_app      VARCHAR(32)  DEFAULT NULL COMMENT 'USER_MINI|PARTNER_MINI|HQ_MINI|SHOP_H5',
  page_path       VARCHAR(128) DEFAULT NULL COMMENT '页面路径',
  ref_type        VARCHAR(32)  DEFAULT NULL COMMENT 'PRODUCT|STORE|ORDER|TAB|ADDRESS|PROMO|...',
  ref_id          BIGINT UNSIGNED DEFAULT NULL,
  keyword         VARCHAR(128) DEFAULT NULL COMMENT '搜索关键词',
  source_type     VARCHAR(32)  DEFAULT NULL COMMENT 'register事件:来源类型快照',
  source_ref_id   BIGINT UNSIGNED DEFAULT NULL COMMENT 'register事件:来源ID',
  extra_json      JSON         DEFAULT NULL COMMENT 'PRD埋点参数',
  created_at      DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_log_user_analytics_user_created (user_id, created_at),
  KEY idx_log_user_analytics_event_created (event_name, created_at),
  KEY idx_log_user_analytics_session (session_id),
  KEY idx_log_user_analytics_ref (ref_type, ref_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户行为埋点日志';

DROP TABLE IF EXISTS log_store_analytics;
CREATE TABLE log_store_analytics (
  id               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  store_account_id BIGINT UNSIGNED DEFAULT NULL COMMENT '门店账号ID，系统/HQ操作可为NULL',
  store_id         BIGINT UNSIGNED NOT NULL COMMENT '门店ID',
  event_name       VARCHAR(64)  NOT NULL COMMENT '门店行为事件名',
  client_app       VARCHAR(32)  DEFAULT NULL COMMENT 'SHOP_H5|HQ_WEB|PARTNER_H5',
  ref_type         VARCHAR(32)  DEFAULT NULL COMMENT 'REDEEM_RECORD|STORE_PAYOUT|...',
  ref_id           BIGINT UNSIGNED DEFAULT NULL,
  extra_json       JSON         DEFAULT NULL,
  created_at       DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (id),
  KEY idx_log_store_analytics_store_created (store_id, created_at),
  KEY idx_log_store_analytics_account_created (store_account_id, created_at),
  KEY idx_log_store_analytics_event_created (event_name, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='门店商户行为日志';

SET FOREIGN_KEY_CHECKS = 1;