CREATE TABLE JCL_ORG_FIELD (
    ID NUMBER NOT NULL,
    FIELDID NUMBER NULL,
    FIELDLABEL NVARCHAR2(100) NULL,
    FIELDNAME NVARCHAR2(100) NULL,
    CONSTRAINT JCL_ORG_FIELD_PK PRIMARY KEY (ID)
);
/

CREATE SEQUENCE JCL_ORG_FIELD_ID INCREMENT BY 1 START WITH 1 nomaxvalue minvalue 1 NOCYCLE;
/

CREATE
OR REPLACE TRIGGER JCL_ORG_FIELD_ID_TIGGER BEFORE INSERT ON JCL_ORG_FIELD FOR EACH ROW
BEGIN
SELECT
    JCL_ORG_FIELD_ID.nextval INTO:new.ID
FROM
    dual;
END;
/

create view v_jcl_zdzjs as
select  '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 nvl(zj.forbidden_tag,0)<>1 and nvl(zj.delete_type,0)<>1
inner join JCL_ORG_LEVEL zd on zd.scheme_id=xl.scheme_id and nvl(zd.forbidden_tag,0)<>1 and nvl(zd.delete_type,0)<>1
where nvl(xl.forbidden_tag,0)<>1 and nvl(xl.delete_type,0)<>1
union
select 'A'||xl.id||'A'||zj.id,zj.grade_name,'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  ','||zj.level_id||',' like '%,'||zd.id||',%' and nvl(zd.forbidden_tag,0)<>1 and nvl(zd.delete_type,0)<>1
where nvl(zj.forbidden_tag,0)<>1 and nvl(zj.delete_type,0)<>1
union
select 'A'||xl.id||'A'||zj.id||'A'||zd.id,zd.level_name,'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  ','||zj.level_id||',' like '%,'||zd.id||',%'
inner join jcl_org_sequence xl on zj.scheme_id=xl.scheme_id
where nvl(zd.forbidden_tag,0)<>1 and nvl(zd.delete_type,0)<>1;
/

declare
     orgid int;
     cubeuuid1 varchar2(100);
     appid1 int;
     modename varchar2(100);
     uuid varchar2(100);
     treeid int;
     t1 int;
     t2 int;
     t3 int;
     cnt int;
     lableid int;
     fieldname varchar2(100);
     fieldid1 int;
     field varchar2(100);
     sql1 varchar2(2000);

begin
select min(id) into orgid from hrmresource;
cubeuuid1:=RAWTOHEX(sys_guid());
    modename :='聚才林人事模块';

select count(*) into cnt from modetreefield where treeFieldName=modename;

if cnt >0 then
select id into appid1 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 appid1 from modetreefield;
end if;

    uuid :=RAWTOHEX(sys_guid());
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,appid1,uuid,0,0,0,0,'','',0,0,0,'/',0,1);
select id,id into treeid,t1 from mode_customtree where treecode=uuid;
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;
/

DECLARE 
	fieldname varchar(100);
	fieldid int;
	field varchar(100);
	lableid int;
	dmltype varchar(100);
	sqlstr varchar(500);
	cnt int;
	BEGIN
		fieldname:='职等职级';
		select nvl(max(id),0)+1 INTO fieldid from cus_formdict;
		field:='field'||fieldid;
		select max(mainid) INTO dmltype from mode_customtreedetail where tablename='v_jcl_zdzjs';
		
		select count(*) INTO cnt from HtmlLabelIndex where indexdesc=fieldname;
		if cnt>0 then
	        select min(id) INTO lableid from HtmlLabelIndex where indexdesc=fieldname;
		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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (fieldid,'varchar(1000)',3,256,field,field,'');
		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);
		
		sqlstr:='alter table cus_fielddata add '||field||' varchar(1000)';
		execute immediate sqlstr;
		insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (fieldid,fieldname,field);
	
		fieldname:='等级方案';
		dmltype:='schemeBrowser';
		select max(id)+1 INTO fieldid from cus_formdict;
		field:='field'||fieldid;
		
		select count(*) INTO cnt from HtmlLabelIndex where indexdesc=fieldname;
		if cnt>0 then
	        select min(id) INTO lableid from HtmlLabelIndex where indexdesc=fieldname;
		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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (fieldid,'varchar(1000)',3,161,field,field,'');
		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);
		sqlstr:='alter table cus_fielddata add '||field||' varchar(1000)';
		execute immediate sqlstr;
		insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (fieldid,fieldname,field);
	
		fieldname:='职等';
		dmltype:='LevelBrowser';
		select max(id)+1 INTO fieldid from cus_formdict;
		field:='field'||fieldid;
		
		select count(*) INTO cnt from HtmlLabelIndex where indexdesc=fieldname;
		if cnt>0 then
	        select min(id) INTO lableid from HtmlLabelIndex where indexdesc=fieldname;
		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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (fieldid,'varchar(1000)',3,161,field,field,'');
		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);
		sqlstr:='alter table cus_fielddata add '||field||' varchar(1000)';
		execute immediate sqlstr;
		insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (fieldid,fieldname,field);

		fieldname:='职级';
		dmltype:='gradeBrowser';
		select max(id)+1 INTO fieldid from cus_formdict;
		field:='field'||fieldid;
		
		select count(*) INTO cnt from HtmlLabelIndex where indexdesc=fieldname;
		if cnt>0 then
	        select min(id) INTO lableid from HtmlLabelIndex where indexdesc=fieldname;
		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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (fieldid,'varchar(1000)',3,161,field,field,'');
		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);
		sqlstr:='alter table cus_fielddata add '||field||' varchar(1000)';
		execute immediate sqlstr;
		insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (fieldid,fieldname,field);
	
		fieldname:='岗位序列';
		dmltype:='sequenceBrowser';
		select max(id)+1 INTO fieldid from cus_formdict;
		field:='field'||fieldid;
		
		select count(*) INTO cnt from HtmlLabelIndex where indexdesc=fieldname;
		if cnt>0 then
	        select min(id) INTO lableid from HtmlLabelIndex where indexdesc=fieldname;
		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_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope) values (fieldid,'varchar(1000)',3,161,field,field,'');
		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);
		sqlstr:='alter table cus_fielddata add '||field||' varchar(1000)';
		execute immediate sqlstr;
		insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (fieldid,fieldname,field);
			
	END;
/