

create trigger HrmCompany_ed on HrmCompany after update as

    if update(companyname)
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int
            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date


            set @id=0


            set @ftype=0


            set @fobjid=0


            select @fecid=id,@uuid=uuid,@fnumber='00',@fname=companyname from inserted


            set @fclass=0
            set @fclassname='行政维度'



            select @fleader=cast(fvalue as int) from jcl_info where id=1

            select @fleaderimg=messagerurl,@fleadername=lastname,@fleaderjobid=jobtitle
            from hrmresource where id=@fleader

            select @fleaderjob=jobtitlemark from hrmjobtitles where id=@fleaderjobid


            select @fleaderst=isnull(field100008,''),@fleaderlv=isnull(field100007,'') from cus_fielddata 
            where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fleader


            set @fparentid=-1


            set @fobjparentid=0


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            select @fplan=sum(isnull(staff_num,0))  from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=@fdatebegin and time_end>=@fdatebegin)


            select @fonjob=count(1) from hrmresource where status<=3


            set @fisvitual=0


            delete from jcl_org_map where fobjid=0 and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where fobjid=0 and fdateend>@fdatebegin

            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)
            values
            (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
            @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
            @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
    end
go


create trigger JCL_ORG_COMP_ed on JCL_ORG_COMP after update,insert as

    if update(comp_name) or update(comp_principal) or update(parent_company) or update(forbidden_tag)
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int

            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date
            declare @delete int

            set @ftype=1


            select @fobjid=id,@id=id,@uuid=uuid,@fnumber=comp_no,@fname=comp_name,
                   @fleader=comp_principal,@fparentid=isnull(parent_company,0),
                   @fobjparentid=isnull(parent_company,0),@delete=isnull(forbidden_tag,0)
            from inserted


            select @fecid=id from HrmSubCompany where uuid=@uuid


            set @fclass=0
            set @fclassname='行政维度'


            select @fleaderimg=messagerurl,@fleadername=lastname,@fleaderjobid=jobtitle
            from hrmresource where id=@fleader

            select @fleaderjob=jobtitlemark from hrmjobtitles where id=@fleaderjobid


            select @fleaderst=isnull(field100008,''),@fleaderlv=isnull(field100007,'') from cus_fielddata 
            where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fleader


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            select @fplan=sum(isnull(staff_num,0))  from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=@fdatebegin and time_end>=@fdatebegin)
            and comp_id=@fobjid;


            select @fonjob=count(1) from hrmresource where status<=3 and subcompanyid1=@fecid


            set @fisvitual=0
			SET NOCOUNT ON

            delete from jcl_org_map where ftype=1 and fobjid=@fobjid and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where ftype=1 and fobjid=@fobjid and fdateend>@fdatebegin


            if @delete=0
                begin
                    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)
                    values
                    (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
                    @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
                    @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
                end
			SET NOCOUNT OFF
    end
go

create trigger JCL_ORG_COMP_del on JCL_ORG_COMP after update as
    if update(delete_type)
    begin

        declare @deletetype int
        declare @ftype int
        declare @fobjid int
        declare @fdatebegin date

        set @ftype=1
        select @fobjid=id from deleted
        select @fdatebegin=cast(GETDATE() as date)

        select @deletetype=isnull(delete_type,0) from inserted

        if @deletetype=1
            begin
                delete from jcl_org_map where ftype=1 and fobjid=@fobjid and fdatebegin=@fdatebegin


                update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
                where ftype=1 and fobjid=@fobjid and fdateend>@fdatebegin
            end
    end
go




create trigger JCL_ORG_dept_ed on JCL_ORG_dept after update,insert as

    if update(dept_name) or update(dept_principal) or update(parent_comp) or update(parent_dept) or update(forbidden_tag)
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int

            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date
            declare @delete int

            declare @st int

            set @st=100000000

            set @ftype=2


            select @fobjid=id,@id=id+@st,@uuid=uuid,@fnumber=dept_no,@fname=dept_name,
                   @fleader=dept_principal,
                   @fparentid=(case isnull(parent_dept,0) when 0 then parent_comp else parent_dept+@st end),
                   @fobjparentid=(case isnull(parent_dept,0) when 0 then parent_comp else parent_dept end),
                   @delete=isnull(forbidden_tag,0)
            from inserted


            select @fecid=id from HrmDepartment where uuid=@uuid

            set @fclass=0
            set @fclassname='行政维度'


            select @fleaderimg=messagerurl,@fleadername=lastname,@fleaderjobid=jobtitle
            from hrmresource where id=@fleader

            select @fleaderjob=jobtitlemark from hrmjobtitles where id=@fleaderjobid


            select @fleaderst=isnull(field100008,''),@fleaderlv=isnull(field100007,'') from cus_fielddata 
            where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fleader


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            select @fplan=sum(isnull(staff_num,0))  from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=@fdatebegin and time_end>=@fdatebegin)
            and dept_id=@fobjid


            select @fonjob=count(1) from hrmresource where status<=3 and departmentid=@fecid


            set @fisvitual=0
			SET NOCOUNT ON

            delete from jcl_org_map where ftype=2 and fobjid=@fobjid and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where ftype=2 and fobjid=@fobjid and fdateend>@fdatebegin


            if @delete=0
                begin
                    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)
                    values
                    (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
                    @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
                    @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
                end
			SET NOCOUNT OFF
    end
go

create trigger JCL_ORG_dept_del on JCL_ORG_dept after update as
if update(delete_type)
    begin
        declare @deletetype int
        declare @ftype int
        declare @fobjid int
        declare @fdatebegin date

        set @ftype=2
        select @fobjid=id from deleted
        select @fdatebegin=cast(GETDATE() as date)
        select @deletetype=isnull(delete_type,0) from inserted
        if @deletetype=1
            begin
                delete from jcl_org_map where ftype=@ftype and fobjid=@fobjid and fdatebegin=@fdatebegin


                update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
                where ftype=@ftype and fobjid=@fobjid and fdateend>@fdatebegin
            end
    end
go



create trigger JCL_ORG_job_ed on JCL_ORG_job after update,insert as

    if update(job_name) or update(parent_dept) or update(parent_job) or update(forbidden_tag)
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int

            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date
            declare @delete int
            declare @parentdept int

            declare @st int

            set @st=100000000

            declare @sj int

            set @sj=200000000


            set @ftype=3


            select @fobjid=id,@id=id+@sj,@fnumber=job_no,@fname=job_name,
                   @fparentid=(case isnull(parent_job,0) when 0 then parent_dept+@st else parent_job+@sj end),
                   @fobjparentid=(case isnull(parent_job,0) when 0 then parent_dept else parent_job end),
                   @delete=isnull(forbidden_tag,0),@parentdept=parent_dept
            from inserted


            set @fclass=0
            set @fclassname='行政维度'


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            select @fplan=sum(isnull(staff_num,0))  from JCL_ORG_STAFF
            where plan_id in (select id from JCL_ORG_STAFFPLAN
            where time_start<=@fdatebegin and time_end>=@fdatebegin)
            and job_id=@fobjid


            select @fonjob=count(1) from hrmresource where status<=3
            and departmentid=(select id from hrmdepartment where uuid=(select uuid from JCL_ORG_DEPT where id=@parentdept))
            and jobtitle in (select id from hrmjobtitles where JOBTITLENAME=@fname);


            set @fisvitual=0
			SET NOCOUNT ON

            delete from jcl_org_map where ftype=@ftype and fobjid=@fobjid and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where ftype=@ftype and fobjid=@fobjid and fdateend>@fdatebegin


            if @delete=0
                begin
                    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)
                    values
                    (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
                    @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
                    @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
                end
			SET NOCOUNT OFF
    end
go

create trigger JCL_ORG_job_del on JCL_ORG_job after update as
if update(delete_type)
    begin
        declare @deletetype int
        declare @ftype int
        declare @fobjid int
        declare @fdatebegin date

        set @ftype=3
        select @fobjid=id from deleted
        select @fdatebegin=cast(GETDATE() as date)
        select @deletetype=isnull(delete_type,0) from inserted
        if @deletetype=1
            begin
                delete from jcl_org_map where ftype=@ftype and fobjid=@fobjid and fdatebegin=@fdatebegin


                update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
                where ftype=@ftype and fobjid=@fobjid and fdateend>@fdatebegin
            end
    end
go



create trigger hrmresource_ed on hrmresource after update,insert as

    if update(lastname) or update(status)
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int

            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date
            declare @delete int
            declare @wz int
            declare @str varchar(100)


            declare @st int

            set @st=100000000

            declare @sj int

            set @sj=200000000

            declare @sk int

            set @sk=300000000


            set @ftype=4


            select @fobjid=id,@id=id+@sk,@fnumber=workcode,@fname=lastname,
                   @delete=case when status<=3 then 0 else 1 end,
                   @fleaderimg=messagerurl,@fleadername=lastname,@fleaderjobid=jobtitle
            from inserted


            select @str=isnull(field100002,'')
            from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fobjid
            set @wz=charindex('_',@str)
            set @fobjparentid=cast(right(@str,len(@str)-@wz) as int)
            set @fparentid=@fobjparentid+@sj


            set @fclass=0
            set @fclassname='行政维度'

            select @fleaderjob=jobtitlemark from hrmjobtitles where id=@fleaderjobid


            select @fleaderst=isnull(field100008,''),@fleaderlv=isnull(field100007,'') from cus_fielddata 
            where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fobjid


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            set @fisvitual=0


            delete from jcl_org_map where ftype=@ftype and fobjid=@fobjid and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where ftype=@ftype and fobjid=@fobjid and fdateend>@fdatebegin


            if @delete=0
                begin
                    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)
                    values
                    (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
                    @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
                    @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
                end
    end
go


create trigger cus_fielddata_jclgw on cus_fielddata after update as

	if update(field100002)
	
        begin
            declare @ftype int
            declare @id int
            declare @fobjid int
            declare @fecid int
            declare @uuid varchar(100)
            declare @fclass int
            declare @fclassname varchar(100)
            declare @fnumber varchar(100)
            declare @fname varchar(255)
            declare @fleader int

            declare @fleaderimg varchar(255)
            declare @fleadername varchar(100)
            declare @fleaderjobid int
            declare @fleaderjob varchar(255)
            declare @fleaderlv varchar(100)
            declare @fleaderst varchar(100)
            declare @fparentid int
            declare @fobjparentid int
            declare @fplan int
            declare @fonjob int
            declare @fisvitual int
            declare @fdatebegin date
            declare @fdateend date
            declare @delete int
            declare @wz int
            declare @str varchar(100)
        
            declare @st int

            set @st=100000000

            declare @sj int

            set @sj=200000000

            declare @sk int

            set @sk=300000000


            set @ftype=4


            select @fobjid=id,@id=id+@sk,@str=isnull(field100002,'') from inserted
            
            select @fnumber=workcode,@fname=lastname,
                   @delete=case when status<=3 then 0 else 1 end,
                   @fleaderimg=messagerurl,@fleadername=lastname,@fleaderjobid=jobtitle
            from hrmresource where id=@fobjid


            set @wz=charindex('_',@str)
            set @fobjparentid=cast(right(@str,len(@str)-@wz) as int)
            set @fparentid=@fobjparentid+@sj

            set @fclass=0
            set @fclassname='行政维度'

            select @fleaderjob=jobtitlemark from hrmjobtitles where id=@fleaderjobid


            select @fleaderst=isnull(field100008,''),@fleaderlv=isnull(field100007,'') from cus_fielddata 
            where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@fobjid


            select @fdatebegin=cast(GETDATE() as date)


            set @fdateend='2099-12-31'


            set @fisvitual=0


            delete from jcl_org_map where ftype=@ftype and fobjid=@fobjid and fdatebegin=@fdatebegin


            update jcl_org_map set fdateend=dateadd(day,-1,@fdatebegin)
            where ftype=@ftype and fobjid=@fobjid and fdateend>@fdatebegin


            if @delete=0
                begin
                    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)
                    values
                    (@id,@ftype,@fobjid,@fecid,@uuid,@fclass,@fclassname,@fnumber,@fname,@fleader,@fleaderimg,
                    @fleadername,@fleaderjobid,@fleaderjob,@fleaderlv,@fleaderst,@fparentid,@fobjparentid,
                    @fplan,@fonjob,@fisvitual,@fdatebegin,@fdateend)
                end
    end
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,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.field100007,g.field100008,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 cus_fielddata g on c.id=g.id and g.scope='HrmCustomFieldByInfoType' and g.scopeid=3
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,a.dept_principal,c.messagerurl,
       c.lastname,c.jobtitle,d.jobtitlemark,g.field100007,g.field100008,
       (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 cus_fielddata g on c.id=g.id and g.scope='HrmCustomFieldByInfoType' and g.scopeid=3
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 and a.id>=10
go


delete from cus_fielddata where scopeid=-1
go
insert into cus_fielddata(scope, scopeid, id, field100002)
select 'HrmCustomFieldByInfoType',-1,a.id,'199_'+cast(e.id as varchar(10))
from hrmresource a
left join HrmJobTitles b on a.jobtitle=b.id
left join hrmdepartment c on a.departmentid=c.id
left join JCL_ORG_DEPT d on c.uuid=d.uuid
left join jcl_org_job e on d.id=e.parent_dept and e.job_name=b.jobtitlename
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,a.id,a.uuid,0,'行政维度',a.workcode,a.lastname,a.messagerurl,a.jobtitle,
		b.jobtitlemark,c.field100007,c.field100008,
       cast(right(isnull(field100002,''),len(isnull(field100002,''))-charindex('_',isnull(field100002,''))) as int)+200000000,
       cast(right(isnull(field100002,''),len(isnull(field100002,''))-charindex('_',isnull(field100002,''))) as int),
       0,cast(GETDATE() as date),'2099-12-31'
from hrmresource a
left join hrmjobtitles b on a.jobtitle=b.id
left join cus_fielddata c on a.id=c.id and c.scope='HrmCustomFieldByInfoType' and c.scopeid=-1
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