CREATE OR REPLACE FUNCTION pro_create_table()
RETURNS VOID AS $$
DECLARE
year VARCHAR(4);
    sqlStr TEXT;
    table_name VARCHAR(100);
    is_exists INTEGER;
BEGIN
    -- 获取当前年份
year := TO_CHAR(CURRENT_DATE, 'YYYY');

    -- 判断是否存在 data_min + 当前年份 的表
    table_name := 'data_min' || year;
SELECT COUNT(*) INTO is_exists FROM pg_tables WHERE tablename = table_name;
IF is_exists = 0 THEN
        sqlStr := 'CREATE TABLE ' || table_name || ' (
            id BIGSERIAL PRIMARY KEY,
            device_num VARCHAR(20),
            device_code VARCHAR(20),
            device_type VARCHAR(100),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            used_value NUMERIC(24,2),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            grade INTEGER,
            UNIQUE (device_num, cur_time)
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在 data_hour + 当前年份 的表
    table_name := 'data_hour' || year;
SELECT COUNT(*) INTO is_exists FROM pg_tables WHERE tablename = table_name;
IF is_exists = 0 THEN
        sqlStr := 'CREATE TABLE ' || table_name || ' (
            id BIGSERIAL PRIMARY KEY,
            device_num VARCHAR(20),
            device_code VARCHAR(20),
            device_type VARCHAR(100),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            used_value NUMERIC(24,2),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            grade INTEGER,
            UNIQUE (device_num, cur_time)
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在 data_day + 当前年份 的表
    table_name := 'data_day' || year;
SELECT COUNT(*) INTO is_exists FROM pg_tables WHERE tablename = table_name;
IF is_exists = 0 THEN
        sqlStr := 'CREATE TABLE ' || table_name || ' (
            id BIGSERIAL PRIMARY KEY,
            device_num VARCHAR(20),
            device_code VARCHAR(20),
            device_type VARCHAR(100),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            used_value NUMERIC(24,2),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            grade INTEGER,
            UNIQUE (device_num, cur_time)
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在 data_month 的表
    table_name := 'data_month';
SELECT COUNT(*) INTO is_exists FROM pg_tables WHERE tablename = table_name;
IF is_exists = 0 THEN
        sqlStr := 'CREATE TABLE ' || table_name || ' (
            id BIGSERIAL PRIMARY KEY,
            device_num VARCHAR(20),
            device_code VARCHAR(20),
            device_type VARCHAR(100),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            used_value NUMERIC(24,2),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            grade INTEGER,
            UNIQUE (device_num, cur_time)
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在 data_year 的表
    table_name := 'data_year';
SELECT COUNT(*) INTO is_exists FROM pg_tables WHERE tablename = table_name;
IF is_exists = 0 THEN
        sqlStr := 'CREATE TABLE ' || table_name || ' (
            id BIGSERIAL PRIMARY KEY,
            device_num VARCHAR(20),
            device_code VARCHAR(20),
            device_type VARCHAR(100),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            used_value NUMERIC(24,2),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            grade INTEGER,
            UNIQUE (device_num, cur_time)
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

END;
$$ LANGUAGE plpgsql;
