# -*- coding: utf-8 -*-
import pandas as pd
import datetime
from config import Config
from api import API
import pymysql  # 使用 pymysql 替代 mysql.connector
from back_ground_module import CommonModule
from log_config import configure_task_logger, configure_error_task_logger

# 获取已经配置好的常规日志记录器
logger = configure_task_logger()
error_task_logger = configure_error_task_logger()
start_time = datetime.datetime.now()
api_instance = API()
common_module = CommonModule()


class importGroupNotificationData:
    """短信数据支撑"""

    def __init__(self):
        self.field_mapping = {}
        self.fields()

    def main(self):
        task_start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        try:
            payload = {"api_key": Config.SaaS_Tasks_APP_ID,
                       "entry_id": Config.GroupNotification_form_ID,
                       }
            abnormal_service = api_instance.entry_data_list(payload)
            abnormal_list = abnormal_service.get("data")  # api请求格式，将数据封装在data字典里

            delete_id_list = []
            try:
                for i in range(0, len(abnormal_list)):  # 删除历史数据
                    delete_id_list.append(abnormal_list[i]['_id'])
                delete_payload = {"api_key": Config.SaaS_Tasks_APP_ID,
                                  "entry_id": Config.GroupNotification_form_ID,
                                  "data_ids": delete_id_list}

                api_instance.entry_data_batch_delete(delete_payload)
            except Exception as e:
                error_task_logger.error(f"删除历史数据失败：{e}")
                common_module.send_task_error(task_start_time, "短信数据支撑", str(e))

            data_commission = common_module.get_GroupNotification_details()

            # 生成包含所有行转换后的字典列表
            data_commission = [self.row_to_dict(row, self.field_mapping) for index, row in data_commission.iterrows()]
            data_commission = {'api_key': Config.SaaS_Tasks_APP_ID, 'entry_id': Config.GroupNotification_form_ID,
                               "data_list": data_commission}

            api_instance.entry_data_batch_create(data_commission)
            common_module.send_task_status(task_start_time, "短信数据支撑")
        except Exception as e:
            error_task_logger.error(f"短信数据支撑失败：{e}")
            common_module.send_task_error(task_start_time, "短信数据支撑", str(e))

    @staticmethod
    def row_to_dict(row, field_mapping):
        """将一行数据转换为指定格式的字典"""
        result = {}
        for col_name, widget_id in field_mapping.items():
            if col_name in row:
                value = row[col_name]
                clean_value = None if pd.isna(value) else value
                result[widget_id] = {"value": clean_value}
        return result

    def fields(self):
        self.field_mapping = {
            "公司id": "_widget_1743065201885",
            "是否启动短信功能": "_widget_1743065201886",
            "是否购买短信包": "_widget_1743065201887",
            "累计购买总数": "_widget_1743065201888",
            "累计发送成功总人数": "_widget_1743065201889",
            "剩余短信条数": "_widget_1743065201890",
            "第一次短信购买时间": "_widget_1743065201891",
            "最近一次短信购买时间": "_widget_1743065201892",
            "实付总金额": "_widget_1743065201893",
            "短信剩余量是否小于20%": "_widget_1743065201894"
        }


if __name__ == '__main__':
    start = importGroupNotificationData()
    start.main()
