-- RT_DAILY_PROD_STATE：设备当日实时产量状态表
-- 兼容：Oracle 11 / Oracle 19

CREATE TABLE RT_DAILY_PROD_STATE (
    PROD_DATE         DATE            NOT NULL,
    DEVICE_CODE       VARCHAR2(64)    NOT NULL,
    PARAM_NAME        VARCHAR2(128)   NOT NULL,
    LAST_PARAM_VAL    NUMBER(18,4)    DEFAULT 0 NOT NULL,
    CURRENT_TOTAL     NUMBER(18,4)    DEFAULT 0 NOT NULL,
    RESET_COUNT       NUMBER(10)      DEFAULT 0 NOT NULL,
    DIRTY_FLAG        NUMBER(1)       DEFAULT 0 NOT NULL,
    LAST_COLLECT_TIME DATE,
    UPDATE_TIME       DATE            DEFAULT SYSDATE NOT NULL
);

COMMENT ON TABLE RT_DAILY_PROD_STATE IS '设备当日实时产量状态表';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.PROD_DATE IS '生产日期，按自然日截断';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.DEVICE_CODE IS '设备编码';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.PARAM_NAME IS '产量参数名称，当前固定为机台状态-实际产出数量';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.LAST_PARAM_VAL IS '最近一次采集到的参数值';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.CURRENT_TOTAL IS '当日累计产量';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.RESET_COUNT IS '当天计数器回退/清零次数';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.DIRTY_FLAG IS '脏标记，预留给人工修正/对账';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.LAST_COLLECT_TIME IS '最近一次参与累计的采集时间';
COMMENT ON COLUMN RT_DAILY_PROD_STATE.UPDATE_TIME IS '最近更新时间';

ALTER TABLE RT_DAILY_PROD_STATE
    ADD CONSTRAINT PK_RT_DAILY_PROD_STATE
    PRIMARY KEY (PROD_DATE, DEVICE_CODE, PARAM_NAME);

CREATE INDEX IDX_RT_DAILY_PROD_STATE_COLLECT
    ON RT_DAILY_PROD_STATE (LAST_COLLECT_TIME);

CREATE INDEX IDX_RT_DAILY_PROD_STATE_DEVICE
    ON RT_DAILY_PROD_STATE (DEVICE_CODE, PROD_DATE);
