#include <stdint.h>
#include <time.h>

// RO
#define G100_RESET_COUNT    "G100ResetCount"  // 30天内复位次数
#define G100_LOCK_STATE     "G100LockState"  // 锁定状态     0:正常 1:锁定; 2:管理员锁定
#define G100_ABNORMAL_STATE "G100AbnormalState" // 异常状态     0:正常 1:逆流; 2:过载
#define G100_COUNT_10MIN    "G100AbnornalCount10Min" // 10分钟内逆流次数 10分钟内逆流次数超过后锁定
#define G100_COUNT_DAY      "G100AbnornalCountDay"   // 一天内内逆流次数 一天内逆流次数超过后锁定

// RW
#define G100_ENABLE         "G100Enable"             // 使能G100检测
#define G100_UNLOCK_TIME    "G100UnlockTime"         // 解锁时间 锁定后允许解锁时间
#define G100_MAXCOUNT_MON   "G100ResetMaxCount"       // 30天内最大次数
#define G100_MAXCOUNT_10MIN "G100BabnormalCount10Min" // 10分钟内逆流次数 10分钟内逆流次数超过后锁定
#define G100_MAXCOUNT_DAY   "G100BabnormalCountDay"   // 一天内内逆流次数 一天内逆流次数超过后锁定

#define G100_BACKFLOW_LIMIT     "G100BackflowLimit"    // 逆流限额  -grid 超过该值后认为为逆流
#define G100_BACKFLOW_SEC       "G100BackflowSec"      // 逆流时间超时 持续逆流超过时间后置逆流状态
#define G100_BACKFLOW_LOCK_TIME "G100BackflowLockTime" // 逆流锁定时间 持续逆流超过时间锁定

#define G100_OVERLOAD_LIMIT     "G100OverloadLimit"    // 过载限额 grid + 该值 超过 (低压)变压器容量后认为过载
#define G100_OVERLOAD_SEC       "G100OverloadSec"      // 过载时间超时 持续过载超过时间后置过载状态
#define G100_OVERLOAD_LOCK_TIME "G100OverloadLockTime" // 过载锁定时间 持续过载超过时间锁定

typedef enum g100_protect_abnormal_state
{
    G100_ABNORMAL_STATE_NONE = 0,
    G100_ABNORMAL_STATE_BACKFLOW,
    G100_ABNORMAL_STATE_OVERLOAD,
} g100_abnormal_state;

typedef enum g100_protect_state
{
    G100_STATE_INIT = 0,
    G100_STATE_NORMAL,
    G100_STATE_TO_ABNORMAL,
    G100_STATE_ABNORMAL,
    G100_STATE_TO_LOCK,
    G100_STATE_LOCK,
    G100_STATE_MAX,
} g100_protect_state;

typedef enum g100_protect_result
{
    G100_RESULT_NONE = -1,
    G100_RESULT_RECOVER,
    G100_RESULT_UNLOCK,
    G100_RESULT_ADUNLOCK,
    G100_RESULT_LOCK,
    G100_RESULT_ADMIN_LOCK,
    G100_RESULT_MAX,
} g100_protect_result;

typedef struct g100_protect_t
{
    void*    db;
    int      abnormal;
    int      state;
    int      result;
    uint64_t start_time;

} g100_protect_t;

int g100_protect_init(g100_protect_t* g100_protect);
int g100_protect_task(g100_protect_t* g100_protect, time_t n);
int g100_protoec_unlock(g100_protect_t* g100_protect, char* result);
int g100_protoec_admin_unlock(g100_protect_t* g100_protect, char* result);

// sql
#define TAOS_CREATE_G100_HISTORY_DB "CREATE DATABASE IF NOT EXISTS G100History;"
#define TAOS_USE_G100_HISTORY_DB    "USE G100History;"
#define CREATE_G100_HISTORY_STB     "CREATE TABLE IF NOT EXISTS g100History (start_time TIMESTAMP, abnormal INT, result INT);"

#define TAOS_INSERT_G100_HISTORY_STB "INSERT INTO g100History VALUES (%" PRIu64 ", %d, %d);"
// 获取10分钟内的异常次数
#define TAOS_SELECT_G100_COUNT_10MIN "SELECT * FROM g100History WHERE start_time >= %" PRIu64 ";"
// 获取一天内的异常次数
#define TAOS_SELECT_G100_COUNT_DAY "SELECT * FROM g100History WHERE start_time >= %" PRIu64 ";"
// 获取最近一次的XX状态
#define TAOS_SELECT_G100_LAST_RECORD "SELECT * FROM g100History ORDER BY start_time DESC LIMIT 1;"
//获取30天内记录
#define TAOS_SELECT_G100_RECORD_30DAY "SELECT * FROM g100History WHERE start_time >= %" PRIu64 ";"

#define SQL_UPDATA_G100_HISTORY_STB "UPDATE g100History SET result = %d WHERE start_time = %" PRIu64 ";"

// 保留最近30天内 最新的N条数据
#define TAOS_GET_DEL_G100_HISTORY_TIME "SELECT start_time FROM g100History ORDER BY start_time DESC LIMIT 1 OFFSET %d;"
#define TAOS_DE_G100L_HISTORY_SQL      "DELETE FROM g100History WHERE start_time < %" PRIu64 ";"

// sqlite3保留最近30天内 最新的N条数据
#define SQL_DEL_G100_HISTORY "DELETE FROM g100History WHERE start_time < (SELECT start_time FROM g100History ORDER BY start_time DESC LIMIT 1 OFFSET %d);"

#define G100_DATABASE_COUNT       1000
#define G100_SQLITE_DATABASE_PATH "/app/database/g100_history.db"

extern g100_protect_t g100_protect;
