#1.用户层级关系表
#创建表
drop table if exists `application`.app_user_lvl_info;
create table `application`.app_user_lvl_info
(
lvl  varchar(100) NOT NULL COMMENT '运营id',
lvl_name varchar(100) comment '运营名称',
role_id varchar(100) comment '运营角色id',
role_name varchar(100) comment '运营角色名称',
dep_id varchar(100)  comment '运营部门id',
org_code varchar(100) comment '运营组织id',
depart_name varchar(100) comment '运营组织名称',
lvl2 varchar(100) comment '一级经理id',
lvl2_name varchar(100) comment '一级经理名称',
role_id2 varchar(100) comment '一级经理角色id',
role_name2 varchar(100) comment '一级经理角色名称',
lvl3 varchar(100) comment '二级经理id',
lvl3_name varchar(100) comment '二级经理名称',
role_id3 varchar(100) comment '二级经理角色id',
role_name3 varchar(100) comment '二级经理角色名称',
lvl4 varchar(100) comment '三级经理id',
lvl4_name varchar(100) comment '三级经理名称',
type  bigint comment '类型',
`data_create_time` timestamp DEFAULT CURRENT_TIMESTAMP,
`data_update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`lvl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='用户层级关系表';


#插入数据
replace into `application`.app_user_lvl_info (lvl,lvl_name,role_id,role_name,dep_id,org_code,depart_name,lvl2,lvl2_name,role_id2,role_name2,lvl3,lvl3_name,role_id3,role_name3,lvl4,lvl4_name,type)
select * from
(select a.id lvl,a.realname lvl_name,c.role_id,c.role_name,g.dep_id,g.org_code,g.depart_name,a.leader_id lvl2,b.realname lvl2_name,d.role_id role_id2,d.role_name role_name2,
b.leader_id lvl3, e.realname lvl3_name,f.role_id role_id3,f.role_name role_name3,e.leader_id lvl4 ,v.realname lvl4_name, case when d.role_id in ('12b1f86a4d4adda3e5ad1739685c74ff','6a0429d0a879c8e884bcb1a169d66678','8e3b10a1e82ef0f742fa219839a8abdc') then 1
     when d.role_id='8d3e2ed58bdedf17716f698ea674da27' then 2 else 0 end type
from `jeecg-boot`.sys_user a
left join `jeecg-boot`.sys_user b
on a.leader_id=b.id
left join
(select a.*,b.role_name from `jeecg-boot`.sys_user_role  a
left join (select * from `jeecg-boot`.sys_role) b
on a.role_id=b.id) c
on a.id=c.user_id
left join
(select a.*,b.role_name from `jeecg-boot`.sys_user_role  a
left join (select * from `jeecg-boot`.sys_role) b
on a.role_id=b.id) d
on a.leader_id=d.user_id
left join `jeecg-boot`.sys_user e
on b.leader_id=e.id
left join `jeecg-boot`.sys_user v
on e.leader_id=v.id
left join
(select a.*,b.role_name from `jeecg-boot`.sys_user_role  a
left join (select * from `jeecg-boot`.sys_role) b
on a.role_id=b.id) f
on b.leader_id=f.user_id
left join
(select m.user_id,m.dep_id,n.org_code,n.depart_name from `jeecg-boot`.sys_user_depart m
left join `jeecg-boot`.sys_depart n
on m.dep_id=n.id)g
on a.id=g.user_id
where a.status=1 ) v

#2.用户与经理关系表
#创建表
drop table if exists `application`.app_user_manage;
create table `application`.app_user_manage
(
user_id   varchar(100) NOT NULL COMMENT '用户id',
user_name varchar(200) comment '用户名称',
manage_id  varchar(100) NOT NULL comment '经理id',
manage_name varchar(100) NOT NULL comment '经理名称',
type bigint  comment '类型,1:运营;2:设计;0:其它',
flag bigint  comment '级别',
`data_create_time` timestamp DEFAULT CURRENT_TIMESTAMP,
`data_update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`,`manage_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='用户与经理关系表';
#插入数据
replace into `application`.app_user_manage (user_id,user_name,manage_id,manage_name,type,flag)
select * from
(select distinct lvl user_id ,lvl_name user_name,ifnull(lvl4,'-') manage_id,ifnull(lvl4_name,'-') manage_name,type,3 flag from `application`.app_user_lvl_info
union all
select distinct lvl user_id ,lvl_name user_name,ifnull(lvl3,'-') manage_id,ifnull(lvl3_name,'-') manage_name,type,2 flag from `application`.app_user_lvl_info
union all
select distinct lvl user_id ,lvl_name user_name,ifnull(lvl2,'-') manage_id,ifnull(lvl2_name,'-') manage_name,type,1 flag from `application`.app_user_lvl_info )m