20261015_dml.sql 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. -- -----------------------------------------------------
  2. -- 1. 上报批次表 data_power_forecast_report_batch
  3. -- -----------------------------------------------------
  4. CREATE TABLE IF NOT EXISTS data_power_forecast_report_batch (
  5. id SERIAL NOT NULL,
  6. station_code VARCHAR(64) NOT NULL,
  7. station_name VARCHAR(255),
  8. batch_no VARCHAR(128) NOT NULL,
  9. type VARCHAR(16) NOT NULL,
  10. report_day INTEGER NOT NULL,
  11. est_report_time VARCHAR(16) NOT NULL,
  12. status INTEGER NOT NULL DEFAULT 1,
  13. generate_source VARCHAR(16) NOT NULL DEFAULT 'auto',
  14. create_time TIMESTAMP NOT NULL DEFAULT now(),
  15. update_time TIMESTAMP NOT NULL DEFAULT now(),
  16. remark VARCHAR(512),
  17. CONSTRAINT pk_data_power_forecast_report_batch PRIMARY KEY (id)
  18. );
  19. -- 单场站幂等保险(任务按 station_code + report_day + est_report_time + type 查重)
  20. CREATE INDEX IF NOT EXISTS uk_data_power_forecast_report_batch
  21. ON data_power_forecast_report_batch (station_code, report_day, est_report_time, type);
  22. COMMENT ON TABLE data_power_forecast_report_batch IS '功率预测上报批次';
  23. COMMENT ON COLUMN data_power_forecast_report_batch.id IS '唯一标识';
  24. COMMENT ON COLUMN data_power_forecast_report_batch.station_code IS '场站编码';
  25. COMMENT ON COLUMN data_power_forecast_report_batch.station_name IS '场站名称';
  26. COMMENT ON COLUMN data_power_forecast_report_batch.batch_no IS '上报批次号';
  27. COMMENT ON COLUMN data_power_forecast_report_batch.type IS '类型(short短期,ultra超短期)';
  28. COMMENT ON COLUMN data_power_forecast_report_batch.report_day IS '上报日(yyyyMMdd,北京时间)';
  29. COMMENT ON COLUMN data_power_forecast_report_batch.est_report_time IS '预计上报时间(HH:mm:ss,北京时间)';
  30. COMMENT ON COLUMN data_power_forecast_report_batch.status IS '状态(1待确认,2已确认)';
  31. COMMENT ON COLUMN data_power_forecast_report_batch.generate_source IS '生成来源(auto自动,manually手动)';
  32. COMMENT ON COLUMN data_power_forecast_report_batch.create_time IS '创建时间';
  33. COMMENT ON COLUMN data_power_forecast_report_batch.update_time IS '更新时间';
  34. COMMENT ON COLUMN data_power_forecast_report_batch.remark IS '备注';
  35. -- -----------------------------------------------------
  36. -- 2. report 表加 batch_no 列(101901 统计查询按 batch_no is null 隔离)
  37. -- -----------------------------------------------------
  38. -- 字段说明:batch_no VARCHAR(128) 上报批次号
  39. DO $$
  40. BEGIN
  41. -- 1. 添加字段(若不存在)
  42. IF NOT EXISTS (
  43. SELECT 1
  44. FROM information_schema.columns
  45. WHERE table_schema = current_schema()
  46. AND table_name = 'data_short_term_power_forecast_report'
  47. AND column_name = 'batch_no'
  48. ) THEN
  49. ALTER TABLE data_short_term_power_forecast_report
  50. ADD COLUMN batch_no VARCHAR(128);
  51. COMMENT ON COLUMN data_short_term_power_forecast_report.batch_no IS '上报批次号';
  52. END IF;
  53. -- 2. 创建索引(若不存在)
  54. IF NOT EXISTS (
  55. SELECT 1
  56. FROM pg_indexes
  57. WHERE schemaname = current_schema()
  58. AND tablename = 'data_short_term_power_forecast_report'
  59. AND indexname = 'idx_dstpfr_batch_no'
  60. ) THEN
  61. CREATE INDEX idx_dstpfr_batch_no
  62. ON data_short_term_power_forecast_report (batch_no);
  63. END IF;
  64. END
  65. $$;
  66. DO $$
  67. BEGIN
  68. -- 1. 添加字段(若不存在)
  69. IF NOT EXISTS (
  70. SELECT 1
  71. FROM information_schema.columns
  72. WHERE table_schema = current_schema()
  73. AND table_name = 'data_super_short_term_forecast_report'
  74. AND column_name = 'batch_no'
  75. ) THEN
  76. ALTER TABLE data_super_short_term_forecast_report
  77. ADD COLUMN batch_no VARCHAR(128);
  78. COMMENT ON COLUMN data_super_short_term_forecast_report.batch_no IS '上报批次号';
  79. END IF;
  80. -- 2. 创建索引(若不存在)
  81. IF NOT EXISTS (
  82. SELECT 1
  83. FROM pg_indexes
  84. WHERE schemaname = current_schema()
  85. AND tablename = 'data_super_short_term_forecast_report'
  86. AND indexname = 'idx_dssstfr_batch_no'
  87. ) THEN
  88. CREATE INDEX idx_dssstfr_batch_no
  89. ON data_super_short_term_forecast_report (batch_no);
  90. END IF;
  91. END
  92. $$;