create or replace view v_jcl_zdzj as
select id*-1 fid,sequence_name fname,0 fparentid,sequence_no fno
from jcl_org_sequence
where nvl(forbidden_tag,0)<>1 and nvl(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 nvl(zj.forbidden_tag,0)<>1 and nvl(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  ','+zj.level_id+',' like '%,'+cast(zd.id as varchar(10))+',%'
where nvl(zd.forbidden_tag,0)<>1 and nvl(zd.delete_type,0)<>1
/

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

CREATE OR REPLACE FUNCTION newid RETURN VARCHAR2
IS
guid VARCHAR2 (50);
BEGIN
  guid := (RAWTOHEX(sys_guid()));
RETURN substr(guid,1,8)||'-'||substr(guid,9,4)||'-'||substr(guid,13,4)||'-'||substr(guid,17,4)||'-'||substr(guid,21,12);
END newid
/


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:=newid();
    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,cubeuuid)
        values('聚才林人事模块',1,',0,1',1,-999,'聚才林专项应用（谨慎调整）',0,orgid,
               'icon-cube-app-team','#fff','#4da0f8',cubeuuid1);
select id into appid1 from modetreefield where cubeuuid=cubeuuid1;
end if;

    uuid :=lower(replace(newid(),'-',''));
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_zdzj','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','fno','','','',1,0);
uuid :=lower(replace(newid(),'-',''));
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,t3 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_post','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','','','','',1,0);


fieldname:='职等职级';
    fieldid1:=100001;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',-1,lableid,fieldid1,9,1,1,1,fieldname,t1,0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(1000)',3,256,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(1000)';
execute immediate sql1;

fieldname:='担任岗位';
    fieldid1:=100002;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',-1,lableid,fieldid1,8,1,1,1,fieldname,t2,0);

insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(1000)',3,666,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(1000)';
execute immediate sql1;

fieldname:='担任职务';
    fieldid1:=100003;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',-1,lableid,fieldid1,10,1,1,1,fieldname,t3,0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(1000)',3,256,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(1000)';
execute immediate sql1;

fieldname:='职务序列名称';
    fieldid1:=100005;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='等级方案';
    fieldid1:=100006;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='职级';
    fieldid1:=100007;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='职等';
    fieldid1:=100008;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='职务分类';
    fieldid1:=100009;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='职务';
    fieldid1:=100010;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',3,lableid,fieldid1,1,0,1,5,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'varchar(200)',1,1,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;

fieldname:='虚线上级';
    fieldid1:=100004;
    field:='field'||to_char(fieldid1);

select count(*) into cnt from HtmlLabelIndex where indexdesc=fieldname;

if cnt>0 then
select max(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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',-1,lableid,fieldid1,20,0,1,1,fieldname,'',0);
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (fieldid1,'text',3,17,field,field,'');
sql1 :='alter table cus_fielddata add '||field||' varchar(200)';
execute immediate sql1;
end;
/

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'
/
