create table "RUOYI-VUE-PRO".SYSTEM_USER_DEPT (
                                                  ID          BIGINT primary key,
                                                  USER_ID     BIGINT not null,
                                                  DEPT_ID     BIGINT not null,
                                                  TENANT_ID   BIGINT default 0 not null,
                                                  REMARK      VARCHAR(2000),
                                                  CREATOR     VARCHAR(256) default '',
                                                  CREATE_TIME TIMESTAMP default CURRENT_TIMESTAMP not null,
                                                  UPDATER     VARCHAR(256) default '',
                                                  DELETED       TINYINT default 0 not null,
                                                  UPDATE_TIME TIMESTAMP default CURRENT_TIMESTAMP not null
);

comment on table "RUOYI-VUE-PRO".SYSTEM_USER_DEPT is '用户与部门关系表';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.ID is '主键ID';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.USER_ID is '用户ID';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.DEPT_ID is '部门ID';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.TENANT_ID is '租户编号';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.REMARK is '备注';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.CREATOR is '创建者';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.CREATE_TIME is '创建时间';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.UPDATER is '更新者';
comment on column "RUOYI-VUE-PRO".SYSTEM_USER_DEPT.UPDATE_TIME is '更新时间';

-- 1. 将原有用户表中的 DEPT_ID 字段数据迁移到关系表（ID 自增，无需指定）
insert into "RUOYI-VUE-PRO".SYSTEM_USER_DEPT (
    USER_ID, DEPT_ID, TENANT_ID, REMARK, CREATOR, CREATE_TIME, UPDATER, UPDATE_TIME
)
select
    ID as USER_ID,
    DEPT_ID,
    TENANT_ID,
    null as REMARK,
    CREATOR,
    CREATE_TIME,
    UPDATER,
    UPDATE_TIME
from "RUOYI-VUE-PRO".SYSTEM_USERS
where DEPT_ID is not null;

-- 移除用户表中的 DEPT_ID 字段
alter table "RUOYI-VUE-PRO".SYSTEM_USERS drop column DEPT_ID;