IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_xlsrqs]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_xlsrqs]
GO
CREATE PROCEDURE userfunc_752_get_xlsrqs(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@detail varchar(50),@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);
    
    declare @qrydate varchar(10);
    declare @sql nvarchar(1000);
    DECLARE @partdataxl table(name varchar(128),value int);
    DECLARE @partdatasr table(name varchar(128),value decimal(18,2));

    CREATE TABLE #temp (bz1 decimal(10,2),q1z1 decimal(10,2),q2z1 decimal(10,2),q3z1 decimal(10,2),q4z1 decimal(10,2),q5z1 decimal(10,2),q6z1 decimal(10,2),q7z1 decimal(10,2),
                        bz2 decimal(10,2),q1z2 decimal(10,2),q2z2 decimal(10,2),q3z2 decimal(10,2),q4z2 decimal(10,2),q5z2 decimal(10,2),q6z2 decimal(10,2),q7z2 decimal(10,2),
                        bz3 decimal(10,2),q1z3 decimal(10,2),q2z3 decimal(10,2),q3z3 decimal(10,2),q4z3 decimal(10,2),q5z3 decimal(10,2),q6z3 decimal(10,2),q7z3 decimal(10,2),
                        bz4 decimal(10,2),q1z4 decimal(10,2),q2z4 decimal(10,2),q3z4 decimal(10,2),q4z4 decimal(10,2),q5z4 decimal(10,2),q6z4 decimal(10,2),q7z4 decimal(10,2));
                        
    SET @qrydate = CONVERT(varchar(10), DATEADD(DAY,-1,GETDATE()) , 120);
    
    select top 1 @q7zt = [前七周],@q6zt = [前六周],@q5zt = [前五周],@q4zt = [前四周],@q3zt = [前三周],@q2zt = [前二周],@q1zt = [前一周],@bzt = [本周] FROM [DBTC].[datas].[dbo].[dd_fwk_date]

    set @sql = 'insert into #temp' 
             +'(bz1,q1z1,q2z1,q3z1,q4z1,q5z1,q6z1,q7z1,'
             + 'bz2,q1z2,q2z2,q3z2,q4z2,q5z2,q6z2,q7z2,'
             + 'bz3,q1z3,q2z3,q3z3,q4z3,q5z3,q6z3,q7z3,'
             + 'bz4,q1z4,q2z4,q3z4,q4z4,q5z4,q6z4,q7z4)'
             +' 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([前七周总毛收入])'
             +' from [DBTC].[datas].[dbo].[dd_date_dyzm]'
             +' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    
    if @viewid = 'branchf' 
    begin
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + '''';
    end 
    else if @viewid = 'area'
    begin 
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
    end
    else if @viewid = 'store'
    begin 
       set @sql = @sql + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
    end
    
    if @grade != '全部类别' and @viewid != 'store'
    begin
       set @sql = @sql + ' and [门店类别] =''' +  @grade + '''';
    end

    EXEC(@sql);
   
          insert into @partdataxl(name,value) values(@q5zt,(select q5z1 from #temp))
    insert into @partdataxl(name,value) values(@q4zt,(select q4z1 from #temp))
    insert into @partdataxl(name,value) values(@q3zt,(select q3z1 from #temp))
    insert into @partdataxl(name,value) values(@q2zt,(select q2z1 from #temp))
    insert into @partdataxl(name,value) values(@q1zt,(select q1z1 from #temp))
    insert into @partdataxl(name,value) values(@bzt,(select bz1 from #temp))
  
          insert into @partdatasr(name,value) values(@q5zt,(select q5z4 from #temp)/10000)
    insert into @partdatasr(name,value) values(@q4zt,(select q4z4 from #temp)/10000)
    insert into @partdatasr(name,value) values(@q3zt,(select q3z4 from #temp)/10000)
    insert into @partdatasr(name,value) values(@q2zt,(select q2z4 from #temp)/10000)
    insert into @partdatasr(name,value) values(@q1zt,(select q1z4 from #temp)/10000)
    insert into @partdatasr(name,value) values(@bzt,(select bz4 from #temp)/10000)
       
    select 'chart_zbqs' tableid,'终端销量(台)' tbtitle,'bar' charttype,* from @partdataxl
    select 'chart_zbqs' tableid,'总毛利收入(万)' tbtitle,'line' charttype,* from @partdatasr


end