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,g.id,c.messagerurl,
       c.lastname,c.jobtitle,d.jobtitlemark,g.job_level,g.job_grade,isnull(parent_company,0),isnull(parent_company,0),
       isnull(e.fcnt,0),isnull(f.fcnt,0),0,cast(GETDATE() as date),'2099-12-31'
from
JCL_ORG_comp as a
left join HrmSubCompany as b on a.uuid=b.uuid
left join hrmresource as c on a.comp_principal=c.id
left join hrmjobtitles as d on c.jobtitle=d.id
left join (select comp_id,sum(isnull(staff_num,0))  fcnt from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=cast(GETDATE() as date) and time_end>cast(GETDATE() as date))
            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 isnull(a.delete_type,0) <>1 and isnull(a.forbidden_tag,0) <>1
go


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,g.id,c.messagerurl,
       c.lastname,c.jobtitle,d.jobtitlemark,g.job_level,g.job_grade,
       (case isnull(parent_dept,0) when 0 then parent_comp else parent_dept+100000000 end),
       (case isnull(parent_dept,0) when 0 then parent_comp else parent_dept end),
       isnull(e.fcnt,0),isnull(f.fcnt,0),0,cast(GETDATE() as date),'2099-12-31'
from
JCL_ORG_dept as a
left join HrmDepartment as b on a.uuid=b.uuid
left join hrmresource as c on a.dept_principal=c.id
left join hrmjobtitles as d on c.jobtitle=d.id
left join (select dept_id,sum(isnull(staff_num,0))  fcnt from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=cast(GETDATE() as date) and time_end>cast(GETDATE() as date))
            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 isnull(a.delete_type,0) <>1 and isnull(a.forbidden_tag,0) <>1
go


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 isnull(parent_job,0) when 0 then parent_dept+100000000 else parent_job+200000000 end),
       (case isnull(parent_job,0) when 0 then parent_dept else parent_job end),
       isnull(e.fcnt,0),isnull(f.fcnt,0),0,cast(GETDATE() as date),'2099-12-31'
from JCL_ORG_job as a
left join (select job_id,sum(isnull(staff_num,0))  fcnt from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=cast(GETDATE() as date) and time_end>cast(GETDATE() as date))
            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 isnull(a.delete_type,0) <>1 and isnull(a.forbidden_tag,0) <>1
go

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,
       cast(isnull(b.id,'') as int)+200000000,
       cast(isnull(b.id,'') as int),
       0,cast(GETDATE() as date),'2099-12-31'
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
GO

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,cast(getdate() as date),'2099-12-31' from HrmCompany
GO