IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_dtsrwc]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_dtsrwc]
GO
CREATE PROCEDURE userfunc_752_get_dtsrwc(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@viewid varchar(10),@date varchar(10),@types varchar(50),@branch varchar(20),@area varchar(50),@store varchar(50),@grade varchar(20))
as
begin



       set nocount on;
    
    DECLARE @partdata1 table(name varchar(128),value decimal(10,2));
    DECLARE @partdata2 table(name varchar(128),value decimal(10,2));
    
    declare @sql1 nvarchar(1000);
    
    CREATE TABLE #temp1 (xl1 decimal(10,2),xl2 decimal(10,2),zhsr1 decimal(10,2),zhsr2 decimal(10,2),
                         zdsr1 decimal(10,2),zdsr2 decimal(10,2),zzsr1 decimal(10,2),zzsr2 decimal(10,2),
                         ydsr1 decimal(10,2),ydsr2 decimal(10,2),fqsr1 decimal(10,2),fqsr2 decimal(10,2),
                         pjsr1 decimal(10,2),pjsr2 decimal(10,2));
                       
    set @sql1 = 'insert into #temp1(xl1,xl2,zhsr1,zhsr2,zdsr1,zdsr2,zzsr1,zzsr2,ydsr1,ydsr2,fqsr1,fqsr2,pjsr1,pjsr2)'
              + ' select sum([终端销量预算量]),sum([终端销量完成量]),sum([主营毛收入预算量]),sum([主营毛收入完成量])'
              + ' ,sum([终端毛收入预算量]),sum([终端毛收入完成量]),sum([增值毛收入预算量]),sum([增值毛收入完成量])'
              + ' ,sum([移动毛收入预算量]),sum([移动毛收入完成量]),sum([其它毛收入预算量]),sum([其它毛收入完成量])'
              + ' ,sum([配件毛收入预算量]),sum([配件毛收入完成量])'
              + ' from [DBTC].[datas].[dbo].[dd_date_avg]' 
              + ' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    if @viewid = 'branchf' 
    begin
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + '''';
    end 
    else if @viewid = 'area'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
    end
    else if @viewid = 'store'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
    end
    
    if @grade != '全部类别' and @viewid != 'store'
    begin
       set @sql1 = @sql1 + ' and [门店类别] =''' +  @grade + '''';
    end 
    
    EXEC(@sql1);  
    
    insert into @partdata1(name,value) values('主营单毛',case when (select xl1 from #temp1) > 0 then ((select zhsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
    insert into @partdata1(name,value) values('终端单毛',case when (select xl1 from #temp1) > 0 then ((select zdsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
    insert into @partdata1(name,value) values('移动单毛',case when (select xl1 from #temp1) > 0 then ((select ydsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
    insert into @partdata1(name,value) values('配件单毛',case when (select xl1 from #temp1) > 0 then ((select pjsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
    insert into @partdata1(name,value) values('增值单毛',case when (select xl1 from #temp1) > 0 then ((select zzsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
    insert into @partdata1(name,value) values('其它单毛',case when (select xl1 from #temp1) > 0 then ((select fqsr1 from #temp1)/(select xl1 from #temp1)) else 0 end)
        
    insert into @partdata2(name,value) values('主营单毛',case when (select xl2 from #temp1) > 0 then ((select zhsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    insert into @partdata2(name,value) values('终端单毛',case when (select xl2 from #temp1) > 0 then ((select zdsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    insert into @partdata2(name,value) values('移动单毛',case when (select xl2 from #temp1) > 0 then ((select ydsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    insert into @partdata2(name,value) values('配件单毛',case when (select xl2 from #temp1) > 0 then ((select pjsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    insert into @partdata2(name,value) values('增值单毛',case when (select xl2 from #temp1) > 0 then ((select zzsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    insert into @partdata2(name,value) values('其它单毛',case when (select xl2 from #temp1) > 0 then ((select fqsr2 from #temp1)/(select xl2 from #temp1)) else 0 end)
    
  
    select 'chart_dtsrwc' tableid,'当月目标' tbtitle,'bar' charttype,* from @partdata1
    select 'chart_dtsrwc' tableid,'当月累计' tbtitle,'bar' charttype,* from @partdata2
    


end