using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace AsbCloudDb.Migrations
{
    /// <inheritdoc />
    public partial class Add_ProcessMapPlanRotor_And_Slide : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "t_process_map_plan_rotor",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false, comment: "Идентификатор")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    rop_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Максимально допустимая скорость, м/ч"),
                    pressure_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Максимально допустимое давление, атм"),
                    differential_pressure = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм. Уставка"),
                    differential_pressure_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм. Ограничение"),
                    weight_on_bit = table.Column<double>(type: "double precision", nullable: false, comment: "Нагрузка, т. Уставка"),
                    weight_on_bit_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Нагрузка, т. Ограничение"),
                    top_drive_torque = table.Column<double>(type: "double precision", nullable: false, comment: "Момент на ВСП, кН*м. Уставка"),
                    top_drive_torque_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Момент на ВСП, кН*м. Ограничение"),
                    revolution_per_minute = table.Column<double>(type: "double precision", nullable: false, comment: "Обороты на ВСП, об/мин. Уставка"),
                    revolutions_per_minute_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Обороты на ВСП, об/мин. Ограничение"),
                    flow_rate = table.Column<double>(type: "double precision", nullable: false, comment: "Расход л/с. Уставка"),
                    flow_rate_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Расход л/с. Ограничение"),
                    note = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: false, comment: "Примечание"),
                    id_author = table.Column<int>(type: "integer", nullable: false, comment: "Автор"),
                    id_editor = table.Column<int>(type: "integer", nullable: true, comment: "Редактор"),
                    creation = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "дата создания"),
                    obsolete = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true, comment: "дата устаревания"),
                    id_state = table.Column<int>(type: "integer", nullable: false, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    id_previous = table.Column<int>(type: "integer", nullable: true, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    id_well = table.Column<int>(type: "integer", nullable: false, comment: "Id скважины"),
                    id_wellsection_type = table.Column<int>(type: "integer", nullable: false, comment: "Тип секции"),
                    depth_start = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу от, м"),
                    depth_end = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу до, м")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_process_map_plan_rotor", x => x.id);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_rotor_t_process_map_plan_rotor_id_previo~",
                        column: x => x.id_previous,
                        principalTable: "t_process_map_plan_rotor",
                        principalColumn: "id");
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_rotor_t_user_id_author",
                        column: x => x.id_author,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_rotor_t_user_id_editor",
                        column: x => x.id_editor,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_rotor_t_well_id_well",
                        column: x => x.id_well,
                        principalTable: "t_well",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_rotor_t_well_section_type_id_wellsection~",
                        column: x => x.id_wellsection_type,
                        principalTable: "t_well_section_type",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "РТК план бурение ротор");

            migrationBuilder.CreateTable(
                name: "t_process_map_plan_slide",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false, comment: "Идентификатор")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    rop_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Максимально допустимая скорость, м/ч"),
                    pressure_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Максимально допустимое давление, атм"),
                    differential_pressure = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм. Уставка"),
                    differential_pressure_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм. Ограничение"),
                    weight_on_bit = table.Column<double>(type: "double precision", nullable: false, comment: "Нагрузка, т. Уставка"),
                    weight_on_bit_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Нагрузка, т. Ограничение"),
                    flow_rate = table.Column<double>(type: "double precision", nullable: false, comment: "Расход л/с. Уставка"),
                    flow_rate_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Расход л/с. Ограничение"),
                    spring = table.Column<double>(type: "double precision", nullable: false, comment: "Расчётная пружина, градус"),
                    tool_buckling = table.Column<double>(type: "double precision", nullable: false, comment: "Складывание инструмента, м"),
                    note = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: false, comment: "Примечание"),
                    id_author = table.Column<int>(type: "integer", nullable: false, comment: "Автор"),
                    id_editor = table.Column<int>(type: "integer", nullable: true, comment: "Редактор"),
                    creation = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "дата создания"),
                    obsolete = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true, comment: "дата устаревания"),
                    id_state = table.Column<int>(type: "integer", nullable: false, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    id_previous = table.Column<int>(type: "integer", nullable: true, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    id_well = table.Column<int>(type: "integer", nullable: false, comment: "Id скважины"),
                    id_wellsection_type = table.Column<int>(type: "integer", nullable: false, comment: "Тип секции"),
                    depth_start = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу от, м"),
                    depth_end = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу до, м")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_process_map_plan_slide", x => x.id);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_slide_t_process_map_plan_slide_id_previo~",
                        column: x => x.id_previous,
                        principalTable: "t_process_map_plan_slide",
                        principalColumn: "id");
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_slide_t_user_id_author",
                        column: x => x.id_author,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_slide_t_user_id_editor",
                        column: x => x.id_editor,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_slide_t_well_id_well",
                        column: x => x.id_well,
                        principalTable: "t_well",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_slide_t_well_section_type_id_wellsection~",
                        column: x => x.id_wellsection_type,
                        principalTable: "t_well_section_type",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "РТК план бурение слайд");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_rotor_id_author",
                table: "t_process_map_plan_rotor",
                column: "id_author");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_rotor_id_editor",
                table: "t_process_map_plan_rotor",
                column: "id_editor");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_rotor_id_previous",
                table: "t_process_map_plan_rotor",
                column: "id_previous");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_rotor_id_well",
                table: "t_process_map_plan_rotor",
                column: "id_well");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_rotor_id_wellsection_type",
                table: "t_process_map_plan_rotor",
                column: "id_wellsection_type");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_slide_id_author",
                table: "t_process_map_plan_slide",
                column: "id_author");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_slide_id_editor",
                table: "t_process_map_plan_slide",
                column: "id_editor");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_slide_id_previous",
                table: "t_process_map_plan_slide",
                column: "id_previous");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_slide_id_well",
                table: "t_process_map_plan_slide",
                column: "id_well");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_slide_id_wellsection_type",
                table: "t_process_map_plan_slide",
                column: "id_wellsection_type");

            migrationBuilder.Sql(@"INSERT INTO public.t_process_map_plan_rotor 
            (id, id_wellsection_type, rop_limit_max, depth_start, depth_end, differential_pressure, 
            differential_pressure_limit_max, weight_on_bit, weight_on_bit_limit_max, 
            top_drive_torque, top_drive_torque_limit_max, revolution_per_minute, 
            revolutions_per_minute_limit_max, flow_rate, flow_rate_limit_max, note, 
            pressure_limit_max, id_author, creation, id_state, id_well, id_editor) 
            SELECT id, id_wellsection_type, rop_plan, depth_start, depth_end, delta_pressure_plan, 
            delta_pressure_limit_max, axial_load_plan, axial_load_limit_max, 
            top_drive_torque_plan, top_drive_torque_limit_max, top_drive_speed_plan, 
            top_drive_speed_limit_max, flow_plan, flow_limit_max, comment, 0, id_author, creation, id_state, id_well, 
            id_editor
            FROM public.t_process_map_plan_drilling WHERE id_mode = 1 AND obsolete is null");

            migrationBuilder.Sql(@"INSERT INTO public.t_process_map_plan_slide 
             (id, id_wellsection_type, rop_limit_max, depth_start, depth_end, differential_pressure, 
             differential_pressure_limit_max, weight_on_bit, weight_on_bit_limit_max, 
             flow_rate, flow_rate_limit_max, note, pressure_limit_max, id_author, creation, 
             id_state, id_well, spring, tool_buckling, id_editor) 
             SELECT id, id_wellsection_type, rop_plan, depth_start, depth_end, delta_pressure_plan, 
             delta_pressure_limit_max, axial_load_plan, axial_load_limit_max, 
             flow_plan, flow_limit_max, comment, 0, id_author, creation, id_state, id_well, 0, 0, id_editor
             FROM public.t_process_map_plan_drilling 
             WHERE id_mode = 2 AND obsolete is null");

            migrationBuilder.DropTable(name: "t_process_map_plan_drilling");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "t_process_map_plan_rotor");

            migrationBuilder.DropTable(
                name: "t_process_map_plan_slide");

            migrationBuilder.CreateTable(
                name: "t_process_map_plan_drilling",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false, comment: "Идентификатор")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    id_author = table.Column<int>(type: "integer", nullable: false, comment: "Автор"),
                    id_editor = table.Column<int>(type: "integer", nullable: true, comment: "Редактор"),
                    id_previous = table.Column<int>(type: "integer", nullable: true, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    id_well = table.Column<int>(type: "integer", nullable: false, comment: "Id скважины"),
                    id_wellsection_type = table.Column<int>(type: "integer", nullable: false, comment: "Тип секции"),
                    axial_load_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Осевая нагрузка, т, допустимый максимум"),
                    axial_load_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Осевая нагрузка, т, план"),
                    comment = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: false, comment: "Комментарий"),
                    creation = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "дата создания"),
                    delta_pressure_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм, допустимый максимум"),
                    delta_pressure_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Перепад давления, атм, план"),
                    depth_end = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу до, м"),
                    depth_start = table.Column<double>(type: "double precision", nullable: false, comment: "Глубина по стволу от, м"),
                    flow_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Расход, л/с, допустимый максимум"),
                    flow_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Расход, л/с, план"),
                    id_mode = table.Column<int>(type: "integer", nullable: false, comment: "Id режима (1- ротор, 2 слайд)"),
                    id_state = table.Column<int>(type: "integer", nullable: false, comment: "ИД состояния записи: \n0 - актуальная\n1 - замененная\n2 - удаленная"),
                    obsolete = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true, comment: "дата устаревания"),
                    rop_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Плановая механическая скорость, м/ч"),
                    top_drive_speed_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Обороты на ВСП, допустимый максимум"),
                    top_drive_speed_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Обороты на ВСП, план"),
                    top_drive_torque_limit_max = table.Column<double>(type: "double precision", nullable: false, comment: "Момент на ВСП, допустимый максимум"),
                    top_drive_torque_plan = table.Column<double>(type: "double precision", nullable: false, comment: "Момент на ВСП, план"),
                    usage_saub = table.Column<double>(type: "double precision", nullable: false, comment: "Плановый процент использования АКБ"),
                    usage_spin = table.Column<double>(type: "double precision", nullable: false, comment: "Плановый процент использования spin master")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_process_map_plan_drilling", x => x.id);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_drilling_t_process_map_plan_drilling_id_~",
                        column: x => x.id_previous,
                        principalTable: "t_process_map_plan_drilling",
                        principalColumn: "id");
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_drilling_t_user_id_author",
                        column: x => x.id_author,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_drilling_t_user_id_editor",
                        column: x => x.id_editor,
                        principalTable: "t_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_drilling_t_well_id_well",
                        column: x => x.id_well,
                        principalTable: "t_well",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_t_process_map_plan_drilling_t_well_section_type_id_wellsect~",
                        column: x => x.id_wellsection_type,
                        principalTable: "t_well_section_type",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "РТК план бурение");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_drilling_id_author",
                table: "t_process_map_plan_drilling",
                column: "id_author");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_drilling_id_editor",
                table: "t_process_map_plan_drilling",
                column: "id_editor");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_drilling_id_previous",
                table: "t_process_map_plan_drilling",
                column: "id_previous");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_drilling_id_well",
                table: "t_process_map_plan_drilling",
                column: "id_well");

            migrationBuilder.CreateIndex(
                name: "IX_t_process_map_plan_drilling_id_wellsection_type",
                table: "t_process_map_plan_drilling",
                column: "id_wellsection_type");
        }
    }
}