# -*- coding: utf-8 -*-
"""充值记录事实表"""

from ..base_loader import BaseLoader


class TopupLoader(BaseLoader):
    """写入 fact_topup"""

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

        sql = """
        INSERT INTO billiards.fact_topup (
            store_id,
            topup_id,
            member_id,
            member_name,
            member_phone,
            card_id,
            card_type_name,
            pay_amount,
            consume_money,
            settle_status,
            settle_type,
            settle_name,
            settle_relate_id,
            pay_time,
            create_time,
            operator_id,
            operator_name,
            payment_method,
            refund_amount,
            cash_amount,
            card_amount,
            balance_amount,
            online_amount,
            rounding_amount,
            adjust_amount,
            goods_money,
            table_charge_money,
            service_money,
            coupon_amount,
            order_remark,
            raw_data
        )
        VALUES (
            %(store_id)s,
            %(topup_id)s,
            %(member_id)s,
            %(member_name)s,
            %(member_phone)s,
            %(card_id)s,
            %(card_type_name)s,
            %(pay_amount)s,
            %(consume_money)s,
            %(settle_status)s,
            %(settle_type)s,
            %(settle_name)s,
            %(settle_relate_id)s,
            %(pay_time)s,
            %(create_time)s,
            %(operator_id)s,
            %(operator_name)s,
            %(payment_method)s,
            %(refund_amount)s,
            %(cash_amount)s,
            %(card_amount)s,
            %(balance_amount)s,
            %(online_amount)s,
            %(rounding_amount)s,
            %(adjust_amount)s,
            %(goods_money)s,
            %(table_charge_money)s,
            %(service_money)s,
            %(coupon_amount)s,
            %(order_remark)s,
            %(raw_data)s
        )
        ON CONFLICT (store_id, topup_id) DO UPDATE SET
            member_id          = EXCLUDED.member_id,
            member_name        = EXCLUDED.member_name,
            member_phone       = EXCLUDED.member_phone,
            card_id            = EXCLUDED.card_id,
            card_type_name     = EXCLUDED.card_type_name,
            pay_amount         = EXCLUDED.pay_amount,
            consume_money      = EXCLUDED.consume_money,
            settle_status      = EXCLUDED.settle_status,
            settle_type        = EXCLUDED.settle_type,
            settle_name        = EXCLUDED.settle_name,
            settle_relate_id   = EXCLUDED.settle_relate_id,
            pay_time           = EXCLUDED.pay_time,
            create_time        = EXCLUDED.create_time,
            operator_id        = EXCLUDED.operator_id,
            operator_name      = EXCLUDED.operator_name,
            payment_method     = EXCLUDED.payment_method,
            refund_amount      = EXCLUDED.refund_amount,
            cash_amount        = EXCLUDED.cash_amount,
            card_amount        = EXCLUDED.card_amount,
            balance_amount     = EXCLUDED.balance_amount,
            online_amount      = EXCLUDED.online_amount,
            rounding_amount    = EXCLUDED.rounding_amount,
            adjust_amount      = EXCLUDED.adjust_amount,
            goods_money        = EXCLUDED.goods_money,
            table_charge_money = EXCLUDED.table_charge_money,
            service_money      = EXCLUDED.service_money,
            coupon_amount      = EXCLUDED.coupon_amount,
            order_remark       = EXCLUDED.order_remark,
            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)
