IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_29_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_29_data]
GO
CREATE PROCEDURE report_29_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@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_mdgs int DEFAULT 0,--门店个数
     ord_jcpc int DEFAULT 0,--检查频次
     ord_bhgs int DEFAULT 0,--不合格数
     ord_hgl decimal(18,2) default 0.0,--合格率
     ord_hgldf int DEFAULT 0,--合格的分
 ord_mddf decimal(18,1) default 0.0,--门店得分
 ord_yjs int DEFAULT 0,--夜间检查数
 ord_aqdf decimal(18,1) default 0.0,--安全得分
 ord_bcid int DEFAULT 0--公司id
 )

insert into #temp(tableid, ord_branch,ord_bcid)
select DISTINCT 'tb_report1' tableid,ord_szfgs,ord_zbgsbcid
from order_248
where ord_status != '已作废' and charindex('临',ord_no) = 0
and ord_date >= @qry_date1 + ' 00:00:00' and  ord_date <= @qry_date2 + ' 23:59:59'
group by ord_szfgs,ord_zbgsbcid





update #temp set ord_mdgs =(
select count(dp_id) from mps_depart a
    where dp_isdepart = 1 and  #temp.ord_bcid=a.dp_bcid and a.dp_status != '关店'
)

update #temp set ord_mdgs = 50 where ord_branch='成都分公司'


update #temp set ord_jcpc =(select count(distinct ord_dpmc) from order_248 a
where ord_status != '已作废' and charindex('临',ord_no) = 0
and ord_date >= @qry_date1 + ' 00:00:00' and  ord_date <= @qry_date2 + ' 23:59:59'
and #temp.ord_bcid=a.ord_zbgsbcid)

update #temp set ord_bhgs =(select count(distinct ord_dpmc) from order_248 a
where ord_status != '已作废' and charindex('临',ord_no) = 0
and a.ord_zgzt != '不需要整改'
and ord_date >= @qry_date1 + ' 00:00:00' and  ord_date <= @qry_date2 + ' 23:59:59'
and #temp.ord_bcid=a.ord_zbgsbcid)

update #temp set ord_hgl =  CONVERT(decimal(18, 2),(ord_mdgs - ord_bhgs) * 1.0 / ord_mdgs)


update #temp set ord_hgldf = 90 where ord_hgl >=0.9 and ord_jcpc = ord_mdgs
update #temp set ord_hgldf = 85 where ord_hgl >=0.8 and ord_hgl<0.9  and ord_jcpc = ord_mdgs
update #temp set ord_hgldf = 75 where ord_hgl <0.8 or ord_jcpc != ord_mdgs

update #temp set ord_mddf = 75-0.5*(ord_mdgs - ord_jcpc)



update #temp set ord_yjs =(select count(distinct ord_dpmc) from order_248 a,order_248_2 b
where ord_status != '已作废' and charindex('临',ord_no) = 0 and ord_id = b.ord2_ordid 
and ord_date >= @qry_date1 + ' 00:00:00' and  ord_date <= @qry_date2 + ' 23:59:59'
and ord2_jclx = '夜间检查'
and ord2_jcqk != '未检查'
and #temp.ord_bcid=a.ord_zbgsbcid)



update #temp set ord_aqdf = ord_hgldf where ord_mddf < ord_hgldf
update #temp set ord_aqdf = ord_mddf where ord_mddf > ord_hgldf



select * from #temp
END