-- 支付风控：黑名单配置 + 风险命中记录（daxpay-plugin-risk）
-- 升级脚本：已部署库执行本文件；COMMENT 使用 PG 标准 COMMENT ON

-- ----------------------------
-- Table structure for pay_blacklist
-- ----------------------------
CREATE TABLE IF NOT EXISTS "public"."pay_blacklist" (
  "id" int8 NOT NULL,
  "type" varchar(32) COLLATE "pg_catalog"."default" NOT NULL,
  "value" varchar(128) COLLATE "pg_catalog"."default" NOT NULL,
  "channel" varchar(32) COLLATE "pg_catalog"."default",
  "channel_app_id" varchar(64) COLLATE "pg_catalog"."default",
  "status" varchar(16) COLLATE "pg_catalog"."default" NOT NULL,
  "reason" varchar(255) COLLATE "pg_catalog"."default",
  "expire_time" timestamptz(6),
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "creator" int8,
  "create_time" timestamptz(6),
  "last_modifier" int8,
  "last_modified_time" timestamptz(6),
  "version" int4 DEFAULT 0,
  "deleted" bool NOT NULL DEFAULT false
);

COMMENT ON TABLE "public"."pay_blacklist" IS '支付黑名单配置';
COMMENT ON COLUMN "public"."pay_blacklist"."id" IS '主键';
COMMENT ON COLUMN "public"."pay_blacklist"."type" IS '类型: ip/open_id';
COMMENT ON COLUMN "public"."pay_blacklist"."value" IS '名单值(IP或openId)';
COMMENT ON COLUMN "public"."pay_blacklist"."channel" IS '通道族(openId建议wechat/alipay, IP可空)';
COMMENT ON COLUMN "public"."pay_blacklist"."channel_app_id" IS '通道应用AppId(可选)';
COMMENT ON COLUMN "public"."pay_blacklist"."status" IS '状态: enable/disable';
COMMENT ON COLUMN "public"."pay_blacklist"."reason" IS '拉黑原因';
COMMENT ON COLUMN "public"."pay_blacklist"."expire_time" IS '过期时间(空=永久)';
COMMENT ON COLUMN "public"."pay_blacklist"."remark" IS '备注';
COMMENT ON COLUMN "public"."pay_blacklist"."creator" IS '创建者ID';
COMMENT ON COLUMN "public"."pay_blacklist"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."pay_blacklist"."last_modifier" IS '最后修改者ID';
COMMENT ON COLUMN "public"."pay_blacklist"."last_modified_time" IS '最后修改时间';
COMMENT ON COLUMN "public"."pay_blacklist"."version" IS '版本号';
COMMENT ON COLUMN "public"."pay_blacklist"."deleted" IS '删除标志';

ALTER TABLE "public"."pay_blacklist" DROP CONSTRAINT IF EXISTS "pay_blacklist_pkey";
ALTER TABLE "public"."pay_blacklist" ADD CONSTRAINT "pay_blacklist_pkey" PRIMARY KEY ("id");

CREATE INDEX IF NOT EXISTS "idx_pay_blacklist_type_value" ON "public"."pay_blacklist" ("type", "value");
CREATE INDEX IF NOT EXISTS "idx_pay_blacklist_status_expire" ON "public"."pay_blacklist" ("status", "expire_time");

-- ----------------------------
-- Table structure for pay_risk_hit
-- ----------------------------
CREATE TABLE IF NOT EXISTS "public"."pay_risk_hit" (
  "id" int8 NOT NULL,
  "phase" varchar(16) COLLATE "pg_catalog"."default" NOT NULL,
  "hit_type" varchar(32) COLLATE "pg_catalog"."default" NOT NULL,
  "hit_value" varchar(128) COLLATE "pg_catalog"."default" NOT NULL,
  "blacklist_id" int8,
  "mch_no" varchar(32) COLLATE "pg_catalog"."default",
  "app_id" varchar(32) COLLATE "pg_catalog"."default",
  "trade_no" varchar(64) COLLATE "pg_catalog"."default",
  "order_no" varchar(64) COLLATE "pg_catalog"."default",
  "biz_order_no" varchar(100) COLLATE "pg_catalog"."default",
  "trade_type" varchar(32) COLLATE "pg_catalog"."default",
  "method" varchar(32) COLLATE "pg_catalog"."default",
  "product" varchar(32) COLLATE "pg_catalog"."default",
  "channel" varchar(32) COLLATE "pg_catalog"."default",
  "client_ip" varchar(64) COLLATE "pg_catalog"."default",
  "openid" varchar(128) COLLATE "pg_catalog"."default",
  "buyer_id" varchar(128) COLLATE "pg_catalog"."default",
  "scene" varchar(32) COLLATE "pg_catalog"."default",
  "handle_status" varchar(32) COLLATE "pg_catalog"."default" NOT NULL DEFAULT 'pending',
  "handle_remark" varchar(500) COLLATE "pg_catalog"."default",
  "handle_user_id" int8,
  "handle_time" timestamptz(6),
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "creator" int8,
  "create_time" timestamptz(6),
  "last_modifier" int8,
  "last_modified_time" timestamptz(6),
  "version" int4 DEFAULT 0,
  "deleted" bool NOT NULL DEFAULT false
);

COMMENT ON TABLE "public"."pay_risk_hit" IS '支付风险命中记录';
COMMENT ON COLUMN "public"."pay_risk_hit"."id" IS '主键';
COMMENT ON COLUMN "public"."pay_risk_hit"."phase" IS '阶段: before_pay/after_pay';
COMMENT ON COLUMN "public"."pay_risk_hit"."hit_type" IS '命中类型: ip/open_id';
COMMENT ON COLUMN "public"."pay_risk_hit"."hit_value" IS '命中值快照';
COMMENT ON COLUMN "public"."pay_risk_hit"."blacklist_id" IS '关联黑名单ID';
COMMENT ON COLUMN "public"."pay_risk_hit"."mch_no" IS '商户号';
COMMENT ON COLUMN "public"."pay_risk_hit"."app_id" IS '应用号';
COMMENT ON COLUMN "public"."pay_risk_hit"."trade_no" IS '平台交易号';
COMMENT ON COLUMN "public"."pay_risk_hit"."order_no" IS '容器单号';
COMMENT ON COLUMN "public"."pay_risk_hit"."biz_order_no" IS '商户业务单号';
COMMENT ON COLUMN "public"."pay_risk_hit"."trade_type" IS '交易类型';
COMMENT ON COLUMN "public"."pay_risk_hit"."method" IS '支付方式';
COMMENT ON COLUMN "public"."pay_risk_hit"."product" IS '支付产品';
COMMENT ON COLUMN "public"."pay_risk_hit"."channel" IS '支付通道';
COMMENT ON COLUMN "public"."pay_risk_hit"."client_ip" IS '客户端IP快照';
COMMENT ON COLUMN "public"."pay_risk_hit"."openid" IS '下单openId快照';
COMMENT ON COLUMN "public"."pay_risk_hit"."buyer_id" IS '通道buyerId快照';
COMMENT ON COLUMN "public"."pay_risk_hit"."scene" IS '来源场景: api/gateway/code/manual';
COMMENT ON COLUMN "public"."pay_risk_hit"."handle_status" IS '处理状态: pending/ignored/added_blacklist/merchant_disabled/other';
COMMENT ON COLUMN "public"."pay_risk_hit"."handle_remark" IS '处理说明';
COMMENT ON COLUMN "public"."pay_risk_hit"."handle_user_id" IS '处理人ID';
COMMENT ON COLUMN "public"."pay_risk_hit"."handle_time" IS '处理时间';
COMMENT ON COLUMN "public"."pay_risk_hit"."remark" IS '备注';
COMMENT ON COLUMN "public"."pay_risk_hit"."creator" IS '创建者ID';
COMMENT ON COLUMN "public"."pay_risk_hit"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."pay_risk_hit"."last_modifier" IS '最后修改者ID';
COMMENT ON COLUMN "public"."pay_risk_hit"."last_modified_time" IS '最后修改时间';
COMMENT ON COLUMN "public"."pay_risk_hit"."version" IS '版本号';
COMMENT ON COLUMN "public"."pay_risk_hit"."deleted" IS '删除标志';

ALTER TABLE "public"."pay_risk_hit" DROP CONSTRAINT IF EXISTS "pay_risk_hit_pkey";
ALTER TABLE "public"."pay_risk_hit" ADD CONSTRAINT "pay_risk_hit_pkey" PRIMARY KEY ("id");

CREATE INDEX IF NOT EXISTS "idx_pay_risk_hit_handle_time" ON "public"."pay_risk_hit" ("handle_status", "create_time");
CREATE INDEX IF NOT EXISTS "idx_pay_risk_hit_type_value" ON "public"."pay_risk_hit" ("hit_type", "hit_value");
CREATE INDEX IF NOT EXISTS "idx_pay_risk_hit_mch_no" ON "public"."pay_risk_hit" ("mch_no");
CREATE INDEX IF NOT EXISTS "idx_pay_risk_hit_trade_no" ON "public"."pay_risk_hit" ("trade_no");
CREATE INDEX IF NOT EXISTS "idx_pay_risk_hit_order_no" ON "public"."pay_risk_hit" ("order_no");

-- ----------------------------
-- 敏感词词库 + 命中审计（capability-sensitive-word）
-- ----------------------------
CREATE TABLE IF NOT EXISTS "public"."system_sensitive_word" (
  "id" int8 NOT NULL,
  "word" varchar(64) COLLATE "pg_catalog"."default" NOT NULL,
  "category" varchar(32) COLLATE "pg_catalog"."default",
  "match_mode" varchar(16) COLLATE "pg_catalog"."default" NOT NULL DEFAULT 'contains',
  "level" varchar(16) COLLATE "pg_catalog"."default" DEFAULT 'reject',
  "status" varchar(16) COLLATE "pg_catalog"."default" NOT NULL,
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "creator" int8,
  "create_time" timestamptz(6),
  "last_modifier" int8,
  "last_modified_time" timestamptz(6),
  "version" int4 DEFAULT 0,
  "deleted" bool NOT NULL DEFAULT false
);

COMMENT ON TABLE "public"."system_sensitive_word" IS '敏感词词库';
COMMENT ON COLUMN "public"."system_sensitive_word"."id" IS '主键';
COMMENT ON COLUMN "public"."system_sensitive_word"."word" IS '敏感词原文(建议简体)';
COMMENT ON COLUMN "public"."system_sensitive_word"."category" IS '分类: politic/porn/violence/ad/custom';
COMMENT ON COLUMN "public"."system_sensitive_word"."match_mode" IS '匹配模式: contains/exact';
COMMENT ON COLUMN "public"."system_sensitive_word"."level" IS '处理级别: reject/warn';
COMMENT ON COLUMN "public"."system_sensitive_word"."status" IS '状态: enable/disable';
COMMENT ON COLUMN "public"."system_sensitive_word"."remark" IS '备注';
COMMENT ON COLUMN "public"."system_sensitive_word"."creator" IS '创建者ID';
COMMENT ON COLUMN "public"."system_sensitive_word"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."system_sensitive_word"."last_modifier" IS '最后修改者ID';
COMMENT ON COLUMN "public"."system_sensitive_word"."last_modified_time" IS '最后修改时间';
COMMENT ON COLUMN "public"."system_sensitive_word"."version" IS '版本号';
COMMENT ON COLUMN "public"."system_sensitive_word"."deleted" IS '删除标志';

ALTER TABLE "public"."system_sensitive_word" DROP CONSTRAINT IF EXISTS "system_sensitive_word_pkey";
ALTER TABLE "public"."system_sensitive_word" ADD CONSTRAINT "system_sensitive_word_pkey" PRIMARY KEY ("id");

CREATE UNIQUE INDEX IF NOT EXISTS "uk_system_sensitive_word_word" ON "public"."system_sensitive_word" ("word") WHERE deleted = false;
CREATE INDEX IF NOT EXISTS "idx_system_sensitive_word_status" ON "public"."system_sensitive_word" ("status");

CREATE TABLE IF NOT EXISTS "public"."system_sensitive_word_hit" (
  "id" int8 NOT NULL,
  "word_id" int8,
  "hit_word" varchar(64) COLLATE "pg_catalog"."default" NOT NULL,
  "content_preview" varchar(200) COLLATE "pg_catalog"."default",
  "scene" varchar(32) COLLATE "pg_catalog"."default" NOT NULL,
  "source" varchar(32) COLLATE "pg_catalog"."default",
  "mch_no" varchar(32) COLLATE "pg_catalog"."default",
  "app_id" varchar(50) COLLATE "pg_catalog"."default",
  "operator_id" int8,
  "client_ip" varchar(64) COLLATE "pg_catalog"."default",
  "request_path" varchar(255) COLLATE "pg_catalog"."default",
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "creator" int8,
  "create_time" timestamptz(6),
  "last_modifier" int8,
  "last_modified_time" timestamptz(6),
  "version" int4 DEFAULT 0,
  "deleted" bool NOT NULL DEFAULT false
);

COMMENT ON TABLE "public"."system_sensitive_word_hit" IS '敏感词命中记录';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."id" IS '主键';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."word_id" IS '关联词库ID';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."hit_word" IS '命中词快照';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."content_preview" IS '原文摘要';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."scene" IS '场景: pay_title/goods_name/...';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."source" IS '来源: admin/merchant/unipay/app_admin';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."mch_no" IS '商户号';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."app_id" IS '应用号';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."operator_id" IS '操作人用户ID';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."client_ip" IS '客户端IP';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."request_path" IS '请求路径';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."remark" IS '备注';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."creator" IS '创建者ID';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."last_modifier" IS '最后修改者ID';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."last_modified_time" IS '最后修改时间';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."version" IS '版本号';
COMMENT ON COLUMN "public"."system_sensitive_word_hit"."deleted" IS '删除标志';

ALTER TABLE "public"."system_sensitive_word_hit" DROP CONSTRAINT IF EXISTS "system_sensitive_word_hit_pkey";
ALTER TABLE "public"."system_sensitive_word_hit" ADD CONSTRAINT "system_sensitive_word_hit_pkey" PRIMARY KEY ("id");

CREATE INDEX IF NOT EXISTS "idx_system_sensitive_word_hit_time" ON "public"."system_sensitive_word_hit" ("create_time");
CREATE INDEX IF NOT EXISTS "idx_system_sensitive_word_hit_word" ON "public"."system_sensitive_word_hit" ("hit_word");
CREATE INDEX IF NOT EXISTS "idx_system_sensitive_word_hit_mch" ON "public"."system_sensitive_word_hit" ("mch_no", "create_time");
CREATE INDEX IF NOT EXISTS "idx_system_sensitive_word_hit_scene" ON "public"."system_sensitive_word_hit" ("scene");
