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

    -- 判断是否存在表 chillers_data_min + year
    table_name := 'chillers_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_code VARCHAR(20),
            device_num VARCHAR(20),
            collection_num VARCHAR(20),
            register_address VARCHAR(50),
            register_name VARCHAR(50),
            fun_code VARCHAR(10),
            last_value NUMERIC(24,2),
            last_time TIMESTAMP,
            cur_value NUMERIC(24,2),
            cur_time TIMESTAMP,
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            local_time TIMESTAMP,
            grade INTEGER,
            ddc_addr VARCHAR(50),
            register_id INTEGER
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在表 chillers_data_hour + year
    table_name := 'chillers_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_code VARCHAR(20),
            device_num VARCHAR(20),
            collection_num VARCHAR(20),
            register_address VARCHAR(50),
            register_name VARCHAR(50),
            fun_code VARCHAR(10),
            last_value NUMERIC(24,2),
            last_time VARCHAR(13),
            cur_value NUMERIC(24,2),
            cur_time VARCHAR(13),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            local_time TIMESTAMP,
            grade INTEGER,
            ddc_addr VARCHAR(50),
            register_id INTEGER
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

    -- 判断是否存在表 chillers_data_day + year
    table_name := 'chillers_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_code VARCHAR(20),
            device_num VARCHAR(20),
            collection_num VARCHAR(20),
            register_address VARCHAR(50),
            register_name VARCHAR(50),
            fun_code VARCHAR(10),
            last_value NUMERIC(24,2),
            last_time VARCHAR(10),
            cur_value NUMERIC(24,2),
            cur_time VARCHAR(10),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            local_time TIMESTAMP,
            grade INTEGER,
            ddc_addr VARCHAR(50),
            register_id INTEGER
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

-- 判断是否存在表 chillers_data_month
    table_name := 'chillers_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_code VARCHAR(20),
            device_num VARCHAR(20),
            collection_num VARCHAR(20),
            register_address VARCHAR(50),
            register_name VARCHAR(50),
            fun_code VARCHAR(10),
            last_value NUMERIC(24,2),
            last_time VARCHAR(10),
            cur_value NUMERIC(24,2),
            cur_time VARCHAR(10),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            local_time TIMESTAMP,
            grade INTEGER,
            ddc_addr VARCHAR(50),
            register_id INTEGER
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

-- 判断是否存在表 chillers_data_year
    table_name := 'chillers_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_code VARCHAR(20),
            device_num VARCHAR(20),
            collection_num VARCHAR(20),
            register_address VARCHAR(50),
            register_name VARCHAR(50),
            fun_code VARCHAR(10),
            last_value NUMERIC(24,2),
            last_time VARCHAR(10),
            cur_value NUMERIC(24,2),
            cur_time VARCHAR(10),
            ratio INTEGER,
            calc_value NUMERIC(24,2),
            local_time TIMESTAMP,
            grade INTEGER,
            ddc_addr VARCHAR(50),
            register_id INTEGER
        );
        CREATE INDEX cls_' || table_name || ' ON ' || table_name || ' (cur_time DESC)';
        RAISE NOTICE '%', sqlStr;
EXECUTE sqlStr;
END IF;

END;
$$ LANGUAGE plpgsql;
