CREATE TABLE JCL_ORG_FIELD (
    id int IDENTITY(1,1) NOT NULL,
    fieldid int NULL,
    fieldlabel varchar(100) NULL,
    fieldname varchar(100) null,
    CONSTRAINT JJCL_ORG_FIELD_PK PRIMARY KEY (id)
)
GO

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

declare @orgid int
declare @cubeuuid varchar(100)
declare @appid int
declare @modename varchar(100)
declare @uuid varchar(100)
declare @treeid int
declare @t1 int
declare @t2 int
declare @t3 int

select @orgid=min(id) from hrmresource
select @cubeuuid=newid()
set @modename='聚才林人事模块'

if exists(select 1 from modetreefield where treeFieldName=@modename)
    begin
        select @appid=id from modetreefield where treeFieldName=@modename
    end
else
    begin

        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 @appid=max(id) from modetreefield
    end
    

select @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,@appid,@uuid,0,0,0,0,'','',0,0,0,'/',0,1)

select @treeid=id,@t1=id 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)

GO

declare @fieldname varchar(100)
declare @fieldid int
declare @field varchar(100)
declare @lableid int
declare @dmltype varchar(100)
declare @sql varchar(500)

set @fieldname='职等职级'
select @fieldid=isnull(max(id),0)+1 from cus_formdict
set @field='field'+cast(@fieldid as varchar(10))
select @dmltype=max(mainid) from mode_customtreedetail where tablename='v_jcl_zdzjs'

if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname)
    begin
        select @lableid=id from HtmlLabelIndex where indexdesc=@fieldname
    end
else
    begin
        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

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)

set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)
insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@field)


set @fieldname='等级方案'
set @dmltype='schemeBrowser'
select @fieldid=max(id)+1 from cus_formdict
set @field='field'+cast(@fieldid as varchar(10))

if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname)
    begin
        select @lableid=id from HtmlLabelIndex where indexdesc=@fieldname
    end
else
    begin
        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

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)
set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)
insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@field)

set @fieldname='职等'
set @dmltype='LevelBrowser'
select @fieldid=max(id)+1 from cus_formdict
set @field='field'+cast(@fieldid as varchar(10))

if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname)
    begin
        select @lableid=id from HtmlLabelIndex where indexdesc=@fieldname
    end
else
    begin
        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

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)
set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)
insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@field)

set @fieldname='职级'
set @dmltype='gradeBrowser'
select @fieldid=max(id)+1 from cus_formdict
set @field='field'+cast(@fieldid as varchar(10))

if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname)
    begin
        select @lableid=id from HtmlLabelIndex where indexdesc=@fieldname
    end
else
    begin
        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

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)
set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)
insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@field)

set @fieldname='岗位序列'
set @dmltype='sequenceBrowser'
select @fieldid=max(id)+1 from cus_formdict
set @field='field'+cast(@fieldid as varchar(10))

if exists(select 1 from HtmlLabelIndex where indexdesc=@fieldname)
    begin
        select @lableid=id from HtmlLabelIndex where indexdesc=@fieldname
    end
else
    begin
        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

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)
set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)
insert into jcl_org_field(fieldid,fieldlabel,fieldname) values (@fieldid,@fieldname,@field)
GO