-- 2024-05-07 维修表缺少字段
ALTER TABLE maintain_info
    ADD cost numeric(2, 0) NULL;
EXEC sys.sp_addextendedproperty 'MS_Description', N'材料费用', 'schema', N'dbo', 'table', N'maintain_info', 'column', N'cost';
ALTER TABLE maintain_info
    ADD contents varchar(100) NULL;
EXEC sys.sp_addextendedproperty 'MS_Description', N'维保内容', 'schema', N'dbo', 'table', N'maintain_info', 'column', N'contents';
ALTER TABLE maintain_info
    ADD evaluate varchar(10) NULL;
EXEC sys.sp_addextendedproperty 'MS_Description', N'评价内容', 'schema', N'dbo', 'table', N'maintain_info', 'column', N'evaluate';

-- 训练集合：
begin tran
    insert into history_data_pre(cur_date,building_id,water_value,elect_value,water_level,env_min_temp,env_max_temp)
    select eds.cur_date,
           eds.building_id,
           isnull(eds.water_value,
                  0) as water_value,
           isnull(eds.elect_value,
                  0) as elect_value,
           isnull(convert(numeric (24, 2), t1.water_level),
                  0) as water_level,
           th.tempmin,
           th.tempmax
    from energy_day_sum eds
             left join (select convert(date,
                                       cur_date)                                     as cur_date,
                               building_id,
                               avg(isnull(convert(numeric (24, 2), water_level), 0)) as water_level
                        from history_data
                        group by convert(date,
                                         cur_date),
                                 building_id) t1 on
        eds.cur_date = t1.cur_date and eds.building_id = t1.building_id
             left join temp_history th
                       on eds.cur_date = th.cur_date
    where eds.building_id != '所有'
    order by
        eds.building_id,
        eds.cur_date
rollback

-- 2024-05-09 创建历史预测表
-- 历史水电用量以及预测值
CREATE TABLE history_data_pre
(
    cur_date        date NULL,
    building_id     varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
    env_min_temp    numeric(24, 2) NULL,
    env_max_temp    numeric(24, 2) NULL,
    water_value     numeric(24, 2) NULL,
    elect_value     numeric(24, 2) NULL,
    water_level     numeric(24, 2) NULL,
    id              bigint IDENTITY(1,1) NOT NULL,
    water_value_pre numeric(24, 2) NULL,
    elect_value_pre numeric(24, 2) NULL,
    water_level_pre numeric(24, 2) NULL,
    remark          varchar(200) COLLATE Chinese_PRC_CI_AS NULL,
    CONSTRAINT PK_history_data_pre PRIMARY KEY (id)
);
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'历史水电用量以及预测值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'日期', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'cur_date';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'楼栋编号', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'building_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'环境最低温度', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'env_min_temp';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'环境最高温度', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'env_max_temp';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'实际用水量', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'water_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'实际用电量', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'elect_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'平均水位', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'water_level';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'id', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'用水量预测值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'water_value_pre';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'用电量预测值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'elect_value_pre';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'平均水位预测值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'water_level_pre';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'备注', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'remark';

create index history_data_pre_building_id on history_data_pre (building_id);
create index history_data_pre_cur_date on history_data_pre (cur_date);

-- 2024-05-09 系统参数表增加天气区域
ALTER TABLE SysParam
    ADD proArea varchar(100) NULL;
EXEC sp_addextendedproperty 'MS_Description', N'天气区域', 'schema', N'dbo', 'table', N'SysParam', 'column', N'proArea';

-- 2024-05-15 热泵使用时间表(月表)
CREATE TABLE analysis_runtime_month (
                                                   id bigint IDENTITY(1,1) NOT NULL,
                                                   cur_date varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   item_type varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day01 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day02 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day03 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day04 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day05 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day06 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day07 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day08 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day09 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day10 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day11 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day12 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day13 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day14 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day15 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day16 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day17 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day18 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day19 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day20 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day21 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day22 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day23 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day24 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day25 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day26 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day27 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day28 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day29 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day30 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   day31 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   total_value varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                   building_id varchar(50) COLLATE Chinese_PRC_CI_AS NULL
);
-- 使用时间年表
CREATE TABLE analysis_runtime_year (
                                                  id bigint IDENTITY(1,1) NOT NULL,
                                                  cur_date varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  item_type varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month01 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month02 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month03 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month04 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month05 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month06 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month07 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month08 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month09 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month10 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month11 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  month12 varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  total_value varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  building_id varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  building_name varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
                                                  CONSTRAINT analysis_runtime_year_id PRIMARY KEY (id)
);

-- Extended properties
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'序号', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'日期', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'cur_date';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'类型', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'item_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'1月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month01';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'2月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month02';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'3月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month03';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'4月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month04';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'5月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month05';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'6月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month06';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'7月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month07';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'8月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month08';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'9月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month09';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'10月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month10';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'11月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month11';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'12月用量或比值', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'month12';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'合计用量', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'total_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'楼栋编号', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'building_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'楼栋名称', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'analysis_runtime_year', @level2type=N'Column', @level2name=N'building_name';


-- 2024-06-24 添加楼栋人数
alter table history_data_pre add people_num numeric(24,2) not null default 0;
exec sp_addextendedproperty N'MS_Description', N'每栋楼人数', N'schema', N'dbo',N'table', N'history_data_pre', N'column', N'people_num';

-- 2024-06-26 添加知识库
CREATE TABLE knowledge_data
(
    id              bigint IDENTITY(1,1) NOT NULL,
    title           varchar(100) COLLATE Chinese_PRC_CI_AS NULL,
    description     varchar(200) COLLATE Chinese_PRC_CI_AS NULL,
    content         varchar(2000) COLLATE Chinese_PRC_CI_AS NULL,
    create_time     datetime NULL,
    status          int NULL,
    remark          varchar(200) COLLATE Chinese_PRC_CI_AS NULL,
    CONSTRAINT pk_knowledge_data PRIMARY KEY (id)
);
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'知识库数据', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'id', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'标题', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'title';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'描述', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'description';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'内容', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'content';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'状态', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'knowledge_data', @level2type=N'Column', @level2name=N'status';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'备注', @level0type=N'Schema', @level0name=N'dbo', @level1type=N'Table', @level1name=N'history_data_pre', @level2type=N'Column', @level2name=N'remark';

create index knowledge_data_create_time on history_data_pre (create_time);