UPDATE AKUZ.T_MEDICAL_RECORD set type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'PSYCHO_DISPANSERISATION_MOVENMENT')
WHERE type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'MENTAL_DISPANSERISATION_MOVENMENT')
---
CREATE TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_MOVENMENT]
(
    [MedicalRecordID] uniqueidentifier NOT NULL,
    [SurveillanceType] int NULL,
    [DnGroup] int NULL,
    [MentalDispanserisationType] int NULL
)
ON [PRIMARY]
GO
ALTER TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_MOVENMENT] ADD CONSTRAINT [PK_T_PSYCHO_DISPANSERISATION_MOVENMENT]
PRIMARY KEY CLUSTERED ([MedicalRecordID]) ON [PRIMARY]
GO
ALTER TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_MOVENMENT] ADD CONSTRAINT [FK_T_MENTAL_DISPANSERISATION_MOVENMENT_T_PSYCHO_DISPANSERISATION_MOVENMENT_MedicalRecordID]
FOREIGN KEY([MedicalRecordID]) REFERENCES [AKUZ].[T_MENTAL_DISPANSERISATION_MOVENMENT] ([MedicalRecordID])
GO
---
INSERT INTO AKUZ.T_PSYCHO_DISPANSERISATION_MOVENMENT (MedicalRecordID, SurveillanceType, DnGroup, MentalDispanserisationType)
SELECT MedicalRecordID, SurveillanceType, DnGroup, MentalDispanserisationType
FROM AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT;
---
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_MOVENMENT] DROP COLUMN [SurveillanceType]
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_MOVENMENT] DROP COLUMN [DnGroup]
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_MOVENMENT] DROP COLUMN [MentalDispanserisationType]
GO

ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_END] ADD [CheckUpNarcStatusEnd] int NULL
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_END] ALTER COLUMN [CheckUpStatusEnd] int NULL
GO
---
CREATE TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT]
(
    [MedicalRecordID] uniqueidentifier NOT NULL,
    [PsychoDispanserisationType] int NULL,
    [SurveillanceType] int NOT NULL,
    [DnGroup] int NULL
)
ON [PRIMARY]
GO
ALTER TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT] ADD CONSTRAINT [PK_T_PSYCHO_DISPANSERISATION_INIT]
PRIMARY KEY CLUSTERED ([MedicalRecordID]) ON [PRIMARY]
GO
ALTER TABLE [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT] ADD CONSTRAINT [FK_T_MENTAL_DISPANSERISATION_INIT_T_PSYCHO_DISPANSERISATION_INIT_MedicalRecordID]
FOREIGN KEY([MedicalRecordID]) REFERENCES [AKUZ].[T_MENTAL_DISPANSERISATION_INIT] ([MedicalRecordID])
GO

INSERT INTO [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT] (MedicalRecordID, SurveillanceType, DnGroup, PsychoDispanserisationType)
SELECT MedicalRecordID, SurveillanceType, DnGroup, MentalDispanserisationType FROM [AKUZ].[T_MENTAL_DISPANSERISATION_INIT]


ALTER TABLE [AKUZ].[T_MENTAL_DISEASE_RECORD] ADD [PsychoDispanserisationInit] uniqueidentifier NOT NULL CONSTRAINT [df_PsychoDispanserisationInit] DEFAULT '00000000-0000-0000-0000-000000000000' WITH VALUES
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISEASE_RECORD] DROP CONSTRAINT [df_PsychoDispanserisationInit]
GO
UPDATE [AKUZ].[T_MENTAL_DISEASE_RECORD] set [PsychoDispanserisationInit] = [MentalDispanserisationInit]
GO

ALTER TABLE [AKUZ].[T_MENTAL_DISEASE_RECORD] ADD CONSTRAINT [FK_T_PSYCHO_DISPANSERISATION_INIT_T_MENTAL_DISEASE_RECORD_PsychoDispanserisationInit]
FOREIGN KEY([PsychoDispanserisationInit]) REFERENCES [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT] ([MedicalRecordID])
GO

ALTER TABLE [AKUZ].[T_MENTAL_DISEASE_RECORD] DROP CONSTRAINT [FK_T_MENTAL_DISPANSERISATION_INIT_T_MENTAL_DISEASE_RECORD_MentalDispanserisationInit]
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISEASE_RECORD] DROP COLUMN [MentalDispanserisationInit]
GO


ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] ADD [PsychoDispanserisationInit] uniqueidentifier NOT NULL CONSTRAINT [df_PsychoDispanserisationInit] DEFAULT '00000000-0000-0000-0000-000000000000' WITH VALUES
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] DROP CONSTRAINT [df_PsychoDispanserisationInit]
GO
UPDATE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] set [PsychoDispanserisationInit] = [MentalDispanserisationInit]
GO

ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] ADD CONSTRAINT [FK_T_PSYCHO_DISPANSERISATION_INIT_T_MENTAL_DISPANSERISATION_PRESENCE_PsychoDispanserisationInit]
FOREIGN KEY([PsychoDispanserisationInit]) REFERENCES [AKUZ].[T_PSYCHO_DISPANSERISATION_INIT] ([MedicalRecordID])
GO

ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] DROP CONSTRAINT [FK_T_MENTAL_DISPANSERISATION_INIT_T_MENTAL_DISPANSERISATION_PRESENCE_MentalDispanserisationInit]
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_PRESENCE] DROP COLUMN [MentalDispanserisationInit]
GO


ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_INIT] DROP COLUMN SurveillanceType
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_INIT] DROP COLUMN DnGroup
GO
ALTER TABLE [AKUZ].[T_MENTAL_DISPANSERISATION_INIT] DROP COLUMN MentalDispanserisationType
GO

UPDATE [AKUZ].[T_MEDICAL_RECORD] set type_ = (select EntityID from [VCLib].[T_ENTITIES] where EntName = 'PSYCHO_DISPANSERISATION_INIT')
WHERE type_ = (select EntityID from [VCLib].[T_ENTITIES] where EntName = 'MENTAL_DISPANSERISATION_INIT')



CREATE TABLE [AKUZ].[T_NARCOLOGY_DISPANSERISATION_FILE]
(
    [FileID] uniqueidentifier NOT NULL,
    [NarcologyDispanserisationInit] uniqueidentifier NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [AKUZ].[T_NARCOLOGY_DISPANSERISATION_FILE] ADD CONSTRAINT [PK_T_NARCOLOGY_DISPANSERISATION_FILE]
PRIMARY KEY CLUSTERED ([FileID]) ON [PRIMARY]
GO

ALTER TABLE [AKUZ].[T_NARCOLOGY_DISPANSERISATION_FILE] ADD CONSTRAINT [FK_T_FILE_T_NARCOLOGY_DISPANSERISATION_FILE_FileID]
FOREIGN KEY([FileID]) REFERENCES [VCLib].[T_FILE] ([FileID])
GO

ALTER TABLE [AKUZ].[T_NARCOLOGY_DISPANSERISATION_FILE] ADD CONSTRAINT [FK_T_MENTAL_DISPANSERISATION_INIT_T_NARCOLOGY_DISPANSERISATION_FILE_NarcologyDispanserisationInit]
FOREIGN KEY([NarcologyDispanserisationInit]) REFERENCES [AKUZ].[T_MENTAL_DISPANSERISATION_INIT] ([MedicalRecordID])
GO