-- WWJ Cloud 核心模块测试数据
-- 验证Admin、Member、RBAC、Auth模块的真实字段名和业务逻辑

-- ==========================================
-- 1. Admin模块测试数据
-- ==========================================

-- 插入测试管理员用户
INSERT INTO `sys_user` (
    `username`, 
    `password`, 
    `real_name`, 
    `head_img`,
    `last_ip`,
    `last_time`,
    `create_time`,
    `login_count`,
    `status`,
    `is_del`,
    `delete_time`,
    `update_time`
) VALUES 
('admin', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '超级管理员', '', '127.0.0.1', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 0, 0, UNIX_TIMESTAMP()),
('testadmin', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '测试管理员', '', '127.0.0.1', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 0, 0, UNIX_TIMESTAMP()),
('manager', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '运营经理', '', '127.0.0.1', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 0, 0, UNIX_TIMESTAMP());

-- 插入用户角色关联
INSERT INTO `sys_user_role` (
    `uid`,
    `site_id`,
    `role_ids`,
    `create_time`,
    `is_admin`,
    `status`,
    `delete_time`
) VALUES 
(1, 0, '1', UNIX_TIMESTAMP(), 1, 1, 0),
(2, 0, '2', UNIX_TIMESTAMP(), 0, 1, 0),
(3, 0, '3', UNIX_TIMESTAMP(), 0, 1, 0);

-- 插入用户操作日志
INSERT INTO `sys_user_log` (
    `ip`,
    `site_id`,
    `uid`,
    `username`,
    `operation`,
    `url`,
    `params`,
    `type`,
    `create_time`
) VALUES 
('127.0.0.1', 0, 1, 'admin', '用户登录', '/auth/admin/login', '{"username":"admin"}', 'POST', UNIX_TIMESTAMP()),
('127.0.0.1', 0, 1, 'admin', '查看用户列表', '/adminapi/admin', '{"page":1,"limit":10}', 'GET', UNIX_TIMESTAMP()),
('127.0.0.1', 0, 2, 'testadmin', '用户登录', '/auth/admin/login', '{"username":"testadmin"}', 'POST', UNIX_TIMESTAMP());

-- ==========================================
-- 2. Member模块测试数据
-- ==========================================

-- 插入测试会员用户
INSERT INTO `member` (
    `member_no`,
    `pid`,
    `site_id`,
    `username`,
    `mobile`,
    `password`,
    `nickname`,
    `headimg`,
    `member_level`,
    `member_label`,
    `wx_openid`,
    `weapp_openid`,
    `wx_unionid`,
    `ali_openid`,
    `douyin_openid`,
    `register_channel`,
    `register_type`,
    `login_ip`,
    `login_type`,
    `login_channel`,
    `login_count`,
    `login_time`,
    `create_time`,
    `last_visit_time`,
    `last_consum_time`,
    `sex`,
    `status`,
    `birthday`,
    `id_card`,
    `point`,
    `point_get`,
    `balance`,
    `balance_get`,
    `money`,
    `money_get`,
    `money_cash_outing`,
    `growth`,
    `growth_get`,
    `commission`,
    `commission_get`,
    `commission_cash_outing`,
    `is_member`,
    `member_time`,
    `is_del`,
    `province_id`,
    `city_id`,
    `district_id`,
    `address`,
    `location`,
    `remark`,
    `delete_time`,
    `update_time`
) VALUES 
('M001', 0, 0, 'member', '13800138000', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '测试会员', '', 1, 'VIP', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 100, 100, 1000.00, 1000.00, 500.00, 500.00, 0.00, 50, 50, 0.00, 0.00, 0.00, 1, UNIX_TIMESTAMP(), 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP()),
('M002', 0, 0, 'testmember', '13800138001', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '普通会员', '', 0, '普通', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 50, 50, 500.00, 500.00, 200.00, 200.00, 0.00, 20, 20, 0.00, 0.00, 0.00, 0, 0, 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP()),
('M003', 0, 0, 'vipmember', '13800138002', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'VIP会员', '', 2, '钻石', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 500, 500, 5000.00, 5000.00, 2000.00, 2000.00, 0.00, 200, 200, 100.00, 100.00, 0.00, 1, UNIX_TIMESTAMP(), 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP());

-- 插入会员地址
INSERT INTO `member_address` (
    `member_id`,
    `site_id`,
    `name`,
    `mobile`,
    `province_id`,
    `city_id`,
    `district_id`,
    `address`,
    `address_name`,
    `full_address`,
    `lng`,
    `lat`,
    `is_default`
) VALUES 
(1, 0, '张三', '13800138000', 110000, 110100, 110101, '朝阳区建国路88号', '家', '北京市朝阳区建国路88号', '116.4074', '39.9042', 1),
(1, 0, '张三', '13800138000', 110000, 110100, 110102, '西城区西单大街1号', '公司', '北京市西城区西单大街1号', '116.3741', '39.9139', 0),
(2, 0, '李四', '13800138001', 310000, 310100, 310101, '黄浦区南京东路1号', '家', '上海市黄浦区南京东路1号', '121.4737', '31.2304', 1);

-- 插入会员等级
INSERT INTO `member_level` (
    `level_id`,
    `site_id`,
    `level_name`,
    `level_weight`,
    `level_icon`,
    `level_bg_color`,
    `level_text_color`,
    `level_condition`,
    `level_discount`,
    `level_point_rate`,
    `level_description`,
    `status`,
    `create_time`,
    `update_time`
) VALUES 
(1, 0, '普通会员', 0, '', '#FFFFFF', '#000000', 0, 100, 1, '新注册用户', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, 0, 'VIP会员', 1, '', '#FFD700', '#000000', 1000, 95, 1.2, '消费满1000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(3, 0, '钻石会员', 2, '', '#C0C0C0', '#000000', 5000, 90, 1.5, '消费满5000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

-- ==========================================
-- 3. RBAC模块测试数据
-- ==========================================

-- 插入角色
INSERT INTO `sys_role` (
    `role_id`,
    `site_id`,
    `role_name`,
    `rules`,
    `status`,
    `create_time`,
    `update_time`
) VALUES 
(1, 0, '超级管理员', '1,2,3,4,5,6,7,8,9,10', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, 0, '运营管理员', '1,2,3,4,5', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(3, 0, '内容管理员', '1,2,3', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, 0, '财务管理员', '1,2,6,7', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

-- 插入菜单
INSERT INTO `sys_menu` (
    `id`,
    `app_type`,
    `menu_name`,
    `menu_short_name`,
    `menu_key`,
    `parent_key`,
    `menu_type`,
    `icon`,
    `api_url`,
    `router_path`,
    `view_path`,
    `methods`,
    `sort`,
    `status`,
    `is_show`,
    `create_time`,
    `delete_time`,
    `addon`,
    `source`,
    `menu_attr`,
    `parent_select_key`
) VALUES 
(1, 'admin', '系统管理', '系统', 'system', '', 0, 'setting', '', '/system', 'system/index', '', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
(2, 'admin', '用户管理', '用户', 'user', 'system', 1, 'user', '/adminapi/admin', '/system/user', 'system/user/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(3, 'admin', '角色管理', '角色', 'role', 'system', 1, 'team', '/adminapi/role', '/system/role', 'system/role/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(4, 'admin', '菜单管理', '菜单', 'menu', 'system', 1, 'menu', '/adminapi/menu', '/system/menu', 'system/menu/index', 'GET,POST,PUT,DELETE', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(5, 'admin', '会员管理', '会员', 'member', '', 0, 'user', '', '/member', 'member/index', '', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
(6, 'admin', '会员列表', '列表', 'member_list', 'member', 1, 'table', '/adminapi/member', '/member/list', 'member/list/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'member'),
(7, 'admin', '会员等级', '等级', 'member_level', 'member', 1, 'star', '/adminapi/member-level', '/member/level', 'member/level/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'member'),
(8, 'admin', '财务管理', '财务', 'finance', '', 0, 'money-collect', '', '/finance', 'finance/index', '', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
(9, 'admin', '收入统计', '收入', 'income', 'finance', 1, 'rise', '/adminapi/finance/income', '/finance/income', 'finance/income/index', 'GET', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'finance'),
(10, 'admin', '支出统计', '支出', 'expense', 'finance', 1, 'fall', '/adminapi/finance/expense', '/finance/expense', 'finance/expense/index', 'GET', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'finance');

-- ==========================================
-- 4. Auth模块测试数据
-- ==========================================

-- 创建auth_token表（如果不存在）
CREATE TABLE IF NOT EXISTS `auth_token` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `token` varchar(500) NOT NULL COMMENT 'JWT Token',
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `user_type` varchar(20) NOT NULL COMMENT '用户类型：admin/member',
  `site_id` int(11) NOT NULL DEFAULT 0 COMMENT '站点ID，0为独立版',
  `expires_at` datetime NOT NULL COMMENT '过期时间',
  `refresh_token` varchar(500) DEFAULT NULL COMMENT '刷新Token',
  `refresh_expires_at` datetime DEFAULT NULL COMMENT '刷新Token过期时间',
  `ip_address` varchar(45) DEFAULT NULL COMMENT 'IP地址',
  `user_agent` varchar(500) DEFAULT NULL COMMENT '用户代理',
  `device_type` varchar(20) DEFAULT NULL COMMENT '设备类型：web/mobile/app',
  `is_revoked` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否已撤销：0未撤销，1已撤销',
  `revoked_at` datetime DEFAULT NULL COMMENT '撤销时间',
  `revoked_reason` varchar(200) DEFAULT NULL COMMENT '撤销原因',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_token` (`token`),
  KEY `idx_user_type` (`user_id`,`user_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='认证Token表';

-- 插入测试Token记录
INSERT INTO `auth_token` (
    `token`,
    `user_id`,
    `user_type`,
    `site_id`,
    `expires_at`,
    `refresh_token`,
    `refresh_expires_at`,
    `ip_address`,
    `user_agent`,
    `device_type`,
    `is_revoked`,
    `revoked_at`,
    `revoked_reason`
) VALUES 
('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_admin_token', 1, 'admin', 0, DATE_ADD(NOW(), INTERVAL 7 DAY), 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_admin_refresh', DATE_ADD(NOW(), INTERVAL 30 DAY), '127.0.0.1', 'Mozilla/5.0', 'web', 0, NULL, NULL),
('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_member_token', 1, 'member', 0, DATE_ADD(NOW(), INTERVAL 7 DAY), 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_member_refresh', DATE_ADD(NOW(), INTERVAL 30 DAY), '127.0.0.1', 'Mozilla/5.0', 'web', 0, NULL, NULL);

-- ==========================================
-- 5. 验证数据插入结果
-- ==========================================

-- 查询验证Admin模块数据
SELECT 'Admin模块数据验证' as module, COUNT(*) as count FROM `sys_user` WHERE `is_del` = 0;
SELECT 'Admin角色关联验证' as module, COUNT(*) as count FROM `sys_user_role` WHERE `delete_time` = 0;
SELECT 'Admin操作日志验证' as module, COUNT(*) as count FROM `sys_user_log`;

-- 查询验证Member模块数据
SELECT 'Member模块数据验证' as module, COUNT(*) as count FROM `member` WHERE `is_del` = 0;
SELECT 'Member地址验证' as module, COUNT(*) as count FROM `member_address`;
SELECT 'Member等级验证' as module, COUNT(*) as count FROM `member_level`;

-- 查询验证RBAC模块数据
SELECT 'RBAC角色验证' as module, COUNT(*) as count FROM `sys_role`;
SELECT 'RBAC菜单验证' as module, COUNT(*) as count FROM `sys_menu`;

-- 查询验证Auth模块数据
SELECT 'Auth Token验证' as module, COUNT(*) as count FROM `auth_token` WHERE `is_revoked` = 0;

-- 显示测试数据概览
SELECT 
    '数据概览' as info,
    (SELECT COUNT(*) FROM `sys_user` WHERE `is_del` = 0) as admin_count,
    (SELECT COUNT(*) FROM `member` WHERE `is_del` = 0) as member_count,
    (SELECT COUNT(*) FROM `sys_role`) as role_count,
    (SELECT COUNT(*) FROM `sys_menu`) as menu_count,
    (SELECT COUNT(*) FROM `auth_token` WHERE `is_revoked` = 0) as token_count; 