IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_update_report]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_update_report]
GO
CREATE PROCEDURE [dbo].[mps_update_report]  
@rpt_id int,    
@rpt_cmpid int,  
@rpt_title nvarchar(100),    
@rpt_category nvarchar(100),    
@rpt_categorysub nvarchar(100),    
@rpt_icon nvarchar(200),    
@rpt_interval int,    
@rpt_order varchar(30),    
@rpt_note nvarchar(2000) , 
@rpt_template nvarchar(2000) ,
@rpt_labels nvarchar(1000),
@rpt_labeloperation nvarchar(1000),
@rpt_sql nvarchar(max),
@rpt_url nvarchar(200),
@rpt_accesscount bigint,
@rpt_hidemenu int,
@rpt_hidemenu_mobile int ,
@rpt_widget int  
as  
begin  
 set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
 
 declare @s1 varchar(200) 
declare @s0 varchar(200) 
set @s1 = @rpt_icon 
declare @i int 
set @i = CHARINDEX('|',@s1) 
if (@i>0)
begin
set @s0 = substring(@s1,@i+1,200) 
set @s1 = SUBSTRING(@s1,1,@i-1) 
end else 
begin
set @s0 = '' 
end 
if (@s1='_none_')
begin
set @s1 = '_none_' 
set @s0 = '_none_' 
end 
if (@s1='') set  @s1 = '_none_' 
if (@s0='') set  @s0 = '_none_' 

 if(@rpt_id!=0)  
 begin  
    update mps_report set  
    rpt_cmpid=case when @rpt_cmpid is null or @rpt_cmpid = -999999999  then rpt_cmpid else @rpt_cmpid end,  
    rpt_title=case when @rpt_title is null or @rpt_title = '_none_'  then rpt_title else @rpt_title end,  
    rpt_category=case when @rpt_category is null or @rpt_category = '_none_'  then rpt_category else @rpt_category end,  
    rpt_categorysub=case when @rpt_categorysub is null or @rpt_categorysub = '_none_'  then rpt_categorysub else @rpt_categorysub end,  
    rpt_icon=case when @s1='_none_' then rpt_icon else @s1 end,
rpt_icon0=case when @s0='_none_' then rpt_icon0 else @s0 end,
    rpt_interval=case when @rpt_interval is null or @rpt_interval = -999999999  then rpt_interval else @rpt_interval end,  
    rpt_order=case when @rpt_order is null or @rpt_order = '_none_'  then rpt_order else @rpt_order end,  
rpt_template=case when @rpt_template is null or @rpt_template = '_none_'  then rpt_template else @rpt_template end,
rpt_labels=case when @rpt_labels is null or @rpt_labels = '_none_'  then rpt_labels else @rpt_labels end,
rpt_labeloperation=case when @rpt_labeloperation is null or @rpt_labeloperation = '_none_'  then rpt_labeloperation else @rpt_labeloperation end,
rpt_sql=case when @rpt_sql is null or @rpt_sql = '_none_'  then rpt_sql else @rpt_sql end,
    rpt_note=case when @rpt_note is null or @rpt_note = '_none_'  then rpt_note else @rpt_note end,
rpt_url=case when @rpt_url is null or @rpt_url = '_none_'  then rpt_url else @rpt_url end,
rpt_accesscount=case when @rpt_accesscount =-999999999  then rpt_accesscount else rpt_accesscount+1 end  ,
rpt_widget=case when @rpt_widget =-999999999  then rpt_widget else @rpt_widget  end  ,
rpt_hidemenu=case when @rpt_hidemenu =-999999999  then rpt_hidemenu else @rpt_hidemenu end,
rpt_hidemenu_mobile=case when @rpt_hidemenu_mobile =-999999999  then rpt_hidemenu_mobile else @rpt_hidemenu_mobile end  

    where rpt_id=@rpt_id  
    
    if (@s0<>'' and @s0<>'_none_')
  begin
  declare @cat nvarchar(200) 
  select top 1  @cat = rpt_category from mps_report where rpt_id=@rpt_id 
  update mps_report set rpt_icon0=@s0 where rpt_cmpid=@rpt_cmpid and rpt_category=@cat 
  end 
  
if @rpt_sql !='_none_'
exec mps_gen_createproc_g_reportdata @rpt_cmpid,@rpt_id
 end else --如果rpt_id=0则说明是新建  
 begin  
  set @rpt_category = case when @rpt_category = '_none_' then '' else @rpt_category end;  
  set @rpt_categorysub = case when @rpt_categorysub = '_none_' then '' else @rpt_categorysub end;  
  set @rpt_title = case when @rpt_title = '_none_' then '' else @rpt_title end;  
  set @rpt_order = case when @rpt_order = '_none_' then '' else @rpt_order end;  
  
  insert into mps_report(rpt_cmpid,rpt_category,rpt_categorysub,rpt_title,rpt_order) values(@rpt_cmpid,@rpt_category,@rpt_categorysub,@rpt_title,@rpt_order)  
  --select  case when rpt_categorysub='' then rpt_category else rpt_category+'/'+rpt_categorysub end as rpt_title ,rpt_category,rpt_categorysub,rpt_order from mps_report where rpt_id = @@IDENTITY  
 end  
 
end