# -*- coding: utf-8 -*-
"""助教维度加载器"""

from ..base_loader import BaseLoader


class AssistantLoader(BaseLoader):
    """写入 dim_assistant"""

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

        sql = """
        INSERT INTO billiards.dim_assistant (
            store_id,
            assistant_id,
            assistant_no,
            nickname,
            real_name,
            gender,
            mobile,
            level,
            team_id,
            team_name,
            assistant_status,
            work_status,
            entry_time,
            resign_time,
            start_time,
            end_time,
            create_time,
            update_time,
            system_role_id,
            online_status,
            allow_cx,
            charge_way,
            pd_unit_price,
            cx_unit_price,
            is_guaranteed,
            is_team_leader,
            serial_number,
            show_sort,
            is_delete,
            raw_data
        )
        VALUES (
            %(store_id)s,
            %(assistant_id)s,
            %(assistant_no)s,
            %(nickname)s,
            %(real_name)s,
            %(gender)s,
            %(mobile)s,
            %(level)s,
            %(team_id)s,
            %(team_name)s,
            %(assistant_status)s,
            %(work_status)s,
            %(entry_time)s,
            %(resign_time)s,
            %(start_time)s,
            %(end_time)s,
            %(create_time)s,
            %(update_time)s,
            %(system_role_id)s,
            %(online_status)s,
            %(allow_cx)s,
            %(charge_way)s,
            %(pd_unit_price)s,
            %(cx_unit_price)s,
            %(is_guaranteed)s,
            %(is_team_leader)s,
            %(serial_number)s,
            %(show_sort)s,
            %(is_delete)s,
            %(raw_data)s
        )
        ON CONFLICT (store_id, assistant_id) DO UPDATE SET
            assistant_no    = EXCLUDED.assistant_no,
            nickname        = EXCLUDED.nickname,
            real_name       = EXCLUDED.real_name,
            gender          = EXCLUDED.gender,
            mobile          = EXCLUDED.mobile,
            level           = EXCLUDED.level,
            team_id         = EXCLUDED.team_id,
            team_name       = EXCLUDED.team_name,
            assistant_status= EXCLUDED.assistant_status,
            work_status     = EXCLUDED.work_status,
            entry_time      = EXCLUDED.entry_time,
            resign_time     = EXCLUDED.resign_time,
            start_time      = EXCLUDED.start_time,
            end_time        = EXCLUDED.end_time,
            update_time     = COALESCE(EXCLUDED.update_time, now()),
            system_role_id  = EXCLUDED.system_role_id,
            online_status   = EXCLUDED.online_status,
            allow_cx        = EXCLUDED.allow_cx,
            charge_way      = EXCLUDED.charge_way,
            pd_unit_price   = EXCLUDED.pd_unit_price,
            cx_unit_price   = EXCLUDED.cx_unit_price,
            is_guaranteed   = EXCLUDED.is_guaranteed,
            is_team_leader  = EXCLUDED.is_team_leader,
            serial_number   = EXCLUDED.serial_number,
            show_sort       = EXCLUDED.show_sort,
            is_delete       = EXCLUDED.is_delete,
            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)
