

update t_department set `NAME`=CONCAT(`NAME`,'-001') where id in 
(select
max_id
from (
select max(id) as max_id,`NAME`,DOMAIN_ID,count(1) as ct from t_department group by `NAME`,DOMAIN_ID) as y where ct>1)

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=0 and DOMAIN_ID in (select id from tenant_organizations) 
order by LEVELS asc;

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=1 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=2 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=3 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;


INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=4 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=5 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;

INSERT INTO `obpm2`.`tenant_departments`(`id`, `code`, `created_time`, `display_order`, `index_tree`, `last_updated_time`, `level`, `name`, `permission_config`, `permission_type`, `status`, `total_member`, `organization_id`, `parent_id`) 
select id,id,IFNULL(LASTMODIFYTIME,CURRENT_TIMESTAMP),IFNULL(ORDERBYNO,0),INDEXCODE,LASTMODIFYTIME,LEVELS,`NAME`,null,0,1,100,DOMAIN_ID,SUPERIOR from t_department
where id not in (select id from tenant_departments) and LEVELS=6 and DOMAIN_ID in (select id from tenant_organizations) 
and SUPERIOR in (select id from tenant_departments)
order by LEVELS asc;




INSERT INTO `obpm2`.`tenant_employees`(`id`, `created_time`,  `last_updated_time`,`master_slave_type`, `occupation_type`,`organization_id`, `superior_id`, `tenant_user_id`)
select id,LASTMODIFYTIME,LASTMODIFYTIME,2,1,DOMAINID,SUPERIOR,ID
from t_user where  id in (select id from tenant_users) and DOMAINID in (select id from tenant_organizations) and SUPERIOR is  null;

insert into tenant_employee_relations
(id,created_time,employee_id,organization_id,tenant_user_id)
select  id,created_time,id,organization_id,id
from tenant_employees id where id in (select id from tenant_users)








