-- 商家子客服 merchant_ops：直接建表（绕过 python manage.py migrate）
-- 适用：migrate 因 users 旧 migration 文件报错、无法执行时
-- 执行前：mysql -u用户 -p 数据库名 < install_tables.sql
-- 或 1Panel/phpMyAdmin 粘贴执行
-- 可重复执行：表用 IF NOT EXISTS，迁移记录用 NOT EXISTS 判断

SET NAMES utf8mb4;

-- 1. 权限字典
CREATE TABLE IF NOT EXISTS `merchant_staff_permission` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `perm_code` varchar(64) NOT NULL,
  `perm_name` varchar(64) NOT NULL,
  `sort_order` int NOT NULL DEFAULT 0,
  `status` int NOT NULL DEFAULT 1,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  UNIQUE KEY `perm_code` (`perm_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. 角色
CREATE TABLE IF NOT EXISTS `merchant_staff_role` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` varchar(16) NOT NULL DEFAULT 'xq',
  `merchant_id` varchar(7) DEFAULT NULL,
  `role_code` varchar(32) NOT NULL,
  `role_name` varchar(64) NOT NULL,
  `is_system` tinyint(1) NOT NULL DEFAULT 0,
  `sort_order` int NOT NULL DEFAULT 0,
  `status` int NOT NULL DEFAULT 1,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_role_merchant_id_role_code_uniq` (`merchant_id`,`role_code`),
  KEY `merchant_staff_role_merchant_id_idx` (`merchant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. 邀请码
CREATE TABLE IF NOT EXISTS `merchant_staff_invite` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` varchar(16) NOT NULL DEFAULT 'xq',
  `merchant_id` varchar(7) NOT NULL,
  `invite_code` varchar(32) NOT NULL,
  `status` int NOT NULL DEFAULT 0,
  `created_by` varchar(7) NOT NULL,
  `used_by` varchar(7) DEFAULT NULL,
  `used_at` datetime(6) DEFAULT NULL,
  `expire_at` datetime(6) DEFAULT NULL,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `role_id` bigint DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `invite_code` (`invite_code`),
  KEY `merchant_staff_invite_merchant_id_idx` (`merchant_id`),
  KEY `merchant_staff_invite_role_id_fk` (`role_id`),
  CONSTRAINT `merchant_staff_invite_role_id_fk` FOREIGN KEY (`role_id`) REFERENCES `merchant_staff_role` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 4. 成员
CREATE TABLE IF NOT EXISTS `merchant_staff_member` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` varchar(16) NOT NULL DEFAULT 'xq',
  `merchant_id` varchar(7) NOT NULL,
  `staff_user_id` varchar(7) NOT NULL,
  `status` int NOT NULL DEFAULT 1,
  `display_name` varchar(64) NOT NULL DEFAULT '',
  `joined_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `revoked_at` datetime(6) DEFAULT NULL,
  `today_dispatch_count` int NOT NULL DEFAULT 0,
  `today_dispatch_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `invited_by_code_id` bigint DEFAULT NULL,
  `role_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_member_merchant_staff_uniq` (`merchant_id`,`staff_user_id`),
  KEY `merchant_staff_member_staff_user_id_idx` (`staff_user_id`),
  KEY `merchant_staff_member_role_id_fk` (`role_id`),
  KEY `merchant_staff_member_invited_by_code_id_fk` (`invited_by_code_id`),
  CONSTRAINT `merchant_staff_member_role_id_fk` FOREIGN KEY (`role_id`) REFERENCES `merchant_staff_role` (`id`),
  CONSTRAINT `merchant_staff_member_invited_by_code_id_fk` FOREIGN KEY (`invited_by_code_id`) REFERENCES `merchant_staff_invite` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 5. 角色-权限
CREATE TABLE IF NOT EXISTS `merchant_staff_role_permission` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `perm_code` varchar(64) NOT NULL,
  `role_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_role_permission_role_perm_uniq` (`role_id`,`perm_code`),
  KEY `merchant_staff_role_permission_perm_code_idx` (`perm_code`),
  CONSTRAINT `merchant_staff_role_permission_role_id_fk` FOREIGN KEY (`role_id`) REFERENCES `merchant_staff_role` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 6. 成员-权限覆盖
CREATE TABLE IF NOT EXISTS `merchant_staff_member_permission` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `perm_code` varchar(64) NOT NULL,
  `granted` tinyint(1) NOT NULL DEFAULT 1,
  `member_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_member_permission_member_perm_uniq` (`member_id`,`perm_code`),
  CONSTRAINT `merchant_staff_member_permission_member_id_fk` FOREIGN KEY (`member_id`) REFERENCES `merchant_staff_member` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 7. 钱包
CREATE TABLE IF NOT EXISTS `merchant_staff_wallet` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `merchant_id` varchar(7) NOT NULL,
  `staff_user_id` varchar(7) NOT NULL,
  `quota_total` decimal(12,2) NOT NULL DEFAULT 0.00,
  `quota_used` decimal(12,2) NOT NULL DEFAULT 0.00,
  `quota_frozen` decimal(12,2) NOT NULL DEFAULT 0.00,
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `member_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_wallet_merchant_staff_uniq` (`merchant_id`,`staff_user_id`),
  KEY `merchant_staff_wallet_member_id_fk` (`member_id`),
  CONSTRAINT `merchant_staff_wallet_member_id_fk` FOREIGN KEY (`member_id`) REFERENCES `merchant_staff_member` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 8. 钱包流水
CREATE TABLE IF NOT EXISTS `merchant_staff_wallet_ledger` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `merchant_id` varchar(7) NOT NULL,
  `staff_user_id` varchar(7) NOT NULL,
  `change_type` varchar(32) NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `balance_after` decimal(12,2) NOT NULL,
  `related_order_id` varchar(32) DEFAULT NULL,
  `operator_id` varchar(7) NOT NULL,
  `remark` varchar(255) NOT NULL DEFAULT '',
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `wallet_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  KEY `merchant_staff_wallet_ledger_merchant_id_idx` (`merchant_id`),
  KEY `merchant_staff_wallet_ledger_wallet_id_fk` (`wallet_id`),
  CONSTRAINT `merchant_staff_wallet_ledger_wallet_id_fk` FOREIGN KEY (`wallet_id`) REFERENCES `merchant_staff_wallet` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 9. 客服日统计
CREATE TABLE IF NOT EXISTS `merchant_staff_ri_tongji` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` varchar(16) NOT NULL DEFAULT 'xq',
  `merchant_id` varchar(7) NOT NULL,
  `staff_user_id` varchar(7) NOT NULL,
  `member_id` bigint NOT NULL,
  `stat_date` date NOT NULL,
  `year` smallint unsigned NOT NULL,
  `month` smallint unsigned NOT NULL,
  `day` smallint unsigned NOT NULL,
  `dispatch_count` int NOT NULL DEFAULT 0,
  `dispatch_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `settle_count` int NOT NULL DEFAULT 0,
  `settle_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `refund_count` int NOT NULL DEFAULT 0,
  `refund_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `cancel_count` int NOT NULL DEFAULT 0,
  `penalty_count` int NOT NULL DEFAULT 0,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  UNIQUE KEY `merchant_staff_ri_tongji_uniq` (`merchant_id`,`staff_user_id`,`stat_date`),
  KEY `merchant_staff_ri_tongji_merchant_date_idx` (`merchant_id`,`stat_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 10. 订单派单归属
CREATE TABLE IF NOT EXISTS `merchant_order_dispatch` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `order_id` varchar(32) NOT NULL,
  `merchant_id` varchar(7) NOT NULL,
  `dispatch_actor_type` varchar(16) NOT NULL,
  `dispatch_actor_id` varchar(7) NOT NULL,
  `staff_member_id` bigint DEFAULT NULL,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  UNIQUE KEY `order_id` (`order_id`),
  KEY `merchant_order_dispatch_merchant_id_idx` (`merchant_id`),
  KEY `merchant_order_dispatch_staff_member_id_idx` (`staff_member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 11. 审计日志
CREATE TABLE IF NOT EXISTS `merchant_staff_audit_log` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `merchant_id` varchar(7) NOT NULL,
  `operator_user_id` varchar(7) NOT NULL,
  `operator_type` varchar(16) NOT NULL,
  `staff_member_id` bigint DEFAULT NULL,
  `action` varchar(64) NOT NULL,
  `resource_type` varchar(32) NOT NULL DEFAULT '',
  `resource_id` varchar(64) NOT NULL DEFAULT '',
  `value_before` json DEFAULT NULL,
  `value_after` json DEFAULT NULL,
  `amount` decimal(12,2) DEFAULT NULL,
  `request_ip` varchar(64) NOT NULL DEFAULT '',
  `remark` varchar(500) NOT NULL DEFAULT '',
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  KEY `merchant_staff_audit_log_merchant_id_idx` (`merchant_id`),
  KEY `merchant_staff_audit_log_action_idx` (`action`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 12. 派单模板
CREATE TABLE IF NOT EXISTS `merchant_dispatch_template` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `merchant_id` varchar(7) NOT NULL,
  `template_name` varchar(128) NOT NULL,
  `template_type` varchar(16) NOT NULL DEFAULT 'EXPRESS',
  `payload_json` json NOT NULL,
  `created_by` varchar(7) NOT NULL,
  `created_by_type` varchar(16) NOT NULL DEFAULT 'MERCHANT',
  `status` int NOT NULL DEFAULT 1,
  `use_count` int NOT NULL DEFAULT 0,
  `CreateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `UpdateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`),
  KEY `merchant_dispatch_template_merchant_id_idx` (`merchant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 13. 告诉 Django：merchant_ops 迁移已执行（避免以后 migrate 重复建表）
INSERT INTO `django_migrations` (`app`, `name`, `applied`)
SELECT 'merchant_ops', '0001_initial', NOW(6)
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `django_migrations`
  WHERE `app` = 'merchant_ops' AND `name` = '0001_initial'
);
