-- 审计记录（使用记录）
CREATE TABLE IF NOT EXISTS audit_record(
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id                 INTEGER,--用户ID
    user_nickname           TEXT,--用户昵称
    solution_id             INTEGER,--溶液id
    solution_name           TEXT,--溶液名称
    concentration           REAL,--溶液浓度
    channel_code            TEXT,--通道code
    used_volume             REAL,--使用溶液量
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

-- 领取记录（加液记录）
CREATE TABLE IF NOT EXISTS receive_record (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    issuer_id               INTEGER,--发放人id
    issuer_nickname         INTEGER,--发放人昵称
    receiver_id             INTEGER,--领取人id
    receiver_nickname       TEXT,--领取人昵称
    solution_id             INTEGER,--溶液id
    solution_name           TEXT,--溶液名称
    concentration           REAL,--溶液浓度
    received_volume         REAL,--领取溶液量
    channel_code            TEXT,--通道code
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

-- 通道
CREATE TABLE IF NOT EXISTS channel (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    name                    TEXT,--通道名称
    code                    TEXT,--通道code
    solution_id             INTEGER,--绑定的溶液id
    concentration           REAL,--溶液浓度
    target_volume           REAL,--添加溶液量
    current_volume          REAL,--当前溶液量
    received_volume         REAL,--领取溶液量
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

INSERT OR IGNORE INTO channel (
    id, name, code
) VALUES
      (1, '通道一', 'CHANNEL_1'),
      (2, '通道二', 'CHANNEL_2'),
      (3, '通道三', 'CHANNEL_3'),
      (4, '通道四', 'CHANNEL_4');

-- 配方
CREATE TABLE IF NOT EXISTS formulation (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    volume                  REAL,--加液量
    solution_id             INTEGER,--溶液id
    concentration           REAL,--溶液浓度
    revolutions             REAL,--对应转数
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

-- 溶液
CREATE TABLE IF NOT EXISTS solution (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    name                    TEXT,--溶液名称
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

INSERT OR IGNORE INTO solution (id, name) VALUES
    (1, '盐酸'),
    (2, '硫酸'),
    (3, '硝酸'),
    (4, '氢氟酸');

-- 系统配置
CREATE TABLE IF NOT EXISTS system_config (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    key                     TEXT,
    value                   TEXT,
    description             TEXT,
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

INSERT OR IGNORE INTO system_config (id, key, value) VALUES
    (1, 'SOLUTION_ADD_MODE','AUTO');

-- 系统日志
CREATE TABLE IF NOT EXISTS system_log (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    title                   TEXT,
    content                 TEXT,
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

-- 用户
CREATE TABLE IF NOT EXISTS user (
    id                      INTEGER PRIMARY KEY AUTOINCREMENT,
    username                TEXT    UNIQUE, --用户账号
    nickname                TEXT,--用户昵称
    password                TEXT,--密码
    role                    TEXT,--角色
    fixed_user              TEXT    DEFAULT 'DISABLE',--是否内置用户
    deleted                 TEXT    DEFAULT 'DISABLE',--逻辑删除
    create_time             TEXT DEFAULT CURRENT_TIMESTAMP,
    update_time             TEXT DEFAULT CURRENT_TIMESTAMP
);

INSERT OR IGNORE INTO user ( id, username, nickname, password, role, fixed_user, deleted) VALUES
    (1, 'admin', 'Admin', '9973', 'ADMIN', 'ENABLE', 'DISABLE'),
    (2, 'test',  'test',  '9973', 'ADMIN', 'ENABLE', 'DISABLE');

-- drop table IF EXISTS zapp_sub_module_reg_initial_value;

-- 设备参数 表
CREATE TABLE IF NOT EXISTS device_param_config
(
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    mid         text,
    reg_index   text,
    reg_val     INTEGER,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);
