CREATE TABLE JCL_ORG_FIELD (
    id int auto_increment NOT NULL,
    fieldid int NULL,
    fieldlabel varchar(100) NULL,
    fieldname varchar(100) NULL,
    CONSTRAINT JCL_ORG_FIELD_PK PRIMARY KEY (id)
);


create view v_jcl_zdzjs as
select  concat('A',xl.id) fid,xl.sequence_name fname,'0' fparentid,xl.sequence_no fno,xl.id fobjid,'1' as ftype,0 xlid,0 zjid,0 zdid
from jcl_org_sequence xl
inner join JCL_ORG_GRADE zj on zj.scheme_id=xl.scheme_id and ifnull(zj.forbidden_tag,0)<>1 and ifnull(zj.delete_type,0)<>1
inner join JCL_ORG_LEVEL zd on zd.scheme_id=xl.scheme_id and ifnull(zd.forbidden_tag,0)<>1 and ifnull(zd.delete_type,0)<>1
where ifnull(xl.forbidden_tag,0)<>1 and ifnull(xl.delete_type,0)<>1
union
select concat('A',xl.id,'A',zj.id),zj.grade_name,concat('A',xl.id),zj.grade_no,zj.id,'2' as ftype,0,0,0
from JCL_ORG_GRADE zj
inner join jcl_org_sequence xl on zj.scheme_id=xl.scheme_id
inner join JCL_ORG_LEVEL zd on  concat(',',zj.level_id,',') like concat('%,',zd.id,',%') and ifnull(zd.forbidden_tag,0)<>1 and ifnull(zd.delete_type,0)<>1
where ifnull(zj.forbidden_tag,0)<>1 and ifnull(zj.delete_type,0)<>1
union
select concat('A',xl.id,'A',zj.id,'A',zd.id),zd.level_name,concat('A',xl.id,'A',zj.id),zd.level_no,zd.id,'3' as ftype,xl.id,zj.id,zd.id
from JCL_ORG_LEVEL zd
inner join JCL_ORG_GRADE zj on  concat(',',zj.level_id,',') like concat('%,',zd.id,',%')
inner join jcl_org_sequence xl on zj.scheme_id=xl.scheme_id
where ifnull(zd.forbidden_tag,0)<>1 and ifnull(zd.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)
        values('聚才林人事模块',1,',0,1',1,-999,'聚才林专项应用（谨慎调整）',0,orgid,
               'icon-cube-app-team','#fff','#4da0f8');
        select max(id) into appid from modetreefield;
	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_zdzjs','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','fno','','','',1,0);
  END$$
DELIMITER ;
CALL test();
DROP PROCEDURE IF EXISTS test;


DELIMITER $$
CREATE PROCEDURE test()
begin
	select @fieldstrname:='职等职级';
	select @fieldid:=ifnull(max(id),0)+1 from cus_formdict;
	select @fieldstr:=concat('field',@fieldid);
	select @dmltype:=max(mainid) from mode_customtreedetail where tablename='v_jcl_zdzjs';
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldstrname) then 
	    select @lableid:=id from HtmlLabelIndex where indexdesc=@fieldstrname limit 1;
	else
	    select @lableid:=min(indexid)-1 from HtmlLabelInfo;
	    insert into HtmlLabelInfo(indexid, labelname, languageid) values(@lableid,@fieldstrname,7);
	    insert into HtmlLabelInfo(indexid, labelname, languageid) values(@lableid,@fieldstrname,8);
	    insert into HtmlLabelInfo(indexid, labelname, languageid) values(@lableid,@fieldstrname,9);
	    insert into HtmlLabelIndex(id, indexdesc) values(@lableid,@fieldstrname);
	end if;
	
	insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (@fieldid,'varchar(1000)',3,256,@fieldstr,@fieldstr,'');
	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,@fieldstrname,@dmltype,0);
	select @sqlstr:=concat('alter table cus_fielddata add ',@fieldstr,' varchar(1000)');
	select @dynamicSq:= @sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
	insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldstrname,@fieldstr);

	select @fieldname:='等级方案';
	select @dmltype:='schemeBrowser';
	select @fieldid:=max(id)+1 from cus_formdict;
	select @fieldstr:=concat('field',@fieldid);
	if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname) then
        select @lableid:=id from HtmlLabelIndex where indexdesc=@fieldname;
	else
        select @lableid:=min(indexid)-1 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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (@fieldid,'varchar(1000)',3,161,@fieldstr,@fieldstr,'');
	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,@dmltype,0);
	select @sqlstr:=concat('alter table cus_fielddata add ',@fieldstr,' varchar(1000)');
	select @dynamicSq:= @sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
	insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@fieldstr);

	select @fieldname:='职等';
	select @dmltype:='LevelBrowser';
	select @fieldid:=max(id)+1 from cus_formdict;
	select @fieldstr:=concat('field',@fieldid);
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname) then
        select @lableid:=id from HtmlLabelIndex where indexdesc=@fieldname;
	else
        select @lableid:=min(indexid)-1 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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (@fieldid,'varchar(1000)',3,161,@fieldstr,@fieldstr,'');
	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,@dmltype,0);
	select @sqlstr:=concat('alter table cus_fielddata add ',@fieldstr,' varchar(1000)');
	select @dynamicSq:= @sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
	insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@fieldstr);

	select @fieldname:='职级';
	select @dmltype:='gradeBrowser';
	select @fieldid:=max(id)+1 from cus_formdict;
	select @fieldstr:=concat('field',@fieldid);
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname) then
        select @lableid:=id from HtmlLabelIndex where indexdesc=@fieldname;
	else
        select @lableid:=min(indexid)-1 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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (@fieldid,'varchar(1000)',3,161,@fieldstr,@fieldstr,'');
	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,@dmltype,0);
	select @sqlstr:=concat('alter table cus_fielddata add ',@fieldstr,' varchar(1000)');
	select @dynamicSq:= @sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
	insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@fieldstr);

	select @fieldname:='岗位序列';
	select @dmltype:='sequenceBrowser';
	select @fieldid:=max(id)+1 from cus_formdict;
	select @fieldstr:=concat('field',@fieldid);
	
	if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname) then
        select @lableid:=id from HtmlLabelIndex where indexdesc=@fieldname;
	else
        select @lableid:=min(indexid)-1 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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (@fieldid,'varchar(1000)',3,161,@fieldstr,@fieldstr,'');
	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,@dmltype,0);
	select @sqlstr:=concat('alter table cus_fielddata add ',@fieldstr,' varchar(1000)');
	select @dynamicSq:= @sqlstr;
	PREPARE sqa from @dynamicSq;
	EXECUTE sqa;
	DEALLOCATE PREPARE sqa;
	insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@fieldstr);
  	
END$$
DELIMITER ;
CALL test();
DROP PROCEDURE IF EXISTS test;