-----2022-05-27------------------------

update DataInspection set BatchId=Id where  BatchId is null

update QCChallenge set Code=ChallengeCode

update QCChallenge set ChallengeCode='Q'+ RIGHT('00000'+CAST( Code AS nvarchar(50)),5);




-----2022-08-11------------------------

update [dbo].[Site]  set Code=  convert(int,RIGHT(SiteCode,4))
update Site set  SiteName=  substring(SiteName,1,charindex('医院',SiteName)+1),AliasName=substring(SiteName,charindex('医院',SiteName)+2,30)   where SiteName like '%医院%医院'


update Doctor set Physician='Chief Physician',PhysicianCN='主任医师'  where  RankId='dcbf4d1a-8373-4539-a0ae-1ba76d57bb48'
update Doctor set Physician='Deputy Chief Physician',PhysicianCN='副主任医师'  where  RankId='3a6e3335-cea4-4803-a136-d6301883a937'
update Doctor set RankOther='Professor',RankOtherCN='教授'  where  RankId='dcbf4d1a-8373-4539-a0ae-1ba76d57bb48'
update Doctor set RankOther='Associate Professor',RankOtherCN='副教授'  where  RankId='3a6e3335-cea4-4803-a136-d6301883a937'
update Doctor set Physician='Staff',PhysicianCN='医师'  where  RankId='82411c2e-9832-4c6f-a859-e3b6e796760d'


update DataInspection set JsonDetail= replace(cast(JsonDetail as varchar(max)),'Insepection','CommonData')

update  ReadingClinicalData set FileCount=(select count(*) from ReadingClinicalDataPDF where ReadingClinicalDataId  =ReadingClinicalData.Id)
update  NoneDicomStudy set FileCount=(select count(*) from NoneDicomStudyFile where NoneDicomStudyId  =NoneDicomStudy.Id)


update DataInspection set  CreateUserName= (select UserName from [User] where [User].Id = DataInspection.CreateUserId)

update DataInspection set  RoleName=(select UserName from [User]  INNER join UserType  on [User].UserTypeId =UserType.Id   where [User].Id=DataInspection.CreateUserId)

update DataInspection set  CreateUserRealName=(select LastName+' / '+FirstName  from [User] where [User].Id=DataInspection.CreateUserId)


update  TrialDocUserTypeConfirmedUser  set CreateUserId=ConfirmUserId,CreateTime=SignFirstViewTime
update  SystemDocConfirmedUser  set CreateUserId=ConfirmUserId,CreateTime=SignFirstViewTime
update  TrialDocUserTypeConfirmedUser  set CreateUserId=ConfirmUserId,CreateTime=getdate()  where SignFirstViewTime is null 
update  SystemDocConfirmedUser  set CreateUserId=ConfirmUserId,CreateTime=getdate()   where SignFirstViewTime is null 
update  TrialDocUserTypeConfirmedUser  set IsDeleted=0  where IsDeleted is null 
update  SystemDocConfirmedUser  set IsDeleted=0   where IsDeleted is null 


update EnrollReadingCategory set TrialReadingCriterionId=  ISNUll((select top 1  Id   from ReadingQuestionCriterionTrial where IsConfirm=1  and TrialId=Enroll.TrialId ) ,'00000000-0000-0000-0000-000000000000')
FROM EnrollReadingCategory INNER JOIN Enroll ON (EnrollReadingCategory.EnrollId = Enroll.Id)

update VisitTask set TrialReadingCriterionId=(select top 1   Id  from ReadingQuestionCriterionTrial where IsConfirm=1 and TrialId=VisitTask.TrialId)
select * from  EnrollReadingCategory  where TrialReadingCriterionId is null
select * from  VisitTask  where TrialReadingCriterionId is null


 update Site  set Site.Code = (select b.code2 from    (select Id,row_number()  over(order by Site.SiteName) code2 from Site) b where b.Id =Site.Id)
  update Site set SiteCode= 'S'+ RIGHT('0000'+CAST(Site.Code as varchar(10)),4)

  --不用同步
update FrontAuditConfig set  ValueCN='任务名称' where Value='TaskName' and ValueCN='检查批次/阅片期名称'  and Code='TaskName'
update FrontAuditConfig set  ValueCN='任务盲态标识' where Value='TaskBlindName' and ValueCN='检查批次盲态名称'  and Code='TaskBlindName'

  -----------------------------------------之前的-------------------------------------------------------------



--无序维护sql
update   VisitTask  set TaskBlindName='Follow-up'  
 from VisitTask
INNER join ReadingQuestionCriterionTrial on VisitTask.TrialReadingCriterionId=ReadingQuestionCriterionTrial.Id    
INNER join SubjectVisit on VisitTask.SourceSubjectVisitId=SubjectVisit.Id   
 where  SubjectVisit.IsBaseLine=0    and ReadingQuestionCriterionTrial.IsReadingTaskViewInOrder=0


-- 有序维护sql
update    VisitTask set TaskBlindName='Follow-up '+  cast(sv.rn  as varchar) 
 from VisitTask
join ReadingQuestionCriterionTrial on VisitTask.TrialReadingCriterionId=ReadingQuestionCriterionTrial.Id    
--join SubjectVisit on VisitTask.SourceSubjectVisitId=SubjectVisit.Id    
join (  select Id SubjectVisitId, IsBaseLine, ROW_NUMBER() over( partition by SubjectId  order by VisitNum asc )-1 rn,VisitNum from SubjectVisit where IsLostVisit=0)  sv on  VisitTask.SourceSubjectVisitId=sv.SubjectVisitId    
where  sv.IsBaseLine=0    and ReadingQuestionCriterionTrial.IsReadingTaskViewInOrder=1

update   VisitTask  set TaskBlindName='Baseline'    from VisitTask 
join SubjectVisit on VisitTask.SourceSubjectVisitId=SubjectVisit.Id    where  SubjectVisit.IsBaseLine=1




----------------------------------------------修改 ----------------------------------------
update Dictionary set code =0 where Id='15bb5529-a6fe-439f-5196-08da179a7080'
update Dictionary set code =1 where Id='0628d7be-afba-4471-5197-08da179a7080'
update Dictionary set code =2 where Id='41bfec4b-dbfb-401d-5198-08da179a7080'
update Dictionary set code =-1 where Id='1fac678d-69b6-41c3-5199-08da179a7080'
go


update ReadingQuestionCriterionTrial set DigitPlaces=DigitPlaces-1 where DigitPlaces is not null
update ReadingQuestionCriterionTrial set DigitPlaces=-1 where DigitPlaces=3






--阅片标准维护
update DataInspection set ObjectRelationParentId3  = (select top 1 TrialReadingCriterionId  from   SubjectUser where Id =DataInspection.GeneralId)  where EntityName='SubjectUser'
update DataInspection set ObjectRelationParentId2  = (select top 1 TrialReadingCriterionId  from   ReadingPeriodSet where Id =DataInspection.GeneralId)  where EntityName='ReadingPeriodSet'
update DataInspection set ObjectRelationParentId2  = (select top 1 TrialReadingCriterionId  from   ReadModule where Id =DataInspection.GeneralId)  where EntityName='ReadModule'
update DataInspection set ObjectRelationParentId2  = (select top 1 TrialReadingCriterionId  from   TaskConsistentRule where Id =DataInspection.GeneralId)  where EntityName='TaskConsistentRule'
update DataInspection set ObjectRelationParentId3  = (select top 1 TrialReadingCriterionId  from   VisitTask where Id =DataInspection.GeneralId)  where EntityName='VisitTask'

update ReadingTableQuestionTrial  set DictionaryCode='' where DictionaryCode is null




--维护RowInfo的OrderMark

select *  from  ReadingTableAnswerRowInfo
update ReadingTableAnswerRowInfo set ReadingTableAnswerRowInfo.OrderMark=ReadingQuestionTrial.OrderMark from ReadingQuestionTrial 
inner join ReadingTableAnswerRowInfo on  ReadingTableAnswerRowInfo.QuestionId=ReadingQuestionTrial.Id



delete ReadingTableQuestionAnswer where  TableQuestionId in (select id from ReadingTableQuestionTrial where QuestionMark=3) 
go 
delete ReadingTableQuestionTrial where QuestionMark=3
go 
delete ReadingTableQuestionSystem where QuestionMark=3
go


------------------------------------2022-12-13----------------------------
update ReadingQuestionSystem  set DefaultValue=''
update ReadingQuestionTrial set DefaultValue=''

----------删除之后要重新同步器官数据
delete   OrganInfo where SystemCriterionId='B0450000-9B8E-98FA-6658-08DA4DAB1FAC'
delete OrganTrialInfo
    where (select count(1) as num from OrganInfo where OrganInfo.Id = OrganTrialInfo.OrganInfoId) = 0


----2022-12-15
Update Trial set BlindBaseLineName='Baseline',BlindFollowUpPrefix='Follow-up'
update DataInspection set VisitTaskId  = GeneralId  where EntityName='VisitTask'

--2022-12-17 维护稽查数据
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('OriginalReReadingTaskId":"',JsonDetail)+26,36) where   EntityName='VisitTaskReReading'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='TaskMedicalReview'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='ReadingOncologyTaskInfo'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='ReadingMedicalReviewDialog'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='ReadingTaskQuestionAnswer'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='ReadingTableAnswerRowInfo'
update DataInspection set VisitTaskId= substring(JsonDetail,CHARINDEX('VisitTaskId":"',JsonDetail)+14,36) where   EntityName='ReadingMedicineQuestionAnswer'


update DataInspection set SubjectVisitId=null where EntityName='ReadModule'
update DataInspection set TrialReadingCriterionId=(select TrialReadingCriterionId from VisitTask where Id =VisitTaskId)  where VisitTaskId is not null  and TrialReadingCriterionId is null 



--稽查bug  数据维护

update DataInspection set TrialReadingCriterionId= substring(JsonDetail,CHARINDEX('TrialCriterionId":"',JsonDetail)+19 +
(   DATALENGTH(  left( cast([JsonDetail] as VARCHAR(2500)) ,charindex('TrialCriterionId":"',JsonDetail))  )
    -LEN      (  left( cast([JsonDetail] as VARCHAR(2500)) ,charindex('TrialCriterionId":"',JsonDetail))  ) 
),36) where   EntityName='ReadingTableQuestionTrial'
update DataInspection set TrialReadingCriterionId= substring(JsonDetail,CHARINDEX('ReadingQuestionCriterionTrialId":"',JsonDetail)+34,36) where   EntityName='ReadingQuestionTrial'
update DataInspection set TrialReadingCriterionId=  GeneralId  where   EntityName='ReadingQuestionCriterionTrial'

--维护临床数据状态
update ReadingClinicalData set IsSign=1 where EXISTS(select * from SubjectVisit where Id=ReadingId and IsBaseLine=1 and IsConfirmedClinicalData=1) and IsSign=0
update ReadingClinicalData  set ReadingClinicalDataState=3 where IsSign=1  and ReadingClinicalDataState!=3



--20221223--发布生产开始



update DataInspection  set Identification='ReadingMedicineQuestionAnswer/Add' , ObjectTypeId='E8250000-3E2C-0016-8166-08DA7E8DFF90',OptTypeId='02FCF476-4089-4B2C-B9BE-08DA0E391693',ModuleTypeId='A4460000-3E2C-0016-F305-08DA7E8C40BB'   where EntityName='ReadingMedicineQuestionAnswer'
update DataInspection  set  ObjectTypeId='A4100000-3E2C-0016-A716-08DA7E8DA9BD',OptTypeId='02FCF476-4089-4B2C-B9BE-08DA0E391693',ModuleTypeId='A4460000-3E2C-0016-F305-08DA7E8C40BB'    where Identification='ReadingMedicalReview/saveMedicalReviewInfo/TaskMedicalReview/Update'
update DataInspection  set Identification='ReadingMedicalReviewDialog/Add' , ObjectTypeId='D8270000-3E2C-0016-2A14-08DA3EDB77FD',OptTypeId='02FCF476-4089-4B2C-B9BE-08DA0E391693',ModuleTypeId='A4460000-3E2C-0016-F305-08DA7E8C40BB'   where EntityName='ReadingMedicalReviewDialog'

update DataInspection set Identification='ReadingMedicalReviewDialog/Add/IR'    from DataInspection  INNER JOIN [User] on DataInspection.CreateUserId = [User].Id   where DataInspection.EntityName='ReadingMedicalReviewDialog'  and [User].UserTypeEnum=13
update DataInspection set Identification='ReadingMedicalReviewDialog/Add/MIM'    from DataInspection  INNER JOIN [User] on DataInspection.CreateUserId = [User].Id   where DataInspection.EntityName='ReadingMedicalReviewDialog'  and [User].UserTypeEnum=14



---------修改  ReadingMedicineQuestionAnswer的VisitTaskId
update ReadingMedicineQuestionAnswer set ReadingMedicineQuestionAnswer.VisitTaskId=TaskMedicalReview.VisitTaskId from TaskMedicalReview 
inner join ReadingMedicineQuestionAnswer on ReadingMedicineQuestionAnswer.TaskMedicalReviewId=TaskMedicalReview.id 




update  frontAuditConfig set  DictionaryCode =''   where Code='AuditState' and DictionaryCode='AuditState'

----正式环境 修改TrialCode  错误的





----------------------------2023-1-09

-- 修改Hospital 的SiteId
update Hospital set Hospital.SiteId=Site.Id from Site 
inner join Hospital on Hospital.HospitalName=Site.SiteName 


---2023 -1-10
--处理检查批次
    --基线
update VisitTask set IsNeedClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  

 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=1  and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and (ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1)  and IsNeedClinicalDataSign=0
 
 
 update VisitTask set IsNeedClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  

 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=1  and not EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and (ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1)  and IsNeedClinicalDataSign=1

    --非基线
update VisitTask set IsNeedClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  
 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=0 and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and ClinicalDataLevel=1 and IsConfirm=1) and IsNeedClinicalDataSign =0
 
 
 update VisitTask set IsNeedClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  
 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=0 and not EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and ClinicalDataLevel=1 and IsConfirm=1)  and IsNeedClinicalDataSign =1
	 
--处理裁判

update VisitTask set IsNeedClinicalDataSign =0,IsClinicalDataSign=0    where VisitTask.ReadingCategory=4

--处理肿瘤学

update VisitTask set IsNeedClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=5   and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%'   and ClinicalDataLevel=3 and IsConfirm=1)
  and IsNeedClinicalDataSign =0

  update VisitTask set IsNeedClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=5   and not EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%'   and ClinicalDataLevel=3 and IsConfirm=1)
  and IsNeedClinicalDataSign =1

--处理全局

update VisitTask set IsNeedClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=2  and  EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%'   and ClinicalDataLevel=2 and IsConfirm=1)
 and IsNeedClinicalDataSign =0

  
update VisitTask set IsNeedClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=2  and not EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%'   and ClinicalDataLevel=2  and IsConfirm=1 ) 
 and IsNeedClinicalDataSign=1


 -----处理  是否所有临床数据都签名的字段
--处理检查批次
    --基线
update VisitTask set IsClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  

 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=1   and  VisitTask.IsClinicalDataSign =0 and VisitTask.IsNeedClinicalDataSign=1
  and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and (ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1)
 and (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and ( ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1 and UploadRole=1 
 )
 =(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SourceSubjectVisitId and ClinicalDataTrialSet.UploadRole=1 
        --   有可能仅仅IC Subject级别 需要签名   IC自动签名了  没有PM需要签名的文档  此时应该设置为已签署
        )

--处理基线脏数据
        update VisitTask set IsClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  

 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=1   and  VisitTask.IsClinicalDataSign =1 and VisitTask.IsNeedClinicalDataSign=1
  and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and (ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1)
 and (
 select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and ( ClinicalDataLevel=0 or ClinicalDataLevel=1) and IsConfirm=1 and UploadRole=1 
 )
 !=(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SourceSubjectVisitId and ClinicalDataTrialSet.UploadRole=1 
        --   有可能仅仅IC Subject级别 需要签名   IC自动签名了  没有PM需要签名的文档  此时应该设置为已签署
        )


   
		--非基线
update VisitTask set IsClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  
 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=0  and VisitTask.IsClinicalDataSign =0 and VisitTask.IsNeedClinicalDataSign=1
 
 -- 非基线 可能只有IC 检查批次级别 PM 没有
 and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=1 and IsConfirm=1)
 and (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=1 and IsConfirm=1 and  UploadRole=1 )
 =(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SourceSubjectVisitId and ClinicalDataTrialSet.UploadRole=1)
   
   --维护错误数据
   	 update VisitTask set IsClinicalDataSign =0 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join SubjectVisit on   VisitTask.SourceSubjectVisitId=SubjectVisit.Id  
 where VisitTask.ReadingCategory=1  and SubjectVisit.IsBaseLine=0  and VisitTask.IsClinicalDataSign =1 and VisitTask.IsNeedClinicalDataSign=1
 
 -- 非基线 可能只有IC 检查批次级别 PM 没有
 and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=1 and IsConfirm=1)
 and (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=1 and IsConfirm=1 and  UploadRole=1 )
 !=(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SourceSubjectVisitId and ClinicalDataTrialSet.UploadRole=1)
	 

--处理肿瘤学

update VisitTask set IsClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=5    and VisitTask.IsClinicalDataSign =0 and VisitTask.IsNeedClinicalDataSign=1
 and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=3 and IsConfirm=1)
  and   (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=3 and IsConfirm=1)
 =(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SouceReadModuleId and ClinicalDataTrialSet.UploadRole=1)
			

 
--处理全局
update VisitTask set IsClinicalDataSign =1 from VisitTask 
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
 where VisitTask.ReadingCategory=2  	and  VisitTask.IsClinicalDataSign =0 and VisitTask.IsNeedClinicalDataSign=1
 and  EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=2 and IsConfirm=1)
 
 and     (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=2 and IsConfirm=1)
 =(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SouceReadModuleId and ClinicalDataTrialSet.UploadRole=1)

        --维护脏数据
			update VisitTask set IsClinicalDataSign =0 from VisitTask 
			
			--select  VisitTask.Id as VisitTaskId ,Trial.Id, Trial.ExperimentName,  VisitTask.TaskName,ReadingQuestionCriterionTrial.CriterionName,Subject.Code ,VisitTask.IsNeedClinicalDataSign,VisitTask.IsClinicalDataSign ,VisitTask.DoctorUserId from VisitTask
inner join ReadingQuestionCriterionTrial on   ReadingQuestionCriterionTrial.Id=VisitTask.TrialReadingCriterionId  
inner join ReadModule on   VisitTask.SouceReadModuleId=ReadModule.Id  
inner join Subject on   VisitTask.SubjectId=Subject.Id  
inner join Trial on   VisitTask.TrialId=Trial.Id  
 where VisitTask.ReadingCategory=2  			and  VisitTask.IsClinicalDataSign =1 and VisitTask.IsNeedClinicalDataSign=1
 and EXISTS (select * from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=2 and IsConfirm=1)
 
  and (select  count(*) from ClinicalDataTrialSet where TrialId=VisitTask.TrialId and CriterionEnumListStr LIKE '%|'+ CAST(ReadingQuestionCriterionTrial.CriterionType AS varchar(20))+'|%' and  ClinicalDataLevel=2 and IsConfirm=1 )
 !=(select count(*) from ReadingClinicalData  inner join ClinicalDataTrialSet  on ReadingClinicalData.ClinicalDataTrialSetId =ClinicalDataTrialSet.Id
    	where ReadingClinicalData.TrialId=VisitTask.TrialId and IsSign=1 and ReadingClinicalDataState=3 and ReadingId=VisitTask.SouceReadModuleId and ClinicalDataTrialSet.UploadRole=1 and  ClinicalDataLevel=2 )
 

 --处理字段  IsFrontTaskNeedSignButNotSign
				
 UPDATE v SET  [v].[IsFrontTaskNeedSignButNotSign] =1
FROM [VisitTask] AS [v]
WHERE EXISTS (

 SELECT 1
    FROM [VisitTask] AS [v0] 
		where   [v0].SubjectId=v.SubjectId and [v0].TrialReadingCriterionId=V.TrialReadingCriterionId and [v0].TrialId=V.TrialId and EXISTS(
        SELECT 1
        FROM [VisitTask] AS [v1]
        WHERE [v1].SubjectId=v.SubjectId and V1.TrialReadingCriterionId=V.TrialReadingCriterionId and V1.TrialId=V.TrialId  and  [v1].[IsNeedClinicalDataSign]=1 and V1.IsClinicalDataSign=0 and V1.VisitTaskNum< [v0].[VisitTaskNum]   

				) and  [v0].[IsFrontTaskNeedSignButNotSign]=0      AND [v0].[Id] = [v].[Id] ) 
				
				
 UPDATE v SET  [v].[IsFrontTaskNeedSignButNotSign] =0
FROM [VisitTask] AS [v]
WHERE EXISTS (

 SELECT 1
    FROM [VisitTask] AS [v0] 
		where   [v0].SubjectId=v.SubjectId and [v0].TrialReadingCriterionId=V.TrialReadingCriterionId and [v0].TrialId=V.TrialId and not EXISTS(
        SELECT 1
        FROM [VisitTask] AS [v1]
        WHERE [v1].SubjectId=v.SubjectId and V1.TrialReadingCriterionId=V.TrialReadingCriterionId and V1.TrialId=V.TrialId  and  [v1].[IsNeedClinicalDataSign]=1 and V1.IsClinicalDataSign=0 and V1.VisitTaskNum< [v0].[VisitTaskNum]   

				) and  [v0].[IsFrontTaskNeedSignButNotSign]=1    AND [v0].[Id] = [v].[Id] ) 

    update ReadingClinicalData set IsVisit=1 from ReadingClinicalData INNER JOIN ClinicalDataTrialSet on ReadingClinicalData.ClinicalDataTrialSetId = ClinicalDataTrialSet.Id where ClinicalDataTrialSet.ClinicalDataLevel in (0,1) and  ReadingClinicalData.IsVisit=0
    update VisitTask set IsClinicalDataSign=0 where IsNeedClinicalDataSign=0 and IsClinicalDataSign=1

    -- 判断  检查批次 和阅片期 是否有脏数据吧
--查询确认
select  V.SubjectId, V.SourceSubjectVisitId, V.TrialReadingCriterionId,V.IsSelfAnalysis, v.DoctorUserId,V.ArmEnum,Count(*) as EffectCount,max(v.CreateTime),max(v.Id)  as Id from VisitTask V
join ReadingQuestionCriterionTrial on V.TrialReadingCriterionId = ReadingQuestionCriterionTrial.Id
where V.TaskState=0  and  V.SourceSubjectVisitId is not null and ReadingQuestionCriterionTrial.ReadingType=2
GROUP BY  V.TrialId,  V.SubjectId, V.SourceSubjectVisitId , V.TrialReadingCriterionId ,V.IsSelfAnalysis,V.ArmEnum ,v.DoctorUserId
HAVING Count(*)>1

--删除脏的多生成的任务
delete VisitTask where Id in (select max(v.Id)  as Id from VisitTask V
join ReadingQuestionCriterionTrial on V.TrialReadingCriterionId = ReadingQuestionCriterionTrial.Id
where V.TaskState=0  and  V.SourceSubjectVisitId is not null and ReadingQuestionCriterionTrial.ReadingType=2
GROUP BY  V.TrialId,  V.SubjectId, V.SourceSubjectVisitId , V.TrialReadingCriterionId ,V.IsSelfAnalysis,V.ArmEnum ,v.DoctorUserId
HAVING Count(*)>1)

select V.TrialId, V.SubjectId, V.SouceReadModuleId, V.TrialReadingCriterionId,V.IsSelfAnalysis, V.ArmEnum,Count(*) as EffectCount,max(v.CreateTime),max(v.Id)  as Id from VisitTask V
join ReadingQuestionCriterionTrial on V.TrialReadingCriterionId = ReadingQuestionCriterionTrial.Id
where V.TaskState=0  and  V.SouceReadModuleId is not null and ReadingQuestionCriterionTrial.ReadingType=2
GROUP BY  V.TrialId,  V.SubjectId, V.SouceReadModuleId , V.TrialReadingCriterionId ,V.IsSelfAnalysis,V.ArmEnum
HAVING Count(*)>2


----2023年 开始    
      --  维护临床数据   先运行swagger 维护数据的方法

---------------------维护表格问题ID    2013-1-31
update ReadingTableQuestionTrial   set ReadingTableQuestionTrial.SystemTableQuestionId= ReadingTableQuestionSystem.Id from ReadingTableQuestionSystem
inner join ReadingTableQuestionTrial on ReadingTableQuestionSystem.QuestionName=ReadingTableQuestionTrial.QuestionName
inner join ReadingQuestionTrial on  ReadingTableQuestionTrial.ReadingQuestionId=ReadingQuestionTrial.Id and ReadingQuestionTrial.ReadingQuestionSystemId=ReadingTableQuestionSystem.ReadingQuestionId

where ReadingQuestionTrial.ReadingQuestionSystemId is not null and ReadingTableQuestionTrial.SystemTableQuestionId is null



-------------------维护检查批次任务   IsSelfAnalysis 为 null 的    ------------------------
  
  update VisitTask 
  set RelatedVisitTaskIds= iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']') ,

 PastResultTaskIds=iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  

 where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and  ReadingCategory=1  and  ReadingTaskState =2 and IsSelfAnalysis is null

  
 -----------查询


   select  
 id, VisitTaskNum,
 IsAnalysisCreate,
 IsSelfAnalysis,

 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']') RelatedVisitTaskIds,
 
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  PastResultTaskIds

 from   VisitTask

  where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and  ReadingCategory=1  and  ReadingTaskState =2 and IsSelfAnalysis is null




 -----------------------------------维护检查批次 IsSelfAnalysis 不为null 的    ------------------------



   update VisitTask 
  set RelatedVisitTaskIds=
  iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']')
  ,


 PastResultTaskIds=iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  

 where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and  ReadingCategory=1  and  ReadingTaskState =2 and IsSelfAnalysis is not null



 ------查询 -----------
  select  
 id, VisitTaskNum,
 IsAnalysisCreate,
 IsSelfAnalysis,

 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]', '['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']') RelatedVisitTaskIds,
 
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  PastResultTaskIds

 from   VisitTask

  where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and  ReadingCategory=1  and  ReadingTaskState =2 and IsSelfAnalysis is not null


 -------------------维护全局和肿瘤学任务   IsSelfAnalysis 为 null 的    ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------
  
  update VisitTask 
 set RelatedVisitTaskIds= iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory =1
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory =1
 FOR XML PATH('')),1,1,'') +']') ,
 PastResultTaskIds=iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  

 where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and ( ReadingCategory=2 or ReadingCategory=5)  and  ReadingTaskState =2 and IsSelfAnalysis is null

  
 -----------查询


   select  
 id, VisitTaskNum,
 IsAnalysisCreate,
 IsSelfAnalysis,
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis is null and
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') +']')
 RelatedVisitTaskIds,
 
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis is null and
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  PastResultTaskIds

 from   VisitTask

  where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and ( ReadingCategory=2 or ReadingCategory=5) and  ReadingTaskState =2 and IsSelfAnalysis is null




 -----------------------------------维护全局和肿瘤学 IsSelfAnalysis 不为null 的 



   update VisitTask 
  set RelatedVisitTaskIds= iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
  vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') +']') ,
 PastResultTaskIds=iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  

 where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and ( ReadingCategory=2 or ReadingCategory=5)  and  ReadingTaskState =2 and IsSelfAnalysis is not null



 ------查询 -----------
  select  
 id, VisitTaskNum,
 IsAnalysisCreate,
 IsSelfAnalysis,
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <=VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = 1
 FOR XML PATH('')),1,1,'') +']')
 RelatedVisitTaskIds,
 
 iif(STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') is null,'[]','['+ STUFF((SELECT ',' + '"'+  CONVERT(nvarchar(80),Id)  +'"' FROM VisitTask  vt where  
 vt.TrialId=VisitTask.TrialId and 
 vt.SubjectId =VisitTask.SubjectId and
 vt.ArmEnum =VisitTask.ArmEnum and
 vt.DoctorUserId =VisitTask.DoctorUserId and
 vt.ReadingTaskState =2 and
 vt.TrialReadingCriterionId =VisitTask.TrialReadingCriterionId and
 vt.TaskState =0  and
 vt.VisitTaskNum <VisitTask.VisitTaskNum and
 vt.IsAnalysisCreate =VisitTask.IsAnalysisCreate and
 vt.IsSelfAnalysis = VisitTask.IsSelfAnalysis and 
 vt.ReadingCategory = VisitTask.ReadingCategory
 FOR XML PATH('')),1,1,'') +']' )  PastResultTaskIds

 from   VisitTask

  where  RelatedVisitTaskIds='[]' and  PastResultTaskIds='[]'
 and TaskState=0 and ( ReadingCategory=2 or ReadingCategory=5)  and  ReadingTaskState =2 and IsSelfAnalysis is not null


 --------------------------------------维护阅片标准签名状态----------------
 update ReadingQuestionCriterionTrial set IsSigned=1 where ReadingInfoSignTime is not null





 ---------------------------------------------2023-02-20-----------------------------------
 --维护全局阅片是否修改状态
 update  ReadingTaskQuestionAnswer set IsGlobalChange=1 where  GlobalChangeAnswer is  not null and GlobalChangeAnswer!='' and Answer!=GlobalChangeAnswer


 -----20230301
update CommonDocument set Name='PD进展报告.docx' where Name='PCR_v1.0_Final.docx'
update CommonDocument set Name='入组确认报告.docx' where Name='ECR_v1.0_Final.docx'
update TrialEmailNoticeConfig set FileName='PD进展报告.docx' where FileName='PCR_v1.0_Final.docx'
update TrialEmailNoticeConfig set FileName='入组确认报告.docx' where FileName='ECR_v1.0_Final.docx'



--  出现质疑未关闭  但是实际所有质疑都关闭了（不存在 未关闭的质疑）
select * from SubjectVisit where ChallengeState=2 and not EXISTS(select * from QCChallenge where SubjectVisitId=SubjectVisit.Id and IsClosed=0)
update SubjectVisit set ChallengeState=1 where ChallengeState=2 and not EXISTS(select * from QCChallenge where SubjectVisitId=SubjectVisit.Id and IsClosed=0)




---------------------维护脏的临床数据-------------------


delete ReadingClinicalData where Id in(

select  ReadingClinicalData.Id  from ReadingClinicalData  inner join  ClinicalDataTrialSet on ReadingClinicalData.ClinicalDataTrialSetId=ClinicalDataTrialSet.Id where UploadRole=0   
and ReadingId='00000000-0000-0000-0000-000000000000')


-----维护任务是否可修改加急状态
update  VisitTask set VisitTask.IsCanEditUrgentState=0      where (select count(1) as num from SubjectVisit where SubjectVisit.Id = VisitTask.SourceSubjectVisitId
and (SubjectVisit.PDState=1 or SubjectVisit.IsEnrollmentConfirm=1)

) != 0


update VisitTask  set VisitTask.IsCanEditUrgentState=0   where  (select count(1) as num from ReadModule  inner join  SubjectVisit on ReadModule.SubjectVisitId=SubjectVisit.Id
where ReadModule.Id = VisitTask.SouceReadModuleId
and (SubjectVisit.PDState=1 or SubjectVisit.IsEnrollmentConfirm=1)

) != 0


------------------------维护GroupId
update ReadingQuestionTrial   set GroupId= (select top 1 id from ReadingQuestionTrial a where  a.Type='group'  and a.GroupName=ReadingQuestionTrial.GroupName and a.ReadingQuestionCriterionTrialId=ReadingQuestionTrial.ReadingQuestionCriterionTrialId) where ReadingQuestionTrial.Type!='group'

update ReadingQuestionSystem   set GroupId= (select top 1 id from ReadingQuestionSystem a where  a.Type='group'  and a.GroupName=ReadingQuestionSystem.GroupName and a.ReadingQuestionCriterionSystemId=ReadingQuestionSystem.ReadingQuestionCriterionSystemId) where ReadingQuestionSystem.Type!='group'




-----------------维护快捷键
update ShortcutKey set Text='Page Up' where Text='PageUp'

update ShortcutKey set Text='Page Down' where Text='PageDown'