# -*- coding: utf-8 -*-
"""会员维度表加载器"""
from ..base_loader import BaseLoader

class MemberLoader(BaseLoader):
    """会员维度加载器"""
    
    def upsert_members(self, records: list, store_id: int) -> tuple:
        """加载会员数据"""
        if not records:
            return (0, 0, 0)
        
        sql = """
        INSERT INTO billiards.dim_member (
            store_id, member_id, member_name, phone, balance,
            status, register_time, raw_data
        )
        VALUES (
            %(store_id)s, %(member_id)s, %(member_name)s, %(phone)s, %(balance)s,
            %(status)s, %(register_time)s, %(raw_data)s
        )
        ON CONFLICT (store_id, member_id) DO UPDATE SET
            member_name = EXCLUDED.member_name,
            phone = EXCLUDED.phone,
            balance = EXCLUDED.balance,
            status = EXCLUDED.status,
            register_time = EXCLUDED.register_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)
