CREATE TABLE `developer` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор застройщика',
  `value` VARCHAR(270)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Наименование застройщика',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник застройщиков';

INSERT INTO `developer` (`value`) SELECT DISTINCT (developer_name)
                                  FROM `zipal_newbuilding`
                                  WHERE developer_name != '' AND developer_name IS NOT NULL
                                  ORDER BY developer_name;

CREATE TABLE `newbuilding_housing` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор объекта/корпуса',
  `value` VARCHAR(200)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Наименование объекта/корпуса',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Объекты/корпуса для новостроек';

INSERT INTO `newbuilding_housing` (`value`) SELECT DISTINCT (housing_name)
                                            FROM `zipal_newbuilding`
                                            WHERE housing_name != '' AND housing_name IS NOT NULL
                                            ORDER BY housing_name;

CREATE TABLE `newbuilding_object` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор ЖК',
  `value` VARCHAR(200)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Наименование ЖК',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник ЖК';

INSERT INTO `newbuilding_object` (`value`) SELECT DISTINCT (object_name)
                                           FROM `zipal_newbuilding`
                                           WHERE object_name != '' AND object_name IS NOT NULL
                                           ORDER BY object_name;

CREATE TABLE `newbuildings` (
  `id`                     INT(11)                               NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор новостройки',
  `xml_id`                 VARCHAR(100)
                           CHARACTER SET utf8
                           COLLATE utf8_general_ci               NOT NULL
  COMMENT 'Внешний ИД из xml',
  `name`                   VARCHAR(300)
                           CHARACTER SET utf8
                           COLLATE utf8_general_ci               NOT NULL
  COMMENT 'Наименование новостройки',
  `newbuilding_object_id`  INT(11)                               NOT NULL
  COMMENT 'ИД ЖК',
  `newbuilding_housing_id` INT(11)                               NULL     DEFAULT NULL
  COMMENT 'ИД объекта/корпуса',
  `created_at`             TIMESTAMP                             NOT NULL DEFAULT CURRENT_TIMESTAMP
  COMMENT 'Дата и время создания',
  `updated_at`             TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
  COMMENT 'Дата и время обновления',
  `developer_id`           INT(11)                               NULL     DEFAULT NULL
  COMMENT 'Ид застройщика',
  `cian_id`                INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД ЦИАН',
  `cian_korpus_id`         INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД корпуса ЦИАН',
  `yandex_id`              INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД ЖК Яндекс',
  `emls_id`                INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД ЖК ЕМЛС',
  `avito_object_id`        INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД ЖК Авито',
  `avito_housing_id`       INT(10)                               NULL     DEFAULT NULL
  COMMENT 'ИД корпуса Авито',
  `rf_region_id`           INT(11)                               NOT NULL
  COMMENT 'ИД региона РФ',
  PRIMARY KEY (`id`),
  UNIQUE (`xml_id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник новостроек';

CREATE TABLE `inn` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор ИНН',
  `value` VARCHAR(20)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Значение ИНН',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник ИНН';

INSERT INTO `inn` (`value`) SELECT DISTINCT (investor_inn)
                            FROM `objects`
                            WHERE investor_inn != '' AND investor_inn IS NOT NULL
                            ORDER BY investor_inn;

CREATE TABLE `first_names` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор имени',
  `value` VARCHAR(250)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Значение имени',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник имен';

INSERT INTO `first_names` (`value`) SELECT DISTINCT (investor_first_name)
                                    FROM `objects`
                                    WHERE investor_first_name != '' AND investor_first_name IS NOT NULL
                                    ORDER BY investor_first_name;

CREATE TABLE `last_names` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор фамилии',
  `value` VARCHAR(250)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Значение фамилии',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник фамилий';

INSERT INTO `last_names` (`value`) SELECT DISTINCT (investor_last_name)
                                   FROM `objects`
                                   WHERE investor_last_name != '' AND investor_last_name IS NOT NULL
                                   ORDER BY investor_last_name;

CREATE TABLE `second_names` (
  `id`    INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор отчества',
  `value` VARCHAR(250)
          CHARACTER SET utf8
          COLLATE utf8_general_ci NOT NULL
  COMMENT 'Значение отчества',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Справочник отчеств';

INSERT INTO `second_names` (`value`) SELECT DISTINCT (investor_second_name)
                                     FROM `objects`
                                     WHERE investor_second_name != '' AND investor_second_name IS NOT NULL
                                     ORDER BY investor_second_name;

CREATE TABLE `object_nb_info` (
  `id`                     INT(11)                 NOT NULL AUTO_INCREMENT
  COMMENT 'Идентификатор',
  `object_id`              INT(11)                 NOT NULL
  COMMENT 'Идентификатор объекта',
  `newbuilding_id`          INT(11)                 NULL     DEFAULT NULL
  COMMENT 'ИД новостройки',
  `nb_end_date`            DATE                    NULL     DEFAULT NULL
  COMMENT 'Дата завершения строительства',
  `zipal_new_flat_type_id` INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Ид типа новостройки Зипал',
  `investor_type_id`       INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Тип дольщика',
  `last_name_id`           INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Ид справочника фамилий',
  `first_name_id`          INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Ид справочника имен',
  `second_name_id`         INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Ид справочника отчеств',
  `inn_id`                 INT(11)                 NULL     DEFAULT NULL
  COMMENT 'Ид справочника ИНН',
  `documentation_url`      VARCHAR(1000)
                           CHARACTER SET utf8
                           COLLATE utf8_general_ci NULL     DEFAULT NULL
  COMMENT 'Ссылка на документацию',
  PRIMARY KEY (`id`)
)
  ENGINE = InnoDB
  CHARSET = utf8
  COLLATE utf8_general_ci
  COMMENT = 'Информация о новостройке для объекта';

ALTER TABLE `object_nb_info` ADD `zipal_new_flat_decoration_type_id` INT(11) NULL DEFAULT NULL COMMENT 'Идентификатор отделки для авито' AFTER `zipal_new_flat_type_id`;

ALTER TABLE `objects` DROP `documentation_url`;
ALTER TABLE `objects` DROP `zipal_newbuilding_id`;
ALTER TABLE `objects` DROP `zipal_new_flat_type_id`;
ALTER TABLE `objects` DROP `nb_end_date`;
ALTER TABLE `objects` DROP `investor_type_id`;
ALTER TABLE `objects` DROP `investor_last_name`;
ALTER TABLE `objects` DROP `investor_first_name`;
ALTER TABLE `objects` DROP `investor_second_name`;
ALTER TABLE `objects` DROP `investor_inn`;
ALTER TABLE `objects` DROP `zipal_new_flat_decoration_type_id`;

