insert into JCL_ORG_COMP(comp_no,comp_name_short,comp_name,parent_company,forbidden_tag,
                         show_order,uuid,creator,delete_type,create_time)
select nvl(subcompanycode,''),subcompanyname,subcompanydesc,nvl(supsubcomid,0),
       nvl(canceled,0) ,nvl(showorder,0),uuid,1,0,SYSDATE
from HrmSubCompany
/

update JCL_ORG_COMP t1 set t1.parent_company=(select c.id
                                              from HrmSubCompany a
                                                       inner join HrmSubCompany b on a.supsubcomid=b.id
                                                       inner join JCL_ORG_COMP c on b.uuid=c.uuid
                                              where a.uuid=t1.uuid)
/

insert into JCL_ORG_DEPT(dept_no, dept_name, dept_name_short,parent_comp,
                         show_order,forbidden_tag,creator,delete_type,create_time,uuid)
select a.departmentcode,a.departmentname,a.departmentmark,a.subcompanyid1,a.showorder,nvl(a.canceled,0),1,0,SYSDATE,uuid
from HrmDepartment a
         left join HrmDepartmentDefined b on a.id=b.deptid
/

update JCL_ORG_DEPT t1
set t1.parent_comp=(select c.id
                    from HrmDepartment a
                             inner join HrmSubCompany b on a.subcompanyid1=b.id
                             inner join JCL_ORG_COMP c on b.uuid=c.uuid
                    where t1.uuid=a.uuid)
/

update JCL_ORG_dept t1
set t1.parent_dept=(select c.id
                    from HrmDepartment  a
                             inner join HrmDepartment b on a.supdepid=b.id
                             inner join JCL_ORG_dept c on b.uuid=c.uuid
                    where t1.uuid=a.uuid)
/

insert into jcl_org_job(job_no, job_name, parent_comp, parent_dept,
                        description, work_duty, work_authority,
                        forbidden_tag, creator, delete_type, create_time,
                        show_order)
select b.jobtitlecode,b.jobtitlename,c.comid,d.deptid,
       cast(b.jobresponsibility as varchar(2000)),cast(b.jobresponsibility as varchar(2000)),
       cast(b.jobcompetency as varchar(2000)),nvl(b.canceled,0),1,0,SYSDATE,0
from (select distinct subcompanyid1,departmentid,jobtitle from HrmResource) a
         inner join HrmJobTitles b on a.jobtitle=b.id
         inner join
     (select a.id ecid,b.id comid from HrmSubCompany a inner join JCL_ORG_COMP b on a.uuid=b.uuid) c
     on a.subcompanyid1=c.ecid
         inner join
     (select a.id ecid,b.id deptid from HrmDepartment a inner join JCL_ORG_DEPT b on a.uuid=b.uuid) d
     on a.departmentid=d.ecid
/