-- -----------------------------------
-- 新增账号锁定时长参数
-- -----------------------------------
INSERT INTO "BLADEX"."BLADE_PARAM"("ID","PARAM_NAME","PARAM_KEY","PARAM_VALUE","REMARK","CREATE_USER","CREATE_DEPT","CREATE_TIME","UPDATE_USER","UPDATE_TIME","STATUS","IS_DELETED")
VALUES(1123598819738675204,'账号锁定时长(分钟)','account.lockDuration','30','锁定时长',1123598821738675201,1123598813738675201,'2025-12-01 12:00:00.000000',1123598821738675201,'2025-12-01 12:00:00.000000',1,0);

-- ----------------------------
-- 限流规则表
-- ----------------------------
CREATE TABLE "BLADEX"."BLADE_RATE_LIMIT"
(
 "ID" BIGINT NOT NULL,
 "TENANT_ID" VARCHAR(12) DEFAULT '000000',
 "LIMIT_NAME" VARCHAR(128) NOT NULL,
 "LIMIT_PATTERN" VARCHAR(1000) NOT NULL,
 "REQUEST_METHOD" VARCHAR(16) DEFAULT 'ALL',
 "LIMIT_DIMENSION" VARCHAR(16) DEFAULT 'IP',
 "LIMIT_COUNT" INT DEFAULT 100,
 "LIMIT_PERIOD" INT DEFAULT 1,
 "TIME_UNIT" VARCHAR(16) DEFAULT 'MINUTES',
 "PRIORITY" INT DEFAULT 1,
 "REMARK" VARCHAR(255) NULL,
 "CREATE_USER" BIGINT NULL,
 "CREATE_DEPT" BIGINT NULL,
 "CREATE_TIME" DATETIME NULL,
 "UPDATE_USER" BIGINT NULL,
 "UPDATE_TIME" DATETIME NULL,
 "STATUS" INT DEFAULT 1,
 "IS_DELETED" INT DEFAULT 0
);

ALTER TABLE "BLADEX"."BLADE_RATE_LIMIT" ADD CONSTRAINT  PRIMARY KEY("ID") ;

COMMENT ON TABLE "BLADEX"."BLADE_RATE_LIMIT" IS '限流规则表';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."ID" IS '主键';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."TENANT_ID" IS '租户ID';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."LIMIT_NAME" IS '规则名称';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."LIMIT_PATTERN" IS 'URL匹配模式';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."REQUEST_METHOD" IS '请求方法';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."LIMIT_DIMENSION" IS '限流维度';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."LIMIT_COUNT" IS '请求阈值';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."LIMIT_PERIOD" IS '时间窗口';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."TIME_UNIT" IS '时间单位';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."PRIORITY" IS '优先级';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."REMARK" IS '备注';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."CREATE_USER" IS '创建人';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."CREATE_DEPT" IS '创建部门';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."CREATE_TIME" IS '创建时间';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."UPDATE_USER" IS '修改人';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."UPDATE_TIME" IS '修改时间';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."STATUS" IS '状态';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT"."IS_DELETED" IS '是否已删除';

-- ----------------------------
-- 限流触发日志表
-- ----------------------------
CREATE TABLE "BLADEX"."BLADE_RATE_LIMIT_LOG"
(
 "ID" BIGINT NOT NULL,
 "TENANT_ID" VARCHAR(12) DEFAULT '000000',
 "SERVICE_ID" VARCHAR(32) NULL,
 "SERVER_HOST" VARCHAR(255) NULL,
 "SERVER_IP" VARCHAR(255) NULL,
 "ENV" VARCHAR(255) NULL,
 "LIMIT_ID" BIGINT NULL,
 "LIMIT_NAME" VARCHAR(128) NULL,
 "LIMIT_DIMENSION" VARCHAR(16) NULL,
 "LIMIT_COUNT" INT NULL,
 "LIMIT_PERIOD" INT NULL,
 "TIME_UNIT" VARCHAR(16) NULL,
 "LIMIT_KEY" VARCHAR(255) NULL,
 "METHOD" VARCHAR(10) NULL,
 "REQUEST_URI" VARCHAR(255) NULL,
 "USER_AGENT" VARCHAR(1000) NULL,
 "REMOTE_IP" VARCHAR(255) NULL,
 "PARAMS" CLOB NULL,
 "CREATE_BY" VARCHAR(64) NULL,
 "CREATE_TIME" DATETIME NULL
);

ALTER TABLE "BLADEX"."BLADE_RATE_LIMIT_LOG" ADD CONSTRAINT  PRIMARY KEY("ID") ;

COMMENT ON TABLE "BLADEX"."BLADE_RATE_LIMIT_LOG" IS '限流触发日志表';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."ID" IS '编号';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."TENANT_ID" IS '租户ID';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."SERVICE_ID" IS '服务ID';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."SERVER_HOST" IS '服务器名';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."SERVER_IP" IS '服务器IP地址';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."ENV" IS '服务器环境';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_ID" IS '限流规则主键ID';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_NAME" IS '规则名称';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_DIMENSION" IS '限流维度';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_COUNT" IS '请求阈值';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_PERIOD" IS '时间窗口';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."TIME_UNIT" IS '时间单位';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."LIMIT_KEY" IS '限流计数key';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."METHOD" IS '操作方式';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."REQUEST_URI" IS '请求URI';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."USER_AGENT" IS '用户代理';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."REMOTE_IP" IS '操作IP地址';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."PARAMS" IS '操作提交的数据';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."CREATE_BY" IS '创建者';
COMMENT ON COLUMN "BLADEX"."BLADE_RATE_LIMIT_LOG"."CREATE_TIME" IS '创建时间';

-- ----------------------------
-- 限流管理菜单
-- ----------------------------
INSERT INTO "BLADEX"."BLADE_MENU"("ID","PARENT_ID","CODE","NAME","ALIAS","PATH","SOURCE","SORT","CATEGORY","ACTION","IS_OPEN","COMPONENT","REMARK","IS_DELETED") VALUES(1963598815738675321, 1123598815738675307, 'rate_limit', '限流管理', 'menu', '/authority/ratelimit', 'iconfont icon-APIjieru', 5, 1, 0, 1, '', NULL, 0);
INSERT INTO "BLADEX"."BLADE_MENU"("ID","PARENT_ID","CODE","NAME","ALIAS","PATH","SOURCE","SORT","CATEGORY","ACTION","IS_OPEN","COMPONENT","REMARK","IS_DELETED") VALUES(1963598815738675322, 1963598815738675321, 'rate_limit_add', '新增', 'add', '/authority/ratelimit/add', 'plus', 1, 2, 1, 1, '', NULL, 0);
INSERT INTO "BLADEX"."BLADE_MENU"("ID","PARENT_ID","CODE","NAME","ALIAS","PATH","SOURCE","SORT","CATEGORY","ACTION","IS_OPEN","COMPONENT","REMARK","IS_DELETED") VALUES(1963598815738675323, 1963598815738675321, 'rate_limit_edit', '修改', 'edit', '/authority/ratelimit/edit', 'form', 2, 2, 2, 1, '', NULL, 0);
INSERT INTO "BLADEX"."BLADE_MENU"("ID","PARENT_ID","CODE","NAME","ALIAS","PATH","SOURCE","SORT","CATEGORY","ACTION","IS_OPEN","COMPONENT","REMARK","IS_DELETED") VALUES(1963598815738675324, 1963598815738675321, 'rate_limit_delete', '删除', 'delete', '/api/blade-system/rate-limit/remove', 'delete', 3, 2, 3, 1, '', NULL, 0);
INSERT INTO "BLADEX"."BLADE_MENU"("ID","PARENT_ID","CODE","NAME","ALIAS","PATH","SOURCE","SORT","CATEGORY","ACTION","IS_OPEN","COMPONENT","REMARK","IS_DELETED") VALUES(1963598815738675325, 1963598815738675321, 'rate_limit_view', '查看', 'view', '/authority/ratelimit/view', 'file-text', 4, 2, 2, 1, '', NULL, 0);

-- ----------------------------
-- 客户端授权类型增加行为验证码模式
-- ----------------------------
UPDATE "BLADEX"."BLADE_CLIENT" SET "AUTHORIZED_GRANT_TYPES" = CONCAT("AUTHORIZED_GRANT_TYPES", ',behavior') WHERE "AUTHORIZED_GRANT_TYPES" NOT LIKE '%behavior%';
