IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_cstctgbase]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_cstctgbase]
GO
 
CREATE procedure [dbo].[mps_save_cstctgbase]
-- Add the parameters for the stored procedure here
@cmpid int,
@ct_id int,
@ct_bmcode varchar(50),
@ct_type nvarchar(100),
@ct_allowlogin int,
@ct_security int,
@ct_order int , 
@ct_captionnvarchar(50),
@ct_iconvarchar(200),
@ct_databasevarchar(50),
@ct_groupfieldsvarchar(500),
@ct_statusint,
@ct_querytimeint,
@ct_searchfieldsvarchar(2000),
@ct_selectfuncsnvarchar(500),
@ct_selectstyleint,
@ct_funcsvarchar(200),
@ct_flowsnvarchar(2000),
@ct_paramsvarchar(500),
@ct_viewscopeint ,
@ct_viewsint  ,
@ct_select nvarchar(4000),
@ct_from nvarchar(2000),
@ct_where nvarchar(4000),
@ct_orderby nvarchar(2000),
@ct_loginscope varchar(150)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
UPDATE mps_csttype SET 
ct_loginscope =  CASE WHEN @ct_loginscope = '_none_' THEN ct_loginscope ELSE @ct_loginscope END,
ct_bmcode =  CASE WHEN @ct_bmcode = '_none_' THEN ct_bmcode ELSE @ct_bmcode END,
ct_allowlogin = case when @ct_allowlogin=-999999999 then ct_allowlogin else @ct_allowlogin end , 
ct_type =  CASE WHEN @ct_type = '_none_' THEN ct_type ELSE @ct_type END,
ct_security = CASE WHEN @ct_security = -999999999 THEN ct_security ELSE @ct_security END,
ct_order = CASE WHEN @ct_order = -999999999 THEN ct_order ELSE @ct_order END,
ct_status = CASE WHEN @ct_status = -999999999 THEN ct_status ELSE @ct_status END,
ct_querytime = CASE WHEN @ct_querytime = -999999999 THEN ct_querytime ELSE @ct_querytime END,
ct_selectstyle = CASE WHEN @ct_selectstyle = -999999999 THEN ct_selectstyle ELSE @ct_selectstyle END,
ct_viewscope = CASE WHEN @ct_viewscope = -999999999 THEN  ct_viewscope ELSE @ct_viewscope END,
ct_views = CASE WHEN @ct_views = -999999999 THEN   ct_views ELSE @ct_views END,
ct_caption =  CASE WHEN @ct_caption = '_none_' THEN  ct_caption ELSE @ct_caption END,
ct_icon =  CASE WHEN @ct_icon = '_none_' THEN  ct_icon ELSE @ct_icon END,
ct_select = case when  @ct_select='_none_' then ct_select else @ct_select end ,
    ct_from = case when  @ct_from='_none_' then ct_from else @ct_from end ,
    ct_where = case when  @ct_where='_none_' then ct_where else @ct_where end ,
    ct_orderby = case when  @ct_orderby='_none_' then ct_orderby else @ct_orderby end ,
ct_database =  CASE WHEN @ct_database = '_none_' THEN  ct_database ELSE @ct_database END,
ct_groupfields =  CASE WHEN @ct_groupfields = '_none_' THEN   ct_groupfields ELSE @ct_groupfields END,
ct_searchfields =  CASE WHEN @ct_searchfields = '_none_' THEN  ct_searchfields ELSE @ct_searchfields END,
ct_selectfuncs =  CASE WHEN @ct_selectfuncs = '_none_' THEN   ct_selectfuncs ELSE @ct_selectfuncs END,
ct_funcs =  CASE WHEN @ct_funcs = '_none_' THEN    ct_funcs ELSE @ct_funcs END,
ct_flows =  CASE WHEN @ct_flows = '_none_' THEN     ct_flows ELSE @ct_flows END,
ct_params =  CASE WHEN @ct_params = '_none_' THEN      ct_params ELSE @ct_params END,
ct_changed = 0 
WHERE ct_id = @ct_id AND ct_cmpid = @cmpid

if (@ct_loginscope<>'_none_') 
begin
-- 主要去更新部门表和员工表
declare @dpid int 
select top 1 @dpid=dp_id from mps_depart with(nolock) where dp_cmpid=@cmpid  and dp_fromcc=@ct_id 
set @dpid = isnull(@dpid,0) 
if (@dpid>0) 
begin
update mps_depart set dp_loginscope = @ct_loginscope where dp_id=@dpid 
update mps_employee set cu_loginscope = @ct_loginscope where cu_dpid=@dpid 
update mps_login set cu_loginscope = @ct_loginscope where cu_dpid=@dpid 
update mps_login_role set lgu_loginscope = @ct_loginscope where lgu_dpid=@dpid 
end 
end 
END