-- AlterTable: Add contract_no column to contract_signing_tasks
-- 合同编号格式: accountSequence-yyyyMMddHHmm (例如: 10001-202512251003)

-- 1. 添加可空的 contract_no 列
ALTER TABLE "contract_signing_tasks" ADD COLUMN "contract_no" VARCHAR(30);

-- 2. 为现有记录生成合同编号（基于 account_sequence 和 created_at）
UPDATE "contract_signing_tasks"
SET "contract_no" = "account_sequence" || '-' || TO_CHAR("created_at", 'YYYYMMDDHH24MI')
WHERE "contract_no" IS NULL;

-- 3. 设置为 NOT NULL
ALTER TABLE "contract_signing_tasks" ALTER COLUMN "contract_no" SET NOT NULL;

-- 4. 添加唯一索引
CREATE UNIQUE INDEX "contract_signing_tasks_contract_no_key" ON "contract_signing_tasks"("contract_no");
