import random from datetime import datetime from django.db import models from django.conf import settings from django.utils.translation import gettext_lazy as _ def _generate_number(): max_retries = 3 for _ in range(max_retries): number = 'AP{ts}{randstr}'.format( ts=datetime.now().strftime('%Y%m%d%H%M%S'), randstr=random.randint(1000, 9999) ) # Check if number already exists if not PartnerCompatibilityCertification.objects.filter(number=number).exists(): return number # If all retries failed, use microsecond precision return 'AP{ts}{randstr}'.format( ts=datetime.now().strftime('%Y%m%d%H%M%S%f'), randstr=random.randint(1000, 9999) ) class PartnerCompatibilityCertification(models.Model): STATUS_PENDING = 'pending' STATUS_APPROVED = 'approved' STATUS_REJECTED = 'rejected' STATUS_CHOICES = ( (STATUS_PENDING, _('pending')), (STATUS_APPROVED, _('approved')), (STATUS_REJECTED, _('rejected')), ) SYNC_STATUS_PENDING = 'pending' SYNC_STATUS_SUCCESS = 'success' SYNC_STATUS_FAILED = 'failed' SYNC_STATUS_CHOICES = ( (SYNC_STATUS_PENDING, _('pending')), (SYNC_STATUS_SUCCESS, _('success')), (SYNC_STATUS_FAILED, _('failed')), ) number = models.CharField( max_length=100, help_text='申请单号', default=_generate_number, blank=True, null=True, unique=True ) company = models.CharField(max_length=100) company_address = models.CharField(max_length=100) company_logo = models.ForeignKey( 'pingcap_sso.Upload', blank=True, null=True, on_delete=models.DO_NOTHING, related_name='partner_compatibility_certification_company_logo' ) company_area_code = models.CharField(max_length=100, blank=True, null=True, help_text='公司所在的区域(代码)') company_area_text = models.CharField(max_length=100, blank=True, null=True, help_text='公司所在的区域(描述)') contact_name = models.CharField(max_length=100, help_text='联系人') contact_phone = models.CharField(max_length=100, help_text='联系人电话') contact_email = models.EmailField(max_length=100, help_text='联系人邮箱') contact_job_title_code = models.CharField(max_length=100, blank=True, null=True, help_text='联系人职务(代码)') contact_job_title_text = models.CharField(max_length=100, blank=True, null=True, help_text='联系人职务(描述)') shipping_address = models.CharField(max_length=200, help_text='证书邮寄地址') cert_owner_name = models.CharField(max_length=100, help_text='合作伙伴证书负责人姓名') cert_owner_job_title = models.CharField(max_length=100, help_text='合作伙伴证书负责人职务') product_name = models.CharField(max_length=100, help_text='产品名称') product_description = models.CharField(max_length=200, help_text='产品描述') product_industry_code = models.CharField(max_length=100, blank=True, null=True, help_text='产品行业(代码)') product_industry_text = models.CharField(max_length=100, blank=True, null=True, help_text='产品行业(描述)') infra_category_code = models.CharField(max_length=100, blank=True, null=True, help_text='基础设施分类(代码)') infra_category_text = models.CharField(max_length=100, blank=True, null=True, help_text='基础设施分类(描述)') purpose_of_certification = models.CharField(max_length=100, help_text='认证用途') compatible_database_version_code = models.CharField(max_length=100, blank=True, null=True, help_text='兼容数据库版本(代码)') compatible_database_version_text = models.CharField(max_length=100, blank=True, null=True, help_text='兼容数据库版本(描述)') solution_type_code = models.CharField(max_length=100, blank=True, null=True, help_text='解决方案类型(代码)') solution_type_text = models.CharField(max_length=100, blank=True, null=True, help_text='解决方案类型(描述)') attachment_1 = models.ForeignKey( 'pingcap_sso.Upload', blank=True, null=True, on_delete=models.DO_NOTHING, related_name='partner_compatibility_certification_attachment_1' ) cert_number = models.CharField(max_length=100, blank=True, null=True, help_text='证书编号') cert_valid_from = models.DateField(blank=True, null=True, help_text='证书有效期开始时间') cert_valid_to = models.DateField(blank=True, null=True, help_text='证书有效期结束时间') user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name='created_partner_compatibility_certifications', ) status = models.CharField( max_length=16, default=STATUS_PENDING, choices=STATUS_CHOICES, blank=True ) staff_comment = models.TextField(blank=True, null=True, help_text='审核意见') crm_creator_openid = models.CharField( max_length=64, default='FSUID_8D4890ED424F97459A11A7B4DCA523F0', blank=True, help_text='CRM 线索创建者 OpenID, 默认为王静' ) sync_status = models.CharField( max_length=16, default=SYNC_STATUS_PENDING, choices=SYNC_STATUS_CHOICES, blank=True ) sync_at = models.DateTimeField(blank=True, null=True) request_body = models.TextField(blank=True, null=True) response_body = models.TextField(blank=True, null=True) crm_object_id = models.CharField(max_length=64, blank=True, null=True) sync_from_crm = models.BooleanField(default=False, help_text='是否从 CRM 同步的数据') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = 'pingcap_partner_compatibility_certifications'