import logging import time from django.db.models.signals import post_save from django.dispatch import receiver from django.conf import settings from pingcap_sso.models import ( FXCRMLeads, SiteSetting, UserTiDBUsage, LarkMessage ) from pingcap_sso.discourse.lib import user_summary as asktug_user_summary from pingcap_sso.tasks.crm import create_partner_compatibility_certification_in_fxiaoke_crm logger = logging.getLogger(__name__) @receiver( post_save, sender="pingcap_sso.OfficialContactUs") def _sync_official_contact_us_to_lark_and_crm(sender, instance, created, **kwargs): if not created: return notify_lark_webhook = SiteSetting.official_new_contact_us_notify_lark_webhook if notify_lark_webhook: contact_us = instance message = "**中文官网 联系我们**\n" if contact_us.user: message += f'- 用户: [{ contact_us.user.username }](https://asktug.com/u/{ contact_us.user.username }/summary)\n' message += f'- 经验值: { contact_us.user.point_stat.experience }\n' message += f'- 是否通过 PCTA: { "是" if contact_us.user.pass_pcta else "否" }\n' message += f'- 是否通过 PCTP: { "是" if contact_us.user.pass_pctp else "否" }\n' tidb_usage = list(UserTiDBUsage.objects.filter( user=contact_us.user, deleted_at__isnull=True ).values_list('version', flat=True).distinct()) if tidb_usage: message += f'- TiDB 使用版本: { ", ".join(tidb_usage) }\n' asktug_summary = asktug_user_summary(contact_us.user) if asktug_summary: message += f'- AskTUG 发帖数: { asktug_summary["topic_count"] }\n' message += f'- AskTUG 回复数: { asktug_summary["post_count"] }\n' message += f"""- 姓名: { contact_us.name } - 手机号: { contact_us.phone } - 公司: { contact_us.company } - 职位: { contact_us.job_title_text } - 咨询类型: { contact_us.consult_category_text } - 咨询事宜: { contact_us.consult_detail } - Person Source: { contact_us.person_source } - Person Source Detail: { contact_us.person_source_details } """ LarkMessage.send( webhook=notify_lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='官网企业版试用' ) if settings.STAGING or settings.DEBUG: return FXCRMLeads.objects.create( leads_type='cCF276lgj', # 商业线索 leads_poll_id='5fc76790f3f0920001fe0baf', # 商业线索池, source_code='35B1f68rk', source_text='官网企业版试用', primary_source_code='0PwuyCOv1', primary_source_text='自然流量', secondary_source_code='uyr219bdu', secondary_source_text='官网留资', leads_stage_code='2', leads_stage_text='MQL', mark_mql_at_str=str(int(time.time() * 1000)), crm_remark=instance.consult_detail, phone=instance.phone, name=instance.name, company=instance.company, consult_category_code=instance.consult_category_code, consult_category_text=instance.consult_category_text, job_title_code=instance.job_title_code, job_title_text=instance.job_title_text, utm_medium=instance.utm_medium, utm_source=instance.utm_source, utm_campaign=instance.utm_campaign, utm_term=instance.utm_term, referer=(instance.referer or '无')[:2000], referer2=(instance.referer2 or '无')[:2000], person_source=instance.person_source, person_source_details=instance.person_source_details, contact_us_url=(instance.current_page or '无')[:2000], related_user=instance.user, remark='官网企业版试用', crm_creator_openid='FSUID_8D4890ED424F97459A11A7B4DCA523F0', # 王静 ) @receiver( post_save, sender="pingcap_sso.FXCRMJointSolutionMutualCertification") def _sync_official_joint_solution_mutual_certification_to_lark_and_crm(sender, instance, created, **kwargs): if not created: return notify_lark_webhook = SiteSetting.official_new_joint_solution_notify_lark_webhook if notify_lark_webhook: message = f"""**中文官网 联合解决方案互认证申请**\n - 公司: { instance.company } - 公司地址: { instance.company_address } - 申请类型: { instance.application_type_text } - 联系人姓名: { instance.contact_name } - 联系人电话: { instance.contact_phone } - 联合解决方案名称: { instance.joint_solution_name } - 产品重点推广行业: { instance.joint_solution_industry_text } - 备注: 这个表单应该废弃了,不会同步数据到 CRM。 """ LarkMessage.send( webhook=notify_lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='官网联合解决方案互认证申请' ) @receiver( post_save, sender="pingcap_sso.PartnerCompatibilityCertification") def _sync_partner_compatibility_certification_to_lark_and_crm(sender, instance, created, **kwargs): if not created: return # 如果从 CRM 同步的历史数据,则不需要再同步到 CRM 以及发送通知 if instance.sync_from_crm: return notify_lark_webhook = SiteSetting.official_new_partner_compatibility_certification_notify_lark_webhook if notify_lark_webhook: message = f"""**中文官网 认证合作伙伴申请**\n - 公司: { instance.company } - 申请类型: { instance.solution_type_text } - 联系人姓名: { instance.contact_name } - 联系人电话: { instance.contact_phone } - 联合解决方案名称: { instance.product_name } - 产品重点推广行业: { instance.product_industry_text } [开始审核](https://www.fxiaoke.com/XV/UI/Home#crm/list/=/object_WP33j__c) """ LarkMessage.send( webhook=notify_lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='合作伙伴认证适配认证申请' ) if settings.STAGING or settings.DEBUG: return create_partner_compatibility_certification_in_fxiaoke_crm.apply_async((instance.id, ), countdown=5) @receiver( post_save, sender="pingcap_sso.CommunityContactUs") def _sync_community_contact_us_to_lark_and_crm(sender, instance, created, **kwargs): if not created: return notify_lark_webhook = SiteSetting.community_new_contact_us_notify_lark_webhook if not notify_lark_webhook: return contact_us = instance message = "**社区 商业支持咨询**\n" if contact_us.user: message += f'- 用户: [{ contact_us.user.username }](https://asktug.com/u/{ contact_us.user.username }/summary)\n' message += f'- 经验值: { contact_us.user.point_stat.experience }\n' message += f'- 是否通过 PCTA: { "是" if contact_us.user.pass_pcta else "否" }\n' message += f'- 是否通过 PCTP: { "是" if contact_us.user.pass_pctp else "否" }\n' tidb_usage = list(UserTiDBUsage.objects.filter( user=contact_us.user, deleted_at__isnull=True ).values_list('version', flat=True).distinct()) if tidb_usage: message += f'- TiDB 使用版本: { ", ".join(tidb_usage) }\n' asktug_summary = asktug_user_summary(contact_us.user) if asktug_summary: message += f'- AskTUG 发帖数: { asktug_summary["topic_count"] }\n' message += f'- AskTUG 回复数: { asktug_summary["post_count"] }\n' message += f"""- 姓名: { contact_us.name } - 手机号: { contact_us.phone } - 公司: { contact_us.company } - 咨询事宜: { contact_us.consult_detail } """ LarkMessage.send( webhook=notify_lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='社区商业支持咨询', ) if settings.STAGING or settings.DEBUG: return FXCRMLeads.objects.create( leads_type='r303W021k', # 社区线索 leads_poll_id=FXCRMLeads.LEADS_POLL_COMMUNITY, source_code='0z11Swnbp', source_text='AskTUG', primary_source_code='N8n065zgC', primary_source_text='社区', secondary_source_code='other', secondary_source_text='其他', leads_stage_code='2', leads_stage_text='MQL', mark_mql_at_str=str(int(time.time() * 1000)), crm_remark=instance.consult_detail, phone=instance.phone, name=instance.name, company=instance.company, related_user=instance.user, remark='社区商业支持咨询', crm_creator_openid='FSUID_EE7A00DA91B32217C313554BCDD45793', # 黄漫绅 )