DELIMITER $$
create trigger HrmCompany_ed  after update  on HrmCompany FOR EACH ROW
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;
    if NEW.companyname <> OLD.companyname then
	    set _id=0;
	    set _ftype=0;
	    set _fobjid=0;
	
	    set _fecid=new.id;
	    set _uuid=new.uuid;
	    set _fnumber='00';
	    set _fname=new.companyname;
	
	    set _fclass=0;
	    set _fclassname='行政维度';
	
	    select cast(fvalue as SIGNED) into  _fleader from jcl_info where id=1;
	
	    select messagerurl, lastname, jobtitle into _fleaderimg, _fleadername, _fleaderjobid from hrmresource where id=_fleader;
	
	    select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;
	
	
	    select ifnull(field100008,''), ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fleader;
	
	    set _fparentid=-1;
	
	    set _fobjparentid=0;
	
	    select cast(now() as date) into _fdatebegin;
	
	    set _fdateend='2099-12-31';
	
	    select sum(ifnull(staff_num,0)) into _fplan from JCL_ORG_STAFF
	    where plan_id in (select id from JCL_ORG_STAFFPLAN
	    where time_start<=_fdatebegin and time_end>=_fdatebegin);
	
	    select count(1) into _fonjob 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=date_add(_fdatebegin,interval -1 day) 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 if;
END $$
DELIMITER ;


DELIMITER $$
create trigger JCL_ORG_COMP_in  after INSERT  on JCL_ORG_COMP FOR EACH ROW
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;
	
	set _fobjid=new.id;
	set _id=new.id;
	set _uuid=new.uuid;
	set _fnumber=new.comp_no;
	set _fname=new.comp_name;
	set _fleader=new.comp_principal;
	set _fparentid=ifnull(new.parent_company,0);
	set _fobjparentid=ifnull(new.parent_company,0);
	set _delete=ifnull(new.forbidden_tag,0);
	
	select id into _fecid from HrmSubCompany where uuid=_uuid;
	
	set _fclass=0;
	set _fclassname='行政维度';
	
	select messagerurl, lastname, jobtitle into _fleaderimg,_fleadername,_fleaderjobid from hrmresource where id=_fleader;
	
	select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;
	
	select ifnull(field100008,''),ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fleader;
	
	select cast(now() as date) into _fdatebegin;
	
	set _fdateend='2099-12-31';
	
	select sum(ifnull(staff_num,0)) into _fplan 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 count(1) into _fonjob from hrmresource where status<=3 and subcompanyid1=_fecid;
	
	set _fisvitual=0;
	
	delete from jcl_org_map where ftype=1 and fobjid=_fobjid and fdatebegin=_fdatebegin;
	
	update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=1 and fobjid=_fobjid and fdateend>_fdatebegin;
	
	if _delete=0 then
        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 if;
END $$
DELIMITER ;

DELIMITER $$
create trigger JCL_ORG_COMP_ed  after update  on JCL_ORG_COMP FOR EACH ROW
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;
	if NEW.comp_name <> OLD.comp_name or NEW.comp_principal <> OLD.comp_principal or NEW.parent_company <> OLD.parent_company or NEW.forbidden_tag <> OLD.forbidden_tag then
		set _ftype=1;
		
		set _fobjid=new.id;
		set _id=new.id;
		set _uuid=new.uuid;
		set _fnumber=new.comp_no;
		set _fname=new.comp_name;
		set _fleader=new.comp_principal;
		set _fparentid=ifnull(new.parent_company,0);
		set _fobjparentid=ifnull(new.parent_company,0);
		set _delete=ifnull(new.forbidden_tag,0);
		
		select id into _fecid from HrmSubCompany where uuid=_uuid;
		
		set _fclass=0;
		set _fclassname='行政维度';
		
		select messagerurl, lastname, jobtitle into _fleaderimg,_fleadername,_fleaderjobid from hrmresource where id=_fleader;
		
		select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;
		
		select ifnull(field100008,''),ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fleader;
		
		select cast(now() as date) into _fdatebegin;
		
		set _fdateend='2099-12-31';
		
		select sum(ifnull(staff_num,0)) into _fplan 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 count(1) into _fonjob from hrmresource where status<=3 and subcompanyid1=_fecid;
		
		set _fisvitual=0;
		
		delete from jcl_org_map where ftype=1 and fobjid=_fobjid and fdatebegin=_fdatebegin;
		
		update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=1 and fobjid=_fobjid and fdateend>_fdatebegin;
		
		if _delete=0 then
	        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 if;
    end if;
END $$
DELIMITER ;


DELIMITER $$
create trigger JCL_ORG_COMP_del  after delete  on JCL_ORG_COMP FOR EACH ROW
begin
    declare _ftype int;
    declare _fobjid int;
    declare _fdatebegin date;

    set _ftype=1;
    set _fobjid =old.id;
    select cast(now() as date) into _fdatebegin;

    delete from jcl_org_map where ftype=1 and fobjid=_fobjid and fdatebegin=_fdatebegin;


    update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=1 and fobjid=_fobjid and fdateend>_fdatebegin;
END $$
DELIMITER ;


DROP trigger IF EXISTS JCL_ORG_DEPT_in;
DELIMITER $$
create trigger JCL_ORG_DEPT_in  after INSERT on JCL_ORG_DEPT FOR EACH ROW
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;


       set _fobjid = new.id;
       set _id = new.id+_st;
       set _uuid = new.uuid;
       set _fnumber = new.dept_no;
       set _fname = new.dept_name;
       set _fleader = new.dept_principal;
       set _fparentid =(case ifnull(new.parent_dept,0) when 0 then new.parent_comp else new.parent_dept+_st end);
       set _fobjparentid = (case ifnull(new.parent_dept,0) when 0 then new.parent_comp else new.parent_dept end);
       set _delete = ifnull(new.forbidden_tag,0);



        select id into _fecid from HrmDepartment where uuid=_uuid;

        set _fclass=0;
        set _fclassname='行政维度';


        select messagerurl,lastname,jobtitle into _fleaderimg,_fleadername,_fleaderjobid from hrmresource where id=_fleader;

        select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;


        select ifnull(field100008,''),ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fleader;


        select cast(now() as date) into _fdatebegin;


        set _fdateend='2099-12-31';


        select sum(ifnull(staff_num,0)) into _fplan  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 count(1) into _fonjob from hrmresource where status<=3 and departmentid=_fecid;


        set _fisvitual=0;

        delete from jcl_org_map where ftype=2 and fobjid=_fobjid and fdatebegin=_fdatebegin;


        update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=2 and fobjid=_fobjid and fdateend>_fdatebegin;


        if _delete=0 then 
            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 if;
END $$
DELIMITER ;

DROP trigger IF EXISTS JCL_ORG_dept_ed;
DELIMITER $$
create trigger JCL_ORG_dept_ed  after update  on JCL_ORG_DEPT FOR EACH ROW
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;
       if (NEW.dept_name <> OLD.dept_name or NEW.dept_principal <> OLD.dept_principal or NEW.parent_dept <> OLD.parent_dept or NEW.forbidden_tag <> OLD.forbidden_tag) then
            set _st=100000000;

            set _ftype=2;

           set _fobjid = new.id;
           set _id = new.id+_st;
           set _uuid = new.uuid;
           set _fnumber = new.dept_no;
           set _fname = new.dept_name;
           set _fleader = new.dept_principal;
           set _fparentid = (case ifnull(new.parent_dept,0) when 0 then new.parent_comp else new.parent_dept+_st end);
           set _fobjparentid = (case ifnull(new.parent_dept,0) when 0 then new.parent_comp else new.parent_dept end);
           set _delete = ifnull(new.forbidden_tag,0);


            select id into _fecid from HrmDepartment where uuid=_uuid;

            set _fclass=0;
            set _fclassname='行政维度';


            select messagerurl, lastname, jobtitle into _fleaderimg,_fleadername,_fleaderjobid from hrmresource where id=_fleader;

            select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;


            select ifnull(field100008,''), ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fleader;


            select cast(now() as date) into _fdatebegin;


            set _fdateend='2099-12-31';


            select sum(ifnull(staff_num,0)) into _fplan  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 count(1) into _fonjob from hrmresource where status<=3 and departmentid=_fecid;


            set _fisvitual=0;

            delete from jcl_org_map where ftype=2 and fobjid=_fobjid and fdatebegin=_fdatebegin;


            update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=2 and fobjid=_fobjid and fdateend>_fdatebegin;


            if _delete=0 then
                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 if;
    end if;
END $$
DELIMITER ;



DELIMITER $$
create trigger JCL_ORG_dept_del  after delete  on JCL_ORG_dept FOR EACH ROW
BEGIN
    declare _ftype int;
    declare _fobjid int;
    declare _fdatebegin date;

    set _ftype=2;
    set _fobjid = old.id;
    select cast(now() as date) into _fdatebegin;

    delete from jcl_org_map where ftype=_ftype and fobjid=_fobjid and fdatebegin=_fdatebegin;

    update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;
END $$
DELIMITER ;

DELIMITER $$
create trigger JCL_ORG_JOB_in  after INSERT  on JCL_ORG_JOB FOR EACH ROW
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;
    declare _sj int;

    set _st=100000000;

    

    set _sj=200000000;


    set _ftype=3;

	set _fobjid  = new.id;
	set _id = new.id+_sj;
	set _fnumber = new.job_no;
	set _fname = new.job_name;
	set _fparentid = (case ifnull(new.parent_job,0) when 0 then new.parent_dept+_st else new.parent_job+_sj end);
	set _fobjparentid = (case ifnull(new.parent_job,0) when 0 then new.parent_dept else new.parent_job end);
	set _delete=ifnull(new.forbidden_tag,0);

    set _fclass=0;
    set _fclassname='行政维度';

    select cast(now() as date) into _fdatebegin;


    set _fdateend='2099-12-31';


    select sum(ifnull(staff_num,0)) into _fplan  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;


    set _fonjob=10;


    set _fisvitual=0;

    delete from jcl_org_map where ftype=_ftype and fobjid=_fobjid and fdatebegin=_fdatebegin;


    update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;


    if _delete=0 then
        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 if;
END $$
DELIMITER ;



DELIMITER $$
create trigger JCL_ORG_JOB_ed  after UPDATE  on JCL_ORG_JOB FOR EACH ROW
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;
	declare	_sj int;
	if NEW.job_name <> OLD.job_name or NEW.parent_dept <> OLD.parent_dept or NEW.parent_job <> OLD.parent_job or NEW.forbidden_tag <> OLD.forbidden_tag then
        set _st=100000000;
        set _sj=200000000;
        set _ftype=3;

       set _fobjid=new.id;
       set _id=new.id+_sj;
       set _fnumber=new.job_no;
       set _fname=new.job_name;
       set _fparentid=(case ifnull(new.parent_job,0) when 0 then new.parent_dept+_st else new.parent_job+_sj end);
       set _fobjparentid=(case ifnull(new.parent_job,0) when 0 then new.parent_dept else new.parent_job end);
       set _delete=ifnull(new.forbidden_tag,0);


        set _fclass=0;
        set _fclassname='行政维度';

        select cast(now() as date) into _fdatebegin;


        set _fdateend='2099-12-31';


        select sum(ifnull(staff_num,0)) into _fplan  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;


        set _fonjob=10;


        set _fisvitual=0;

        delete from jcl_org_map where ftype=_ftype and fobjid=_fobjid and fdatebegin=_fdatebegin;


        update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;


        if _delete=0 then
            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 if;
    end if;
END $$
DELIMITER ;





DELIMITER $$
create trigger JCL_ORG_job_del  after delete  on JCL_ORG_JOB FOR EACH ROW
begin
    declare _ftype int;
    declare _fobjid int;
    declare _fdatebegin date;

    set _ftype=3;
	set _fobjid = old.id;
    select cast(now() as date) into _fdatebegin;

    delete from jcl_org_map where ftype=_ftype and fobjid=_fobjid and fdatebegin=_fdatebegin;


    update jcl_org_map set fdateend=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;
END $$
DELIMITER ;


DELIMITER $$
create trigger hrmresource_in  after INSERT  on hrmresource FOR EACH ROW
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;
    declare _sj int;
    declare _sk int;
    set _st=100000000;
    set _sj=200000000;
    set _sk=300000000;
    set _ftype=4;

   set _fobjid=new.id;
   set _id=new.id+_sk;
   set _fnumber=new.workcode;
   set _fname=new.lastname;
   set _delete=case when new.status<=3 then 0 else 1 end;
   set _fleaderimg=new.messagerurl;
   set _fleadername=new.lastname;
   set _fleaderjobid=new.jobtitle;


    select ifnull(field100002,'') into _str from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fobjid;
    set _wz=LOCATE('_',_str);
    set _fobjparentid=cast(right(_str,length(_str)-_wz) as SIGNED);
    set _fparentid=_fobjparentid+_sj;


    set _fclass=0;
    set _fclassname='行政维度';

    select jobtitlemark into  _fleaderjob from hrmjobtitles where id=_fleaderjobid;


    select ifnull(field100008,''),ifnull(field100007,'') into _fleaderst,_fleaderlv  from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fobjid;


    select cast(now() as date) into _fdatebegin;


    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=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;


    if _delete=0 then
        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 if;
END $$
DELIMITER ;


DELIMITER $$
create trigger hrmresource_ed  after update  on hrmresource FOR EACH ROW
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;
    declare _sj int;
    declare _sk int;
	if NEW.lastname <> OLD.lastname or NEW.status <> OLD.status then
        set _st=100000000;
        set _sj=200000000;
        set _sk=300000000;
        set _ftype=4;

       set _fobjid=new.id;
       set _id=new.id+_sk;
       set _fnumber=new.workcode;
       set _fname=new.lastname;
       set _delete=case when new.status<=3 then 0 else 1 end;
       set _fleaderimg=new.messagerurl;
       set _fleadername=new.lastname;
       set _fleaderjobid=new.jobtitle;


        select ifnull(field100002,'') into _str from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fobjid;
        set _wz=LOCATE('_',_str);
        set _fobjparentid=cast(right(_str,length(_str)-_wz) as SIGNED);
        set _fparentid=_fobjparentid+_sj;


        set _fclass=0;
        set _fclassname='行政维度';

        select jobtitlemark into  _fleaderjob from hrmjobtitles where id=_fleaderjobid;


        select ifnull(field100008,''),ifnull(field100007,'') into _fleaderst,_fleaderlv  from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fobjid;


        select cast(now() as date) into _fdatebegin;


        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=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;


        if _delete=0 then
            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 if;
    end if;
END $$
DELIMITER ;


DROP trigger IF EXISTS cus_fielddata_jclgw;
DELIMITER $$
create trigger cus_fielddata_jclgw  after UPDATE on cus_fielddata FOR EACH ROW
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;
    declare _sj int;
	declare _sk int;
	if NEW.field100002 <> OLD.field100002  then
        set _st=100000000;
        set _sj=200000000;
        set _sk=300000000;
        set _ftype=4;


        set _fobjid=new.id;
        set _id=new.id+_sk;
        set _str=ifnull(new.field100002,'');
        
        select workcode, lastname, case when status <= 3 then 0 else 1 end, messagerurl, lastname, jobtitle into _fnumber, _fname , _delete, _fleaderimg , _fleadername, _fleaderjobid from hrmresource where id = _fobjid;


        set _wz=LOCATE('_',_str);
        set _fobjparentid=cast(right(_str,length(_str)-_wz) as SIGNED);
        set _fparentid=_fobjparentid+_sj;

        set _fclass=0;
        set _fclassname='行政维度';

        select jobtitlemark into _fleaderjob from hrmjobtitles where id=_fleaderjobid;


        select ifnull(field100008,''), ifnull(field100007,'') into _fleaderst,_fleaderlv from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=_fobjid;


        select cast(now() as date) into _fdatebegin;


        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=date_add(_fdatebegin,interval -1 day) where ftype=_ftype and fobjid=_fobjid and fdateend>_fdatebegin;


        if _delete=0 then
            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 if;
    end if;
END $$
DELIMITER ;




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,ifnull(parent_company,0),ifnull(parent_company,0),
       ifnull(e.fcnt,0),ifnull(f.fcnt,0),0,NOW(),'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(ifnull(staff_num,0))  fcnt from JCL_ORG_STAFF
                   where plan_id in (select id from JCL_ORG_STAFFPLAN
                                     where time_start<=NOW() and time_end>NOW())
                   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 ifnull(a.delete_type,0) <>1 and ifnull(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.field100007,g.field100008,
       (case ifnull(parent_dept,0) when 0 then parent_comp else parent_dept+100000000 end),
       (case ifnull(parent_dept,0) when 0 then parent_comp else parent_dept end),
       ifnull(e.fcnt,0),ifnull(f.fcnt,0),0,NOW(),'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(ifnull(staff_num,0))  fcnt from JCL_ORG_STAFF
                   where plan_id in (select id from JCL_ORG_STAFFPLAN
                                     where time_start<=NOW() and time_end>NOW())
                   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 ifnull(a.delete_type,0) <>1 and ifnull(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 ifnull(parent_job,0) when 0 then parent_dept+100000000 else parent_job+200000000 end),
       (case ifnull(parent_job,0) when 0 then parent_dept else parent_job end),
       ifnull(e.fcnt,0),ifnull(f.fcnt,0),0,NOW(),'2099-12-31'
from JCL_ORG_job as a
         left join (select job_id,sum(ifnull(staff_num,0))  fcnt from JCL_ORG_STAFF
                    where plan_id in (select id from JCL_ORG_STAFFPLAN
                                      where time_start<=NOW() and time_end>NOW())
                    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 ifnull(a.delete_type,0) <>1 and ifnull(a.forbidden_tag,0) <>1 and a.id>=10;


delete from cus_fielddata where scopeid=-1;
insert into cus_fielddata(scope, scopeid, id, field100002)
select 'HrmCustomFieldByInfoType',-1,a.id,CONCAT('199_',e.id)
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;

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,
    right(ifnull(field100002,''),LENGTH(ifnull(field100002,''))-LOCATE('_',ifnull(field100002,''))) +200000000,
    right(ifnull(field100002,''),LENGTH(ifnull(field100002,''))-LOCATE('_',ifnull(field100002,''))),
    0,NOW(),'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;