from typing import Dict, Any
from .rpa_complete_ import ExcelWriter
from xianbao.core.DictData import DictManager
from .ikea_status import task_status_to_str


class RpaFillCondition:

    def __init__(self, db_path: str):
        self.sys_config = DictManager(db_name=db_path).get_dict_as_mapping('sys_config')

    # 默认的数据映射实现
    def _data_mapper(self, task: Dict[str, Any]) -> Dict[str, Any]:
        """
        默认的数据映射实现
        :param task: 输入字典数据
        :return: 映射后的字典数据
        """
        data = task['meta']
        res = {
            '序号': '',  # 自动生成
            '参考号': data.get('ums_receipt_info', {}).get('参考号', ''),
            '异常描述': task_status_to_str(task['status']),
            '电脑文件存储地址': self.sys_config['workpath'],
            '网盘地址': '',
            '（必填）签购单': data.get('ums_receipt', ''),
            '（必填）商品照片': data.get('ikea_receipt', ''),
            '（必填）自提单或送货单': data.get('delivery_note', ''),
            '发票图片名称': data.get('invoice', ''),
            '发票代码': data.get('invoice_info', {}).get('发票号码', ''),
            '发票金额': data.get('ikea_receipt_info', {}).get('合计', ''),
            '销售企业名称（注册名称）': '宜家',
            '统一社会信用代码': data.get('invoice_info', {}).get('购买方统一社会信用代码/纳税人识别号', ''),
            '购买方名称': data.get('invoice_info', {}).get('购买方名称', ''),
            '销售企业注册地区行政区划': '',
            '销售企业注册详细地址': '',
            '购买方姓名': '',
            '手机号': data.get('delivery_info', {}).get('电话', ''),
            '所在地区': data.get('delivery_info', {}).get('地区', ''),
            '详细地址': data.get('delivery_info', {}).get('地址', ''),
            '配送情况': '',
            '送货（提货）时间': data.get('ums_receipt_info', {}).get('日期时间', '')
        }
        return res


class Handler:
    def __init__(self, xlsx_path: str, db_path: str=r"D:\install\ylsw\ums-rpa\rpa_ztdb.db"):
        """
        初始化处理类
        :param xlsx_path: xlsx文件路径
        :param db_path: 数据库文件路径
        """
        self.xlsx_path = xlsx_path
        self._writer = ExcelWriter(data_mapper=RpaFillCondition(db_path=db_path), xlsx_path=xlsx_path)

    def doh(self, task_info: dict):
        """
        处理任务
        :param task_info: 任务信息
        """
        self._writer.write_to_excel(task_info)
