from api import API
from back_ground_module import CommonModule
import datetime
import re
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 KeyCustomerRevisit:
    """大客户回访"""
    def __init__(self):
        self.index = None
        self.data_NGV = None
        self.date_list = None
        self.Smart_detection = None
        self.service_remind = None
        self.json_list = []
        self.NGV_data_list = None
        self.permissions_table = None
        self.staff_id_list = None
        self.get_feature_usage = None
        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

    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字典里

        # 获取权限表信息
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "675b96c14e839f90fef1647c"}
        self.permissions_table = api_instance.entry_data_list(payload).get("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))

        # 获取服务提醒-数据支持表单数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "676bb7bda3029720f1083e99"}
        self.service_remind = api_instance.entry_data_list(payload).get("data")

        # 获取智能检测-数据支持表单数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "676bb99649ab3ac975af6e39"}
        self.Smart_detection = api_instance.entry_data_list(payload).get("data")

        # 获取功能使用情况表
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "6763bbf657bd8fb76fcb41b2"}
        self.get_feature_usage = api_instance.entry_data_list(payload).get("data", [])

        # 获取保单识别表
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "6773a60d30ed87ff9f68d3c5"}
        self.policy_recognition = api_instance.entry_data_list(payload).get("data")
        # 提取 _widget_1735632397600 的值并存储在列表中
        self.widget_list = [item['_widget_1735632397600'] for item in self.policy_recognition]

        # 获取私域小程序-数据支持表单数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "67e0f0fae622896749ba5087"}
        self.private_domain = api_instance.entry_data_list(payload).get("data", [])
        # 提取 _widget_1742795002375 的值并存储在列表中
        self.private_domain = [item['_widget_1742795002375'] for item in self.private_domain]

        # 获取公域小程序-数据支持表单数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "67e0c702c8f603b997980999"}
        self.public_domain = api_instance.entry_data_list(payload).get("data", [])
        # 提取 _widget_1742784257506 的值并存储在列表中
        self.public_domain_list = [item['_widget_1742784257506'] for item in self.private_domain]

        # 获取异业合作-数据支持表单数据
        payload = {"api_key": "675b900991ad2491c69389ca", "entry_id": "67e24fdd8dfcfa918e17c30b"}
        self.different_industries = api_instance.entry_data_list(payload).get("data", [])
        # 提取 _widget_1742784257506 的值并存储在列表中
        self.different_industries_list = [item['_widget_1742784257506'] for item in self.different_industries]


    @staticmethod
    def remove_parentheses(text: str) -> str:
        # 使用正则表达式匹配并去除括号及其内容
        # \s* 表示匹配零个或多个空白字符（处理括号前后可能存在的空格）
        # $ 和 $ 分别表示匹配左括号和右括号
        # 中间的 .*? 表示非贪婪地匹配任意数量的字符（包括没有字符的情况）
        cleaned_text = re.sub(r'\s*$.*?$\s*', '', text)
        # 为了确保同时处理中文括号，再进行一次替换
        cleaned_text = re.sub(r'\s*（.*?）\s*', '', cleaned_text)
        return cleaned_text.strip()  # 去除两端多余的空白字符

    @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 main(self):
        task_start_time =datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.load_all_data()

        self.date_list = common_module.get_holiday_list()  # 获取一年中的节假日
        self.data_NGV = common_module.get_ngv_details(days_back=1)  # 获取data_NGV 并转为str
        # self.index = self.build_index(self.json_list)

        data_NGV = self.data_NGV.copy()

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

        # 对整个DataFrame的所有列应用替换函数
        data_NGV = data_NGV.apply(replace_values)
        # data_NGV.to_csv("dayinNGV.csv")

        # 定义优先级顺序
        edition_order = ['皇冠版', '至尊版', '尊享版', '旗舰版', '标准版', '进阶版', '基础版', '入门版']
        customer_type_order = ["F", "E", "D", "C", "B", "A"]  # 索引越小优先级越高
        group_grade_order = ['全国KA（FMVP）', '区域KA（MVP）', '重要客户（SVIP）', '普通客户（VIP）']

        # 创建映射字典，并为不在列表中的值设置默认值
        edition_map = {edition: idx for idx, edition in enumerate(edition_order)}
        customer_type_map = {ctype: idx for idx, ctype in enumerate(customer_type_order)}
        group_grade_map = {grade: idx for idx, grade in enumerate(group_grade_order)}

        # 添加用于排序的新列，并处理不在映射字典中的值
        data_NGV['edition_rank'] = data_NGV['saas_edition_fmt'].map(edition_map).fillna(0).astype(int)  # 缺失值用最高优先级填充
        data_NGV['customer_type_rank'] = data_NGV['saas_customer_type'].map(customer_type_map).fillna(0).astype(int)
        data_NGV['group_grade_rank'] = data_NGV['group_grade'].map(group_grade_map).fillna(0).astype(int)
        # data_NGV.to_csv("88855.csv")

        # 找到每组中 edition_rank 最小值对应的行
        best_edition_idx = data_NGV.groupby('id_own_group')['edition_rank'].idxmin()
        best_edition_rows = data_NGV.loc[best_edition_idx]
        best_edition_rows['max_saas_edition'] = best_edition_rows['saas_edition_fmt']

        # 找到每组中 customer_type_rank 最小值对应的行
        best_customer_type_idx = data_NGV.groupby('id_own_group')['customer_type_rank'].idxmin()
        best_customer_type_rows = data_NGV.loc[best_customer_type_idx]
        best_customer_type_rows['max_saas_customer_type'] = best_customer_type_rows['customer_type_rank'].apply(
            lambda x: customer_type_order[x])

        # 找到每组中 group_grade_rank 最小值对应的行
        best_group_grade_idx = data_NGV.groupby('id_own_group')['group_grade_rank'].idxmin()
        best_group_grade_rows = data_NGV.loc[best_group_grade_idx]
        best_group_grade_rows['max_group_grade'] = best_group_grade_rows['group_grade']

        # 合并最佳值回到原数据集
        best_values = (
            best_edition_rows[['id_own_group', 'max_saas_edition']]
            .merge(best_customer_type_rows[['id_own_group', 'max_saas_customer_type']], on='id_own_group', how='outer')
            .merge(best_group_grade_rows[['id_own_group', 'max_group_grade']], on='id_own_group', how='outer')
        )

        # 将最佳值合并回原数据集
        data_NGV = data_NGV.merge(best_values, on='id_own_group', how='left')

        condition = (data_NGV['is_main_org'] == 1) & (data_NGV['org_status'] == '过期')  # 步骤2: 过滤条件

        ngvv2 = data_NGV[condition]
        # ngvv2.to_excel(r"C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\NGVV2.xlsx")

        data_NGV_V2 = data_NGV.copy()  # 步骤3: 检查id_own_group是否存在于ngvv2中
        data_NGV_V2['条件'] = (data_NGV['org_type'] == "一般") & (data_NGV['org_status'] == '留存') & (
            data_NGV['group_grade'].isin(['全国KA（FMVP）', '区域KA（MVP）'])) & (data_NGV['is_main_org'] != 1)

        data_NGV_V2 = data_NGV_V2.loc[data_NGV_V2["条件"]]
        # 步骤4: 过滤存在的记录
        data_NGV_V2['exists_in_ngvv2'] = data_NGV_V2['id_own_group'].isin(ngvv2['id_own_group'])
        filtered_data = data_NGV_V2[data_NGV_V2['exists_in_ngvv2']]

        fixed_order = ['皇冠版', '至尊版', '尊享版', '旗舰版', '标准版', '进阶版', '基础版', '入门版']
        # sorted_items = sorted(filtered_data, key=lambda x: fixed_order.index(x))

        fixed_order_map = {edition: index for index, edition in enumerate(fixed_order)}
        filtered_data['sort_key'] = filtered_data['saas_edition_fmt'].map(fixed_order_map)
        filtered_data = filtered_data.sort_values(by='sort_key').drop('sort_key', axis=1)

        result = filtered_data.drop_duplicates(subset='id_own_group', keep='first')

        data_NGV['条件'] = (data_NGV['org_type'] == "一般") & (data_NGV['org_status'] == '留存') & (
            data_NGV['group_grade'].isin(['全国KA（FMVP）', '区域KA（MVP）'])) & (data_NGV['is_main_org'] == 1)

        data_NGV = data_NGV.loc[data_NGV["条件"]]

        data_NGV = pd.concat([data_NGV, result], axis=0)

        # 重置索引
        data_NGV = data_NGV.reset_index(drop=True)
        print(f'读取NGV大客户回访 {len(data_NGV)} 条数据！')
        # data_NGV.to_csv("大客户回访数据.csv")
        data_NGV = data_NGV.copy()

        for index_num, row in data_NGV.iterrows():  # 对过滤后的每一条进行派发
            print(row["org_code"])  # 数据验证
            print(row["service_impl_principal"])
            print(row["area_manager"])
            print(row["technician"])
            print(row["group_grade"])
            payload_dict = {}
            # saas_use_year = re.findall(r'第([0-9]+)年', row["saas_use_year"])[0]

            NGV_roles = {
                'relationship_manager': row['service_impl_principal'],  # 运营负责人
                # 'relationship_manager': "张阳",  # 运营负责人
                'area_manager': row['area_manager'],  # 区域经理
                'technician': row['technician'],  # 技术专家
                'salesmen': row['salesmen'],  # 技术专家
            }

            for role, name in NGV_roles.items():
                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, salesmen = [NGV_roles[role] for role in
                                                                        ['relationship_manager', 'area_manager',
                                                                         'technician', 'salesmen']]

            payload_dict.update({
                "_widget_1734590278288": {"value": technician}  # 跟进人是技术专家
            })

            payload_dict.update({
                "_widget_1734590278289": {"value": relationship_manager},  # 运营负责人
                "_widget_1734590278290": {"value": area_manager},  # 区域经理
                "_widget_1734590278291": {"value": technician},  # 技术专家
                "_widget_1735290738545": {"value": salesmen}}  # 销售负责人
            )

            # 输出结果
            print("SaaS开户回访人员：", relationship_manager or "未分配")
            print("SaaS技术专家：", technician or "未分配")
            print("SaaS区域经理：", area_manager or "未分配")

            # 判断权限唯一值
            pattern = r'(.+)\（'
            match = re.search(pattern, row['max_group_grade'])
            group_grade = match.group(1)

            if not row['saas_customer_type'] or row['saas_customer_type'] == 'NA' or row[
                'saas_customer_type'] == 'None':
                row['saas_customer_type'] = "F"

            NGV_store_level_key = group_grade + row['max_saas_edition'] + row['max_saas_customer_type']
            print("权限唯一值：", NGV_store_level_key)
            data_NGV.at[index_num, 'NGV_store_level_key'] = NGV_store_level_key


            Billing = None
            for item in self.permissions_table:
                if NGV_store_level_key == item.get("_widget_1734056507963"):  # 合并（等级-类型-分层）
                    print("该门店开单的权限是：", item.get("_widget_1734055617039"))
                    Billing = item.get("_widget_1734055617039")  # 开单
                    Service_Alerts = item.get("_widget_1734055617040")  # 服务提醒
                    membership = item.get("_widget_1734055617041")  # 会员卡
                    SMS = item.get("_widget_1734055617042")  # 短信
                    Public_domain_applets = item.get("_widget_1734055617043")  # 公域小程序
                    Private_domain_applets = item.get("_widget_1734055617044")  # 私域小程序
                    Test_sheet = item.get("_widget_1734055617045")  # 检测单
                    AI_poster = item.get("_widget_1734055617046")  # AI海报
                    Business_wallets = item.get("_widget_1734055617047")  # 企业钱包
                    Precision_marketing = item.get("_widget_1734055617049")  # 精准营销
                    Paid_memberships = item.get("_widget_1734055617051")  # 付费会员
                    business_WeCom = item.get("_widget_1734055617052")  # 企业微信
                    Insurance_policy_identification = item.get("_widget_1734055617053")  # 保险单识别
                    Insurance_bots = item.get("_widget_1734055617054")  # 保险机器人
                    Camera_pick_up = item.get("_widget_1734055617055")  # 摄像头接车
                    Camera_billing = item.get("_widget_1734055617056")  # 摄像头开单
                    Transparent_workshop = item.get("_widget_1734055617057")  # 透明车间
                    Cross_industry_cooperation = item.get("_widget_1734055617058")  # 异业合作
                    BI_Insights = item.get("_widget_1734055617059")  # BI洞察
                    payload_dict.update(
                        {
                            "_widget_1734073342350": {"value": Billing},
                            "_widget_1735004315757": {"value": Service_Alerts},
                            "_widget_1735004315756": {"value": membership},
                            "_widget_1735004315755": {"value": SMS},
                            "_widget_1735004315754": {"value": Public_domain_applets},
                            "_widget_1735004315753": {"value": Private_domain_applets},
                            "_widget_1735004315752": {"value": Test_sheet},
                            "_widget_1735004315751": {"value": AI_poster},
                            "_widget_1735004315750": {"value": Business_wallets},
                            "_widget_1735004315749": {"value": Precision_marketing},
                            "_widget_1735004315748": {"value": Paid_memberships},
                            "_widget_1735004315747": {"value": business_WeCom},
                            "_widget_1735004315746": {"value": Insurance_policy_identification},
                            "_widget_1735004315745": {"value": Insurance_bots},
                            "_widget_1735004315744": {"value": Camera_pick_up},
                            "_widget_1735004315743": {"value": Camera_billing},
                            "_widget_1735004315742": {"value": Transparent_workshop},
                            "_widget_1735004315741": {"value": Cross_industry_cooperation},
                            "_widget_1734073342352": {"value": BI_Insights},

                        }
                    )
                    feature_dict = {
                        "开单": "_widget_1734073342350",
                        "服务提醒": "_widget_1735004315757",
                        "会员卡": "_widget_1735004315756",
                        "短信": "_widget_1735004315755",
                        "公域小程序": "_widget_1735004315754",
                        "私域小程序": "_widget_1735004315753",
                        "检测单": "_widget_1735004315752",
                        "AI海报": "_widget_1735004315751",
                        "企业钱包": "_widget_1735004315750",
                        "精准营销": "_widget_1735004315749",
                        "付费会员": "_widget_1735004315748",
                        "企业微信": "_widget_1735004315747",
                        "保险单识别": "_widget_1735004315746",
                        "保险机器人": "_widget_1735004315745",
                        "摄像头接车": "_widget_1735004315744",
                        "摄像头开单": "_widget_1735004315743",
                        "透明车间": "_widget_1735004315742",
                        "异业合作": "_widget_1735004315741",
                        "BI洞察": "_widget_1734073342352",

                    }
                    # _widget_1735527329557  下次是否推荐
                    for new_item in self.get_feature_usage:
                        for feature_module, feature_value in feature_dict.items():  # 模块
                            if new_item.get("_widget_1735268263079") == feature_module and new_item.get(
                                    "_widget_1735527329557") == "否" and new_item.get("_widget_1735280720550") == \
                                    row["id_own_org"]:  # 下次是否推荐 功能使用情况表
                                print(f"{feature_value}进行了更改")
                                payload_dict.update({f"{feature_value}": {"value": "×"}})

                            if new_item.get("_widget_1735268263079") == feature_module and new_item.get(
                                    "_widget_1736414617462") == "否" and new_item.get("_widget_1735280720550") == \
                                    row["id_own_org"]:  # 下次是否跟进
                                print(f"{feature_value}进行了更改")
                                payload_dict.update({f"{feature_value}": {"value": "×"}})

                    fields_to_check = {
                        "_widget_1735004315763": Billing,  # 开单
                        "_widget_1735106258016": Service_Alerts,  # 服务提醒
                        "_widget_1735106258036": membership,  # 会员卡
                        "_widget_1735106258086": SMS,  # 短信
                        "_widget_1735106258112": Public_domain_applets,  # 公域小程序
                        "_widget_1735106258141": Private_domain_applets,  # 私域小程序
                        "_widget_1735107354648": Test_sheet,  # 检测单
                        "_widget_1735107354725": AI_poster,  # AI海报
                        "_widget_1735107354811": Business_wallets,  # 企业钱包
                        "_widget_1735107354906": Precision_marketing,  # 精准营销
                        "_widget_1735107354980": Paid_memberships,  # 付费会员
                        "_widget_1735107355093": business_WeCom,  # 企业微信
                        "_widget_1735107355143": Insurance_policy_identification,  # 保险单识别
                        "_widget_1735107355235": Insurance_bots,  # 保险机器人
                        "_widget_1735107355333": Camera_pick_up,  # 摄像头接车
                        "_widget_1735107355392": Camera_billing,  # 摄像头开单
                        "_widget_1735107355502": Transparent_workshop,  # 透明车间
                        "_widget_1735107355618": Cross_industry_cooperation,  # 异业合作
                        "_widget_1735107355740": BI_Insights  # BI洞察
                    }

                    # 遍历每个字段，检查其值并更新payload_dict
                    for widget_id, field_name in fields_to_check.items():
                        if field_name == "√":
                            payload_dict.update({widget_id: {"value": "是"}})

                    break
            if row["active_status_fmt"] == "活跃":  # 开单 是否使用
                payload_dict.update({"_widget_1735004315765": {"value": "是"}})
            else:
                payload_dict.update({"_widget_1735004315765": {"value": "否"}})
            try:
                if row["card_bill_day_count_last_30_day"] != "0":  # 会员卡 是否使用
                    payload_dict.update({"_widget_1735106258038": {"value": "是"}})
                else:
                    payload_dict.update({"_widget_1735106258038": {"value": "否"}})
            except Exception as e:
                print(f"会员卡识别：Error finding customer service: {e}")
            # print(self.service_remind.get("_widget_1735112637045"))
            payload_dict["_widget_1735106258018"] = {"value": "否"}

            for item in self.service_remind:
                if row["id_own_group"] == item.get("_widget_1735112637043"):
                    if int(item.get("_widget_1735112637045")) < 180 and int(
                            item.get("_widget_1735112637046")) == 1:  # 服务提醒 是否使用
                        payload_dict.update({"_widget_1735106258018": {"value": "是"}})
                        break
                    elif int(item.get("_widget_1735112637048")) > 0:
                        payload_dict.update({"_widget_1735106258018": {"value": "是"}})
                        break

            keys_to_check = [
                "_widget_1735113110155"
            ]  # 智能检测 是否使用

            # 初始化默认值为"否"
            payload_dict["_widget_1735107354650"] = {"value": "否"}

            # 检查每个键，如果有一个大于0，则更新为"是"并停止检查
            for key in keys_to_check:
                for item in self.Smart_detection:
                    if row["id_own_org"] == item.get("_widget_1735113110147"):
                        if int(item.get(key, 0)) > 0:  # 使用get方法并提供默认值0防止键不存在的情况
                            payload_dict["_widget_1735107354650"]["value"] = "是"
                            break

            # 近30天业务单量=0 则其它所有模块均不推荐
            try:
                for feature_module, feature_value in feature_dict.items():  # 模块
                    if row["bill_count_last_30_day"] == '0' and payload_dict[feature_value]["value"] == '△':
                        payload_dict.update({f"{feature_value}": {"value": "×"}})
            except Exception as e:
                print(f"不开单识别：Error finding customer service: {e}")
            # 保单识别：从系统中抽取目标门店，针对门店抽取修改是否推荐
            try:
                if row["org_code"] in self.widget_list:
                    payload_dict.update({'_widget_1735004315746': {"value": "△"}})
            except Exception as e:
                print(f"保单识别：Error finding customer service: {e}")

            # 私域小程序：根据是否开通微信小程序判断是否使用，旗舰版及以上算拥有
            try:
                if row["id_own_group"] in self.private_domain:
                    payload_dict.update({'_widget_1735106258143': {"value": "是"}}) # SYXCX:是否使用
                else:
                    payload_dict.update({'_widget_1735106258143': {"value": "否"}}) # SYXCX:是否使用
            except Exception as e:
                print(f"私域小程序：Error finding customer service: {e}")
            try:
                high_version = ['皇冠版', '至尊版', '尊享版', '旗舰版']
                if row["saas_edition_fmt"] in high_version:
                    payload_dict.update({'_widget_1735106258141': {"value": "是"}}) # SYXCX:是否拥有
                else:
                    payload_dict.update({'_widget_1735106258141': {"value": "否"}}) # SYXCX:是否拥有
            except Exception as e:
                print(f"私域小程序：Error finding customer service: {e}")
            
            # 公域小程序：根据是否开通微信小程序判断是否使用，旗舰版及以上算拥有
            try:
                for item in self.public_domain:
                    if row["id_own_org"] == item.get("_widget_1742784257506"): # 门店id
                        if int(item.get("_widget_1742784257509")) > 0 :   # 发布商品数量
                            payload_dict.update({"_widget_1735106258114": {"value": "是"}}) # GYXCX:是否使用
                            break
                        else :
                            payload_dict.update({"_widget_1735106258114": {"value": "否"}}) # GYXCX:是否使用
                            break
            except Exception as e:
                print(f"公域小程序：Error finding customer service: {e}")
            try:
                if row["id_own_org"] in self.public_domain_list:
                    payload_dict.update({'_widget_1735106258112': {"value": "是"}}) # GYXCX:是否拥有
                else:
                    payload_dict.update({'_widget_1735106258112': {"value": "否"}}) # GYXCX:是否拥有
            except Exception as e:
                print(f"公域小程序：Error finding customer service: {e}")

            # 异业合作：根据是否存在判断是否拥有，过滤条件 商品名称包含异业两个字
            try:
                if row["id_own_org"] in self.different_industries_list:
                    payload_dict.update({'_widget_1735107355618': {"value": "是"}}) # YYHZ:是否拥有
                else:
                    payload_dict.update({'_widget_1735107355618': {"value": "否"}}) # YYHZ:是否拥有
            except Exception as e:
                print(f"异业合作：Error finding customer service: {e}")


            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")
                    print(NGV_data_id)
            if not NGV_data_id:
                print("未找到数据ID")

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

            payload_dict.update({
                "_widget_1734590278279": {"value": row["group_name"]},  # 公司名称
                "_widget_1735112931760": {"value": row["id_own_group"]},  # 公司id
                "_widget_1735112931761": {"value": row["id_own_org"]},  # 门店id
                "_widget_1734590278281": {"value": row['org_name']},  # 门店名称
                "_widget_1734590278292": {"value": "2025第一季度大客户回访"},  # 跟进阶段
                "_widget_1734321349021": {"value": NGV_data_id},  # 关data_get联数据
                "_widget_1742548684369": {"value": "定期维护，了解业务变化，保持客户满意度的同时挖掘增购机会。"},  # 主要目的
                "_widget_1734590278266": {"value": row['region_name']},  # 大区
                "_widget_1734590278285": {"value": row['branch_name']},  # 小区
                "_widget_1734590278284": {"value": row['province_name']},  # 省
                "_widget_1734590278283": {"value": row['city_name']},  # 市
                "_widget_1734590278282": {"value": row['area_name']},  # 区
                "_widget_1734590278278": {"value": row['saas_customer_type']},  # 门店分层
                "_widget_1734590278277": {"value": row['group_grade']},  # 公司等级
                "_widget_1734590278276": {"value": row['limit_user_type']},  # 限制账户类型
                "_widget_1734590278275": {"value": row['active_user_type']},  # 有效账户类型
                "_widget_1734590278274": {"value": row['saas_version']},  # ERP操作模式
                "_widget_1734590278273": {"value": row['saas_use_year']},  # 使用时长
                "_widget_1734590278272": {"value": row['org_stage']},  # 门店阶段
                "_widget_1734590278271": {"value": row['manage_model']},  # 经营模式
                "_widget_1734590278267": {"value": row['contacts']},  # 联系人
                "_widget_1734590278287": {"value": row['contact_mobile']},  # 联系手机号
                "_widget_1734590278286": {"value": row['saas_edition_fmt']},  # SaaS版本
                "_widget_1734590278280": {"value": row['org_code']},  # 门店编码
                "_widget_1735287791875": {"value": row['salesmen']},  # 销售负责人
                "_widget_1735096489244": {"value": distribution_date},  # 派发时间
            })

            routine_follow_up_payload = {
                "api_key": "675b900991ad2491c69389ca",
                "entry_id": "675b9c63925cd404038a6b86",
                "is_start_workflow": "true",
                "data": payload_dict,
            }

            print(routine_follow_up_payload)

            res = api_instance.data_batch_create(routine_follow_up_payload)
            logger.info(f"创建结果：{res}")
            print(res)

        # data_NGV.to_csv("final_data_NGV.csv", index=False)
        common_module.send_task_status(task_start_time, "大客户回访")


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