# -*- coding: utf-8 -*-
"""助教流水事实表"""

from ..base_loader import BaseLoader


class AssistantLedgerLoader(BaseLoader):
    """写入 fact_assistant_ledger"""

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

        sql = """
        INSERT INTO billiards.fact_assistant_ledger (
            store_id,
            ledger_id,
            assistant_no,
            assistant_name,
            nickname,
            level_name,
            table_name,
            ledger_unit_price,
            ledger_count,
            ledger_amount,
            projected_income,
            service_money,
            member_discount_amount,
            manual_discount_amount,
            coupon_deduct_money,
            order_trade_no,
            order_settle_id,
            operator_id,
            operator_name,
            assistant_team_id,
            assistant_level,
            site_table_id,
            order_assistant_id,
            site_assistant_id,
            user_id,
            ledger_start_time,
            ledger_end_time,
            start_use_time,
            last_use_time,
            income_seconds,
            real_use_seconds,
            is_trash,
            trash_reason,
            is_confirm,
            ledger_status,
            create_time,
            raw_data
        )
        VALUES (
            %(store_id)s,
            %(ledger_id)s,
            %(assistant_no)s,
            %(assistant_name)s,
            %(nickname)s,
            %(level_name)s,
            %(table_name)s,
            %(ledger_unit_price)s,
            %(ledger_count)s,
            %(ledger_amount)s,
            %(projected_income)s,
            %(service_money)s,
            %(member_discount_amount)s,
            %(manual_discount_amount)s,
            %(coupon_deduct_money)s,
            %(order_trade_no)s,
            %(order_settle_id)s,
            %(operator_id)s,
            %(operator_name)s,
            %(assistant_team_id)s,
            %(assistant_level)s,
            %(site_table_id)s,
            %(order_assistant_id)s,
            %(site_assistant_id)s,
            %(user_id)s,
            %(ledger_start_time)s,
            %(ledger_end_time)s,
            %(start_use_time)s,
            %(last_use_time)s,
            %(income_seconds)s,
            %(real_use_seconds)s,
            %(is_trash)s,
            %(trash_reason)s,
            %(is_confirm)s,
            %(ledger_status)s,
            %(create_time)s,
            %(raw_data)s
        )
        ON CONFLICT (store_id, ledger_id) DO UPDATE SET
            assistant_no            = EXCLUDED.assistant_no,
            assistant_name          = EXCLUDED.assistant_name,
            nickname                = EXCLUDED.nickname,
            level_name              = EXCLUDED.level_name,
            table_name              = EXCLUDED.table_name,
            ledger_unit_price       = EXCLUDED.ledger_unit_price,
            ledger_count            = EXCLUDED.ledger_count,
            ledger_amount           = EXCLUDED.ledger_amount,
            projected_income        = EXCLUDED.projected_income,
            service_money           = EXCLUDED.service_money,
            member_discount_amount  = EXCLUDED.member_discount_amount,
            manual_discount_amount  = EXCLUDED.manual_discount_amount,
            coupon_deduct_money     = EXCLUDED.coupon_deduct_money,
            order_trade_no          = EXCLUDED.order_trade_no,
            order_settle_id         = EXCLUDED.order_settle_id,
            operator_id             = EXCLUDED.operator_id,
            operator_name           = EXCLUDED.operator_name,
            assistant_team_id       = EXCLUDED.assistant_team_id,
            assistant_level         = EXCLUDED.assistant_level,
            site_table_id           = EXCLUDED.site_table_id,
            order_assistant_id      = EXCLUDED.order_assistant_id,
            site_assistant_id       = EXCLUDED.site_assistant_id,
            user_id                 = EXCLUDED.user_id,
            ledger_start_time       = EXCLUDED.ledger_start_time,
            ledger_end_time         = EXCLUDED.ledger_end_time,
            start_use_time          = EXCLUDED.start_use_time,
            last_use_time           = EXCLUDED.last_use_time,
            income_seconds          = EXCLUDED.income_seconds,
            real_use_seconds        = EXCLUDED.real_use_seconds,
            is_trash                = EXCLUDED.is_trash,
            trash_reason            = EXCLUDED.trash_reason,
            is_confirm              = EXCLUDED.is_confirm,
            ledger_status           = EXCLUDED.ledger_status,
            create_time             = EXCLUDED.create_time,
            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)
