-- 泰额版平台主库 User / Role / UserRole / RoleMenu 初始化
-- 执行库（必须）：antis-foodlabeling-host
-- 禁止执行库：antis-foodlabeling-us（美国版，写入会污染）
-- 前置：已执行 th-tenant-menu-seed.sql（主库已有 menu）
-- 默认账号：admin / 123456（密码哈希与框架种子一致）
--
-- ========== 固定 Guid 对照表 ==========
-- 平台超级管理员角色  f0020002-0001-4000-8000-000000000002
-- 平台 admin 用户      f0020001-0001-4000-8000-000000000001
-- 用户-角色关系        f0020003-0001-4000-8000-000000000003
-- 角色-菜单关系        f0030001~f0030026（见下方 INSERT）
-- ======================================

CREATE TABLE IF NOT EXISTS `user` (
  `Id` varchar(36) NOT NULL COMMENT '主键',
  `IsDeleted` tinyint(1) NOT NULL COMMENT '逻辑删除',
  `Name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `Age` int(11) DEFAULT NULL COMMENT '年龄',
  `UserName` varchar(255) NOT NULL COMMENT '用户名',
  `Password` varchar(255) NOT NULL COMMENT '密码',
  `Salt` varchar(255) NOT NULL COMMENT '加密盐值',
  `Icon` varchar(255) DEFAULT NULL COMMENT '头像',
  `Nick` varchar(255) DEFAULT NULL COMMENT '昵称',
  `Email` varchar(255) DEFAULT NULL COMMENT '邮箱',
  `Ip` varchar(255) DEFAULT NULL COMMENT 'Ip',
  `Address` varchar(255) DEFAULT NULL COMMENT '地址',
  `Phone` bigint(20) DEFAULT NULL COMMENT '电话',
  `Introduction` varchar(255) DEFAULT NULL COMMENT '简介',
  `Remark` varchar(255) DEFAULT NULL COMMENT '备注',
  `Sex` int(11) NOT NULL COMMENT '性别',
  `DeptId` varchar(36) DEFAULT NULL COMMENT '部门id',
  `CreationTime` datetime NOT NULL COMMENT '创建时间',
  `CreatorId` varchar(36) DEFAULT NULL COMMENT '创建者',
  `LastModifierId` varchar(36) DEFAULT NULL COMMENT '最后修改者',
  `LastModificationTime` datetime DEFAULT NULL COMMENT '最后修改时间',
  `OrderNum` int(11) NOT NULL COMMENT '排序',
  `State` tinyint(1) NOT NULL COMMENT '状态',
  `ConcurrencyStamp` varchar(255) NOT NULL,
  PRIMARY KEY (`Id`),
  KEY `index_UserName` (`UserName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表-泰额平台主库';

CREATE TABLE IF NOT EXISTS `role` (
  `Id` varchar(36) NOT NULL COMMENT '主键',
  `IsDeleted` tinyint(1) NOT NULL COMMENT '逻辑删除',
  `CreationTime` datetime NOT NULL COMMENT '创建时间',
  `CreatorId` varchar(36) DEFAULT NULL COMMENT '创建者',
  `LastModifierId` varchar(36) DEFAULT NULL COMMENT '最后修改者',
  `LastModificationTime` datetime DEFAULT NULL COMMENT '最后修改时间',
  `OrderNum` int(11) NOT NULL COMMENT '排序',
  `RoleName` varchar(255) NOT NULL COMMENT '角色名',
  `RoleCode` varchar(255) NOT NULL COMMENT '角色编码',
  `Remark` varchar(255) DEFAULT NULL COMMENT '描述',
  `DataScope` int(11) NOT NULL COMMENT '角色数据范围',
  `State` tinyint(1) NOT NULL COMMENT '状态',
  `AccessPermissionCodes` varchar(2000) DEFAULT NULL COMMENT '访问权限 JSON',
  `ConcurrencyStamp` varchar(255) NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表-泰额平台主库';

CREATE TABLE IF NOT EXISTS `userrole` (
  `Id` varchar(36) NOT NULL COMMENT '主键',
  `RoleId` varchar(36) NOT NULL COMMENT '角色id',
  `UserId` varchar(36) NOT NULL COMMENT '用户id',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关系表-泰额平台主库';

CREATE TABLE IF NOT EXISTS `rolemenu` (
  `Id` varchar(36) NOT NULL COMMENT '主键',
  `RoleId` varchar(36) NOT NULL,
  `MenuId` varchar(36) NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色菜单关系表-泰额平台主库';

-- 物理清除旧非法 Id，避免脏数据残留
DELETE FROM `userrole` WHERE `Id` LIKE 'th-platform%' OR `UserId` LIKE 'th-platform%' OR `RoleId` LIKE 'th-platform%';
DELETE FROM `rolemenu` WHERE `RoleId` LIKE 'th-platform%' OR `Id` LIKE 'th-prm-%' OR `Id` LIKE 'th-platform%';
DELETE FROM `user` WHERE `Id` LIKE 'th-platform%';
DELETE FROM `role` WHERE `Id` LIKE 'th-platform%';

INSERT INTO `role`
(`Id`, `IsDeleted`, `CreationTime`, `CreatorId`, `LastModifierId`, `LastModificationTime`,
 `OrderNum`, `RoleName`, `RoleCode`, `Remark`, `DataScope`, `State`, `AccessPermissionCodes`, `ConcurrencyStamp`)
VALUES
('f0020002-0001-4000-8000-000000000002', 0, NOW(), NULL, NULL, NULL,
 999, '平台超级管理员', 'admin', '泰额平台主库超级管理员', 0, 1, NULL, UUID())
ON DUPLICATE KEY UPDATE
  `IsDeleted`=0,
  `LastModificationTime`=NOW(),
  `RoleName`=VALUES(`RoleName`),
  `RoleCode`=VALUES(`RoleCode`),
  `Remark`=VALUES(`Remark`),
  `DataScope`=VALUES(`DataScope`),
  `State`=1,
  `ConcurrencyStamp`=UUID();

INSERT INTO `user`
(`Id`, `IsDeleted`, `Name`, `Age`, `UserName`, `Password`, `Salt`, `Icon`, `Nick`, `Email`,
 `Ip`, `Address`, `Phone`, `Introduction`, `Remark`, `Sex`, `DeptId`,
 `CreationTime`, `CreatorId`, `LastModifierId`, `LastModificationTime`,
 `OrderNum`, `State`, `ConcurrencyStamp`)
VALUES
('f0020001-0001-4000-8000-000000000001', 0, '超级管理员', 20, 'admin',
 'ANg9hGZC3CMBZsfyTE8-AHmP7A30DrI85LxR1btI4HjNwk-wilA9y2-_L4y_kTS14Qq8uIVOeXdYggrShfvNsQ',
 'EMXTQOSLmpRmBGS4tMz4mw==',
 NULL, '超级管理员', 'admin@example.com',
 NULL, '成都', 13800000000, '泰额平台超级管理员', '平台主库', 0, NULL,
 NOW(), NULL, NULL, NULL,
 999, 1, UUID())
ON DUPLICATE KEY UPDATE
  `IsDeleted`=0,
  `LastModificationTime`=NOW(),
  `Name`=VALUES(`Name`),
  `Nick`=VALUES(`Nick`),
  `Email`=VALUES(`Email`),
  `Password`=VALUES(`Password`),
  `Salt`=VALUES(`Salt`),
  `State`=1,
  `ConcurrencyStamp`=UUID();

INSERT INTO `userrole` (`Id`, `RoleId`, `UserId`)
VALUES ('f0020003-0001-4000-8000-000000000003', 'f0020002-0001-4000-8000-000000000002', 'f0020001-0001-4000-8000-000000000001')
ON DUPLICATE KEY UPDATE
  `RoleId`=VALUES(`RoleId`),
  `UserId`=VALUES(`UserId`);

DELETE FROM `rolemenu` WHERE `RoleId` = 'f0020002-0001-4000-8000-000000000002';

INSERT INTO `rolemenu` (`Id`, `RoleId`, `MenuId`)
VALUES
('f0030001-0001-4000-8000-000000000001', 'f0020002-0001-4000-8000-000000000002', 'f0010001-0001-4000-8000-000000000001'),
('f0030002-0001-4000-8000-000000000002', 'f0020002-0001-4000-8000-000000000002', 'f0010002-0001-4000-8000-000000000002'),
('f0030003-0001-4000-8000-000000000003', 'f0020002-0001-4000-8000-000000000002', 'f0010003-0001-4000-8000-000000000003'),
('f0030004-0001-4000-8000-000000000004', 'f0020002-0001-4000-8000-000000000002', 'f0010010-0001-4000-8000-000000000010'),
('f0030005-0001-4000-8000-000000000005', 'f0020002-0001-4000-8000-000000000002', 'f0010011-0001-4000-8000-000000000011'),
('f0030006-0001-4000-8000-000000000006', 'f0020002-0001-4000-8000-000000000002', 'f0010012-0001-4000-8000-000000000012'),
('f0030007-0001-4000-8000-000000000007', 'f0020002-0001-4000-8000-000000000002', 'f0010013-0001-4000-8000-000000000013'),
('f0030008-0001-4000-8000-000000000008', 'f0020002-0001-4000-8000-000000000002', 'f0010014-0001-4000-8000-000000000014'),
('f0030009-0001-4000-8000-000000000009', 'f0020002-0001-4000-8000-000000000002', 'f0010015-0001-4000-8000-000000000015'),
('f0030010-0001-4000-8000-000000000010', 'f0020002-0001-4000-8000-000000000002', 'f0010020-0001-4000-8000-000000000020'),
('f0030011-0001-4000-8000-000000000011', 'f0020002-0001-4000-8000-000000000002', 'f0010021-0001-4000-8000-000000000021'),
('f0030012-0001-4000-8000-000000000012', 'f0020002-0001-4000-8000-000000000002', 'f0010022-0001-4000-8000-000000000022'),
('f0030013-0001-4000-8000-000000000013', 'f0020002-0001-4000-8000-000000000002', 'f0010023-0001-4000-8000-000000000023'),
('f0030014-0001-4000-8000-000000000014', 'f0020002-0001-4000-8000-000000000002', 'f0010024-0001-4000-8000-000000000024'),
('f0030015-0001-4000-8000-000000000015', 'f0020002-0001-4000-8000-000000000002', 'f0010025-0001-4000-8000-000000000025'),
('f0030016-0001-4000-8000-000000000016', 'f0020002-0001-4000-8000-000000000002', 'f0010026-0001-4000-8000-000000000026'),
('f0030017-0001-4000-8000-000000000017', 'f0020002-0001-4000-8000-000000000002', 'f0010030-0001-4000-8000-000000000030'),
('f0030018-0001-4000-8000-000000000018', 'f0020002-0001-4000-8000-000000000002', 'f0010031-0001-4000-8000-000000000031'),
('f0030019-0001-4000-8000-000000000019', 'f0020002-0001-4000-8000-000000000002', 'f0010032-0001-4000-8000-000000000032'),
('f0030020-0001-4000-8000-000000000020', 'f0020002-0001-4000-8000-000000000002', 'f0010033-0001-4000-8000-000000000033'),
('f0030021-0001-4000-8000-000000000021', 'f0020002-0001-4000-8000-000000000002', 'f0010034-0001-4000-8000-000000000034'),
('f0030022-0001-4000-8000-000000000022', 'f0020002-0001-4000-8000-000000000002', 'f0010035-0001-4000-8000-000000000035'),
('f0030023-0001-4000-8000-000000000023', 'f0020002-0001-4000-8000-000000000002', 'f0010036-0001-4000-8000-000000000036'),
('f0030024-0001-4000-8000-000000000024', 'f0020002-0001-4000-8000-000000000002', 'f0010037-0001-4000-8000-000000000037'),
('f0030025-0001-4000-8000-000000000025', 'f0020002-0001-4000-8000-000000000002', 'f0010038-0001-4000-8000-000000000038'),
('f0030026-0001-4000-8000-000000000026', 'f0020002-0001-4000-8000-000000000002', 'f0010039-0001-4000-8000-000000000039')
ON DUPLICATE KEY UPDATE
  `RoleId` = VALUES(`RoleId`),
  `MenuId` = VALUES(`MenuId`);
