--升级TousseDefinition表的includeImplant字段
update TousseDefinition set includeImplant=0
where id in (
	select distinct case when md.isImplant='是' then td.id else 0 end
	from TousseDefinition td
	inner join MaterialInstance mi on mi.tousse_id=td.id
	inner join MaterialDefinition md on md.id=mi.materialDefinition_id
) and (includeImplant is null or includeImplant<>0);
update TousseDefinition set includeImplant=1
where id not in (
	select distinct case when md.isImplant='是' then td.id else 0 end
	from TousseDefinition td
	inner join MaterialInstance mi on mi.tousse_id=td.id
	inner join MaterialDefinition md on md.id=mi.materialDefinition_id
) and (includeImplant is null or includeImplant<>1);
--升级TousseInstance表的isRoutine字段
update TousseInstance set isRoutine='否'
where id in(
	select ti.id
	from TousseInstance ti 
	join TousseDefinition td on td.id=ti.tousseDefinition_id
	join SterilizationRecord sr on sr.id=ti.sterilizationRecord_id
	join Sterilizer s on s.id=sr.sterilizer_id
	where td.includeImplant=0
	and ti.invoiceSendTime is not null
	and (
		sr.biologyResult='不合格'
		or (sr.biologicalMonitoringEndDate is not null and sr.biologicalMonitoringEndDate > ti.invoiceSendTime) 
		or (sr.biologicalMonitoringEndDate is null and s.lagTime is not null and s.biologicalMonitoringTime is not null and dateadd(minute,(s.lagTime+s.biologicalMonitoringTime),sr.endDate) > ti.invoiceSendTime)
	)
) and (isRoutine is null or isRoutine<>'否');
update TousseInstance set isRoutine='是'
where id in(
	select ti.id
	from TousseInstance ti 
	join TousseDefinition td on td.id=ti.tousseDefinition_id
	join SterilizationRecord sr on sr.id=ti.sterilizationRecord_id
	join Sterilizer s on s.id=sr.sterilizer_id
	where td.includeImplant=0
	and ti.invoiceSendTime is not null
	and (sr.biologyResult is null or sr.biologyResult<>'不合格')
	and (
		(sr.biologicalMonitoringEndDate is not null and sr.biologicalMonitoringEndDate < ti.invoiceSendTime) 
		or (sr.biologicalMonitoringEndDate is null and s.lagTime is not null and s.biologicalMonitoringTime is not null and dateadd(minute,(s.lagTime+s.biologicalMonitoringTime),sr.endDate) < ti.invoiceSendTime)
	)
) and (isRoutine is null or isRoutine<>'是');