IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_15_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_15_data]
GO
CREATE PROCEDURE report_15_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_fgs varchar(512)
,@qry_hdlx varchar(512)
,@qry_date1 varchar(512)
,@qry_date2 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)
create table #temp (
    tableid varchar(10),
    ord_branch varchar(100),
    ord_mdbh varchar(100),
    ord_createdepart varchar(100),
    ord_xbd int default 0,
    ord_dgj int default 0,
    ord_yyy int default 0,
    ord_xj int default 0,
    ord_rs int default 0,
    ord_rj decimal(10,2) default 0,
    ord_gj int default 0,
    ord_yy int default 0,
    ord_jt int default 0,
    ord_jtl decimal(10,2) default 0,
    ord_zxcjl decimal(10,2) default 0,
    ord_dd int default 0,
    ord_rjdd decimal(10,2) default 0,
    ord_cj int default 0,
    ord_rjcj decimal(10,2) default 0,
    ord_ddl decimal(10,2) default 0,
    ord_gjl decimal(10,2) default 0,
    ord_thsc decimal(10,2) default 0,
    ord_dgsc decimal(10,2) default 0
)


insert into #temp(tableid, ord_branch,ord_mdbh,ord_createdepart,ord_rs)
select DISTINCT 'tb_report1' tableid, ord_branch,ord_mdbh,ord_createdepart,COUNT(DISTINCT ord_creator)
from order_239
where ord_branch = @qry_fgs
and ord_finished > 0
and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
group by ord_branch,ord_mdbh,ord_createdepart

update #temp set ord_xbd = (
   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 >=  @qry_date1 + ' 00:00:00'
    and d.ord2_whsj <=  @qry_date2 + ' 23:59:59'
    and d.ord2_pre_id = 0
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_mdbh = m.ord_mdbh)
    
update #temp set ord_dgj = (
   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 >=  @qry_date1 + ' 00:00:00'
    and d.ord2_whsj <=  @qry_date2 + ' 23:59:59'
    and d.ord2_pre_id > 0
    and d.ord2_pre_whzt = '待跟进'
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_mdbh = m.ord_mdbh)

update #temp set ord_yyy = (
   select count(ord2_id) 
   from order_239 as m inner join order_239_2 as d
   on m.ord_id = d.ord2_ordid 
   where d.ord2_whsj >=  @qry_date1 + ' 00:00:00'
    and d.ord2_whsj <=  @qry_date2 + ' 23:59:59'
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_mdbh = m.ord_mdbh)

update #temp set ord_xj = ord_xbd + ord_dgj +ord_yyy 

update #temp set ord_jt = ord_xj - (
   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 >= @qry_date1 + ' 00:00:00'
    and d.ord2_whsj <= @qry_date2 + ' 23:59:59'
    and d.ord2_pre_id = 0
    and d.ord2_whzt = '未联系到'
    and (@qry_hdlx = '全部' or m.ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_mdbh = m.ord_mdbh)
    
update #temp set ord_gj = (
   select count(ord_id) 
   from order_239 
   where ord_whsj >= @qry_date1 + ' 00:00:00'
    and ord_whsj <= @qry_date2 + ' 23:59:59'
    and ord_status = '外呼跟进'
    and ord_whzt = '待跟进'
    and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = ord_branch and #temp.ord_mdbh = ord_mdbh)
    
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 >=  @qry_date1 + ' 00:00:00'
    and d.ord2_whsj <=  @qry_date2 + ' 23:59:59'
    and d.ord2_whzt = '已预约'
    and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = m.ord_branch and #temp.ord_mdbh = m.ord_mdbh)

update #temp set ord_dd = (
   select count(ord_id) 
   from order_239 
   where ord_cjsj >= @qry_date1 + ' 00:00:00'
    and ord_cjsj <= @qry_date2 + ' 23:59:59'
    and ord_ddzt = '已到店'
    and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = ord_branch and #temp.ord_mdbh = ord_mdbh)
    
update #temp set ord_cj = (
   select count(ord_id) 
   from order_239 
   where ord_cjsj >= @qry_date1 + ' 00:00:00'
    and ord_cjsj <= @qry_date2 + ' 23:59:59'
    and ord_sfcj = '已成交'
    and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx)
    and #temp.ord_branch = ord_branch and #temp.ord_mdbh = ord_mdbh)
    
update #temp set ord_rj = case when ord_rs > 0 then ord_xj/(ord_rs + 0.0)/(DATEDIFF(day,@qry_date1,@qry_date2) + 1.0) else 0 end,
                 ord_jtl = case when ord_xj > 0 then ord_jt/(ord_xj + 0.0)*100 else 0 end,
                 ord_zxcjl = case when ord_xj - ord_yyy > 0 then ord_yy / (ord_xj - ord_yyy + 0.0)*100 else 0 end,
                 ord_ddl = case when ord_xj > 0 then ord_dd / (ord_xj + 0.0)*100 else 0 end,
                 ord_gjl = case when ord_dd > 0 then ord_cj / (ord_dd + 0.0)*100 else 0 end,
                 ord_rjdd = case when ord_rs > 0 then ord_dd/(ord_rs + 0.0)/(DATEDIFF(day,@qry_date1,@qry_date2) + 1.0) else 0 end,
                 ord_rjcj = case when ord_rs > 0 then ord_cj/(ord_rs + 0.0)/(DATEDIFF(day,@qry_date1,@qry_date2) + 1.0) else 0 end

delete from #temp 
where ord_xbd = 0 and ord_dgj = 0 and ord_yyy = 0 
  and ord_xj = 0 and ord_gj = 0 and ord_yy = 0 
  and ord_jt = 0 and ord_dd = 0 and ord_cj = 0
                
select * from  #temp


    

END