-- ============================================
-- DMS 模块所有序列定义
-- 生成时间: 2026-01-23
-- 说明: 包含 DMS 模块所有 Mapper 中使用的序列
-- ============================================

-- ----------------------------
-- 1. SEQ_DMS_INSPECT_PROJ (点检项目)
-- 表: DMS_BASE_INSPECT_PROJECT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_PROJ"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_PROJ"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 2. SEQ_DMS_INSPECT_ROUTE (点检路线)
-- 表: DMS_BASE_INSPECT_ROUTE
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_ROUTE"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_ROUTE"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 3. SEQ_DMS_INSPECT_STD (点检标准)
-- 表: DMS_BASE_INSPECT_STANDARD
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_STD"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_STD"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 4. SEQ_DMS_INSPECT_RTE_DTL (点检路线明细)
-- 表: DMS_INSPECT_ROUTE_DETAIL
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_RTE_DTL"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSPECT_RTE_DTL"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 4A. SEQ_DMS_INSP_INST_DETAIL (巡检工单明细)
-- 表: DMS_INSPECT_INSTANCE_DETAIL
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSP_INST_DETAIL"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSP_INST_DETAIL"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 4B. SEQ_DMS_INSP_DET_PROJ (巡检明细项目)
-- 表: DMS_INSPECT_INST_DET_PROJ
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_INSP_DET_PROJ"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_INSP_DET_PROJ"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 5. SEQ_DMS_LUBE_STD (润滑标准)
-- 表: DMS_BASE_LUBE_STANDARD
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_LUBE_STD"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_LUBE_STD"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 6. SEQ_DMS_LUBE_STATION (润滑部位)
-- 表: DMS_BASE_LUBE_STATION
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_LUBE_STATION"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_LUBE_STATION"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 7. SEQ_DMS_MAINT_PROJ (保养项目)
-- 表: DMS_BASE_MAINT_PROJECT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_PROJ"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_PROJ"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 8. SEQ_DMS_MAINT_STD (保养标准)
-- 表: DMS_BASE_MAINT_STANDARD
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_STD"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_STD"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 9. SEQ_DMS_MAINT_STATION (保养部位)
-- 表: DMS_BASE_MAINT_STATION
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_STATION"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_STATION"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 10. SEQ_DMS_SHUT_REASON (停机原因)
-- 表: DMS_BASE_SHUT_REASON
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_SHUT_REASON"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_SHUT_REASON"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 11. SEQ_DMS_SHUT_TYPE (停机类型)
-- 表: DMS_BASE_SHUT_TYPE
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_SHUT_TYPE"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_SHUT_TYPE"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 12. SEQ_DMS_BILLS_FAULT_INSTANCE (故障工单)
-- 表: DMS_BILLS_FAULT_INSTANCE
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_FAULT_INSTANCE"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_FAULT_INSTANCE"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 13. SEQ_FAULT_INSTANCE_ACTIVITY (故障工单活动)
-- 表: DMS_FAULT_INSTANCE_ACTIVITY
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_FAULT_INSTANCE_ACTIVITY"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_FAULT_INSTANCE_ACTIVITY"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 14. SEQ_DMS_BILLS_INSPECT_INSTANCE (巡检工单)
-- 表: DMS_BILLS_INSPECT_INSTANCE
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_INSPECT_INSTANCE"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_INSPECT_INSTANCE"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 15. SEQ_DMS_BILLS_INSPECT_ACTIVITY (巡检工单活动)
-- 表: DMS_BILLS_INS_INST_ACT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_INSPECT_ACTIVITY"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_BILLS_INSPECT_ACTIVITY"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- 14. SEQ_DMS_MAINT_INST (保养工单)
-- 表: DMS_BILLS_MAINT_INSTANCE
-- 当前保养计划定时生成工单使用序列之一
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_INST"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_INST"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 14A. SEQ_DMS_MAINT_INST_ACTIVITY (保养工单活动)
-- 表: DMS_MAINT_INSTANCE_ACTIVITY
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_INST_ACTIVITY"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_INST_ACTIVITY"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 15. SEQ_DMS_MAINT_DETAIL (保养工单明细)
-- 表: DMS_BILLS_MAINT_DETAIL
-- 当前保养计划定时生成工单使用序列之一
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_DETAIL"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_DETAIL"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 16. SEQ_DMS_MAINT_DTL_PROJ (保养明细项目)
-- 表: DMS_BILLS_MAINT_DETAIL_PROJECT
-- 当前保养计划定时生成工单使用序列之一
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_DTL_PROJ"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_MAINT_DTL_PROJ"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 17. SEQ_DMS_DISPATCH_PLAN (派工计划)
-- 表: DMS_DISPATCH_PLAN
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_DISPATCH_PLAN"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_DISPATCH_PLAN"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 18. SEQ_DMS_DISPATCH_MEMBER (派工成员)
-- 表: DMS_DISPATCH_MEMBER
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_DISPATCH_MEMBER"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_DISPATCH_MEMBER"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 19. SEQ_DMS_KNOWLEDGE_REPAIR (维修知识库)
-- 表: DMS_KNOWLEDGE_REPAIR
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_KNOWLEDGE_REPAIR"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_KNOWLEDGE_REPAIR"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 20. SEQ_DMS_PLAN_INSPECT (点检计划)
-- 表: DMS_PLAN_INSPECT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_INSPECT"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_INSPECT"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 21. SEQ_DMS_PLAN_MAINT (保养计划)
-- 表: DMS_PLAN_MAINT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_MAINT"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_MAINT"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 22. SEQ_DMS_PLAN_MAINT_DETAIL (保养计划明细)
-- 表: DMS_PLAN_MAINT_DETAIL
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_MAINT_DETAIL"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_MAINT_DETAIL"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 23. SEQ_DMS_PLAN_REPAIR (检修计划)
-- 表: DMS_PLAN_REPAIR
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_REPAIR"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_PLAN_REPAIR"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 24. SEQ_DMS_REPAIR_MATERIAL (维修物料)
-- 表: DMS_REPAIR_MATERIAL
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_MATERIAL"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_MATERIAL"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 25. SEQ_DMS_REPAIR_PROJECT (维修项目)
-- 表: DMS_REPAIR_PROJECT
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_PROJECT"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_PROJECT"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 26. SEQ_DMS_REPAIR_RECORD (维修记录)
-- 表: DMS_REPAIR_RECORD
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_RECORD"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_RECORD"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 27. SEQ_DMS_REPAIR_WORK_ORDER (维修工单)
-- 表: DMS_REPAIR_WORK_ORDER
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_WORK_ORDER"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_REPAIR_WORK_ORDER"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 28. SEQ_DMS_REC_SHUT_DOWN (停机记录)
-- 表: DMS_RECORD_SHUT_DOWN
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_DMS_REC_SHUT_DOWN"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_DMS_REC_SHUT_DOWN"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ----------------------------
-- 29. SEQ_MES_BASE_ATTACH_INFO (附件信息)
-- 表: DMS_BASE_ATTACH_INFO
-- ----------------------------
BEGIN
   EXECUTE IMMEDIATE 'DROP SEQUENCE "HAIWEI"."SEQ_MES_BASE_ATTACH_INFO"';
EXCEPTION
   WHEN OTHERS THEN NULL;
END;
/
CREATE SEQUENCE "HAIWEI"."SEQ_MES_BASE_ATTACH_INFO"
  MINVALUE 1
  MAXVALUE 9999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

-- ============================================
-- 序列对齐：按 MAX(主键列)+1 设置 NEXTVAL（Oracle 11g 兼容方案）
-- ============================================
-- 实现思路：
-- 1) 读取表主键最大值，目标值 = MAX(pk)+1（空表则为1）
-- 2) 读取当前序列 last_number（使用 all_sequences 兼容跨 schema）
-- 3) 若目标值 > last_number，则临时修改 INCREMENT BY (差值)，取一次 NEXTVAL，最后再改回 1
-- 4) 若目标值 <= last_number，则保持不变，仅输出提示
DECLARE
  PROCEDURE sync_seq(p_seq_name VARCHAR2, p_table_name VARCHAR2, p_pk_col VARCHAR2) IS
    v_max_id    NUMBER := 0;
    v_last      NUMBER := 0;
    v_target    NUMBER := 0;
    v_diff      NUMBER := 0;
    v_seq_short VARCHAR2(128);
    v_sql       VARCHAR2(4000);
    v_dummy     NUMBER;
  BEGIN
    -- 取表主键最大值
    v_sql := 'SELECT NVL(MAX(' || p_pk_col || '),0) FROM ' || p_table_name;
    EXECUTE IMMEDIATE v_sql INTO v_max_id;

    -- 取 sequence 名（去 schema 部分）用于 ALL_SEQUENCES 查询
    v_seq_short := UPPER(REGEXP_SUBSTR(p_seq_name, '[^\.]+$'));
    SELECT last_number INTO v_last FROM all_sequences
     WHERE sequence_name = v_seq_short AND sequence_owner = 'HAIWEI';

    v_target := v_max_id + 1;

    IF v_target > v_last THEN
      v_diff := v_target - v_last;
      EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || p_seq_name || ' INCREMENT BY ' || v_diff;
      EXECUTE IMMEDIATE 'SELECT ' || p_seq_name || '.NEXTVAL FROM DUAL' INTO v_dummy;
      EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || p_seq_name || ' INCREMENT BY 1';
      DBMS_OUTPUT.PUT_LINE('SYNC ' || p_seq_name || ' -> ' || v_target || ' (MAX_ID=' || v_max_id || ')');
    ELSE
      DBMS_OUTPUT.PUT_LINE('SKIP ' || p_seq_name || ' (TARGET ' || v_target || ' <= LAST ' || v_last || ')');
    END IF;
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('ERR  ' || p_seq_name || ': ' || SQLERRM);
  END;

  PROCEDURE grant_and_synonym(p_seq_short VARCHAR2) IS
  BEGIN
    EXECUTE IMMEDIATE 'GRANT SELECT ON HAIWEI.' || p_seq_short || ' TO PUBLIC';
    EXECUTE IMMEDIATE 'CREATE OR REPLACE PUBLIC SYNONYM ' || p_seq_short || ' FOR HAIWEI.' || p_seq_short;
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('GRANT/SYN ' || p_seq_short || ' SKIP: ' || SQLERRM);
  END;
BEGIN
  -- 1. 点检项目
  sync_seq('HAIWEI.SEQ_DMS_INSPECT_PROJ',           'HAIWEI.DMS_BASE_INSPECT_PROJECT',      'INSPECT_PROJECT_ID');
  -- 2. 点检路线
  sync_seq('HAIWEI.SEQ_DMS_INSPECT_ROUTE',          'HAIWEI.DMS_BASE_INSPECT_ROUTE',        'INSPECT_ROUTE_ID');
  -- 3. 点检标准
  sync_seq('HAIWEI.SEQ_DMS_INSPECT_STD',            'HAIWEI.DMS_BASE_INSPECT_STANDARD',     'INSPECT_STANDARD_ID');
  -- 4. 点检路线明细
  sync_seq('HAIWEI.SEQ_DMS_INSPECT_RTE_DTL',        'HAIWEI.DMS_INSPECT_ROUTE_DETAIL',      'ROUTE_DETAIL_ID');
  -- 4A. 巡检工单明细
  sync_seq('HAIWEI.SEQ_DMS_INSP_INST_DETAIL',       'HAIWEI.DMS_INSPECT_INSTANCE_DETAIL',   'INSTANCE_DETAIL_ID');
  -- 4B. 巡检明细项目
  sync_seq('HAIWEI.SEQ_DMS_INSP_DET_PROJ',          'HAIWEI.DMS_INSPECT_INST_DET_PROJ',     'INSTANCE_DETAIL_PROJECT_ID');
  -- 5. 润滑标准
  sync_seq('HAIWEI.SEQ_DMS_LUBE_STD',               'HAIWEI.DMS_BASE_LUBE_STANDARD',        'LUBE_STANDARD_ID');
  -- 6. 润滑部位
  sync_seq('HAIWEI.SEQ_DMS_LUBE_STATION',           'HAIWEI.DMS_BASE_LUBE_STATION',         'LUBE_STATION_ID');
  -- 7. 保养项目
  sync_seq('HAIWEI.SEQ_DMS_MAINT_PROJ',             'HAIWEI.DMS_BASE_MAINT_PROJECT',        'MAINT_PROJECT_ID');
  -- 8. 保养标准
  sync_seq('HAIWEI.SEQ_DMS_MAINT_STD',              'HAIWEI.DMS_BASE_MAINT_STANDARD',       'MAINT_STANDARD_ID');
  -- 9. 保养部位
  sync_seq('HAIWEI.SEQ_DMS_MAINT_STATION',          'HAIWEI.DMS_BASE_MAINT_STATION',        'MAINT_STATION_ID');
  -- 10. 停机原因
  sync_seq('HAIWEI.SEQ_DMS_SHUT_REASON',            'HAIWEI.DMS_BASE_SHUT_REASON',          'SHUT_REASON_ID');
  -- 11. 停机类型
  sync_seq('HAIWEI.SEQ_DMS_SHUT_TYPE',              'HAIWEI.DMS_BASE_SHUT_TYPE',            'SHUT_TYPE_ID');
  -- 12. 故障工单
  sync_seq('HAIWEI.SEQ_DMS_BILLS_FAULT_INSTANCE',   'HAIWEI.DMS_BILLS_FAULT_INSTANCE',      'REPAIR_INSTANCE_ID');
  -- 13. 故障工单活动
  sync_seq('HAIWEI.SEQ_FAULT_INSTANCE_ACTIVITY',    'HAIWEI.DMS_FAULT_INSTANCE_ACTIVITY',   'INSTANCE_ACTIVITY_ID');
  -- 14. 保养工单
 sync_seq('HAIWEI.SEQ_DMS_MAINT_INST',             'HAIWEI.DMS_BILLS_MAINT_INSTANCE',      'MAINT_INSTANCE_ID');
  -- 14A. 保养工单活动
  sync_seq('HAIWEI.SEQ_DMS_MAINT_INST_ACTIVITY',    'HAIWEI.DMS_MAINT_INSTANCE_ACTIVITY',    'INSTANCE_ACTIVITY_ID');
  -- 14A. 巡检工单
  sync_seq('HAIWEI.SEQ_DMS_BILLS_INSPECT_INSTANCE', 'HAIWEI.DMS_BILLS_INSPECT_INSTANCE',     'INSPECT_INSTANCE_ID');
  -- 14B. 巡检工单活动
  sync_seq('HAIWEI.SEQ_DMS_BILLS_INSPECT_ACTIVITY', 'HAIWEI.DMS_BILLS_INS_INST_ACT',          'INSTANCE_ACTIVITY_ID');
   -- 15. 保养工单明细
  sync_seq('HAIWEI.SEQ_DMS_MAINT_DETAIL',           'HAIWEI.DMS_BILLS_MAINT_DETAIL',        'BILLS_MAINT_DETAIL_ID');
  -- 16. 保养明细项目
  sync_seq('HAIWEI.SEQ_DMS_MAINT_DTL_PROJ',         'HAIWEI.DMS_BILLS_MAINT_DETAIL_PROJECT','MAINT_DETAIL_PROJECT_ID');
  -- 17. 派工计划
  sync_seq('HAIWEI.SEQ_DMS_DISPATCH_PLAN',          'HAIWEI.DMS_DISPATCH_PLAN',             'PLAN_ID');
  -- 18. 派工成员
  sync_seq('HAIWEI.SEQ_DMS_DISPATCH_MEMBER',        'HAIWEI.DMS_DISPATCH_MEMBER',           'MEMBER_ID');
  -- 19. 维修知识库
  sync_seq('HAIWEI.SEQ_DMS_KNOWLEDGE_REPAIR',       'HAIWEI.DMS_KNOWLEDGE_REPAIR',          'KNOWLEDGE_REPAIR_ID');
  -- 20. 点检计划
  sync_seq('HAIWEI.SEQ_DMS_PLAN_INSPECT',           'HAIWEI.DMS_PLAN_INSPECT',              'PLAN_INSPECT_ID');
  -- 21. 保养计划
  sync_seq('HAIWEI.SEQ_DMS_PLAN_MAINT',             'HAIWEI.DMS_PLAN_MAINT',                'PLAN_MAINT_ID');
  -- 22. 保养计划明细
  sync_seq('HAIWEI.SEQ_DMS_PLAN_MAINT_DETAIL',      'HAIWEI.DMS_PLAN_MAINT_DETAIL',         'PLAN_MAINT_DETAIL_ID');
  -- 23. 检修计划
  sync_seq('HAIWEI.SEQ_DMS_PLAN_REPAIR',            'HAIWEI.DMS_PLAN_REPAIR',               'PLAN_REPAIR_ID');
  -- 24. 维修物料
  sync_seq('HAIWEI.SEQ_DMS_REPAIR_MATERIAL',        'HAIWEI.DMS_REPAIR_MATERIAL',           'MATERIAL_ID');
  -- 25. 维修项目
  sync_seq('HAIWEI.SEQ_DMS_REPAIR_PROJECT',         'HAIWEI.DMS_REPAIR_PROJECT',            'PROJECT_ID');
  -- 26. 维修记录
  sync_seq('HAIWEI.SEQ_DMS_REPAIR_RECORD',          'HAIWEI.DMS_REPAIR_RECORD',             'RECORD_ID');
  -- 27. 维修工单
  sync_seq('HAIWEI.SEQ_DMS_REPAIR_WORK_ORDER',      'HAIWEI.DMS_REPAIR_WORK_ORDER',         'WORK_ORDER_ID');
  -- 28. 停机记录
  sync_seq('HAIWEI.SEQ_DMS_REC_SHUT_DOWN',          'HAIWEI.DMS_RECORD_SHUT_DOWN',          'RECORD_SHUT_DOWN_ID');
  -- 29. 附件信息
  sync_seq('HAIWEI.SEQ_MES_BASE_ATTACH_INFO',       'HAIWEI.DMS_BASE_ATTACH_INFO',          'ATTACH_ID');

  -- 为所有序列授权 + 创建公共同义词（保证跨 schema 应用可用）
  grant_and_synonym('SEQ_DMS_INSPECT_PROJ');
  grant_and_synonym('SEQ_DMS_INSPECT_ROUTE');
  grant_and_synonym('SEQ_DMS_INSPECT_STD');
  grant_and_synonym('SEQ_DMS_INSPECT_RTE_DTL');
  grant_and_synonym('SEQ_DMS_INSP_INST_DETAIL');
  grant_and_synonym('SEQ_DMS_INSP_DET_PROJ');
  grant_and_synonym('SEQ_DMS_LUBE_STD');
  grant_and_synonym('SEQ_DMS_LUBE_STATION');
  grant_and_synonym('SEQ_DMS_MAINT_PROJ');
  grant_and_synonym('SEQ_DMS_MAINT_STD');
  grant_and_synonym('SEQ_DMS_MAINT_STATION');
  grant_and_synonym('SEQ_DMS_SHUT_REASON');
  grant_and_synonym('SEQ_DMS_SHUT_TYPE');
  grant_and_synonym('SEQ_DMS_BILLS_FAULT_INSTANCE');
  grant_and_synonym('SEQ_FAULT_INSTANCE_ACTIVITY');
  grant_and_synonym('SEQ_DMS_BILLS_INSPECT_INSTANCE');
  grant_and_synonym('SEQ_DMS_BILLS_INSPECT_ACTIVITY');
  grant_and_synonym('SEQ_DMS_MAINT_INST');
  grant_and_synonym('SEQ_DMS_MAINT_INST_ACTIVITY');
  grant_and_synonym('SEQ_DMS_MAINT_DETAIL');
  grant_and_synonym('SEQ_DMS_MAINT_DTL_PROJ');
  grant_and_synonym('SEQ_DMS_DISPATCH_PLAN');
  grant_and_synonym('SEQ_DMS_DISPATCH_MEMBER');
  grant_and_synonym('SEQ_DMS_KNOWLEDGE_REPAIR');
  grant_and_synonym('SEQ_DMS_PLAN_INSPECT');
  grant_and_synonym('SEQ_DMS_PLAN_MAINT');
  grant_and_synonym('SEQ_DMS_PLAN_MAINT_DETAIL');
  grant_and_synonym('SEQ_DMS_PLAN_REPAIR');
  grant_and_synonym('SEQ_DMS_REPAIR_MATERIAL');
  grant_and_synonym('SEQ_DMS_REPAIR_PROJECT');
  grant_and_synonym('SEQ_DMS_REPAIR_RECORD');
  grant_and_synonym('SEQ_DMS_REPAIR_WORK_ORDER');
  grant_and_synonym('SEQ_DMS_REC_SHUT_DOWN');
  grant_and_synonym('SEQ_MES_BASE_ATTACH_INFO');
END;
/

-- ============================================
-- 执行完成提示
-- ============================================
-- 共创建 34 个序列，已按 MAX(主键)+1 对齐，已授权+同义词
-- 验证:
-- SELECT SEQUENCE_NAME, LAST_NUMBER FROM ALL_SEQUENCES WHERE SEQUENCE_OWNER='HAIWEI' AND (SEQUENCE_NAME LIKE 'SEQ_DMS%' OR SEQUENCE_NAME LIKE 'SEQ_FAULT%');
