-- Migration: 000166_training_manual_push -- Manual push (手动推送) from the config center push-management tab -- (2026-11 requirement: 和信智汇学院重要通知随时手动推送). -- tr_push_tasks stores one push intent per row: custom title/content/landing -- link, an audience selector (all / by dept / by post / explicit users) and a -- send time (now or scheduled). The training scheduler claims due rows with a -- compare-and-set (pending → sending → sent), resolves the audience at send -- time (so "全员" covers accounts created after scheduling) and fans the task -- out into tr_notifications rows (scene='manual_push', ref_id=task id) that -- flow through the existing WeCom outbox and 推送记录 views. Cancelled tasks -- never fan out. DO $$ BEGIN RAISE NOTICE '[Migration 000166] Creating tr_push_tasks for manual push...'; END $$; CREATE TABLE IF NOT EXISTS tr_push_tasks ( id VARCHAR(36) PRIMARY KEY, tenant_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL DEFAULT '', content TEXT NOT NULL DEFAULT '', link_url VARCHAR(512) NOT NULL DEFAULT '', audience_mode VARCHAR(16) NOT NULL DEFAULT 'all', dept_ids JSONB NOT NULL DEFAULT '[]', posts JSONB NOT NULL DEFAULT '[]', user_ids JSONB NOT NULL DEFAULT '[]', send_at TIMESTAMPTZ NOT NULL, status VARCHAR(16) NOT NULL DEFAULT 'pending', sent_count INT NOT NULL DEFAULT 0, sent_at TIMESTAMPTZ, created_by VARCHAR(36) NOT NULL DEFAULT '', created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, deleted_at TIMESTAMPTZ ); CREATE INDEX IF NOT EXISTS idx_tr_push_tasks_tenant ON tr_push_tasks(tenant_id); -- Due-task scan of the training scheduler tick (partial: pending rows only). CREATE INDEX IF NOT EXISTS idx_tr_push_tasks_due ON tr_push_tasks(send_at) WHERE status = 'pending' AND deleted_at IS NULL; COMMENT ON TABLE tr_push_tasks IS 'Manual push tasks (config center push management); scheduler fans due tasks out into tr_notifications'; COMMENT ON COLUMN tr_push_tasks.audience_mode IS 'Audience selector: all / dept / post / user'; COMMENT ON COLUMN tr_push_tasks.status IS 'pending (waiting or due) / sending (claimed) / sent / cancelled';