-- Version:		2025.7.31  14:52:27
Drop Table T_EQP_HEALTH@

Create Table T_EQP_HEALTH(
EqpId		VARCHAR(20)		NOT NULL,
RuleId		VARCHAR(50)		NOT NULL,
DealTime		TIMESTAMP		NOT NULL,
FaultDay		DECIMAL(5),
HealthLevel		DECIMAL(5,2),
STime		TIMESTAMP,
ETime		TIMESTAMP,
toc		TIMESTAMP,
tom		TIMESTAMP,
mop		VARCHAR(120),
mou		VARCHAR(30),
CONSTRAINT pk_T_EQP_HEALTH PRIMARY KEY (EqpId, RuleId, DealTime)
)
IN TB8KUSREMS INDEX IN TB8KIDXEMS@
Alter Table T_EQP_HEALTH ALTER COLUMN RuleId SET WITH Default '0'@

COMMENT ON Table T_EQP_HEALTH IS '设备健康档案'@

COMMENT ON T_EQP_HEALTH (EqpId IS '设备编号')@
COMMENT ON T_EQP_HEALTH (RuleId IS '规则ID')@
COMMENT ON T_EQP_HEALTH (DealTime IS '处理时间')@
COMMENT ON T_EQP_HEALTH (FaultDay IS '报警累计天数')@
COMMENT ON T_EQP_HEALTH (HealthLevel IS '健康状况（0~100）[严重报警，经用户确认健康度减1]')@
COMMENT ON T_EQP_HEALTH (STime IS '开始时间')@
COMMENT ON T_EQP_HEALTH (ETime IS '结束时间(统计结束日期）')@

/****************************************************************************
						INSERT TRIGGER FOR TABLE
							  T_EQP_HEALTH
*****************************************************************************/
DROP TRIGGER ins_b_T_EQP_HEALTH@

CREATE OR REPLACE TRIGGER ins_b_T_EQP_HEALTH
BEFORE INSERT ON T_EQP_HEALTH REFERENCING NEW AS NEWROW FOR EACH ROW
MODE DB2SQL
BEGIN ATOMIC
	DECLARE	programm	VARCHAR(120);
	DECLARE	auth	VARCHAR(30);

	set (programm,auth) = (SELECT APPL_NAME,AUTHID FROM SYSIBMADM.APPLICATIONS WHERE APPL_ID = (SELECT APPLICATION_ID() FROM dual));

	set NEWROW.toc = (values sysdate);
	set NEWROW.tom = (values sysdate);
	set NEWROW.mop = programm;
	set NEWROW.mou = auth;
END
@
/****************************************************************************
						UPDATE TRIGGER FOR TABLE
							  T_EQP_HEALTH
*****************************************************************************/
DROP TRIGGER upd_b_T_EQP_HEALTH@

CREATE OR REPLACE TRIGGER upd_b_T_EQP_HEALTH
BEFORE UPDATE ON T_EQP_HEALTH REFERENCING NEW AS NEWROW FOR EACH ROW
MODE DB2SQL
BEGIN ATOMIC
	DECLARE	programm	VARCHAR(120);
	DECLARE	auth	VARCHAR(30);

	set (programm,auth) = (SELECT APPL_NAME,AUTHID FROM SYSIBMADM.APPLICATIONS WHERE APPL_ID = (SELECT APPLICATION_ID() FROM dual));

	set NEWROW.tom = (values sysdate);
	set NEWROW.mop = programm;
	set NEWROW.mou = auth;
END
@
