---  1、变更的表名

--EXEC sp_rename 'old_table_name', 'new_table_name';
EXEC sp_rename 'CriterionNidus', 'CriterionNidusSystem';
EXEC sp_rename 'ReadingCriterionDictionary', 'ReadingSystemCriterionDictionary';


----  2、处理发布环境的表数据拆分

--会直接创建表 CriterionNidusSystem_copy
		--SELECT *
		--INTO CriterionNidusSystem_copy
		--FROM CriterionNidusSystem
		--WHERE IsSystemCriterion = 0;
		
--创建CriterionNidusTrial	 将数据迁移过来
SELECT *
INTO CriterionNidusTrial
FROM CriterionNidusSystem
WHERE IsSystemCriterion = 0;


SELECT *
INTO ReadingTrialCriterionDictionary
FROM ReadingSystemCriterionDictionary
WHERE IsSystemCriterion = 0;


-----  三、维护稽查数据
select Count(*) from DataInspection where EntityName='CriterionNidus' 
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Add'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Update'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Deleted'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Add/IsTrial'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Add/IsTrial/Auto'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Update/IsTrial'
select Count(*) from DataInspection where EntityName='CriterionNidus' and Identification='CriterionNidus/Deleted/IsTrial'


select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary' 
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/QuestionType'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/LesionType'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/OncologyAssessType'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/GlobalAssessType'   
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/QuestionType/IsTrial'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/LesionType/IsTrial'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/OncologyAssessType/IsTrial'
select Count(*) from DataInspection where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/GlobalAssessType/IsTrial'


--系统 CriterionNidusSystem
update DataInspection   SET Identification='CriterionNidusSystem/Add', EntityName='CriterionNidusSystem' where EntityName='CriterionNidus' and Identification='CriterionNidus/Add'
update  DataInspection  SET Identification='CriterionNidusSystem/Update' , EntityName='CriterionNidusSystem' where EntityName='CriterionNidus' and Identification='CriterionNidus/Update'
update DataInspection   SET Identification='CriterionNidusSystem/Deleted', EntityName='CriterionNidusSystem' where EntityName='CriterionNidus' and Identification='CriterionNidus/Deleted'

--项目 CriterionNidusTrial
update DataInspection  SET Identification='CriterionNidusTrial/Add' , EntityName='CriterionNidusTrial' where EntityName='CriterionNidus' and Identification='CriterionNidus/Add/IsTrial'
update DataInspection  SET Identification='CriterionNidusTrial/Add/Auto' , EntityName='CriterionNidusTrial' where EntityName='CriterionNidus' and Identification='CriterionNidus/Add/IsTrial/Auto'
update DataInspection  SET Identification='CriterionNidusTrial/Update' , EntityName='CriterionNidusTrial' where EntityName='CriterionNidus' and Identification='CriterionNidus/Update/IsTrial'
update DataInspection  SET Identification='CriterionNidusTrial/Deleted' , EntityName='CriterionNidusTrial' where EntityName='CriterionNidus' and Identification='CriterionNidus/Deleted/IsTrial'

--系统ReadingSystemCriterionDictionary
update DataInspection   SET Identification='ReadingSystemCriterionDictionary/Add/QuestionType',        EntityName='ReadingSystemCriterionDictionary' where EntityName='ReadingCriterionDictionary' and Identification='ReadingCriterionDictionary/Add/QuestionType'
update DataInspection  SET Identification='ReadingSystemCriterionDictionary/Add/LesionType' ,          EntityName='ReadingSystemCriterionDictionary' where EntityName='ReadingCriterionDictionary' and Identification='ReadingCriterionDictionary/Add/LesionType'
update DataInspection   SET Identification='ReadingSystemCriterionDictionary/Add/OncologyAssessType',  EntityName='ReadingSystemCriterionDictionary' where EntityName='ReadingCriterionDictionary' and Identification='ReadingCriterionDictionary/Add/OncologyAssessType'
update DataInspection   SET Identification='ReadingSystemCriterionDictionary/Add/GlobalAssessType',    EntityName='ReadingSystemCriterionDictionary' where EntityName='ReadingCriterionDictionary' and Identification='ReadingCriterionDictionary/Add/GlobalAssessType'   

--项目ReadingTrialCriterionDictionary
update DataInspection   SET Identification='ReadingTrialCriterionDictionary/Add/QuestionType', EntityName='ReadingTrialCriterionDictionary' where EntityName='ReadingCriterionDictionary'          and Identification='ReadingCriterionDictionary/Add/QuestionType/IsTrial'
update  DataInspection  SET Identification='ReadingTrialCriterionDictionary/Add/LesionType' ,  EntityName='ReadingTrialCriterionDictionary' where EntityName='ReadingCriterionDictionary'          and Identification='ReadingCriterionDictionary/Add/LesionType/IsTrial'
update DataInspection    SET Identification='ReadingTrialCriterionDictionary/Add/OncologyAssessType',  EntityName='ReadingTrialCriterionDictionary' where EntityName='ReadingCriterionDictionary'  and Identification='ReadingCriterionDictionary/Add/OncologyAssessType/IsTrial'
update DataInspection  SET Identification='ReadingTrialCriterionDictionary/Add/GlobalAssessType', EntityName='ReadingTrialCriterionDictionary' where EntityName='ReadingCriterionDictionary'         and Identification='ReadingCriterionDictionary/Add/GlobalAssessType/IsTrial'



--国际化indication 维护
UPDATE Trial SET IndicationEnum = CASE 
                                      WHEN Indication = '肺癌' THEN 0
                                      WHEN Indication = '实体瘤' THEN 1
                                      WHEN Indication = '淋巴瘤' THEN 2
                                      WHEN Indication = '乳腺癌' THEN 3
                                      WHEN Indication = '白血病' THEN 4
                                      WHEN Indication = '胃癌' THEN 5
                                      WHEN Indication = '肝癌' THEN 6
                                      WHEN Indication = '骨髓癌' THEN 7
                                      WHEN Indication = '肠癌' THEN 8
                                      WHEN Indication = '前列腺癌' THEN 9
                                      WHEN Indication = '肾癌' THEN 10
                                      WHEN Indication = '宫颈癌' THEN 11
                                      WHEN Indication = '头颈部癌' THEN 12
                                      WHEN Indication = '卵巢癌' THEN 13
                                      WHEN Indication = '胆癌' THEN 14
                                      WHEN Indication = '食管癌' THEN 15
                                      WHEN Indication = '腹膜癌' THEN 16
                                      WHEN Indication = '上皮癌' THEN 17
                                      WHEN Indication = '黑色素瘤' THEN 18
                                      WHEN Indication = '甲状腺癌' THEN 19
                                      WHEN Indication = '输卵管癌' THEN 20
                                      WHEN Indication = '胰腺癌' THEN 21
                                      WHEN Indication = '膀胱癌' THEN 22
                                      WHEN Indication = '鼻咽癌' THEN 23
                                      WHEN Indication = '胶质瘤' THEN 24
                                      WHEN Indication = '子宫癌' THEN 25
                                      WHEN Indication = '尿道癌' THEN 26
                                      WHEN Indication = '软组织肉瘤' THEN 27
                                      WHEN Indication = '神经内分泌瘤' THEN 28
                                      WHEN Indication = '睾丸癌' THEN 29
                                      WHEN Indication = '骨肉瘤' THEN 30
                                      WHEN Indication = '间皮瘤' THEN 31
                                      WHEN Indication = '绒毛膜病' THEN 32
                                      WHEN Indication = '胃肠道间质瘤' THEN 33
                                      WHEN Indication = '骨癌' THEN 34
                                      WHEN Indication = '脑瘤' THEN 35
                                      WHEN Indication = '胸腺瘤' THEN 36
																			WHEN IndicationTypeId = '437a81b3-c5ad-49fd-82a1-8f441c4ba8da' THEN 37
																			WHEN IndicationTypeId = '433d9801-5fa0-42bb-604b-08d9c1232c4f' THEN 38
																			WHEN IndicationTypeId = 'a5fdd212-a716-4df4-604c-08d9c1232c4f' THEN 39
                                      ELSE IndicationEnum
                                 END;
																 
																 select DISTINCT IndicationEnum ,Indication from Trial

                                                                 	 update Trial set Indication=''  where IndicationEnum not in   (37,38,39)


-- 附加评估发布设置之前的默认值
Update ReadingQuestionCriterionTrial set IsAdditionalAssessment=0,IsAutoCreate=1

--- 维护临床数据英文名称
update clinicalDataSystemSet set clinicalDataSystemSet.ClinicalDataSetEnName=DictionaryChild.Value from Dictionary DictionaryChild 
inner join clinicalDataSystemSet on CONVERT(varchar(200),clinicalDataSystemSet.ClinicalDataSetEnum) =DictionaryChild.Code
inner join Dictionary DictionaryParent on DictionaryParent.Id=DictionaryChild.ParentId
where DictionaryParent.Code='ClinicalDataType'
go


update ClinicalDataTrialSet set ClinicalDataTrialSet.ClinicalDataSetEnName=clinicalDataSystemSet.ClinicalDataSetEnName from clinicalDataSystemSet
inner join ClinicalDataTrialSet on ClinicalDataTrialSet.SystemClinicalDataSetId=clinicalDataSystemSet.Id



---------------维护项目器官表  将器官信息在项目表里面存冗余
update OrganTrialInfo set
OrganTrialInfo.PartEN=OrganInfo.PartEN ,
OrganTrialInfo.Part=OrganInfo.Part,
OrganTrialInfo.TULOC=OrganInfo.TULOC,
OrganTrialInfo.TULOCEN=OrganInfo.TULOCEN,
OrganTrialInfo.TULAT=OrganInfo.TULAT,
OrganTrialInfo.TULATEN=OrganInfo.TULATEN,
OrganTrialInfo.IsLymphNodes=OrganInfo.IsLymphNodes,
OrganTrialInfo.IsCanEditPosition=OrganInfo.IsCanEditPosition,
OrganTrialInfo.Classification=OrganInfo.Classification,
OrganTrialInfo.ClassificationEN=OrganInfo.ClassificationEN,
OrganTrialInfo.ShowOrder=OrganInfo.ShowOrder,
OrganTrialInfo.OrganType=OrganInfo.OrganType
from  OrganInfo inner join OrganTrialInfo
on OrganInfo.Id=OrganTrialInfo.OrganInfoId where OrganTrialInfo.OrganType is null




update FrontAuditConfig set Identification='' where Identification is NULL

--新增字段

ALTER TABLE [dbo].[Trial] ADD [AttendedReviewerTypes] varchar(255) COLLATE Chinese_PRC_CI_AS DEFAULT '' NOT NULL
GO

ALTER TABLE [dbo].[Trial] ADD [DeclarationTypes] varchar(255) COLLATE Chinese_PRC_CI_AS DEFAULT '' NOT NULL
GO


--维护sql
UPDATE Trial SET AttendedReviewerTypes = '|' + CAST(AttendedReviewerType AS VARCHAR(10)) + '|'

update Trial set DeclarationTypes ='|' + (select Code  from Dictionary where Id= Trial.DeclarationTypeId) + '|'


--验证sql
select DISTINCT AttendedReviewerType ,AttendedReviewerTypes, DeclarationTypeId, Dictionary.Code,DeclarationTypes,Dictionary.[Value] from Trial inner join Dictionary on Trial.DeclarationTypeId=Dictionary.Id








