# -*- coding: utf-8 -*-
"""助教作废事实表"""

from ..base_loader import BaseLoader


class AssistantAbolishLoader(BaseLoader):
    """写入 fact_assistant_abolish"""

    def upsert_records(self, records: list) -> tuple:
        if not records:
            return (0, 0, 0)

        sql = """
        INSERT INTO billiards.fact_assistant_abolish (
            store_id,
            abolish_id,
            table_id,
            table_name,
            table_area_id,
            table_area,
            assistant_no,
            assistant_name,
            charge_minutes,
            abolish_amount,
            create_time,
            trash_reason,
            raw_data
        )
        VALUES (
            %(store_id)s,
            %(abolish_id)s,
            %(table_id)s,
            %(table_name)s,
            %(table_area_id)s,
            %(table_area)s,
            %(assistant_no)s,
            %(assistant_name)s,
            %(charge_minutes)s,
            %(abolish_amount)s,
            %(create_time)s,
            %(trash_reason)s,
            %(raw_data)s
        )
        ON CONFLICT (store_id, abolish_id) DO UPDATE SET
            table_id        = EXCLUDED.table_id,
            table_name      = EXCLUDED.table_name,
            table_area_id   = EXCLUDED.table_area_id,
            table_area      = EXCLUDED.table_area,
            assistant_no    = EXCLUDED.assistant_no,
            assistant_name  = EXCLUDED.assistant_name,
            charge_minutes  = EXCLUDED.charge_minutes,
            abolish_amount  = EXCLUDED.abolish_amount,
            create_time     = EXCLUDED.create_time,
            trash_reason    = EXCLUDED.trash_reason,
            raw_data        = EXCLUDED.raw_data,
            updated_at      = now()
        RETURNING (xmax = 0) AS inserted
        """

        inserted, updated = self.db.batch_upsert_with_returning(
            sql, records, page_size=self._batch_size()
        )
        return (inserted, updated, 0)
