IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_752_get_mdrjcl]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_752_get_mdrjcl]
GO
CREATE PROCEDURE userfunc_752_get_mdrjcl(@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 @dblmb decimal(18,2);
    
    create table #temp21(dbmds decimal(18,2))
    create table #temp22(mdzs decimal(18,2))
    create table #temp3(zmsrys decimal(18,2),zmsrwc decimal(18,2),rs decimal(18,2))
    
    declare @sql1 nvarchar(1000);
    declare @sql21 nvarchar(1000);
    declare @sql22 nvarchar(1000);
    declare @sql3 nvarchar(1000);
    
    DECLARE @partdata1 table(name varchar(128),value decimal(10,2));
    DECLARE @partdata2 table(name varchar(128),value decimal(10,2));
    
       if  @viewid = 'branchz' or  @viewid = 'branchf' 
    begin
      select @dblmb = 0.67;
    end
    else if @viewid = 'area'
    begin
      select @dblmb = [单店总毛收入预算达标率] from [DBTC].[datas].[dbo].[dd_date_avg] 
      where [分公司名称] = @branch and [区域] =  @area ;
    end
    else 
    begin
       select @dblmb = 0;
    end
    
    delete from #temp21;
    delete from #temp22;
    delete from #temp3;
    
       set @sql21 = ' insert into #temp21(dbmds) select count(*) from [DBTC].[datas].[dbo].[dd_date_avg]'
               + ' where [总毛收入完成率] >= 1 and [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')'; 
       set @sql22 = 'insert into #temp22(mdzs) select count(*)  from [DBTC].[datas].[dbo].[dd_date_avg]'
               + ' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
               
    set @sql3 = 'insert into #temp3(zmsrys,zmsrwc,rs) select sum([总毛收入预算量]), sum([总毛收入完成量]),sum([人数]) from [DBTC].[datas].[dbo].[dd_date_avg]' 
              + ' where [数据日期]=''' + @date + ''' and  [商圈类型] in (' +  @types + ')';
    
    if @viewid = 'branchf' 
    begin
       set @sql21 = @sql21 + ' and [分公司名称] = ''' + @branch + '''';
       set @sql22 = @sql22 + ' and [分公司名称] = ''' + @branch + '''';
       set @sql3 = @sql3 + ' and [分公司名称] = ''' + @branch + '''';
    end 
    else if @viewid = 'area'
    begin 
       set @sql21 = @sql21 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
       set @sql22 = @sql22 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
       set @sql3 = @sql3 + ' and [分公司名称] = ''' + @branch + ''' and [区域] = ''' + @area + '''';
    end
    else if @viewid = 'store'
    begin 
       set @sql21 = @sql21 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
       set @sql22 = @sql22 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
       set @sql3 = @sql3 + ' and [分公司名称] = ''' + @branch + ''' and [门店号] = ''' + @store + '''';
    end
    
    if @grade != '全部类别' and @viewid != 'store'
    begin
       set @sql21 = @sql21 + ' and [门店类别] =''' +  @grade + '''';
       set @sql22 = @sql22 + ' and [门店类别] =''' +  @grade + '''';
       set @sql3 = @sql3 + ' and [门店类别] =''' +  @grade + '''';
    end
    
    EXEC(@sql21);
    EXEC(@sql22);
    EXEC(@sql3);
    
    insert into @partdata1(name,value) values('当月目标',@dblmb * 100.00)
    insert into @partdata1(name,value) values('当月累计',case when (select mdzs from #temp22) > 0 then (select dbmds from #temp21)/(select mdzs from #temp22)*100 else 0 end)
    
    insert into @partdata2(name,value) values('当月目标',case when (select rs from #temp3) > 0 then (select zmsrys from #temp3)/(select rs from #temp3) else 0 end)
    insert into @partdata2(name,value) values('当月累计',case when (select rs from #temp3) > 0 then (select zmsrwc from #temp3)/(select rs from #temp3) else 0 end)
    
    select 'chart_mdrjcl' tableid,'预算达标率' tbtitle,'bar' charttype,* from @partdata1
    select 'chart_mdrjcl' tableid,'人均创利' tbtitle,'bar' charttype,* from @partdata2
    


end