create view v_jcl_zdzj as
select id*-1 fid,sequence_name fname,0 fparentid,sequence_no fno
from jcl_org_sequence
where ifnull(forbidden_tag,0)<>1 and ifnull(delete_type,0)<>1
union
select zj.id+1000,zj.grade_name,xl.id*-1,zj.grade_no
from JCL_ORG_GRADE zj
inner join jcl_org_sequence xl on zj.scheme_id=xl.scheme_id
where ifnull(zj.forbidden_tag,0)<>1 and ifnull(zj.delete_type,0)<>1
union
select zd.id,zd.level_name,zj.id+1000,zd.level_no
from JCL_ORG_LEVEL zd inner join JCL_ORG_GRADE zj
    on  concat(',' ,zj.level_id ,',') like concat('%,' ,cast(zd.id as SIGNED) ,',%')
where ifnull(zd.forbidden_tag,0)<>1 and ifnull(zd.delete_type,0)<>1;


create view v_jcl_post as
select id*-1 fid,post_name fname,0 fparentid from JCL_ORG_POST
where ifnull(delete_type,0)<>1
union
select id,post_info_name,post_id*-1 from JCL_ORG_POST_INFO
where ifnull(forbidden_tag,0)<>1 and ifnull(delete_type,0)<>1;




DELIMITER $$
CREATE PROCEDURE test()
  BEGIN
   	DECLARE orgid VARCHAR(100);
	DECLARE uuid1 VARCHAR(100);
	DECLARE appid VARCHAR(100);
 	DECLARE modename varchar(100);
	DECLARE uuid2 varchar(100);
	DECLARE treeid varchar(100);
	DECLARE t1 varchar(100);
	DECLARE t2 varchar(100);
	DECLARE t3 varchar(100);

	DECLARE lableid varchar(100);
	DECLARE fieldname varchar(100);
	DECLARE fieldid varchar(100);
	DECLARE fieldcolumn varchar(100);
	DECLARE sqlstr varchar(2000);

    select min(id) into orgid from hrmresource;
    select uuid() into uuid1;
	select '聚才林人事模块' into modename;

    if exists(select 1 from modetreefield where treeFieldName=modename) then 
		select id into appid from modetreefield where treeFieldName=modename;
	else
		insert into modetreefield(treeFieldName, superFieldid, allSuperFieldId, treelevel, showOrder, treeFieldDesc,
                                  isdelete, subcompanyid, icon, iconColor, iconBg,cubeuuid)
        values('聚才林人事模块',1,',0,1',1,-999,'聚才林专项应用（谨慎调整）',0,orgid,
               'icon-cube-app-team','#fff','#4da0f8',uuid1);
        select id into appid from modetreefield where cubeuuid=uuid1;
	end if;


	select uuid() into uuid2 ;
	insert into mode_customtree(treename,creater, createdate, createtime, rootname, defaultaddress,
                            showtype, isselsub, isonlyleaf, appid, treecode, isRefreshTree, isshowsearchtab,
                            searchbrowserid, isQuickSearch, treeremark, combintree, combintreeway, defaulthideleft,
                            showlevel, levelsplitchar, iscancellink,expandfirstnode)
	values('职等职级',1,'2022-07-01','00:00:00','职等职级','',1,0,1,appid,uuid2,0,0,0,0,'','',0,0,0,'/',0,1);

	select id into treeid from mode_customtree where treecode=uuid2;
	select id into t1 from mode_customtree where treecode=uuid2;

	insert into mode_customtreedetail(mainid, nodename, nodedesc, sourcefrom, sourceid, tablename, tablekey, tablesup, showfield,
                                  hreftype, hrefid, hreftarget, hrefrelatefield, nodeicon, supnode, supnodefield, nodefield, showorder,
                                  iconField, dataorder, datacondition, hrefField, rootids, isshowrootnode, isContainsSub)
	values(treeid,'职等职级','',2,0,'v_jcl_zdzj','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','fno','','','',1,0);


	select uuid() into uuid2 ;
	insert into mode_customtree(treename,creater, createdate, createtime, rootname, defaultaddress,
                            showtype, isselsub, isonlyleaf, appid, treecode, isRefreshTree, isshowsearchtab,
                            searchbrowserid, isQuickSearch, treeremark, combintree, combintreeway, defaulthideleft,
                            showlevel, levelsplitchar, iscancellink,expandfirstnode)
	values('职务',1,'2022-07-01','00:00:00','职务','',1,0,1,appid,uuid2,0,0,0,0,'','',0,0,0,'/',0,1);
	select id into treeid from mode_customtree where treecode=uuid2;
	select id into t3 from mode_customtree where treecode=uuid2;
	insert into mode_customtreedetail(mainid, nodename, nodedesc, sourcefrom, sourceid, tablename, tablekey, tablesup, showfield,
                                  hreftype, hrefid, hreftarget, hrefrelatefield, nodeicon, supnode, supnodefield, nodefield, showorder,
                                  iconField, dataorder, datacondition, hrefField, rootids, isshowrootnode, isContainsSub)
	values(treeid,'职务','',2,0,'v_jcl_post','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','','','','',1,0);



	set fieldname='职等职级';
	set fieldid='100001';
	set fieldcolumn= concat('field',fieldid);
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
		select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
		select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',-1,lableid,fieldid,9,1,1,1,fieldname,t1,0);

	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(1000)',3,256,fieldcolumn,fieldcolumn,'');

	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;


	set fieldname='担任岗位';
	set fieldid='100002';
	set fieldcolumn= concat('field',fieldid);
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;


	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',-1,lableid,fieldid,8,1,1,1,fieldname,t2,0);
	
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(1000)',3,666,fieldcolumn,fieldcolumn,'');
	
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;


	set fieldname='担任职务';
	set fieldid='100003';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',-1,lableid,fieldid,10,1,1,1,fieldname,t3,0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(1000)',3,256,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;


	set fieldname='职务序列名称';
	set fieldid='100005';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

	set fieldname='等级方案';
	set fieldid='100006';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
 
	set fieldname='职级';
	set fieldid='100007';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

	set fieldname='职等';
	set fieldid='100008';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

	set fieldname='职务分类';
	set fieldid='100009';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

	set fieldname='职务';
	set fieldid='100010';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'varchar(200)',1,1,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

	set fieldname='虚线上级';
	set fieldid='100004';
	set fieldcolumn= concat('field',fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=fieldname) then 
        select id into lableid from HtmlLabelIndex where indexdesc=fieldname limit 1;
	else
        select min(indexid)-1 into lableid from HtmlLabelInfo;
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,7);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,8);
		insert into HtmlLabelInfo(indexid, labelname, languageid) values(lableid,fieldname,9);
		insert into HtmlLabelIndex(id, indexdesc) values(lableid,fieldname);
	end if;
	insert into cus_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
	                          groupid, hrm_fieldlable, dmlUrl, ismodify)
	values('HrmCustomFieldByInfoType',3,lableid,fieldid,1,0,1,5,fieldname,'',0);
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
	values (fieldid,'text',3,17,fieldcolumn,fieldcolumn,'');
	set sqlstr=concat('alter table cus_fielddata add ',fieldcolumn,' varchar(1000)');
	set @dynamicSq = sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;

  END$$
DELIMITER ;
CALL test();
DROP PROCEDURE IF EXISTS test;

update hrm_formfield set isuse=1,ismand=1,allowhide=-1 where fieldname='sex';
update hrm_formfield set isuse=1,ismand=0,allowhide=-1 where fieldname='departmentid';
update hrm_formfield set isuse=1,ismand=0,allowhide=-1 where fieldname='jobtitle';
update hrm_formfield set isuse=0,ismand=0,allowhide=-1 where fieldname='jobactivity';
update hrm_formfield set isuse=0,ismand=0,allowhide=-1 where fieldname='joblevel';
update hrm_formfield set isuse=0,ismand=0,allowhide=-1 where fieldname='jobcall';
update hrm_formfield set isuse=0,ismand=0,allowhide=-1 where fieldname='jobGroupId';
update hrm_formfield set isuse=0,ismand=0,allowhide=-1 where fieldname='jobactivitydesc';
update hrm_formfield set isuse=1,ismand=0,allowhide=1,groupid=1,fieldorder=18 where fieldname='managerid';
update hrm_formfield set isuse=1,ismand=0,allowhide=1,groupid=1,fieldorder=99 where fieldname='systemlanguage';
update hrm_formfield set isuse=1,ismand=0,allowhide=1,groupid=1,fieldorder=21 where fieldname='assistantid';