IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_dtsrqs]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_dtsrqs]
GO
CREATE PROCEDURE userfunc_752_get_dtsrqs(@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 @q7zt varchar(20);
    declare @q6zt varchar(20);
    declare @q5zt varchar(20);
    declare @q4zt varchar(20);
    declare @q3zt varchar(20);
    declare @q2zt varchar(20);
    declare @q1zt varchar(20);
    declare @bzt varchar(20);
    
    create table #data1 (name varchar(128),value decimal(18,2));
    create table #data2 (name varchar(128),value decimal(18,2));
    create table #data3 (name varchar(128),value decimal(18,2));
    create table #data4 (name varchar(128),value decimal(18,2));
    create table #data5 (name varchar(128),value decimal(18,2));
    create table #data6 (name varchar(128),value decimal(18,2));
    
    declare @sql1 nvarchar(2000);
    
    CREATE TABLE #temp1 (xl1 decimal(18,2),xl2 decimal(18,2),xl3 decimal(18,2),xl4 decimal(18,2),xl5 decimal(18,2),xl6 decimal(18,2),
        zhsr1 decimal(18,2),zhsr2 decimal(18,2),zhsr3 decimal(18,2),zhsr4 decimal(18,2),zhsr5 decimal(18,2),zhsr6 decimal(18,2),
        zdsr1 decimal(18,2),zdsr2 decimal(18,2),zdsr3 decimal(18,2),zdsr4 decimal(18,2),zdsr5 decimal(18,2),zdsr6 decimal(18,2),
        zzsr1 decimal(18,2),zzsr2 decimal(18,2),zzsr3 decimal(18,2),zzsr4 decimal(18,2),zzsr5 decimal(18,2),zzsr6 decimal(18,2),
        ydsr1 decimal(18,2),ydsr2 decimal(18,2),ydsr3 decimal(18,2),ydsr4 decimal(18,2),ydsr5 decimal(18,2),ydsr6 decimal(18,2),
        fqsr1 decimal(18,2),fqsr2 decimal(18,2),fqsr3 decimal(18,2),fqsr4 decimal(18,2),fqsr5 decimal(18,2),fqsr6 decimal(18,2),
        pjsr1 decimal(18,2),pjsr2 decimal(18,2),pjsr3 decimal(18,2),pjsr4 decimal(18,2),pjsr5 decimal(18,2),pjsr6 decimal(18,2));
        
    select top 1 @q7zt = [前七周],@q6zt = [前六周],@q5zt = [前五周],@q4zt = [前四周],@q3zt = [前三周],@q2zt = [前二周],@q1zt = [前一周],@bzt = [本周] 
    FROM [DBTC].[datas].[dbo].[dd_fwk_date];
                       
    set @sql1 = 'insert into #temp1(xl1,xl2,xl3,xl4,xl5,xl6,zhsr1,zhsr2,zhsr3,zhsr4,zhsr5,zhsr6'
              + ',zdsr1,zdsr2,zdsr3,zdsr4,zdsr5,zdsr6,zzsr1,zzsr2,zzsr3,zzsr4,zzsr5,zzsr6'
              + ',ydsr1,ydsr2,ydsr3,ydsr4,ydsr5,ydsr6,fqsr1,fqsr2,fqsr3,fqsr4,fqsr5,fqsr6,pjsr1,pjsr2,pjsr3,pjsr4,pjsr5,pjsr6)'
              + ' select sum([前五周终端]),sum([前四周终端]),sum([前三周终端]),sum([前二周终端]),sum([前一周终端]),sum([本周终端])'
              + ',sum([前五周主营毛收入]),sum([前四周主营毛收入]),sum([前三周主营毛收入]),sum([前二周主营毛收入]),sum([前一周主营毛收入]),sum([本周主营毛收入])'
              + ',sum([前五周终端毛收入]),sum([前四周终端毛收入]),sum([前三周终端毛收入]),sum([前二周终端毛收入]),sum([前一周终端毛收入]),sum([本周终端毛收入])'
              + ',sum([前五周增值毛收入]),sum([前四周增值毛收入]),sum([前三周增值毛收入]),sum([前二周增值毛收入]),sum([前一周增值毛收入]),sum([本周增值毛收入])'
              + ',sum([前五周移动毛收入]),sum([前四周移动毛收入]),sum([前三周移动毛收入]),sum([前二周移动毛收入]),sum([前一周移动毛收入]),sum([本周移动毛收入])'
              + ',sum([前五周其它毛收入]),sum([前四周其它毛收入]),sum([前三周其它毛收入]),sum([前二周其它毛收入]),sum([前一周其它毛收入]),sum([本周其它毛收入])'
              + ',sum([前五周配件毛收入]),sum([前四周配件毛收入]),sum([前三周配件毛收入]),sum([前二周配件毛收入]),sum([前一周配件毛收入]),sum([本周配件毛收入])'
              + ' from [DBTC].[datas].[dbo].[dd_date_lavg]' 
              + ' 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 #data1(name,value) select @q5zt,(case when xl1 > 0 then zhsr1/xl1 else 0 end) from #temp1;
    insert into #data1(name,value) select @q4zt,(case when xl2 > 0 then zhsr2/xl2 else 0 end) from #temp1;
    insert into #data1(name,value) select @q3zt,(case when xl3 > 0 then zhsr3/xl3 else 0 end) from #temp1;
    insert into #data1(name,value) select @q2zt,(case when xl4 > 0 then zhsr4/xl4 else 0 end) from #temp1;
    insert into #data1(name,value) select @q1zt,(case when xl5 > 0 then zhsr5/xl5 else 0 end) from #temp1;
    insert into #data1(name,value) select @bzt,(case when xl6 > 0 then zhsr6/xl6 else 0 end) from #temp1;
    
    insert into #data2(name,value) select @q5zt,(case when xl1 > 0 then zdsr1/xl1 else 0 end) from #temp1;
    insert into #data2(name,value) select @q4zt,(case when xl2 > 0 then zdsr2/xl2 else 0 end) from #temp1;
    insert into #data2(name,value) select @q3zt,(case when xl3 > 0 then zdsr3/xl3 else 0 end) from #temp1;
    insert into #data2(name,value) select @q2zt,(case when xl4 > 0 then zdsr4/xl4 else 0 end) from #temp1;
    insert into #data2(name,value) select @q1zt,(case when xl5 > 0 then zdsr5/xl5 else 0 end) from #temp1;
    insert into #data2(name,value) select @bzt,(case when xl6 > 0 then zdsr6/xl6 else 0 end) from #temp1;
    
    insert into #data3(name,value) select @q5zt,(case when xl1 > 0 then zzsr1/xl1 else 0 end) from #temp1;
    insert into #data3(name,value) select @q4zt,(case when xl2 > 0 then zzsr2/xl2 else 0 end) from #temp1;
    insert into #data3(name,value) select @q3zt,(case when xl3 > 0 then zzsr3/xl3 else 0 end) from #temp1;
    insert into #data3(name,value) select @q2zt,(case when xl4 > 0 then zzsr4/xl4 else 0 end) from #temp1;
    insert into #data3(name,value) select @q1zt,(case when xl5 > 0 then zzsr5/xl5 else 0 end) from #temp1;
    insert into #data3(name,value) select @bzt,(case when xl6 > 0 then zzsr6/xl6 else 0 end) from #temp1;
    
    insert into #data4(name,value) select @q5zt,(case when xl1 > 0 then ydsr1/xl1 else 0 end) from #temp1;
    insert into #data4(name,value) select @q4zt,(case when xl2 > 0 then ydsr2/xl2 else 0 end) from #temp1;
    insert into #data4(name,value) select @q3zt,(case when xl3 > 0 then ydsr3/xl3 else 0 end) from #temp1;
    insert into #data4(name,value) select @q2zt,(case when xl4 > 0 then ydsr4/xl4 else 0 end) from #temp1;
    insert into #data4(name,value) select @q1zt,(case when xl5 > 0 then ydsr5/xl5 else 0 end) from #temp1;
    insert into #data4(name,value) select @bzt,(case when xl6 > 0 then ydsr6/xl6 else 0 end) from #temp1;
    
    insert into #data5(name,value) select @q5zt,(case when xl1 > 0 then fqsr1/xl1 else 0 end) from #temp1;
    insert into #data5(name,value) select @q4zt,(case when xl2 > 0 then fqsr2/xl2 else 0 end) from #temp1;
    insert into #data5(name,value) select @q3zt,(case when xl3 > 0 then fqsr3/xl3 else 0 end) from #temp1;
    insert into #data5(name,value) select @q2zt,(case when xl4 > 0 then fqsr4/xl4 else 0 end) from #temp1;
    insert into #data5(name,value) select @q1zt,(case when xl5 > 0 then fqsr5/xl5 else 0 end) from #temp1;
    insert into #data5(name,value) select @bzt,(case when xl6 > 0 then fqsr6/xl6 else 0 end) from #temp1;
    
    insert into #data6(name,value) select @q5zt,(case when xl1 > 0 then pjsr1/xl1 else 0 end) from #temp1;
    insert into #data6(name,value) select @q4zt,(case when xl2 > 0 then pjsr2/xl2 else 0 end) from #temp1;
    insert into #data6(name,value) select @q3zt,(case when xl3 > 0 then pjsr3/xl3 else 0 end) from #temp1;
    insert into #data6(name,value) select @q2zt,(case when xl4 > 0 then pjsr4/xl4 else 0 end) from #temp1;
    insert into #data6(name,value) select @q1zt,(case when xl5 > 0 then pjsr5/xl5 else 0 end) from #temp1;
    insert into #data6(name,value) select @bzt,(case when xl6 > 0 then pjsr6/xl6 else 0 end) from #temp1;
    
    select 'chart_dtsrqs' tableid,'主营单毛' tbtitle,'bar' charttype,* from #data1
    select 'chart_dtsrqs' tableid,'终端单毛' tbtitle,'line' charttype,* from #data2
    select 'chart_dtsrqs' tableid,'移动单毛' tbtitle,'line' charttype,* from #data4
    select 'chart_dtsrqs' tableid,'配件单毛' tbtitle,'line' charttype,* from #data6
    select 'chart_dtsrqs' tableid,'增值单毛' tbtitle,'line' charttype,* from #data3
    select 'chart_dtsrqs' tableid,'其它单毛' tbtitle,'line' charttype,* from #data5
    


end