-- XXL-JOB v2.4.0 适配 Kingbase V8 Oracle 模式
-- 特点：所有COMMENT单独创建，所有索引单独创建

-- 序列创建：用于实现自增主键
CREATE SEQUENCE SEQ_XXL_JOB_INFO_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_LOG_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_LOG_REPORT_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_LOGGLUE_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_REGISTRY_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_GROUP_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

CREATE SEQUENCE SEQ_XXL_JOB_USER_ID
START WITH 1
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
NOCACHE;

-- 1. 任务信息表（仅表结构，无COMMENT和索引）
CREATE TABLE xxl_job_info (
  id                        NUMBER(11)      NOT NULL,
  job_group                 NUMBER(11)      NOT NULL,
  job_desc                  VARCHAR(255)    NOT NULL,
  add_time                  TIMESTAMP       DEFAULT NULL,
  update_time               TIMESTAMP       DEFAULT NULL,
  author                    VARCHAR(64)     DEFAULT NULL,
  alarm_email               VARCHAR(255)    DEFAULT NULL,
  schedule_type             VARCHAR(50)     NOT NULL DEFAULT 'NONE',
  schedule_conf             VARCHAR(128)    DEFAULT NULL,
  misfire_strategy          VARCHAR(50)     NOT NULL DEFAULT 'DO_NOTHING',
  executor_route_strategy   VARCHAR(50)     DEFAULT NULL,
  executor_handler          VARCHAR(255)    DEFAULT NULL,
  executor_param            VARCHAR(512)    DEFAULT NULL,
  executor_block_strategy   VARCHAR(50)     DEFAULT NULL,
  executor_timeout          NUMBER(11)      NOT NULL DEFAULT 0,
  executor_fail_retry_count NUMBER(11)      NOT NULL DEFAULT 0,
  glue_type                 VARCHAR(50)     NOT NULL,
  glue_source               CLOB,
  glue_remark               VARCHAR(128)    DEFAULT NULL,
  glue_updatetime           TIMESTAMP       DEFAULT NULL,
  child_jobid               VARCHAR(255)    DEFAULT NULL,
  trigger_status            NUMBER(4)       NOT NULL DEFAULT 0,
  trigger_last_time         NUMBER(13)      NOT NULL DEFAULT 0,
  trigger_next_time         NUMBER(13)      NOT NULL DEFAULT 0,
  PRIMARY KEY (id)
);

-- 2. 任务日志表（仅表结构）
CREATE TABLE xxl_job_log (
  id                        NUMBER(20)      NOT NULL,
  job_group                 NUMBER(11)      NOT NULL,
  job_id                    NUMBER(11)      NOT NULL,
  executor_address          VARCHAR(255)    DEFAULT NULL,
  executor_handler          VARCHAR(255)    DEFAULT NULL,
  executor_param            VARCHAR(512)    DEFAULT NULL,
  executor_sharding_param   VARCHAR(20)     DEFAULT NULL,
  executor_fail_retry_count NUMBER(11)      NOT NULL DEFAULT 0,
  trigger_time              TIMESTAMP       DEFAULT NULL,
  trigger_code              NUMBER(11)      NOT NULL,
  trigger_msg               CLOB,
  handle_time               TIMESTAMP       DEFAULT NULL,
  handle_code               NUMBER(11)      NOT NULL,
  handle_msg                CLOB,
  alarm_status              NUMBER(4)       NOT NULL DEFAULT 0,
  PRIMARY KEY (id)
);

-- 3. 日志报表表（仅表结构）
CREATE TABLE xxl_job_log_report (
  id            NUMBER(11)      NOT NULL,
  trigger_day   TIMESTAMP       DEFAULT NULL,
  running_count NUMBER(11)      NOT NULL DEFAULT 0,
  suc_count     NUMBER(11)      NOT NULL DEFAULT 0,
  fail_count    NUMBER(11)      NOT NULL DEFAULT 0,
  update_time   TIMESTAMP       DEFAULT NULL,
  PRIMARY KEY (id)
);

-- 4. GLUE日志表（仅表结构）
CREATE TABLE xxl_job_logglue (
  id          NUMBER(11)      NOT NULL,
  job_id      NUMBER(11)      NOT NULL,
  glue_type   VARCHAR(50)     DEFAULT NULL,
  glue_source CLOB,
  glue_remark VARCHAR(128)    NOT NULL,
  add_time    TIMESTAMP       DEFAULT NULL,
  update_time TIMESTAMP       DEFAULT NULL,
  PRIMARY KEY (id)
);

-- 5. 注册信息表（仅表结构）
CREATE TABLE xxl_job_registry (
  id             NUMBER(11)      NOT NULL,
  registry_group VARCHAR(50)     NOT NULL,
  registry_key   VARCHAR(255)    NOT NULL,
  registry_value VARCHAR(255)    NOT NULL,
  update_time    TIMESTAMP       DEFAULT NULL,
  PRIMARY KEY (id)
);

-- 6. 执行器组表（仅表结构）
CREATE TABLE xxl_job_group (
  id           NUMBER(11)      NOT NULL,
  app_name     VARCHAR(64)     NOT NULL,
  title        VARCHAR(12)     NOT NULL,
  address_type NUMBER(4)       NOT NULL DEFAULT 0,
  address_list CLOB,
  update_time  TIMESTAMP       DEFAULT NULL,
  PRIMARY KEY (id)
);

-- 7. 用户表（仅表结构）
CREATE TABLE xxl_job_user (
  id         NUMBER(11)      NOT NULL,
  username   VARCHAR(50)     NOT NULL,
  password   VARCHAR(50)     NOT NULL,
  role       NUMBER(4)       NOT NULL,
  permission VARCHAR(255)    DEFAULT NULL,
  PRIMARY KEY (id)
);

-- 8. 锁表（仅表结构）
CREATE TABLE xxl_job_lock (
  lock_name VARCHAR(50) NOT NULL,
  PRIMARY KEY (lock_name)
);

-- 触发器创建：实现主键自增
CREATE OR REPLACE TRIGGER TRG_XXL_JOB_INFO_ID
BEFORE INSERT ON xxl_job_info
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_INFO_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_LOG_ID
BEFORE INSERT ON xxl_job_log
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_LOG_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_LOG_REPORT_ID
BEFORE INSERT ON xxl_job_log_report
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_LOG_REPORT_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_LOGGLUE_ID
BEFORE INSERT ON xxl_job_logglue
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_LOGGLUE_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_REGISTRY_ID
BEFORE INSERT ON xxl_job_registry
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_REGISTRY_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_GROUP_ID
BEFORE INSERT ON xxl_job_group
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_GROUP_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

CREATE OR REPLACE TRIGGER TRG_XXL_JOB_USER_ID
BEFORE INSERT ON xxl_job_user
FOR EACH ROW
BEGIN
  IF :NEW.id IS NULL THEN
    SELECT SEQ_XXL_JOB_USER_ID.NEXTVAL INTO :NEW.id FROM DUAL;
  END IF;
END;

-- ########################### 单独创建表和列的COMMENT ###########################
-- 1. xxl_job_info 表注释
COMMENT ON TABLE xxl_job_info IS '任务信息表';
COMMENT ON COLUMN xxl_job_info.job_group IS '执行器主键ID';
COMMENT ON COLUMN xxl_job_info.job_desc IS '任务描述';
COMMENT ON COLUMN xxl_job_info.add_time IS '创建时间';
COMMENT ON COLUMN xxl_job_info.update_time IS '更新时间';
COMMENT ON COLUMN xxl_job_info.author IS '作者';
COMMENT ON COLUMN xxl_job_info.alarm_email IS '报警邮件';
COMMENT ON COLUMN xxl_job_info.schedule_type IS '调度类型';
COMMENT ON COLUMN xxl_job_info.schedule_conf IS '调度配置，值含义取决于调度类型';
COMMENT ON COLUMN xxl_job_info.misfire_strategy IS '调度过期策略';
COMMENT ON COLUMN xxl_job_info.executor_route_strategy IS '执行器路由策略';
COMMENT ON COLUMN xxl_job_info.executor_handler IS '执行器任务handler';
COMMENT ON COLUMN xxl_job_info.executor_param IS '执行器任务参数';
COMMENT ON COLUMN xxl_job_info.executor_block_strategy IS '阻塞处理策略';
COMMENT ON COLUMN xxl_job_info.executor_timeout IS '任务执行超时时间，单位秒';
COMMENT ON COLUMN xxl_job_info.executor_fail_retry_count IS '失败重试次数';
COMMENT ON COLUMN xxl_job_info.glue_type IS 'GLUE类型';
COMMENT ON COLUMN xxl_job_info.glue_source IS 'GLUE源代码';
COMMENT ON COLUMN xxl_job_info.glue_remark IS 'GLUE备注';
COMMENT ON COLUMN xxl_job_info.glue_updatetime IS 'GLUE更新时间';
COMMENT ON COLUMN xxl_job_info.child_jobid IS '子任务ID，多个逗号分隔';
COMMENT ON COLUMN xxl_job_info.trigger_status IS '调度状态：0-停止，1-运行';
COMMENT ON COLUMN xxl_job_info.trigger_last_time IS '上次调度时间';
COMMENT ON COLUMN xxl_job_info.trigger_next_time IS '下次调度时间';

-- 2. xxl_job_log 表注释
COMMENT ON TABLE xxl_job_log IS '任务日志表';
COMMENT ON COLUMN xxl_job_log.job_group IS '执行器主键ID';
COMMENT ON COLUMN xxl_job_log.job_id IS '任务，主键ID';
COMMENT ON COLUMN xxl_job_log.executor_address IS '执行器地址，本次执行的地址';
COMMENT ON COLUMN xxl_job_log.executor_handler IS '执行器任务handler';
COMMENT ON COLUMN xxl_job_log.executor_param IS '执行器任务参数';
COMMENT ON COLUMN xxl_job_log.executor_sharding_param IS '执行器任务分片参数，格式如 1/2';
COMMENT ON COLUMN xxl_job_log.executor_fail_retry_count IS '失败重试次数';
COMMENT ON COLUMN xxl_job_log.trigger_time IS '调度-时间';
COMMENT ON COLUMN xxl_job_log.trigger_code IS '调度-结果';
COMMENT ON COLUMN xxl_job_log.trigger_msg IS '调度-日志';
COMMENT ON COLUMN xxl_job_log.handle_time IS '执行-时间';
COMMENT ON COLUMN xxl_job_log.handle_code IS '执行-状态';
COMMENT ON COLUMN xxl_job_log.handle_msg IS '执行-日志';
COMMENT ON COLUMN xxl_job_log.alarm_status IS '告警状态：0-默认、1-无需告警、2-告警成功、3-告警失败';

-- 3. xxl_job_log_report 表注释
COMMENT ON TABLE xxl_job_log_report IS '日志报表表';
COMMENT ON COLUMN xxl_job_log_report.trigger_day IS '调度-时间';
COMMENT ON COLUMN xxl_job_log_report.running_count IS '运行中-日志数量';
COMMENT ON COLUMN xxl_job_log_report.suc_count IS '执行成功-日志数量';
COMMENT ON COLUMN xxl_job_log_report.fail_count IS '执行失败-日志数量';
COMMENT ON COLUMN xxl_job_log_report.update_time IS '更新时间';

-- 4. xxl_job_logglue 表注释
COMMENT ON TABLE xxl_job_logglue IS 'GLUE日志表';
COMMENT ON COLUMN xxl_job_logglue.job_id IS '任务，主键ID';
COMMENT ON COLUMN xxl_job_logglue.glue_type IS 'GLUE类型';
COMMENT ON COLUMN xxl_job_logglue.glue_source IS 'GLUE源代码';
COMMENT ON COLUMN xxl_job_logglue.glue_remark IS 'GLUE备注';
COMMENT ON COLUMN xxl_job_logglue.add_time IS '创建时间';
COMMENT ON COLUMN xxl_job_logglue.update_time IS '更新时间';

-- 5. xxl_job_registry 表注释
COMMENT ON TABLE xxl_job_registry IS '注册信息表';
COMMENT ON COLUMN xxl_job_registry.registry_group IS '注册分组';
COMMENT ON COLUMN xxl_job_registry.registry_key IS '注册键';
COMMENT ON COLUMN xxl_job_registry.registry_value IS '注册值';
COMMENT ON COLUMN xxl_job_registry.update_time IS '更新时间';

-- 6. xxl_job_group 表注释
COMMENT ON TABLE xxl_job_group IS '执行器组表';
COMMENT ON COLUMN xxl_job_group.app_name IS '执行器AppName';
COMMENT ON COLUMN xxl_job_group.title IS '执行器名称';
COMMENT ON COLUMN xxl_job_group.address_type IS '执行器地址类型：0=自动注册、1=手动录入';
COMMENT ON COLUMN xxl_job_group.address_list IS '执行器地址列表，多地址逗号分隔';
COMMENT ON COLUMN xxl_job_group.update_time IS '更新时间';

-- 7. xxl_job_user 表注释
COMMENT ON TABLE xxl_job_user IS '用户表';
COMMENT ON COLUMN xxl_job_user.username IS '账号';
COMMENT ON COLUMN xxl_job_user.password IS '密码';
COMMENT ON COLUMN xxl_job_user.role IS '角色：0-普通用户、1-管理员';
COMMENT ON COLUMN xxl_job_user.permission IS '权限：执行器ID列表，多个逗号分割';

-- 8. xxl_job_lock 表注释
COMMENT ON TABLE xxl_job_lock IS '调度锁表';
COMMENT ON COLUMN xxl_job_lock.lock_name IS '锁名称';

-- ########################### 单独创建所有索引 ###########################
-- 1. xxl_job_log 表索引
CREATE INDEX I_trigger_time ON xxl_job_log(trigger_time);
CREATE INDEX I_handle_code ON xxl_job_log(handle_code);

-- 2. xxl_job_log_report 表唯一索引
CREATE UNIQUE INDEX UK_XXL_JOB_LOG_REPORT_TRIGGERDAY ON xxl_job_log_report(trigger_day);

-- 3. xxl_job_registry 表索引
CREATE INDEX I_g_k_v ON xxl_job_registry(registry_group, registry_key, registry_value);

-- 4. xxl_job_user 表唯一索引
CREATE UNIQUE INDEX UK_XXL_JOB_USER_USERNAME ON xxl_job_user(username);

-- ########################### 初始化数据 ###########################
INSERT INTO xxl_job_group(id, app_name, title, address_type, address_list, update_time)
VALUES (1, 'xxl-job-executor-sample', '示例执行器', 0, NULL, TO_TIMESTAMP('2018-11-03 22:21:31', 'YYYY-MM-DD HH24:MI:SS'));

INSERT INTO xxl_job_info(id, job_group, job_desc, add_time, update_time, author, alarm_email,
                         schedule_type, schedule_conf, misfire_strategy, executor_route_strategy,
                         executor_handler, executor_param, executor_block_strategy, executor_timeout,
                         executor_fail_retry_count, glue_type, glue_source, glue_remark, glue_updatetime, child_jobid)
VALUES (1, 1, '测试任务1', 
        TO_TIMESTAMP('2018-11-03 22:21:31', 'YYYY-MM-DD HH24:MI:SS'),
        TO_TIMESTAMP('2018-11-03 22:21:31', 'YYYY-MM-DD HH24:MI:SS'), 
        'XXL', '', 'CRON', '0 0 0 * * ? *', 'DO_NOTHING', 'FIRST',
        'demoJobHandler', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN', '', 
        'GLUE代码初始化', TO_TIMESTAMP('2018-11-03 22:21:31', 'YYYY-MM-DD HH24:MI:SS'), '');

INSERT INTO xxl_job_user(id, username, password, role, permission)
VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL);

INSERT INTO xxl_job_lock(lock_name)
VALUES ('schedule_lock');

COMMIT;
