from django.db import models
from apps.system.models import CommonBModel,CommonAModel,Dict
from simple_history.models import HistoricalRecords
from django.contrib.postgres.fields import ArrayField
import uuid
# Create your models here.
class Enterprise(CommonBModel):
    code = models.CharField('工厂编号',unique=True, max_length=300)
    name = models.CharField('工厂名称',null=True, blank=True,max_length=1000)
    ename =  models.CharField('工厂英文名称',null=True, blank=True,max_length=1000)
    type = models.ForeignKey(Dict, on_delete=models.SET_NULL,null=True, blank=True,verbose_name='机构类型', related_name= 'enterprise_type')
    legal =  models.CharField('申请单位法人代表',null=True, blank=True,max_length=300)
    build_time = models.DateField('建厂时间',null=True, blank=True)
    person_count = models.IntegerField('工厂人数',null=True, blank=True)
    ceramics_output = models.IntegerField('陶瓷年产量',null=True, blank=True)
    gassets = models.CharField('固定资产',max_length=100,null=True, blank=True)
    linkman1_name = models.CharField('第1联系人',max_length=300,null=True, blank=True)
    linkman1_duty = models.CharField('第1联系人职务',max_length=300,null=True, blank=True)
    linkman1_email = models.EmailField('第1联系人邮箱',max_length=300,null=True, blank=True)
    linkman1_tel = models.CharField('第1联系人电话',max_length=300,null=True, blank=True)
    linkman1_mobile = models.CharField('第1联系人手机',max_length=300,null=True, blank=True)
    linkman1_fax = models.CharField('第1联系人传真',max_length=300,null=True, blank=True)
    linkman2_name = models.CharField('备用联系人',max_length=300,null=True, blank=True)
    linkman2_email = models.EmailField('备用联系人邮箱',max_length=300,null=True, blank=True)
    linkman2_tel = models.CharField('备用联系人电话',max_length=300,null=True, blank=True)
    linkman2_duty = models.CharField('备用联系人职务',max_length=300,null=True, blank=True)
    linkman2_mobile = models.CharField('备用联系人手机',max_length=300,null=True, blank=True)
    linkman2_fax = models.CharField('备用联系人传真',max_length=300,null=True, blank=True)
    country_code = models.ForeignKey(Dict, on_delete=models.SET_NULL,null=True, blank=True,verbose_name='生产企业所在国家地区代码', related_name= 'enterprise_country_code')
    credit_code = models.CharField('统一社会信用代码',max_length=300,null=True, blank=True)
    region_code = models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='县/区',null=True, blank=True, related_name= 'enterprise_region_code1')
    professional = models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='所属专业',null=True, blank=True, related_name= 'enterprise_professional')
    qms_person = models.IntegerField('QMS人数',null=True, blank=True)
    ems_person = models.IntegerField('EMS人数',null=True, blank=True)
    pcv_person = models.IntegerField('PCV人数',null=True, blank=True)
    ohs_person = models.IntegerField('OHS人数',null=True, blank=True)
    all_person = models.IntegerField('组织员工数',null=True, blank=True)
    economy_class = models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='经济行业',null=True, blank=True, related_name= 'enterprise_economy_class')
    economy_type = models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='经济类型',null=True, blank=True, related_name= 'enterprise_economy_type')
    business_type = models.CharField('经营范围',max_length=1000,null=True, blank=True)
    credit_rate = models.CharField('企业信誉级别',max_length=300,null=True, blank=True)
    credit_remark = models.CharField('企业信誉级别备注',max_length=1000,null=True, blank=True)
    # xinyu_oper = models.CharField('信誉法人',null=True, blank=True, max_length=1000)
    # xinyu_date = models.CharField('信誉时间',null=True, blank=True, max_length=1000)
    query_code = models.CharField('组织查询码',max_length=1000,null=True, blank=True)
    linkman3_name = models.CharField('ES联系人',max_length=300,null=True, blank=True)
    linkman3_duty = models.CharField('ES联系人职务',max_length=300,null=True, blank=True)
    linkman3_email = models.EmailField('ES联系人邮箱',max_length=300,null=True, blank=True)
    linkman3_tel = models.CharField('ES联系人电话',max_length=300,null=True, blank=True)
    linkman3_mobile = models.CharField('ES联系人手机',max_length=300,null=True, blank=True)
    linkman3_fax = models.CharField('ES联系人传真',max_length=300,null=True, blank=True)
    remarks = models.CharField('备注',max_length=1000,null=True, blank=True)
    class Meta:
        verbose_name = '组织信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
class EnterpriseAddress(CommonAModel):
    type_choices = (
        ('注册地址', '注册地址'),
        ('通信地址', '通信地址'),
        ('生产地址', '生产地址')
    )
    enterprise= models.ForeignKey(Enterprise, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='地址属性',  related_name='enterpriseaddress_enterprise')
    address = models.CharField('地址名称',max_length=1000,null=True, blank=True)
    eaddress = models.CharField('地址英文名称',max_length=1000,null=True, blank=True)
    code = models.CharField('地址邮编',max_length=1000,null=True, blank=True)
    type = models.CharField('地址类型', max_length=50,choices=type_choices, default='生产地址')
    county_code =  models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='县/区代码',null=True, blank=True, related_name= 'enterpriseaddress_county_code')
    country_code =  models.ForeignKey(Dict, on_delete=models.SET_NULL,verbose_name='国家代码',null=True, blank=True, related_name= 'enterpriseaddress_country_code')
    countyarry = ArrayField(models.IntegerField(),null=True, blank=True)
    class Meta:
        verbose_name = '组织地址'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

