
create view v_jcl_zdzj as
select id*-1 fid,sequence_name fname,0 fparentid,sequence_no fno
from jcl_org_sequence
where isnull(forbidden_tag,0)<>1 and isnull(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 isnull(zj.forbidden_tag,0)<>1 and isnull(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 isnull(zd.forbidden_tag,0)<>1 and isnull(zd.delete_type,0)<>1
go



create view v_jcl_post as
select id*-1 fid,post_name fname,0 fparentid from JCL_ORG_POST
union
select id,post_info_name,post_id*-1 from JCL_ORG_POST_INFO
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,cubeuuid)
        values('聚才林人事模块',1,',0,1',1,-999,'聚才林专项应用（谨慎调整）',0,@orgid,
               'icon-cube-app-team','#fff','#4da0f8',@cubeuuid)
        select @appid=id from modetreefield where cubeuuid=@cubeuuid
    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_zdzj','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','fno','','','',1,0)



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,@t3=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_post','fid','fparentid','fname',2,0,'#','','',0,'','',1,'','','','','',1,0)



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

set @fieldname='职等职级'
set @fieldid='100001'
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

/*
select a.id as groupid,grouptype as scopeid,b.indexdesc
       from hrm_fieldgroup as a
inner join HtmlLabelIndex b on a.grouplabel=b.id
*/

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,@field,@field,'')

set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'

exec(@sql)

set @fieldname='担任岗位'
set @fieldid='100002'
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_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,@field,@field,'')

set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'

exec(@sql)

set @fieldname='担任职务'
set @fieldid='100003'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(1000)'
exec(@sql)


set @fieldname='职务序列名称'
set @fieldid='100005'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='等级方案'
set @fieldid='100006'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='职级'
set @fieldid='100007'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='职等'
set @fieldid='100008'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='职务分类'
set @fieldid='100009'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='职务'
set @fieldid='100010'
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_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,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' varchar(200)'
exec(@sql)

set @fieldname='虚线上级'
set @fieldid='100004'
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_formfield(scope, scopeid, fieldlable, fieldid, fieldorder, ismand, isuse,
                          groupid, hrm_fieldlable, dmlUrl, ismodify)
values('HrmCustomFieldByInfoType',-1,@lableid,@fieldid,20,0,1,1,@fieldname,'',0)
insert into cus_formdict(id, fielddbtype, fieldhtmltype, type, fieldname, fieldlabel, scope)
values (@fieldid,'text',3,17,@field,@field,'')
set @sql='alter table cus_fielddata add '+@field+' text'
exec(@sql)
GO


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='status'         
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'
GO



create trigger cus_fielddata_jcl on cus_fielddata after update,insert as

    declare @empid int 
    declare @str varchar(200)
    declare @wz int
    declare @zdid int
    declare @zdzjid int
    declare @gwid int
    declare @bmid int
    declare @gsid int
    declare @ecgwid int
    declare @ecbmid int
    declare @ecgsid int
    declare @zwid int
    declare @zwlxid int
    declare @uuid varchar(100)
    declare @name varchar(200)

    if update(field100001)
    begin
        select @str=isnull(field100001,''),@empid=id from inserted
        set @wz=charindex('_',@str)

        if not exists(select 1 from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@empid)
            begin
                insert into cus_fielddata(scope, scopeid, id) values ('HrmCustomFieldByInfoType',3,@empid)
            end

        if @wz>0
        begin
            set @zdid=cast(right(@str,len(@str)-@wz) as int)


            select @zdzjid=scheme_id,@name=level_name from JCL_ORG_LEVEL where id =@zdid
            update cus_fielddata set field100008=@name where scope='HrmCustomFieldByInfoType' and id=@empid and scopeid=3


            select @name=scheme_name from JCL_ORG_SCHEME where id=@zdzjid
            update cus_fielddata set field100006=@name where scope='HrmCustomFieldByInfoType' and id=@empid and scopeid=3


            select @name=grade_name from jcl_org_grade where ','+level_id+',' like '%,'+cast(@zdid as varchar(20))+',%'
            update cus_fielddata set field100007=@name where scope='HrmCustomFieldByInfoType' and id=@empid and scopeid=3


            select @name=sequence_name from jcl_org_sequence where scheme_id=@zdzjid
            update cus_fielddata set field100005=@name where scope='HrmCustomFieldByInfoType' and id=@empid
        end
    end

    if update(field100002)

    begin
        select @str=isnull(field100002,''),@empid=id from inserted
        set @wz=charindex('_',@str)

        if @wz>0
        begin
            set @gwid=cast(right(@str,len(@str)-@wz) as int)
            select @name=job_name,@bmid=parent_dept,@gsid=parent_comp from JCL_ORG_JOB where id=@gwid


            select @ecgwid=max(id) from HrmJobTitles where jobtitlename=@name
            update hrmresource set jobtitle=@ecgwid where id=@empid


            select @uuid=uuid from JCL_ORG_DEPT where id=@bmid
            select @ecbmid=id from HrmDepartment where uuid=@uuid
            update hrmresource set departmentid=@ecbmid where id=@empid


            select @uuid=uuid from JCL_ORG_COMP where id=@gsid
            select @ecgsid=id from HrmSubCompany where uuid=@uuid
            update hrmresource set subcompanyid1=@ecgsid where id=@empid
        end

    end

    if update(field100003)

    begin
        select @str=isnull(field100003,''),@empid=id from inserted
        set @wz=charindex('_',@str)

        if not exists(select 1 from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid=3 and id=@empid)
            begin
                insert into cus_fielddata(scope, scopeid, id) values ('HrmCustomFieldByInfoType',3,@empid)
            end

        if @wz>0
        begin
            set @zwid=cast(right(@str,len(@str)-@wz) as int)


            select @name=post_info_name,@zwlxid=post_id from jcl_org_post_info where id=@zwid
            update cus_fielddata set field100010=@name where scope='HrmCustomFieldByInfoType' and id=@empid and scopeid=3


            select @name=post_name from jcl_org_post where id=@zwlxid
            update cus_fielddata set field100009=@name where scope='HrmCustomFieldByInfoType' and id=@empid and scopeid=3

        end

    end
GO
