IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_tbqs]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_tbqs]
GO
CREATE PROCEDURE userfunc_752_get_tbqs(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@tblx varchar(10),@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 @sql1 nvarchar(1000);
    declare @sql2 nvarchar(1000);
    
    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 @partdata1 table(name varchar(128),value decimal(10,2));
    DECLARE @partdata2 table(name varchar(128),value decimal(10,2));
    DECLARE @partdata3 table(name varchar(128),value decimal(10,2));
    DECLARE @partdata4 table(name varchar(128),value decimal(10,2));
    
    create table #tempstore(storeno varchar(10));
    
    CREATE TABLE #temp1 (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));
                        
    CREATE TABLE #temp2 (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));
    
    
    select top 1 @q7zt = [前七周],@q6zt = [前六周],@q5zt = [前五周],@q4zt = [前四周],@q3zt = [前三周],@q2zt = [前二周],@q1zt = [前一周],@bzt = [本周] FROM [DBTC].[datas].[dbo].[dd_fwk_date]
    
    insert into  #tempstore(storeno)
    SELECT dy.[门店号]
    FROM [DBTC].[datas].[dbo].[dd_date_dy] as dy inner join [DBTC].[datas].[dbo].[dd_date_td] as td
    on dy.[门店号] = td.[门店号] and dy.[数据日期] = td.[数据日期]
    where dy.[数据日期] = @date
    
    set @sql1 = 'insert into #temp1' 
             +'(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_dy]'
             +' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    
    set @sql2 = 'insert into #temp2' 
             +'(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([前七周总毛收入])'
    if @tblx = 'TQ'
    begin
        set @sql2 = @sql2 +' from [DBTC].[datas].[dbo].[dd_date_tq]'
    end
    else if @tblx = 'TD'
    begin
        set @sql2 = @sql2 +' from [DBTC].[datas].[dbo].[dd_date_td]'
    end      
    set @sql2 = @sql2 +' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    
    if @viewid = 'branchf' 
    begin
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + '''';
       set @sql2 = @sql2 + ' and [分公司名称] = ''' + @branch + '''';
    end 
    else if @viewid = 'area'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
       set @sql2 = @sql2 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
    end
    else if @viewid = 'store'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
       set @sql2 = @sql2 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
    end
    
    if @viewid != 'store'
    begin
       set @sql1 = @sql1 + ' and [门店号] in (select storeno from #tempstore)';
       set @sql2 = @sql2 + ' and [门店号] in (select storeno from #tempstore)';
    end
    
    if @grade != '全部类别' and @viewid != 'store'
    begin
       set @sql1 = @sql1 + ' and [门店类别] =''' +  @grade + '''';
       set @sql2 = @sql2 + ' and [门店类别] =''' +  @grade + '''';
    end

    EXEC(@sql1);
    EXEC(@sql2);
    
          insert into @partdata1(name,value) values(@q5zt,case when (select q5z1 from #temp2) > 0 then ((select q5z1 from #temp1)/(select q5z1 from #temp2) - 1)*100 else 0 end)
    insert into @partdata1(name,value) values(@q4zt,case when (select q4z1 from #temp2) > 0 then ((select q4z1 from #temp1)/(select q4z1 from #temp2) - 1)*100 else 0 end)
    insert into @partdata1(name,value) values(@q3zt,case when (select q3z1 from #temp2) > 0 then ((select q3z1 from #temp1)/(select q3z1 from #temp2) - 1)*100 else 0 end)
    insert into @partdata1(name,value) values(@q2zt,case when (select q2z1 from #temp2) > 0 then ((select q2z1 from #temp1)/(select q2z1 from #temp2) - 1)*100 else 0 end)
    insert into @partdata1(name,value) values(@q1zt,case when (select q1z1 from #temp2) > 0 then ((select q1z1 from #temp1)/(select q1z1 from #temp2) - 1)*100 else 0 end)
    insert into @partdata1(name,value) values(@bzt,case when (select bz1 from #temp2) > 0 then ((select bz1 from #temp1)/(select bz1 from #temp2) - 1)*100 else 0 end)
    
          insert into @partdata2(name,value) values(@q5zt,case when (select q5z2 from #temp2) > 0 then ((select q5z2 from #temp1)/(select q5z2 from #temp2) - 1)*100 else 0 end)
    insert into @partdata2(name,value) values(@q4zt,case when (select q4z2 from #temp2) > 0 then ((select q4z2 from #temp1)/(select q4z2 from #temp2) - 1)*100 else 0 end)
    insert into @partdata2(name,value) values(@q3zt,case when (select q3z2 from #temp2) > 0 then ((select q3z2 from #temp1)/(select q3z2 from #temp2) - 1)*100 else 0 end)
    insert into @partdata2(name,value) values(@q2zt,case when (select q2z2 from #temp2) > 0 then ((select q2z2 from #temp1)/(select q2z2 from #temp2) - 1)*100 else 0 end)
    insert into @partdata2(name,value) values(@q1zt,case when (select q1z2 from #temp2) > 0 then ((select q1z2 from #temp1)/(select q1z2 from #temp2) - 1)*100 else 0 end)
    insert into @partdata2(name,value) values(@bzt,case when (select bz2 from #temp2) > 0 then ((select bz2 from #temp1)/(select bz2 from #temp2) - 1)*100 else 0 end)
    
          insert into @partdata3(name,value) values(@q5zt,case when (select q5z3 from #temp2) > 0 then ((select q5z3 from #temp1)/(select q5z3 from #temp2) - 1)*100 else 0 end)
    insert into @partdata3(name,value) values(@q4zt,case when (select q4z3 from #temp2) > 0 then ((select q4z3 from #temp1)/(select q4z3 from #temp2) - 1)*100 else 0 end)
    insert into @partdata3(name,value) values(@q3zt,case when (select q3z3 from #temp2) > 0 then ((select q3z3 from #temp1)/(select q3z3 from #temp2) - 1)*100 else 0 end)
    insert into @partdata3(name,value) values(@q2zt,case when (select q2z3 from #temp2) > 0 then ((select q2z3 from #temp1)/(select q2z3 from #temp2) - 1)*100 else 0 end)
    insert into @partdata3(name,value) values(@q1zt,case when (select q1z3 from #temp2) > 0 then ((select q1z3 from #temp1)/(select q1z3 from #temp2) - 1)*100 else 0 end)
    insert into @partdata3(name,value) values(@bzt,case when (select bz3 from #temp2) > 0 then ((select bz3 from #temp1)/(select bz3 from #temp2) - 1)*100 else 0 end)
    
          insert into @partdata4(name,value) values(@q5zt,case when (select q5z4 from #temp2) > 0 then ((select q5z4 from #temp1)/(select q5z4 from #temp2) - 1)*100 else 0 end)
    insert into @partdata4(name,value) values(@q4zt,case when (select q4z4 from #temp2) > 0 then ((select q4z4 from #temp1)/(select q4z4 from #temp2) - 1)*100 else 0 end)
    insert into @partdata4(name,value) values(@q3zt,case when (select q3z4 from #temp2) > 0 then ((select q3z4 from #temp1)/(select q3z4 from #temp2) - 1)*100 else 0 end)
    insert into @partdata4(name,value) values(@q2zt,case when (select q2z4 from #temp2) > 0 then ((select q2z4 from #temp1)/(select q2z4 from #temp2) - 1)*100 else 0 end)
    insert into @partdata4(name,value) values(@q1zt,case when (select q1z4 from #temp2) > 0 then ((select q1z4 from #temp1)/(select q1z4 from #temp2) - 1)*100 else 0 end)
    insert into @partdata4(name,value) values(@bzt,case when (select bz4 from #temp2) > 0 then ((select bz4 from #temp1)/(select bz4 from #temp2) - 1)*100 else 0 end)
    
    select 'chart_tbqs' tableid,'终端销量' tbtitle,'line' charttype,* from @partdata1
    select 'chart_tbqs' tableid,'终端毛利收入' tbtitle,'line' charttype,* from @partdata2
    select 'chart_tbqs' tableid,'配件毛利收入' tbtitle,'line' charttype,* from @partdata3
    select 'chart_tbqs' tableid,'销售毛利收入' tbtitle,'line' charttype,* from @partdata4
    


end