using Dapper;
using SWRIS.Core;
using System;

namespace SWRIS.Migrations
{
    public class Add_Table_Alarms : SqLiteBaseRepository
    {
        public static DateTime Time { get; } = DateTime.Parse("2025-11-24 15:06:00");
        public static void Excute()
        {
            using (var conn = DbConnection())
            {
                conn.Open();

                var isExist = conn.QueryFirstOrDefault<int>(
                    "SELECT COUNT(1) FROM Migrations WHERE MigrationId = @MigrationId;",
                    new { MigrationId = "Add_Table_Alarms" });
                if (isExist > 0)
                {
                    return;
                }
                var isDelay = conn.QueryFirstOrDefault<bool>(
                    "SELECT MAX(Time) > @Time AS IsGreater FROM Migrations;", new { Time });
                if (isDelay)
                {
                    return;
                }
                string @string = @"CREATE TABLE IF NOT EXISTS Alarms (
                                    Id              INTEGER    PRIMARY KEY AUTOINCREMENT,
                                    RopeNumber      INTEGER,
                                    DamagePosition  REAL,
                                    DamageValue     REAL,
                                    DamageLevel     INTEGER,
                                    CreateTime      DATETIME   NOT NULL,
                                    SourceType      INTEGER
                                  );";
                conn.Execute(@string.ToString());

                #region Migrations
                @string = (@"INSERT INTO Migrations 
                                ( MigrationId, Time, ExcuteTime ) VALUES 
                                ( @MigrationId, @Time, @ExcuteTime );");
                conn.Execute(@string.ToString(), new { MigrationId = "Add_Table_Alarms", Time, ExcuteTime = DateTime.Now });
                #endregion
            }
        }
    }
}
