insert into jcl_org_map
(id,ftype,fobjid,fecid,uuid,fclass,fclassname,fnumber,fname,fleader,fleaderimg,
 fleadername,fleaderjobid,fleaderjob,fleaderlv,fleaderst,fparentid,fobjparentid,
 fplan,fonjob,fisvitual,fdatebegin,fdateend)
select 0,0,0,1,uuid,0,'行政维度','00',companyname,0,'','',0,'','','',-1,0,0,0,0,to_date(sysdate),to_date('2099-12-31','yyyy-mm-dd') from HrmCompany
/

insert into  jcl_org_map
(id,ftype,fobjid,fecid,uuid,fclass,fclassname,fnumber,fname,fleader,fleaderimg,
 fleadername,fleaderjobid,fleaderjob,fleaderlv,fleaderst,fparentid,fobjparentid,
 fplan,fonjob,fisvitual,fdatebegin,fdateend)
select a.id,1,a.id,b.id,a.uuid,0,'行政维度',a.comp_no,a.comp_name,a.comp_principal,c.messagerurl,
       c.lastname,c.jobtitle,d.jobtitlemark,g.job_level,g.job_grade,nvl(parent_company,0),nvl(parent_company,0),
       nvl(e.fcnt,0),nvl(f.fcnt,0),0,to_date(sysdate),to_date('2099-12-31','yyyy-mm-dd')
from
    JCL_ORG_comp a
        left join HrmSubCompany b on a.uuid=b.uuid
        left join hrmresource c on a.comp_principal=c.id
        left join hrmjobtitles d on c.jobtitle=d.id
        left join (select comp_id,sum(nvl(staff_num,0))  fcnt from JCL_ORG_STAFF
                   where plan_id in (select id from JCL_ORG_STAFFPLAN
                                     where time_start<=to_date(sysdate) and time_end>to_date(sysdate))
                   group by comp_id) e on a.id=e.comp_id
        left join (select subcompanyid1,count(1) fcnt from hrmresource where status<=3 group by subcompanyid1) f
                  on f.subcompanyid1=b.id
        left join jcl_org_hrmresource g ON c.uuid = g.uuid
where nvl(a.delete_type,0) <>1 and nvl(a.forbidden_tag,0) <>1
/

insert into  jcl_org_map
(id,ftype,fobjid,fecid,uuid,fclass,fclassname,fnumber,fname,fleader,fleaderimg,
 fleadername,fleaderjobid,fleaderjob,fleaderlv,fleaderst,fparentid,fobjparentid,
 fplan,fonjob,fisvitual,fdatebegin,fdateend)
select a.id+100000000,2,a.id,b.id,a.uuid,0,'行政维度',a.dept_no,a.dept_name,a.dept_principal,c.messagerurl,
       c.lastname,c.jobtitle,d.jobtitlemark,g.job_level,g.job_grade,
       (case nvl(parent_dept,0) when 0 then parent_comp else parent_dept+100000000 end),
       (case nvl(parent_dept,0) when 0 then parent_comp else parent_dept end),
       nvl(e.fcnt,0),nvl(f.fcnt,0),0,to_date(sysdate),to_date('2099-12-31','yyyy-mm-dd')
from
    JCL_ORG_dept a
        left join HrmDepartment b on a.uuid=b.uuid
        left join hrmresource c on a.dept_principal=c.id
        left join hrmjobtitles d on c.jobtitle=d.id
        left join (select dept_id,sum(nvl(staff_num,0))  fcnt from JCL_ORG_STAFF
                   where plan_id in (select id from JCL_ORG_STAFFPLAN
                                     where time_start<=to_date(sysdate) and time_end>to_date(sysdate))
                   group by dept_id) e on a.id=e.dept_id
        left join (select departmentid,count(1) fcnt from hrmresource where status<=3 group by departmentid) f on f.departmentid=b.id
        left join jcl_org_hrmresource g ON c.uuid = g.uuid
where nvl(a.delete_type,0) <>1 and nvl(a.forbidden_tag,0) <>1
/

insert into  jcl_org_map
(id,ftype,fobjid,fclass,fclassname,fnumber,fname,
 fparentid,fobjparentid,
 fplan,fonjob,fisvitual,fdatebegin,fdateend)
select a.id+200000000,3,a.id,0,'行政维度',a.job_no,a.job_name,
       (case nvl(parent_job,0) when 0 then parent_dept+100000000 else parent_job+200000000 end),
       (case nvl(parent_job,0) when 0 then parent_dept else parent_job end),
       nvl(e.fcnt,0),nvl(f.fcnt,0),0,to_date(sysdate),to_date('2099-12-31','yyyy-mm-dd')
from JCL_ORG_job a
         left join (select job_id,sum(nvl(staff_num,0))  fcnt from JCL_ORG_STAFF
                    where plan_id in (select id from JCL_ORG_STAFFPLAN
                                      where time_start<=to_date(sysdate) and time_end>to_date(sysdate))
                    group by job_id) e on a.id=e.job_id
         left join (select d.id,b.jobtitlename,count(1) fcnt
                    from hrmresource a
                             inner join HrmJobTitles b on a.jobtitle=b.id
                             inner join hrmdepartment c on a.departmentid=c.id
                             inner join jcl_org_dept d on a.uuid=b.uuid
                    group by d.id,b.jobtitlename) f
                   on f.id=a.parent_dept and a.job_name=f.jobtitlename
where nvl(a.delete_type,0) <>1 and nvl(a.forbidden_tag,0) <>1
/

insert into  jcl_org_map
(id,ftype,fobjid,fecid,uuid,fclass,fclassname,fnumber,fname,fleaderimg,
 fleaderjobid,fleaderjob,fleaderlv,fleaderst,fparentid,fobjparentid,
 fisvitual,fdatebegin,fdateend)
select a.id+300000000,4,a.id,c.id,a.uuid,0,'行政维度',a.work_code,a.last_name,c.messagerurl,b.id ,
       b.job_name,a.job_level,a.job_grade,
       to_number(nvl(b.id,''))+200000000,
       to_number(nvl(b.id,'')),
       0,to_date(sysdate),to_date('2099-12-31','yyyy-mm-dd')
from jcl_org_hrmresource a
         left join jcl_org_job b ON a.job_title = b.id
         left join hrmresource c on a.uuid = c.uuid
where a.status<4
/