IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_30_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_30_data]
GO
CREATE PROCEDURE report_30_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_date1 varchar(512)
,@qry_mdlx varchar(512)
,@qry_hdlx varchar(512)
,@qry_fgs varchar(512)

AS
BEGIN
 set nocount on 

declare @dataIds table(objid int)
if @objids!='_NONE' and @objids!='_none_' and @objids!='' 
insert into @dataIds(objid)
select value from dbo.fn_split_int(@objids)
declare @date1 varchar(10)
declare @date2 varchar(10)
declare @date3 varchar(10)
declare @date4 varchar(10)
declare @date5 varchar(10)
declare @date6 varchar(10)
declare @date7 varchar(10)
set @date1 = @qry_date1
set @date2 = CONVERT(varchar(10),DATEADD(day,1,convert(datetime,@date1)),20)
set @date3 = CONVERT(varchar(10),DATEADD(day,2,convert(datetime,@date1)),20)
set @date4 = CONVERT(varchar(10),DATEADD(day,3,convert(datetime,@date1)),20)
set @date5 = CONVERT(varchar(10),DATEADD(day,4,convert(datetime,@date1)),20)
set @date6 = CONVERT(varchar(10),DATEADD(day,5,convert(datetime,@date1)),20)
set @date7 = CONVERT(varchar(10),DATEADD(day,6,convert(datetime,@date1)),20)

declare @dpids table(dp_id int)

create table #temp (
    tableid varchar(10),
    ord_branch varchar(100),
    ord_createdepart varchar(100),
    ord_bds1 int default 0,
    ord_bdrs1 int default 0,
    ord_bds2 int default 0,
    ord_bdrs2 int default 0,
    ord_bds3 int default 0,
    ord_bdrs3 int default 0,
    ord_bds4 int default 0,
    ord_bdrs4 int default 0,
    ord_bds5 int default 0,
    ord_bdrs5 int default 0,
    ord_bds6 int default 0,
    ord_bdrs6 int default 0,
    ord_bds7 int default 0,
    ord_bdrs7 int default 0,
    ord_bdshj int default 0,
    ord_bdrshj int default 0,
    ord_rjbd decimal(10,2) default 0,
    ord_yy int default 0,
    ord_rjyy decimal(10,2) default 0,
    ord_cj int default 0,
    ord_rjcj decimal(10,2) default 0
)

create table #temp1 (
    ord_type varchar(10),
    ord_yxbd varchar(10),
    ord_id int default 0,
    ord2_id int default 0,
    ord_branch varchar(100),
    ord_createdepart varchar(100),
    ord_creator varchar(100),
    ord2_whsj varchar(10)
)

if @qry_mdlx = '全部门店' 
begin
   insert into @dpids(dp_id) select dp_id from mps_depart where dp_isdepart = 1
end
else if @qry_mdlx = '专卖店' 
begin
   insert into @dpids(dp_id) select dp_id from mps_depart where dp_isdepart = 1 and dp_ext1 like '%专卖店%'
end
else if @qry_mdlx = '综合店' 
begin
   insert into @dpids(dp_id) select dp_id from mps_depart where dp_isdepart = 1 and (dp_ext1 like '%商圈店%' or dp_ext1 like '%社区店%')
end
else if @qry_mdlx = '商圈店' 
begin
   insert into @dpids(dp_id) select dp_id from mps_depart where dp_isdepart = 1 and dp_ext1 like '%商圈店%'
end
else if @qry_mdlx = '社区店' 
begin
   insert into @dpids(dp_id) select dp_id from mps_depart where dp_isdepart = 1 and dp_ext1 like '%社区店%'
end

--新拨打
insert into #temp1(ord_type, ord_yxbd,ord_id,ord2_id,ord_branch,ord_createdepart,ord_creator,ord2_whsj)
select '拨打','是',m.ord_id,d.ord2_id,m.ord_branch,m.ord_createdepart,m.ord_creator,CONVERT(varchar(10),d.ord2_whsj,20)
   from order_239 as m inner join order_239_1 as d
   on m.ord_id = d.ord2_ordid 
   where d.ord2_whsj >=  @date1 + ' 00:00:00'
    and d.ord2_whsj <=  @date7 + ' 23:59:59'
    and d.ord2_pre_id = 0
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and m.ord_branch = @qry_fgs
    and m.ord_createdpid in (select dp_id from @dpids)

--待跟进拨打    
insert into #temp1(ord_type, ord_yxbd,ord_id,ord2_id,ord_branch,ord_createdepart,ord_creator,ord2_whsj)
select '拨打', case when d.ord2_pre_whzt = '待跟进' then '是' else '否' end,
      m.ord_id,d.ord2_id,m.ord_branch,m.ord_createdepart,m.ord_creator,CONVERT(varchar(10),d.ord2_whsj,20)
   from order_239 as m inner join order_239_1 as d
   on m.ord_id = d.ord2_ordid 
   where d.ord2_whsj >=  @date1 + ' 00:00:00'
    and d.ord2_whsj <=  @date7 + ' 23:59:59'
    and d.ord2_pre_id > 0
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and m.ord_branch = @qry_fgs
    and m.ord_createdpid in (select dp_id from @dpids)

--已预约拨打
insert into #temp1(ord_type,ord_yxbd,ord_id,ord2_id,ord_branch,ord_createdepart,ord_creator,ord2_whsj)
select '拨打','是',m.ord_id,d.ord2_id,m.ord_branch,m.ord_createdepart,m.ord_creator,CONVERT(varchar(10),d.ord2_whsj,20)
   from order_239 as m inner join order_239_2 as d
   on m.ord_id = d.ord2_ordid 
   where d.ord2_whsj >=  @date1 + ' 00:00:00'
    and d.ord2_whsj <=  @date7 + ' 23:59:59'
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and m.ord_branch = @qry_fgs
    and m.ord_createdpid in (select dp_id from @dpids)
    
insert into #temp(tableid, ord_branch,ord_createdepart,ord_bds1,ord_bdrs1,ord_bds2,ord_bdrs2,
                    ord_bds3,ord_bdrs3,ord_bds4,ord_bdrs4,ord_bds5,ord_bdrs5,
                    ord_bds6,ord_bdrs6,ord_bds7,ord_bdrs7)
select 'tb_report1',ord_branch,ord_createdepart,  
       count(case when ord2_whsj = @date1 and ord_yxbd = '是' then ord_createdepart end) bds1,
       count(DISTINCT (case when ord2_whsj = @date1 then ord_creator end)) bdrs1,
       count(case when ord2_whsj = @date2 and ord_yxbd = '是' then ord_createdepart end) bds2,
       count(DISTINCT (case when ord2_whsj = @date2 then ord_creator end)) bdrs2,
       count(case when ord2_whsj = @date3 and ord_yxbd = '是' then ord_createdepart end) bds3,
       count(DISTINCT (case when ord2_whsj = @date3 then ord_creator end)) bdrs3,
       count(case when ord2_whsj = @date4 and ord_yxbd = '是' then ord_createdepart end) bds4,
       count(DISTINCT (case when ord2_whsj = @date4 then ord_creator end)) bdrs4,
       count(case when ord2_whsj = @date5 and ord_yxbd = '是' then ord_createdepart end) bds5,
       count(DISTINCT (case when ord2_whsj = @date5 then ord_creator end)) bdrs5,
       count(case when ord2_whsj = @date6 and ord_yxbd = '是' then ord_createdepart end) bds6,
       count(DISTINCT (case when ord2_whsj = @date6 then ord_creator end)) bdrs6,
       count(case when ord2_whsj = @date7 and ord_yxbd = '是' then ord_createdepart end) bds7,
       count(DISTINCT (case when ord2_whsj = @date7 then ord_creator end)) bdrs7
from #temp1
group by ord_branch,ord_createdepart
order by ord_branch,ord_createdepart

update #temp set ord_yy =  (
   select count(ord2_id) 
   from order_239 as m inner join order_239_1 as d
   on m.ord_id = d.ord2_ordid 
   where d.ord2_whsj >=  @date1 + ' 00:00:00'
    and d.ord2_whsj <=  @date2 + ' 23:59:59'
    and d.ord2_whzt = '已预约'
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_createdepart = m.ord_createdepart
    and m.ord_createdpid in (select dp_id from @dpids))

update #temp set ord_cj = (
   select count(ord_id) 
   from order_239 
   where ord_cjsj >= @date1 + ' 00:00:00'
    and ord_cjsj <= @date2 + ' 23:59:59'
    and ord_sfcj = '已成交'
    and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = ord_branch and #temp.ord_createdepart = ord_createdepart
    and ord_createdpid in (select dp_id from @dpids))

/*insert into #temp(tableid,ord_branch,ord_createdepart,ord_bds1,ord_bdrs1,ord_bds2,ord_bdrs2,
                    ord_bds3,ord_bdrs3,ord_bds4,ord_bdrs4,ord_bds5,ord_bdrs5,
                    ord_bds6,ord_bdrs6,ord_bds7,ord_bdrs7)
select 'tb_report1','','合计',sum(ord_bds1),sum(ord_bdrs1),sum(ord_bds2),sum(ord_bdrs2),
                    sum(ord_bds3),sum(ord_bdrs3),sum(ord_bds4),sum(ord_bdrs4),sum(ord_bds5),sum(ord_bdrs5),
                    sum(ord_bds6),sum(ord_bdrs6),sum(ord_bds7),sum(ord_bdrs7)
from #temp*/


update #temp set ord_bdshj =  ord_bds1 + ord_bds2 + ord_bds3 + ord_bds4 + ord_bds5 + ord_bds6 + ord_bds7,
                 ord_bdrshj =  ord_bdrs1 + ord_bdrs2 + ord_bdrs3 + ord_bdrs4 + ord_bdrs5 + ord_bdrs6 + ord_bdrs7
                
update #temp set ord_rjbd = case when ord_bdrshj > 0 then ord_bdshj/(ord_bdrshj + 0.0) else 0 end,
                 ord_rjyy = case when ord_bdrshj > 0 then ord_yy/(ord_bdrshj + 0.0) else 0 end,
                 ord_rjcj = case when ord_bdrshj > 0 then ord_cj/(ord_bdrshj + 0.0) else 0 end

              

select * from  #temp


    

END