CREATE TABLE PDI_PLTM
(
  SID                           NUMBER(10),
  ROLLPROGRAMNB                 NUMBER(10),
  SEQUENCENB                    NUMBER(3),
  STATUS                        NUMBER(3)       DEFAULT 0                     NOT NULL,
  SCHEDULE_CODE                 VARCHAR2(32 BYTE),
  COILID                        VARCHAR2(32 BYTE) NOT NULL,
  ENTRY_COIL_THICKNESS          NUMBER(10,3),
  ENTRY_COIL_THICKNESS_MAX      NUMBER(10,3),
  ENTRY_COIL_THICKNESS_MIN      NUMBER(10,3),
  ENTRY_COIL_WIDTH              NUMBER(10,3),
  ENTRY_COIL_WIDTH_MAX          NUMBER(10,3),
  ENTRY_COIL_WIDTH_MIN          NUMBER(10,3),
  ENTRY_COIL_WEIGHT             NUMBER(10,3),
  ENTRY_OF_COIL_LENGTH          NUMBER(10,3),
  ENTRY_OF_COIL_INNER_DIAMETER  NUMBER(10,3),
  ENTRY_OF_COIL_OUTER_DIAMETER  NUMBER(10,3),
  TRIMMING                      NUMBER(1),
  TRIMMING_WIDTH                NUMBER(10,3),
  SMP_LENGTH                    NUMBER(10,3),
  SMP_NUM                       NUMBER(10,3),
  SMP_FRQ                       VARCHAR2(32 BYTE),
  SMP_NUM_HEAD                  NUMBER(10,3),
  SMP_NUM_MID                   NUMBER(10,3),
  SMP_NUM_TAIL                  NUMBER(10,3),
  PRECEDING_PROCESS_CODE        VARCHAR2(32 BYTE),
  NEXT_PROCESS_CODE             VARCHAR2(32 BYTE),
  HOT_MILL_DELIVERY_TEMP        NUMBER(10,3),
  FINISHED_COIL_TEMP            NUMBER(10,3),
  CROWN_AVERAGE                 NUMBER(10,3),
  COIL_FLATNESS_AVERAGE         NUMBER(10,3),
  COIL_FLATNESS_MAX_VALUE       NUMBER(10,3),
  COIL_FLATNESS_MIN_VALUE       NUMBER(10,3),
  MATERIAL_YIELD_POINT          NUMBER(10,3),
  MATERIAL_TENSILE              NUMBER(10,3),
  HOTACTFMWEDGEAVG              NUMBER(10,3),
  WEIGHT_MODE                   VARCHAR2(32 BYTE),
  DUMMY_COIL_MRK                VARCHAR2(32 BYTE),
  CUT_MODE                      VARCHAR2(32 BYTE),
  OFF_GAUGE_HEAD_LENGTH         NUMBER(10,3),
  OFF_GAUGE_TAIL_LENGTH         NUMBER(10,3),
  EXIT_COIL_NO                  VARCHAR2(32 BYTE),
  EXIT_COIL_WEIGHT              NUMBER(10,3),
  EXIT_COIL_WEIGHT_MAX          NUMBER(10,3),
  EXIT_COIL_WEIGHT_MIN          NUMBER(10,3),
  EXIT_COIL_THICKNESS           NUMBER(10,3),
  EXIT_COIL_THICKNESS_MAX       NUMBER(10,3),
  EXIT_COIL_THICKNESS_MIN       NUMBER(10,3),
  EXIT_COIL_WIDTH               NUMBER(10,3),
  EXIT_COIL_WIDTH_MAX           NUMBER(10,3),
  EXIT_COIL_WIDTH_MIN           NUMBER(10,3),
  WORK_ORDER_NO                 VARCHAR2(32 BYTE),
  ORDER_QUALITY                 VARCHAR2(32 BYTE),
  STEEL_GRADE                   VARCHAR2(32 BYTE),
  SG_SIGN                       VARCHAR2(32 BYTE),
  ORDER_THICKNESS               NUMBER(10,3),
  ORDER_THICKNESS_MAX           NUMBER(10,3),
  ORDER_THICKNESS_MIN           NUMBER(10,3),
  ORDER_WIDTH                   NUMBER(10,3),
  ORDER_WIDTH_MAX               NUMBER(10,3),
  ORDER_WIDTH_MIN               NUMBER(10,3),
  SLEEVE_CODE_OF_COLD_COIL      VARCHAR2(32 BYTE),
  PACKING_TYPE_CODE             VARCHAR2(32 BYTE),
  THK_DS                        VARCHAR2(32 BYTE),
  EXT_NUM_01                    VARCHAR2(32 BYTE),
  C                             NUMBER(10,3),
  SI                            NUMBER(10,3),
  MN                            NUMBER(10,3),
  P                             NUMBER(10,3),
  S                             NUMBER(10,3),
  CU                            NUMBER(10,3),
  NI                            NUMBER(10,3),
  CR                            NUMBER(10,3),
  MO                            NUMBER(10,3),
  V                             NUMBER(10,3),
  TI                            NUMBER(10,3),
  SOL_AL                        NUMBER(10,3),
  FE                            NUMBER(10,3),
  MG                            NUMBER(10,3),
  PB                            NUMBER(10,3),
  SN                            NUMBER(10,3),
  ZN                            NUMBER(10,3),
  ZR                            NUMBER(10,3),
  NA                            NUMBER(10,3),
  LI                            NUMBER(10,3),
  GA                            NUMBER(10,3),
  CA                            NUMBER(10,3),
  B                             NUMBER(10,3),
  BE                            NUMBER(10,3),
  BI                            NUMBER(10,3),
  W                             NUMBER(10,3),
  N                             NUMBER(10,3),
  NB                            NUMBER(10,3),
  TA                            NUMBER(10,3),
  O                             NUMBER(10,3),
  H                             NUMBER(10,3),
  AR                            NUMBER(10,3),
  AG                            NUMBER(10,3),
  AS1                           NUMBER(10,3),
  CD                            NUMBER(10,3),
  CL                            NUMBER(10,3),
  CO                            NUMBER(10,3),
  K                             NUMBER(10,3),
  SB                            NUMBER(10,3),
  SE                            NUMBER(10,3),
  CREATED_BY                    VARCHAR2(32 BYTE),
  CREATED_DT                    DATE,
  CREATED_BY_NAME               VARCHAR2(32 BYTE),
  UPDATED_BY                    VARCHAR2(32 BYTE),
  UPDATED_DT                    DATE,
  UPDATED_BY_NAME               VARCHAR2(32 BYTE),
  SEND_FLAG                     VARCHAR2(32 BYTE),
  SEND_DATE                     DATE,
  TRANSACTION_ID                VARCHAR2(32 BYTE),
  VERSION                       NUMBER(9),
  TEXT1                         VARCHAR2(32 BYTE),
  TEXT2                         VARCHAR2(32 BYTE),
  TEXT3                         VARCHAR2(32 BYTE),
  TEXT4                         VARCHAR2(32 BYTE),
  TEXT5                         VARCHAR2(32 BYTE),
  TOC                           DATE,
  TOM                           DATE,
  MOP                           VARCHAR2(60 BYTE),
  POSITION                      NUMBER(8)       DEFAULT 0,
  CROSS_SECTION_AREA            NUMBER(13,3),
  UNCOILER_TENSION              NUMBER(13,3),
  LOOPER_TENSION_1              NUMBER(13,3),
  PL_TENSION                    NUMBER(13,3),
  LOOPER_TENSION_2              NUMBER(13,3),
  LOOPER_TENSION_3              NUMBER(13,3),
  METERWEIGHT                   NUMBER(13,3),
  METER_D_OUTSIDE               NUMBER(13,3),
  METER_WIDTH                   NUMBER(13,3),
  SCRAP_CUT_HEAD_LEN            NUMBER(13,3),
  SCRAP_CUT_TAIL_LEN            NUMBER(13,3),
  COILER_DIAMETER               NUMBER(4),
  L2_GRADE                      VARCHAR2(32 BYTE)
)
TABLESPACE USERS
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          20M
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN PDI_PLTM.SID IS '������ID,ϵͳ����,��ORACLE Sequence�Զ�������ID';

COMMENT ON COLUMN PDI_PLTM.ROLLPROGRAMNB IS '�������κ�';

COMMENT ON COLUMN PDI_PLTM.SEQUENCENB IS '���κ���˳��';

COMMENT ON COLUMN PDI_PLTM.STATUS IS '״̬';

COMMENT ON COLUMN PDI_PLTM.SCHEDULE_CODE IS '�ƻ���';

COMMENT ON COLUMN PDI_PLTM.COILID IS 'ԭ�Ͼ��';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_THICKNESS IS 'ԭ�Ϻ��';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_THICKNESS_MAX IS 'ԭ�Ϻ�����ֵ';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_THICKNESS_MIN IS 'ԭ�Ϻ����Сֵ';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_WIDTH IS 'ԭ�Ͽ��';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_WIDTH_MAX IS 'ԭ�Ͽ�����ֵ';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_WIDTH_MIN IS 'ԭ�Ͽ����Сֵ';

COMMENT ON COLUMN PDI_PLTM.ENTRY_COIL_WEIGHT IS 'ԭ������';

COMMENT ON COLUMN PDI_PLTM.ENTRY_OF_COIL_LENGTH IS 'ԭ�ϳ���';

COMMENT ON COLUMN PDI_PLTM.ENTRY_OF_COIL_INNER_DIAMETER IS 'ԭ���ھ�';

COMMENT ON COLUMN PDI_PLTM.ENTRY_OF_COIL_OUTER_DIAMETER IS 'ԭ���⾶';

COMMENT ON COLUMN PDI_PLTM.TRIMMING IS '�б߱��';

COMMENT ON COLUMN PDI_PLTM.TRIMMING_WIDTH IS '�бߺ���';

COMMENT ON COLUMN PDI_PLTM.SMP_LENGTH IS 'ȡ������';

COMMENT ON COLUMN PDI_PLTM.SMP_NUM IS 'ȡ������';

COMMENT ON COLUMN PDI_PLTM.SMP_FRQ IS 'ȡ��Ŀ��';

COMMENT ON COLUMN PDI_PLTM.SMP_NUM_HEAD IS 'ͷ��ȡ������';

COMMENT ON COLUMN PDI_PLTM.SMP_NUM_MID IS '�в�ȡ������';

COMMENT ON COLUMN PDI_PLTM.SMP_NUM_TAIL IS 'β��ȡ������';

COMMENT ON COLUMN PDI_PLTM.PRECEDING_PROCESS_CODE IS '�ϵ�����';

COMMENT ON COLUMN PDI_PLTM.NEXT_PROCESS_CODE IS '�µ�����';

COMMENT ON COLUMN PDI_PLTM.HOT_MILL_DELIVERY_TEMP IS '�����¶�';

COMMENT ON COLUMN PDI_PLTM.FINISHED_COIL_TEMP IS '�����¶�';

COMMENT ON COLUMN PDI_PLTM.CROWN_AVERAGE IS '������͹��';

COMMENT ON COLUMN PDI_PLTM.COIL_FLATNESS_AVERAGE IS 'ƽֱ��Ŀ��ֵ';

COMMENT ON COLUMN PDI_PLTM.COIL_FLATNESS_MAX_VALUE IS 'ƽֱ����С';

COMMENT ON COLUMN PDI_PLTM.COIL_FLATNESS_MIN_VALUE IS 'ƽֱ�����';

COMMENT ON COLUMN PDI_PLTM.MATERIAL_YIELD_POINT IS 'ԭ������ǿ��';

COMMENT ON COLUMN PDI_PLTM.MATERIAL_TENSILE IS 'ԭ�Ͽ���ǿ��';

COMMENT ON COLUMN PDI_PLTM.HOTACTFMWEDGEAVG IS '������Ш��ƽ��ֵ';

COMMENT ON COLUMN PDI_PLTM.WEIGHT_MODE IS '�����־�ģʽ';

COMMENT ON COLUMN PDI_PLTM.DUMMY_COIL_MRK IS '�����ϱ��';

COMMENT ON COLUMN PDI_PLTM.CUT_MODE IS '����ģʽ';

COMMENT ON COLUMN PDI_PLTM.OFF_GAUGE_HEAD_LENGTH IS 'ͷ�������';

COMMENT ON COLUMN PDI_PLTM.OFF_GAUGE_TAIL_LENGTH IS 'β�������';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_NO IS '���ھ��';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WEIGHT IS '����Ŀ������';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WEIGHT_MAX IS '����Ŀ���������ֵ';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WEIGHT_MIN IS '����Ŀ��������Сֵ';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_THICKNESS IS '����Ŀ����';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_THICKNESS_MAX IS '����Ŀ�������ֵ';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_THICKNESS_MIN IS '����Ŀ������Сֵ';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WIDTH IS '����Ŀ����';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WIDTH_MAX IS '����Ŀ�������ֵ';

COMMENT ON COLUMN PDI_PLTM.EXIT_COIL_WIDTH_MIN IS '����Ŀ������Сֵ';

COMMENT ON COLUMN PDI_PLTM.WORK_ORDER_NO IS '��ͬ��';

COMMENT ON COLUMN PDI_PLTM.ORDER_QUALITY IS '�ּ�����';

COMMENT ON COLUMN PDI_PLTM.STEEL_GRADE IS '���ֱ��';

COMMENT ON COLUMN PDI_PLTM.SG_SIGN IS '�ⲿ�ƺ�/�����ƺ�';

COMMENT ON COLUMN PDI_PLTM.ORDER_THICKNESS IS '�������';

COMMENT ON COLUMN PDI_PLTM.ORDER_THICKNESS_MAX IS '�����������';

COMMENT ON COLUMN PDI_PLTM.ORDER_THICKNESS_MIN IS '�����������';

COMMENT ON COLUMN PDI_PLTM.ORDER_WIDTH IS '�������';

COMMENT ON COLUMN PDI_PLTM.ORDER_WIDTH_MAX IS '�����������';

COMMENT ON COLUMN PDI_PLTM.ORDER_WIDTH_MIN IS '�����������';

COMMENT ON COLUMN PDI_PLTM.SLEEVE_CODE_OF_COLD_COIL IS '��Ʒ��Ͳ';

COMMENT ON COLUMN PDI_PLTM.PACKING_TYPE_CODE IS '��װ���ʹ���';

COMMENT ON COLUMN PDI_PLTM.THK_DS IS '���Ӵ���';

COMMENT ON COLUMN PDI_PLTM.EXT_NUM_01 IS '���±�ʶ';

COMMENT ON COLUMN PDI_PLTM.C IS 'C';

COMMENT ON COLUMN PDI_PLTM.SI IS 'Si';

COMMENT ON COLUMN PDI_PLTM.MN IS 'Mn';

COMMENT ON COLUMN PDI_PLTM.P IS 'P';

COMMENT ON COLUMN PDI_PLTM.S IS 'S';

COMMENT ON COLUMN PDI_PLTM.CU IS 'Cu';

COMMENT ON COLUMN PDI_PLTM.NI IS 'Ni';

COMMENT ON COLUMN PDI_PLTM.CR IS 'Cr';

COMMENT ON COLUMN PDI_PLTM.MO IS 'Mo';

COMMENT ON COLUMN PDI_PLTM.V IS 'V';

COMMENT ON COLUMN PDI_PLTM.TI IS 'Ti';

COMMENT ON COLUMN PDI_PLTM.SOL_AL IS 'Sol_Al';

COMMENT ON COLUMN PDI_PLTM.FE IS 'Fe';

COMMENT ON COLUMN PDI_PLTM.MG IS 'Mg';

COMMENT ON COLUMN PDI_PLTM.PB IS 'Pb';

COMMENT ON COLUMN PDI_PLTM.SN IS 'Sn';

COMMENT ON COLUMN PDI_PLTM.ZN IS 'Zn';

COMMENT ON COLUMN PDI_PLTM.ZR IS 'Zr';

COMMENT ON COLUMN PDI_PLTM.NA IS 'Na';

COMMENT ON COLUMN PDI_PLTM.LI IS 'Li';

COMMENT ON COLUMN PDI_PLTM.GA IS 'Ga';

COMMENT ON COLUMN PDI_PLTM.CA IS 'Ca';

COMMENT ON COLUMN PDI_PLTM.B IS 'B';

COMMENT ON COLUMN PDI_PLTM.BE IS 'Be';

COMMENT ON COLUMN PDI_PLTM.BI IS 'Bi';

COMMENT ON COLUMN PDI_PLTM.W IS 'W';

COMMENT ON COLUMN PDI_PLTM.N IS 'N';

COMMENT ON COLUMN PDI_PLTM.NB IS 'Nb';

COMMENT ON COLUMN PDI_PLTM.TA IS 'Ta';

COMMENT ON COLUMN PDI_PLTM.O IS 'O';

COMMENT ON COLUMN PDI_PLTM.H IS 'H';

COMMENT ON COLUMN PDI_PLTM.AR IS 'Ar';

COMMENT ON COLUMN PDI_PLTM.AG IS 'Ar';

COMMENT ON COLUMN PDI_PLTM.AS1 IS 'As';

COMMENT ON COLUMN PDI_PLTM.CD IS 'Cd';

COMMENT ON COLUMN PDI_PLTM.CL IS 'Ci';

COMMENT ON COLUMN PDI_PLTM.CO IS 'Co';

COMMENT ON COLUMN PDI_PLTM.K IS 'K';

COMMENT ON COLUMN PDI_PLTM.SB IS 'Sb';

COMMENT ON COLUMN PDI_PLTM.SE IS 'Se';

COMMENT ON COLUMN PDI_PLTM.CREATED_BY IS '������';

COMMENT ON COLUMN PDI_PLTM.CREATED_DT IS '����ʱ��';

COMMENT ON COLUMN PDI_PLTM.CREATED_BY_NAME IS '����������';

COMMENT ON COLUMN PDI_PLTM.UPDATED_BY IS '������';

COMMENT ON COLUMN PDI_PLTM.UPDATED_DT IS '����ʱ��';

COMMENT ON COLUMN PDI_PLTM.UPDATED_BY_NAME IS '�޸�������';

COMMENT ON COLUMN PDI_PLTM.SEND_FLAG IS 'ͬ����ʶλ';

COMMENT ON COLUMN PDI_PLTM.SEND_DATE IS '����ʱ��';

COMMENT ON COLUMN PDI_PLTM.TRANSACTION_ID IS '����ID';

COMMENT ON COLUMN PDI_PLTM.VERSION IS '�汾��';

COMMENT ON COLUMN PDI_PLTM.TEXT1 IS 'Ԥ���ֶ�1';

COMMENT ON COLUMN PDI_PLTM.TEXT2 IS 'Ԥ���ֶ�2';

COMMENT ON COLUMN PDI_PLTM.TEXT3 IS 'Ԥ���ֶ�3';

COMMENT ON COLUMN PDI_PLTM.TEXT4 IS 'Ԥ���ֶ�4';

COMMENT ON COLUMN PDI_PLTM.TEXT5 IS 'Ԥ���ֶ�5';

COMMENT ON COLUMN PDI_PLTM.CROSS_SECTION_AREA IS '������-m*m';

COMMENT ON COLUMN PDI_PLTM.UNCOILER_TENSION IS '���������-kN';

COMMENT ON COLUMN PDI_PLTM.LOOPER_TENSION_1 IS '1�Ż������� ����ڻ��ף�-kN';

COMMENT ON COLUMN PDI_PLTM.PL_TENSION IS '��ϴ������-kN';

COMMENT ON COLUMN PDI_PLTM.LOOPER_TENSION_2 IS '2�Ż�������������1�ţ�-kN';

COMMENT ON COLUMN PDI_PLTM.LOOPER_TENSION_3 IS '3�Ż�������������2�ţ�-kN';

COMMENT ON COLUMN PDI_PLTM.METERWEIGHT IS 'ʵ������Real-����';

COMMENT ON COLUMN PDI_PLTM.METER_D_OUTSIDE IS 'ʵ���Real-��';

COMMENT ON COLUMN PDI_PLTM.METER_WIDTH IS 'ʵ����Real-��';

COMMENT ON COLUMN PDI_PLTM.SCRAP_CUT_HEAD_LEN IS '//ͷ�����г���--Real--��';

COMMENT ON COLUMN PDI_PLTM.SCRAP_CUT_TAIL_LEN IS '//β�����г���--Real--��';



CREATE UNIQUE INDEX PK_PDI_PLTM ON PDI_PLTM
(COILID)
LOGGING
TABLESPACE USERS
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          2M
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;


CREATE OR REPLACE TRIGGER DEL_B_PDI_PLTM BEFORE DELETE ON PDI_PLTM REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
DECLARE
   tmpVar     NUMBER;
   szCoilID   VARCHAR2 (20);
/******************************************************************************
   NAME:       DEL_B_PDI_PLTM
   PURPOSE:

   REVISIONS:
   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
   1.0        2023-12-8      Administrator       1. Created this trigger.

   NOTES:

   Automatically available Auto Replace Keywords:
      Object Name:     DEL_B_PDI_PLTM
      Sysdate:         2023-12-8
      Date and Time:   2023-12-8, 16:45:20, and 2023-12-8 16:45:20
      Username:        Administrator (set in TOAD Options, Proc Templates)
      Table Name:      PDI_PLTM (set in the "New PL/SQL Object" dialog)
      Trigger Options:  (set in the "New PL/SQL Object" dialog)
******************************************************************************/
BEGIN
   tmpVar := 0;

   --SELECT MySeq.NEXTVAL INTO tmpVar FROM dual;
   --:NEW.SequenceColumn := tmpVar;
   --:NEW.CreatedDate := SYSDATE;
   --:NEW.CreatedUser := USER;
   -------------------------------------------------------------------------------
   szCoilID := :OLD.COILID || '%';

   DELETE RASTCMDB.PDI WHERE COILID LIKE szCoilID;
EXCEPTION
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END DEL_B_PDI_PLTM;
/


CREATE OR REPLACE TRIGGER INS_B_PDI_PLTM
   BEFORE INSERT
   ON PDI_PLTM
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
DECLARE
   --PRAGMA AUTONOMOUS_TRANSACTION;
   Programm                    VARCHAR (60);
   USER                        VARCHAR (30);
   session_id                  NUMBER;
   nRollProgramNb              PDI_PLTM.ROLLPROGRAMNB%TYPE;
   nSequenceNb                 PDI_PLTM.SEQUENCENB%TYPE;

   nCrossSectionArea           PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   nMinCrossSectionArea        PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   nMaxCrossSectionArea        PDI_PLTM.CROSS_SECTION_AREA%TYPE;

   TablePL_BM_TENSION_XQ       PL_BM_TENSION_XQ%ROWTYPE;

   TablePL_BM_TENSION_XQ_MIN   PL_BM_TENSION_XQ%ROWTYPE;
   TablePL_BM_TENSION_XQ_MAX   PL_BM_TENSION_XQ%ROWTYPE;

   nMinKeyID                   NUMBER;
   nMaxKeyID                   NUMBER;

   nCount                      NUMBER;

   iCross_section_areaMin      PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tensionMin        PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1Min        PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tensionMin              PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2Min        PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3Min        PDI_PLTM.LOOPER_TENSION_3%TYPE;

   iCross_section_areaMax      PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tensionMax        PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1Max        PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tensionMax              PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2Max        PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3Max        PDI_PLTM.LOOPER_TENSION_3%TYPE;

   --iCross_section_area         PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tension           PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1           PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tension                 PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2           PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3           PDI_PLTM.LOOPER_TENSION_3%TYPE;

   szCoilID                    VARCHAR2 (20);
   szFileName                  VARCHAR (64);
   startDate                   DATE;
BEGIN
   -------------------------------------------------------------------------------
   startDate := SYSDATE;
   -------------------------------------------------------------------------------
   szFileName := TO_CHAR (SYSDATE, 'yyyymmdd') || 'INS_B_PDI_PLTM';

   LOG_TEXT (szFileName,
             TO_CHAR (startDate, 'hh24:mi:ss') || ' I: start INS_B_PDI_PLTM');

   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------
   SELECT USERENV ('SESSIONID') INTO session_id FROM DUAL;

   BEGIN
      SELECT program, username
        INTO programm, USER
        FROM V$SESSION
       WHERE audsid = session_id;
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         programm := 'UNKNOWN';
         USER := 'UNKNOWN';
      WHEN OTHERS
      THEN
         IF (session_id = 0)
         THEN
            programm := 'JOB';
         ELSE
            programm := 'UNKNOWN';
         END IF;
   END;

   -------------------------------------------------------------------------------


   --:NEW.ROLLPROGRAMNB := nRollProgramNb;
   --:NEW.SEQUENCENB := nSequenceNb;
   -------------------------------------------------------------------------------
   :NEW.STATUS := 0;

   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------
   --*****************************************************************************


   -------------------------------------------------------------------------------
   nCount := 0;

   IF nCount = nCount                                                  -- 1000
   THEN
      -------------------------------------------------------------------------------
      LOG_TEXT (szFileName,
                'INSERT INTO RASTCMDB.V_EVN_NEW_PDI ' || :NEW.COILID);

      DELETE RASTCMDB.V_EVN_NEW_PDI
       WHERE SUBSTR (COIL_ID, 1, 12) = :NEW.COILID;

      INSERT INTO RASTCMDB.V_EVN_NEW_PDI (COIL_ID,
                                          ENTRY_THICK,
                                          WIDTH_ENTRY,
                                          WEIGHT,
                                          ENTRY_LENGTH,
                                          OUTER_DIAM,
                                          CROWN,
                                          EXIT_COIL_ID,
                                          EXIT_THICK,
                                          WIDTH,
                                          HOT_GRADE,
                                          LABEL_GRADE,
                                          GRADE,
                                          PACK_TYPE,
                                          INITIAL_GAUGE,
                                          CONTRACT_NO,
                                          SCHEDULE_NO,
                                          COILER_DIAMETER,
                                          -------------------------------------------------------------------------------
                                          WEIGHT_MODE, --       �����־�ģʽ/�Ƿ�־� 1 �־�
                                          WEIGHT_EXIT             --�־�� ����Ŀ������
                                                     --DESTINATION
                                          )
              VALUES (
                        :NEW.COILID,
                        :NEW.ENTRY_COIL_THICKNESS,
                        :NEW.ENTRY_COIL_WIDTH,
                        :NEW.ENTRY_COIL_WEIGHT,
                        :NEW.ENTRY_OF_COIL_LENGTH,
                        :NEW.ENTRY_OF_COIL_OUTER_DIAMETER,
                        :NEW.CROWN_AVERAGE,
                        :NEW.EXIT_COIL_NO,
                        :NEW.EXIT_COIL_THICKNESS,
                        :NEW.EXIT_COIL_WIDTH,
                        :NEW.STEEL_GRADE,
                        :NEW.SG_SIGN,
                        :NEW.L2_GRADE,
                        :NEW.PACKING_TYPE_CODE,
                        :NEW.ENTRY_COIL_THICKNESS,
                        :NEW.WORK_ORDER_NO,
                        :NEW.SCHEDULE_CODE,
                        :NEW.COILER_DIAMETER,
                        -------------------------------------------------------------------------------
                        TO_NUMBER (
                           DECODE (:NEW.WEIGHT_MODE,
                                   NULL, 0,
                                   :NEW.WEIGHT_MODE)),            -- '�����־�ģʽ';
                        :NEW.EXIT_COIL_WEIGHT                     -- '����Ŀ������';
                                             --:NEW.NEXT_PROCESS_CODE
                        );

      LOG_TEXT (szFileName,
                'INSERT INTO RASTCMDB.V_EVN_NEW_PDI ��� ' || :NEW.COILID);

      RASTCMDB.PROC_PROCESS_PDI_TEST_1 ();

      LOG_TEXT (
         szFileName,
            'INSERT INTO RASTCMDB.PROC_PROCESS_PDI_TEST_1 ��� '
         || :NEW.COILID);

      -------------------------------------------------------------------------------
      szCoilID := :NEW.COILID || '-00';

      -------------------------------------------------------------------------------
      LOG_TEXT (
         szFileName,
            'UPDATE RASTCMDB.PDI '
         || TO_CHAR (:NEW.ROLLPROGRAMNB)
         || ' '
         || TO_CHAR (:NEW.SEQUENCENB));

      UPDATE RASTCMDB.PDI
         SET ROLLPROGRAMNB = :NEW.ROLLPROGRAMNB, SEQUENCENB = :NEW.SEQUENCENB
       WHERE COILID = szCoilID;

      -------------------------------------------------------------------------------

      LOG_TEXT (szFileName, 'UPDATE RASTCMDB.PDI ' || szCoilID);
   -------------------------------------------------------------------------------
   END IF;

   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------
   :new.toc := SYSDATE;
   :new.mop := SUBSTR (programm, 1, 60);
   
   
   -------------------------------------------------------------------------------
   --*****************************************************************************
   nCrossSectionArea := :NEW.ENTRY_COIL_THICKNESS * :NEW.ENTRY_COIL_WIDTH;

   -------------------------------------------------------------------------------
   --1.
   SELECT COUNT (*)
     INTO nCount
     FROM PL_BM_TENSION_XQ
    WHERE CROSS_SECTION_AREA = nCrossSectionArea;

   IF nCount <> 0
   THEN
      SELECT *
        INTO TablePL_BM_TENSION_XQ
        FROM PL_BM_TENSION_XQ
       WHERE CROSS_SECTION_AREA = nCrossSectionArea AND ROWNUM = 1;

      :NEW.CROSS_SECTION_AREA := TablePL_BM_TENSION_XQ.CROSS_SECTION_AREA;
      :NEW.UNCOILER_TENSION := TablePL_BM_TENSION_XQ.UNCOILER_TENSION;
      :NEW.LOOPER_TENSION_1 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_1;
      :NEW.PL_TENSION := TablePL_BM_TENSION_XQ.PL_TENSION;
      :NEW.LOOPER_TENSION_2 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_2;
      :NEW.LOOPER_TENSION_3 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_3;

      RETURN;
   END IF;

   -------------------------------------------------------------------------------
   --2.


   SELECT MIN (KEYID),
          MAX (KEYID),
          MIN (CROSS_SECTION_AREA),
          MAX (CROSS_SECTION_AREA)
     INTO nMinKeyID,
          nMaxKeyID,
          nMinCrossSectionArea,
          nMaxCrossSectionArea
     FROM PL_BM_TENSION_XQ;

   -------------------------------------------------------------------------------
   IF    nCrossSectionArea <= nMinCrossSectionArea
      OR nCrossSectionArea >= nMaxCrossSectionArea
   THEN
      -------------------------------------------------------------------------------
      --2.�ҵ��ǲ��������С֮�⣬����ǣ�ѡ��һ����ֱ��ʹ��
      IF nCrossSectionArea <= nMinCrossSectionArea
      THEN
         nCrossSectionArea := nMinCrossSectionArea;
      ELSE
         nCrossSectionArea := nMaxCrossSectionArea;
      END IF;

      SELECT *
        INTO TablePL_BM_TENSION_XQ
        FROM PL_BM_TENSION_XQ
       WHERE CROSS_SECTION_AREA = nCrossSectionArea;

      :NEW.CROSS_SECTION_AREA := TablePL_BM_TENSION_XQ.CROSS_SECTION_AREA;
      :NEW.UNCOILER_TENSION := TablePL_BM_TENSION_XQ.UNCOILER_TENSION;
      :NEW.LOOPER_TENSION_1 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_1;
      :NEW.PL_TENSION := TablePL_BM_TENSION_XQ.PL_TENSION;
      :NEW.LOOPER_TENSION_2 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_2;
      :NEW.LOOPER_TENSION_3 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_3;

      RETURN;
   -------------------------------------------------------------------------------
   ELSE
      -------------------------------------------------------------------------------
      --3.
      SELECT KEYID
        INTO nMinKeyID
        FROM (  SELECT *
                  FROM PL_BM_TENSION_XQ
                 WHERE CROSS_SECTION_AREA < nCrossSectionArea
              ORDER BY CROSS_SECTION_AREA DESC)
       WHERE ROWNUM = 1;

      SELECT KEYID
        INTO nMaxKeyID
        FROM (  SELECT *
                  FROM PL_BM_TENSION_XQ
                 WHERE CROSS_SECTION_AREA > nCrossSectionArea
              ORDER BY CROSS_SECTION_AREA ASC)
       WHERE ROWNUM = 1;

      SELECT *
        INTO TablePL_BM_TENSION_XQ_MIN
        FROM PL_BM_TENSION_XQ
       WHERE KEYID = nMinKeyID;

      SELECT *
        INTO TablePL_BM_TENSION_XQ_MAX
        FROM PL_BM_TENSION_XQ
       WHERE KEYID = nMaxKeyID;

      iCross_section_areaMin := TablePL_BM_TENSION_XQ_MIN.CROSS_SECTION_AREA;
      iUncoiler_tensionMin := TablePL_BM_TENSION_XQ_MIN.UNCOILER_TENSION;
      iLooper_tension_1Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_1;
      iPL_tensionMin := TablePL_BM_TENSION_XQ_MIN.PL_TENSION;
      iLooper_tension_2Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_2;
      iLooper_tension_3Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_3;

      iCross_section_areaMax := TablePL_BM_TENSION_XQ_MAX.CROSS_SECTION_AREA;
      iUncoiler_tensionMax := TablePL_BM_TENSION_XQ_MAX.UNCOILER_TENSION;
      iLooper_tension_1Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_1;
      iPL_tensionMax := TablePL_BM_TENSION_XQ_MAX.PL_TENSION;
      iLooper_tension_2Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_2;
      iLooper_tension_3Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_3;

      :NEW.CROSS_SECTION_AREA := nCrossSectionArea;
      --iUncoiler_tension :=
      :NEW.UNCOILER_TENSION :=
           (    (iUncoiler_tensionMax - iUncoiler_tensionMin)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_1 :=
           (    (iLooper_tension_1Max - iLooper_tension_1Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
      :NEW.PL_TENSION :=
           (    (iLooper_tension_1Max - iPL_tensionMax)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_2 :=
           (    (iLooper_tension_2Max - iLooper_tension_2Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_3 :=
           (    (iLooper_tension_3Max - iLooper_tension_3Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
   -------------------------------------------------------------------------------
   END IF;

   -------------------------------------------------------------------------------
EXCEPTION
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END INS_B_PDI_PLTM;
/


CREATE OR REPLACE TRIGGER UPD_B_PDI_PLTM
BEFORE UPDATE
ON PDI_PLTM
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
   tmpVar                      NUMBER;
   nIndex                      NUMBER;
   --PRAGMA AUTONOMOUS_TRANSACTION;

   nCrossSectionArea           PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   nMinCrossSectionArea        PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   nMaxCrossSectionArea        PDI_PLTM.CROSS_SECTION_AREA%TYPE;

   TablePL_BM_TENSION_XQ       PL_BM_TENSION_XQ%ROWTYPE;

   TablePL_BM_TENSION_XQ_MIN   PL_BM_TENSION_XQ%ROWTYPE;
   TablePL_BM_TENSION_XQ_MAX   PL_BM_TENSION_XQ%ROWTYPE;

   nMinKeyID                   NUMBER;
   nMaxKeyID                   NUMBER;

   nCount                      NUMBER;

   iCross_section_areaMin      PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tensionMin        PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1Min        PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tensionMin              PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2Min        PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3Min        PDI_PLTM.LOOPER_TENSION_3%TYPE;

   iCross_section_areaMax      PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tensionMax        PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1Max        PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tensionMax              PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2Max        PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3Max        PDI_PLTM.LOOPER_TENSION_3%TYPE;

   --iCross_section_area         PDI_PLTM.CROSS_SECTION_AREA%TYPE;
   iUncoiler_tension           PDI_PLTM.UNCOILER_TENSION%TYPE;
   iLooper_tension_1           PDI_PLTM.LOOPER_TENSION_1%TYPE;
   iPL_tension                 PDI_PLTM.PL_TENSION%TYPE;
   iLooper_tension_2           PDI_PLTM.LOOPER_TENSION_2%TYPE;
   iLooper_tension_3           PDI_PLTM.LOOPER_TENSION_3%TYPE;

   szCoilID                    VARCHAR2 (20);
BEGIN
   tmpVar := 0;

   -------------------------------------------------------------------------------
   --*****************************************************************************

   -------------------------------------------------------------------------------
   szCoilID := :NEW.COILID || '%';

   SELECT COUNT (COILID)
     INTO nCount
     FROM RASTCMDB.PDI
    WHERE COILID LIKE szCoilID;

   IF nCount > 0
   THEN
      szCoilID := :NEW.COILID || '%';

      IF :NEW.STATUS = :OLD.STATUS                                     ------ AND :NEW.STATUS = 0
      THEN
         IF NOT :NEW.L2_GRADE IS NULL AND TRIM (:NEW.L2_GRADE) <> ''
         THEN
            UPDATE RASTCMDB.PDI
               SET GRADE = :NEW.L2_GRADE,
                   HOT_GRADE = :NEW.STEEL_GRADE,
                   LABEL_GRADE = :NEW.SG_SIGN,                          --��������
                   -------------------------------------------------------------------------------
                   PACK_TYPE = :NEW.PACKING_TYPE_CODE,
                   CONTRACT_NO = :NEW.WORK_ORDER_NO,
                   SCHEDULE_NO = :NEW.SCHEDULE_CODE,
                   COILER_DIAMETER = :NEW.COILER_DIAMETER,
                   EXIT_COIL_ID = :NEW.EXIT_COIL_NO,
                   WEIGHT = :NEW.ENTRY_COIL_WEIGHT,
                   ENTRYLENGTH = :NEW.ENTRY_OF_COIL_LENGTH,
                   OUTSIDEDIAM = :NEW.ENTRY_OF_COIL_OUTER_DIAMETER,
                   CROWN = :NEW.CROWN_AVERAGE,
                   -------------------------------------------------------------------------------
                   contract_exitthick = :NEW.ORDER_THICKNESS,           --�������
                   -------------------------------------------------------------------------------

                   ENTRYTHICK = :NEW.ENTRY_COIL_THICKNESS,              --���Ϻ��
                   INITIAL_GAUGE = :NEW.ENTRY_COIL_THICKNESS,          --�����Ϻ��
                   WIDTH = :NEW.EXIT_COIL_WIDTH,                        --��ڿ��
                   --WIDTH_ENTRY = :NEW.ENTRY_COIL_WIDTH,       --���Ͽ��
                   -------------------------------------------------------------------------------
                   EXITTHICK = :NEW.EXIT_COIL_THICKNESS,                --��Ʒ���
                   EXITWIDTH = :NEW.EXIT_COIL_WIDTH,                    --��Ʒ���
                   -------------------------------------------------------------------------------
                   WEIGHT_MODE = TO_NUMBER (:NEW.WEIGHT_MODE), --       �����־�ģʽ/�Ƿ�־� 1 �־�
                   WEIGHT_EXIT = :NEW.EXIT_COIL_WEIGHT,           --�־�� ����Ŀ������
                   -------------------------------------------------------------------------------
                   ROLLPROGRAMNB = :NEW.ROLLPROGRAMNB,
                   SEQUENCENB = :NEW.SEQUENCENB
             -------------------------------------------------------------------------------
             WHERE COILID LIKE szCoilID;
         ELSE
            UPDATE RASTCMDB.PDI
               SET                                    --GRADE = :NEW.L2_GRADE,
                  HOT_GRADE = :NEW.STEEL_GRADE,
                   LABEL_GRADE = :NEW.SG_SIGN,                          --��������
                   -------------------------------------------------------------------------------
                   PACK_TYPE = :NEW.PACKING_TYPE_CODE,
                   CONTRACT_NO = :NEW.WORK_ORDER_NO,
                   SCHEDULE_NO = :NEW.SCHEDULE_CODE,
                   COILER_DIAMETER = :NEW.COILER_DIAMETER,
                   EXIT_COIL_ID = :NEW.EXIT_COIL_NO,
                   WEIGHT = :NEW.ENTRY_COIL_WEIGHT,
                   ENTRYLENGTH = :NEW.ENTRY_OF_COIL_LENGTH,
                   OUTSIDEDIAM = :NEW.ENTRY_OF_COIL_OUTER_DIAMETER,
                   CROWN = :NEW.CROWN_AVERAGE,
                   -------------------------------------------------------------------------------
                   contract_exitthick = :NEW.ORDER_THICKNESS,           --�������
                   -------------------------------------------------------------------------------

                   ENTRYTHICK = :NEW.ENTRY_COIL_THICKNESS,              --���Ϻ��
                   INITIAL_GAUGE = :NEW.ENTRY_COIL_THICKNESS,          --�����Ϻ��
                   WIDTH = :NEW.EXIT_COIL_WIDTH,                        --��ڿ��
                   --WIDTH_ENTRY = :NEW.ENTRY_COIL_WIDTH,       --���Ͽ��
                   -------------------------------------------------------------------------------
                   EXITTHICK = :NEW.EXIT_COIL_THICKNESS,                --��Ʒ���
                   EXITWIDTH = :NEW.EXIT_COIL_WIDTH,                    --��Ʒ���
                   -------------------------------------------------------------------------------
                   WEIGHT_MODE = TO_NUMBER (:NEW.WEIGHT_MODE), --       �����־�ģʽ/�Ƿ�־� 1 �־�
                   WEIGHT_EXIT = :NEW.EXIT_COIL_WEIGHT,           --�־�� ����Ŀ������
                   -------------------------------------------------------------------------------
                   ROLLPROGRAMNB = :NEW.ROLLPROGRAMNB,
                   SEQUENCENB = :NEW.SEQUENCENB
             -------------------------------------------------------------------------------
             WHERE COILID LIKE szCoilID;
         END IF;
      END IF;
   END IF;

   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------
   nCrossSectionArea := :NEW.ENTRY_COIL_THICKNESS * :NEW.ENTRY_COIL_WIDTH;

   -------------------------------------------------------------------------------
   --1.
   SELECT COUNT (*)
     INTO nCount
     FROM PL_BM_TENSION_XQ
    WHERE CROSS_SECTION_AREA = nCrossSectionArea;

   IF nCount <> 0
   THEN
      SELECT *
        INTO TablePL_BM_TENSION_XQ
        FROM PL_BM_TENSION_XQ
       WHERE CROSS_SECTION_AREA = nCrossSectionArea AND ROWNUM = 1;

      :NEW.CROSS_SECTION_AREA := TablePL_BM_TENSION_XQ.CROSS_SECTION_AREA;
      :NEW.UNCOILER_TENSION := TablePL_BM_TENSION_XQ.UNCOILER_TENSION;
      :NEW.LOOPER_TENSION_1 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_1;
      :NEW.PL_TENSION := TablePL_BM_TENSION_XQ.PL_TENSION;
      :NEW.LOOPER_TENSION_2 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_2;
      :NEW.LOOPER_TENSION_3 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_3;

      RETURN;
   END IF;

   -------------------------------------------------------------------------------
   --2.


   SELECT MIN (KEYID),
          MAX (KEYID),
          MIN (CROSS_SECTION_AREA),
          MAX (CROSS_SECTION_AREA)
     INTO nMinKeyID,
          nMaxKeyID,
          nMinCrossSectionArea,
          nMaxCrossSectionArea
     FROM PL_BM_TENSION_XQ;

   -------------------------------------------------------------------------------
   IF    nCrossSectionArea <= nMinCrossSectionArea
      OR nCrossSectionArea >= nMaxCrossSectionArea
   THEN
      -------------------------------------------------------------------------------
      --2.�ҵ��ǲ��������С֮�⣬����ǣ�ѡ��һ����ֱ��ʹ��
      IF nCrossSectionArea <= nMinCrossSectionArea
      THEN
         nCrossSectionArea := nMinCrossSectionArea;
      ELSE
         nCrossSectionArea := nMaxCrossSectionArea;
      END IF;

      SELECT *
        INTO TablePL_BM_TENSION_XQ
        FROM PL_BM_TENSION_XQ
       WHERE CROSS_SECTION_AREA = nCrossSectionArea;

      :NEW.CROSS_SECTION_AREA := TablePL_BM_TENSION_XQ.CROSS_SECTION_AREA;
      :NEW.UNCOILER_TENSION := TablePL_BM_TENSION_XQ.UNCOILER_TENSION;
      :NEW.LOOPER_TENSION_1 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_1;
      :NEW.PL_TENSION := TablePL_BM_TENSION_XQ.PL_TENSION;
      :NEW.LOOPER_TENSION_2 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_2;
      :NEW.LOOPER_TENSION_3 := TablePL_BM_TENSION_XQ.LOOPER_TENSION_3;

      RETURN;
   -------------------------------------------------------------------------------
   ELSE
      -------------------------------------------------------------------------------
      --3.
      SELECT KEYID
        INTO nMinKeyID
        FROM (  SELECT *
                  FROM PL_BM_TENSION_XQ
                 WHERE CROSS_SECTION_AREA < nCrossSectionArea
              ORDER BY CROSS_SECTION_AREA DESC)
       WHERE ROWNUM = 1;

      SELECT KEYID
        INTO nMaxKeyID
        FROM (  SELECT *
                  FROM PL_BM_TENSION_XQ
                 WHERE CROSS_SECTION_AREA > nCrossSectionArea
              ORDER BY CROSS_SECTION_AREA ASC)
       WHERE ROWNUM = 1;

      SELECT *
        INTO TablePL_BM_TENSION_XQ_MIN
        FROM PL_BM_TENSION_XQ
       WHERE KEYID = nMinKeyID;

      SELECT *
        INTO TablePL_BM_TENSION_XQ_MAX
        FROM PL_BM_TENSION_XQ
       WHERE KEYID = nMaxKeyID;

      iCross_section_areaMin := TablePL_BM_TENSION_XQ_MIN.CROSS_SECTION_AREA;
      iUncoiler_tensionMin := TablePL_BM_TENSION_XQ_MIN.UNCOILER_TENSION;
      iLooper_tension_1Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_1;
      iPL_tensionMin := TablePL_BM_TENSION_XQ_MIN.PL_TENSION;
      iLooper_tension_2Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_2;
      iLooper_tension_3Min := TablePL_BM_TENSION_XQ_MIN.LOOPER_TENSION_3;

      iCross_section_areaMax := TablePL_BM_TENSION_XQ_MAX.CROSS_SECTION_AREA;
      iUncoiler_tensionMax := TablePL_BM_TENSION_XQ_MAX.UNCOILER_TENSION;
      iLooper_tension_1Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_1;
      iPL_tensionMax := TablePL_BM_TENSION_XQ_MAX.PL_TENSION;
      iLooper_tension_2Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_2;
      iLooper_tension_3Max := TablePL_BM_TENSION_XQ_MAX.LOOPER_TENSION_3;

      --iUncoiler_tension :=
      :NEW.CROSS_SECTION_AREA := nCrossSectionArea;
      :NEW.UNCOILER_TENSION :=
           (    (iUncoiler_tensionMax - iUncoiler_tensionMin)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iUncoiler_tensionMin)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_1 :=
           (    (iLooper_tension_1Max - iLooper_tension_1Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iLooper_tension_1Min)
         * nCrossSectionArea;
      :NEW.PL_TENSION :=
           (    (iLooper_tension_1Max - iPL_tensionMin)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iPL_tensionMin)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_2 :=
           (    (iLooper_tension_2Max - iLooper_tension_2Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iLooper_tension_2Min)
         * nCrossSectionArea;
      :NEW.LOOPER_TENSION_3 :=
           (    (iLooper_tension_3Max - iLooper_tension_3Min)
              / (iCross_section_areaMax - iCross_section_areaMin)
              * (nCrossSectionArea - iCross_section_areaMin)
            + iLooper_tension_3Min)
         * nCrossSectionArea;
   -------------------------------------------------------------------------------
   END IF;
-------------------------------------------------------------------------------

--szCoilID := :NEW.COILID || '%';
-------------------------------------------------------------------------------

EXCEPTION
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END UPD_B_PDI_PLTM;
/


ALTER TABLE PDI_PLTM ADD (
  CONSTRAINT PK_PDI_PLTM
  PRIMARY KEY
  (COILID)
  USING INDEX PK_PDI_PLTM
  ENABLE VALIDATE);


GRANT DELETE, INSERT, SELECT, UPDATE ON PLTM.PDI_PLTM TO PLTM;
