import datetime
import os
import time
import requests
from api import API
from back_ground_module import CommonModule
import pandas as pd
from log_config import configure_task_logger, configure_error_task_logger

api_instance = API()
common_module = CommonModule()
# start_time = datetime.datetime.now()

# 获取已经配置好的常规日志记录器
logger = configure_task_logger()

# 获取已经配置好的错误任务日志记录器
error_task_logger = configure_error_task_logger()


class NewExceptionTask:
    """
    SaaS异常回访
    """

    def __init__(self):
        self.get_feature_usage = None
        self.saas_create_time = None
        self.index = None
        self.date_one = None
        self.data_yichang_S = None
        self.date_list = None
        self.Smart_detection = None
        self.service_remind = None
        self.NGV_data_list = None
        self.permissions_table = None
        self.staff_id_list = None
        self.json_list = []
        self.policy_recognition = None
        self.widget_list = None
        self.private_domain = None
        self.public_domain = None
        self.public_domain_list = None
        self.different_industries = None
        self.different_industries_list = None
        self.groupnotification = None

    def calculate_date_one(self, start_offset=0):
        """
        计算从当前日期（或指定偏移量的日期）开始，往前遍历遇到date_list中日期的次数。

        参数:
        - start_offset: 从当前日期起始的天数偏移量，默认为0（即今天）。负数表示过去，正数表示未来。

        返回:
        - date_one: 遍历到date_list中日期的次数。
        """
        jdy_date = datetime.datetime.now().strftime("%Y-%m-%d")
        jdy_start_time = datetime.datetime.now().strftime("%Y-%m-%d ")
        # 设置起始日期
        now_time = datetime.datetime.now() + datetime.timedelta(days=start_offset)

        # 初始化计数器
        date_one = 1
        print("当前日期：", now_time.strftime("%Y-%m-%d"))
        # 检查起始日期是否在date_list中
        if now_time.strftime("%Y-%m-%d") in self.date_list:
            date_one = 0
            print("开始次数：", date_one)

        else:
            # 遍历日期
            for i in range(1, 10):
                new_date = now_time + datetime.timedelta(days=-i)
                new_date_str = new_date.strftime("%Y-%m-%d")
                print("遍历日期：", new_date_str)
                if new_date_str in self.date_list:
                    date_one += 1
                    print("节假日期：", new_date_str)
                else:
                    break

        print("遍历次数：", date_one)
        return date_one

    @staticmethod
    def download_url_content(url, save_path):
        """
        下载指定 URL 的内容并保存到本地文件。

        :param url: 要下载内容的 URL
        :param save_path: 保存文件的路径
        """
        try:
            # 发送 GET 请求以获取内容
            response = requests.get(url, stream=True)
            response.raise_for_status()  # 如果响应状态码不是 200，抛出异常

            # 确保保存目录存在
            os.makedirs(os.path.dirname(save_path), exist_ok=True)

            # 将内容写入文件
            with open(save_path, 'wb') as file:
                for chunk in response.iter_content(chunk_size=8192):  # 分块写入，避免占用过多内存
                    if chunk:  # 过滤掉空块
                        file.write(chunk)

            print(f"文件已成功保存到 {save_path}")

        except requests.exceptions.RequestException as e:
            print(f"下载失败: {e}")
        except Exception as e:
            print(f"发生错误: {e}")

    def load_all_data(self):
        """加载所有必要的数据表"""
        # 省市区人员关系表
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "676512ac3e54dc3159460c0a"}
        json_dict = api_instance.entry_data_list(payload)
        self.json_list = json_dict.get("data")

        # 获取简道云员工id
        payload = {"api_key": "6694d3c4fcb69ca9a111a6c4",
                   "entry_id": "6769204a1902c9341340a1bc",
                   }
        staff_id = api_instance.entry_data_list(payload)
        self.staff_id_list = staff_id.get("data")  # api请求格式，将数据封装在data字典里

        # 获取NGV数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "675bb02bd2d53c2034c665e4"}
        self.NGV_data_list = api_instance.entry_data_list(payload).get("data", [])
        # print("NGV获取后的类型：", type(self.NGV_data_list))

    @staticmethod
    def build_index(json_list):
        index = {}
        for json_item in json_list:
            try:
                key = (json_item['_widget_1734677164861'], json_item['_widget_1734677164862'],
                       json_item['_widget_1734677164863'])  # 省市区
                if '_widget_1734677164870' not in json_item:  # 异常回访客服
                    raise KeyError("缺少 '异常回访客服' 键")
                index[key] = json_item
            except KeyError as e:
                print(f"警告：{e}，跳过该条记录: {json_item}")
                continue
        print('index', index)
        return index

    @staticmethod
    def find_customer_service(province_name, city_name, area_name, index):
        key = (province_name, city_name, area_name)
        # print(index)
        if key not in index:
            return "数据缺失: 未找到对应的异常回访客服"

        return index[key]

    @staticmethod
    def get_staff_id(row_item, name):
        """辅助函数，用于获取员工ID"""
        if str(row_item["_widget_1734942794144"]) == str(name):  # 检查姓名是否匹配
            return row_item["_widget_1734942794145"]  # 返回员工ID
        return None

    def assign_customer_service(self, province_name, city_name, area_name, index):
        """根据省市区派发给异常回访客服"""
        # try:
        customer_service_info = self.find_customer_service(province_name, city_name, area_name, index)
        customer_service = customer_service_info.get('_widget_1734677164870', {}).get('username')  # 异常回访客服
        return customer_service
        # except Exception as e:
        #     print(f"Error finding customer service: {e}")
        #     return "分配失败，请检查", "分配失败，请检查", "分配失败，请检查"

    def main(self):

        task_start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        try:
            global png_url, key, upload_key, province_name, city_name, area_name
            self.load_all_data()

            self.data_yichang_S = common_module.get_yichang_details(days_back=1).astype(str)  # 获取data_NGV 并转为str
            self.index = self.build_index(self.json_list)

            logger.info("开始运行SaaS异常回访")

            data_yichang = self.data_yichang_S.copy()

            def replace_values(series):
                # 使用条件判断来进行替换
                return series.apply(lambda x: '' if pd.isna(x) or x in ['NA', 'None', ''] else x)

            # 对整个DataFrame的所有列应用替换函数
            data_yichang = data_yichang.apply(replace_values)

            for index_num, row in data_yichang.iterrows():  # 对过滤后的每一条进行派发
                try:
                    payload_dict = {}

                    distribution_date = datetime.datetime.now(datetime.timezone.utc)
                    distribution_date = distribution_date.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'

                    date_obj1 = datetime.datetime.strptime(row["init_day"], "%Y%m%d").strftime("%Y-%m-%d")
                    date_obj2 = datetime.datetime.strptime(row["push_day"], "%Y%m%d").strftime("%Y-%m-%d")

                    NGV_roles = {
                        'service_impl_principal': row['service_impl_principal'],  # 运营负责人
                        'area_manager': row['area_manager'],  # 区域经理
                        'technician': row['technician'],  # 运营专家
                    }
                    for role, name in NGV_roles.items():  # 寻找对应的员工ID
                        for row_item in self.staff_id_list:
                            staff_id = self.get_staff_id(row_item, name)
                            if staff_id:
                                NGV_roles[role] = staff_id
                                break  # 找到后退出循环
                        else:
                            NGV_roles[role] = None  # 如果没有找到对应的员工ID
                    relationship_manager, area_manager, technician = [NGV_roles[role] for role in
                                                                      ['service_impl_principal',
                                                                       'area_manager',
                                                                       'technician']]

                    UUid = time.strftime("%Y%m%d%H%M%S", time.localtime())

                    NGV_data_id = None
                    # 获取关联数据
                    for NGV_Data in self.NGV_data_list:
                        # NGV_Data = NGV_Data.get("data")
                        if row["org_code"] == NGV_Data.get("_widget_1734062123071"):  # 门店编码
                            NGV_data_id = NGV_Data.get("_id")
                            province_name = NGV_Data.get("_widget_1734062123090")
                            city_name = NGV_Data.get("_widget_1734062123092")
                            area_name = NGV_Data.get("_widget_1734062123094")
                            logger.info(f"获取关联数据成功:{NGV_data_id}, {province_name}, {city_name}, {area_name}")

                    if not NGV_data_id:
                        logger.warning(f"未找到关联数据，请检查门店编码: {row['org_code']}")

                    # 根据省市区派发给异常回访客服
                    customer_service = self.assign_customer_service(province_name, city_name, area_name, self.index)

                    payload_dict.update({
                        "_widget_1748241895829": {"value": row["health_warning_info"]},  # 活跃健康状态变化

                        "_widget_1748241895830": {"value": row["org_name"]},  # 门店名称

                        "_widget_1748241895831": {"value": row["contacts"]},  # 联系人

                        "_widget_1748241895832": {"value": row['contact_mobile']},  # 联系方式

                        "_widget_1748241895833": {
                            "value": int(time.mktime(time.strptime(date_obj1, "%Y-%m-%d")) * 1000) if row[
                                                                                                          "init_day"] != '' else ''},
                        # 初始日

                        "_widget_1748241895834": {
                            "value": int(time.mktime(time.strptime(date_obj2, "%Y-%m-%d")) * 1000) if row[
                                                                                                          "push_day"] != '' else ''},
                        # 推进日

                        "_widget_1748246808678": {"value": customer_service},  # 当前跟进人

                        "_widget_1748246808679": {"value": relationship_manager},  # 运营负责人

                        "_widget_1748246808680": {"value": customer_service},  # 区域客服

                        "_widget_1748241895839": {
                            "value": int(time.mktime(time.strptime(row["saas_create_time"], "%Y-%m-%d")) * 1000) if row[
                                                                                                                        "saas_create_time"] != '' else ''},
                        # 开户时间

                        "_widget_1748246808681": {"value": technician},  # 技术专家

                        "_widget_1748246808682": {"value": area_manager},  # 区域经理

                        "_widget_1748241895842": {"value": row['org_code']},  # 门店编码

                        "_widget_1748241895844": {"value": row['group_name']},  # 公司名称

                        "_widget_1748241895846": {"value": row['group_grade']},  # 公司等级

                        "_widget_1748241895847": {"value": row['region_name']},  # 大区

                        "_widget_1748241895848": {"value": row['province_name']},  # 省

                        "_widget_1748241895849": {"value": row['org_type']},  # 门店类型

                        "_widget_1748241895850": {"value": row['saas_edition_fmt']},  # 系统版本

                        "_widget_1748241895851": {"value": row['saas_customer_type']},  # saas客户类型

                        "_widget_1748241895852": {"value": row['org_stage']},  # 门店阶段

                        "_widget_1748241895853": {"value": row['contact_mobile']},  # 操作模式E.L/E.S

                        "_widget_1748241895855": {"value": row['city_name']},  # 城市

                        "_widget_1748247754304": {"value": NGV_data_id},  # 数据id

                        "_widget_1748512176655": {"value": "未处理"},  # 跟进状态

                    })

                    routine_follow_up_payload = {
                        "api_key": "675b900991ad2491c69389ca",
                        "entry_id": "68340de79f116c0b66b6b0cc",  # 异常服务跟进待办
                        "is_start_workflow": "true",
                        "data": payload_dict,
                        "transaction_id": UUid
                    }

                    res = api_instance.data_batch_create(routine_follow_up_payload)
                    logger.info(f"创建结果：{res}")
                except:
                    pass
            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))


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