
--无序维护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


--2022 12 -9 同步
--  Dictionary  UserTypeMenu Menu  FrontAuditConfig  ReadingQuestionCriterionSystem  ReadingTableQuestionSystem 



--阅片标准维护
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


