-- CreateTable: 钉钉员工本地表
CREATE TABLE IF NOT EXISTS "platform_automation"."dingtalk_employees" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL DEFAULT '',
    "employee_number" TEXT NOT NULL DEFAULT '',
    "department" TEXT NOT NULL DEFAULT '',
    "position" TEXT NOT NULL DEFAULT '',
    "rank" TEXT NOT NULL DEFAULT '',
    "phone" TEXT NOT NULL DEFAULT '',
    "country" TEXT NOT NULL DEFAULT '',
    "work_place" TEXT NOT NULL DEFAULT '',
    "contract_company" TEXT NOT NULL DEFAULT '',
    "bank" TEXT NOT NULL DEFAULT '',
    "bank_card_last4" TEXT NOT NULL DEFAULT '',
    "monthly_standard_hours" TEXT NOT NULL DEFAULT '',
    "work_hour_limit" TEXT NOT NULL DEFAULT '',
    "join_date" TIMESTAMPTZ(3),
    "work_start_date" TIMESTAMPTZ(3),
    "status" TEXT NOT NULL DEFAULT '正常',
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "dingtalk_employees_pkey" PRIMARY KEY ("id")
);

-- CreateTable: SAP 采购同步记录
CREATE TABLE IF NOT EXISTS "platform_automation"."sap_purchase_sync_records" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "serial_number" TEXT NOT NULL,
    "form_instance_id" TEXT NOT NULL,
    "applicant_name" TEXT NOT NULL,
    "budget_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "sap_pr_number" TEXT,
    "sap_status" TEXT NOT NULL,
    "sap_messages" JSONB,
    "approver_chain" TEXT,
    "synced_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "sap_purchase_sync_records_pkey" PRIMARY KEY ("id")
);

-- CreateTable: SAP 采购申请缓存
CREATE TABLE IF NOT EXISTS "platform_automation"."sap_purchase_cache" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "serial_number" TEXT NOT NULL,
    "form_instance_id" TEXT NOT NULL,
    "sap_env" TEXT NOT NULL,
    "applicant_name" TEXT NOT NULL DEFAULT '',
    "applicant_dept" TEXT NOT NULL DEFAULT '',
    "budget_number" TEXT NOT NULL DEFAULT '',
    "budget_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "purchase_project_description" TEXT NOT NULL DEFAULT '',
    "application_date" TEXT NOT NULL DEFAULT '',
    "demand_category" TEXT NOT NULL DEFAULT '',
    "purchasing_company_name" TEXT NOT NULL DEFAULT '',
    "material_group" TEXT NOT NULL DEFAULT '',
    "form_data" JSONB NOT NULL DEFAULT '{}',
    "yida_modified_at" TEXT NOT NULL DEFAULT '',
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

    CONSTRAINT "sap_purchase_cache_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "dingtalk_employees_user_id_key" ON "platform_automation"."dingtalk_employees"("user_id");
CREATE INDEX IF NOT EXISTS "dingtalk_employees_status_idx" ON "platform_automation"."dingtalk_employees"("status");
CREATE INDEX IF NOT EXISTS "dingtalk_employees_name_idx" ON "platform_automation"."dingtalk_employees"("name");

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "sap_purchase_sync_records_serial_number_key" ON "platform_automation"."sap_purchase_sync_records"("serial_number");
CREATE INDEX IF NOT EXISTS "sap_purchase_sync_records_status_idx" ON "platform_automation"."sap_purchase_sync_records"("sap_status");
CREATE INDEX IF NOT EXISTS "sap_purchase_sync_records_synced_at_idx" ON "platform_automation"."sap_purchase_sync_records"("synced_at");

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "sap_purchase_cache_serial_number_key" ON "platform_automation"."sap_purchase_cache"("serial_number");
CREATE INDEX IF NOT EXISTS "sap_purchase_cache_env_idx" ON "platform_automation"."sap_purchase_cache"("sap_env");
CREATE INDEX IF NOT EXISTS "sap_purchase_cache_applicant_idx" ON "platform_automation"."sap_purchase_cache"("applicant_name");
