from django.db import models

# Create your models here.
from django.utils.translation import gettext_lazy as _

class BindingType(models.Model):
    name=models.CharField(_('绑定方式'),max_length=50,unique=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('瓶盖内外码绑定方式')
        verbose_name_plural=_('瓶盖内外码绑定方式')

    def __str__(self):
        return self.name

class Factory(models.Model):
    company=models.OneToOneField('company.Company',verbose_name=_('瓶盖厂名称'),on_delete=models.CASCADE,related_name='factory_company')
    customers=models.JSONField(_('瓶盖厂客户'),default=list)
    tm_create=models.DateTimeField(_('创建时间'),auto_now_add=True)
    tm_edit=models.DateTimeField(_('最后修改时间'),auto_now=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('瓶盖厂客户')
        verbose_name_plural=_('瓶盖厂客户')

    def __str__(self):
        return self.company.name

class CodeType(models.Model):
    name=models.CharField(_('名称'),max_length=20,unique=True)
    code=models.CharField(_('代码'),max_length=20,unique=True)
    remark=models.CharField(_('备注'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('瓶盖码类型')
        verbose_name_plural=_('瓶盖码类型')

    def __str__(self):
        return self.name

class Inform(models.Model):
    factory=models.ForeignKey('company.Company',verbose_name=_('瓶盖厂名称'),on_delete=models.CASCADE,related_name='inform_factory')
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),on_delete=models.CASCADE,related_name='inform_company')
    codetype=models.ForeignKey(CodeType,verbose_name=_('盖码类型'),on_delete=models.CASCADE,related_name='inform_codetype')
    bindingtype=models.ForeignKey(BindingType,verbose_name=_('内外码绑定方式'),on_delete=models.CASCADE,related_name='factory_bindingtype',default=2,blank=True,null=True)
    can_download=models.BooleanField(_('是否允许下载'),default=True)
    is_delete=models.BooleanField(_('是否删除'),default=False)
    downloaded=models.BooleanField(_('是否已下载'),default=False)
    product=models.CharField(_('产品名称'),max_length=50)
    inform_id=models.IntegerField(_('业务通知单id'))
    inform_code=models.CharField(_('业务通知单编号'),max_length=50)
    segment=models.CharField(_('号段'),max_length=10)
    sn_fr=models.IntegerField(_('起始号码'))
    sn_to=models.IntegerField(_('结束号码'))
    qty=models.IntegerField(_('数量'))
    dt_inform=models.DateField(_('下单日期'))
    tm_inform=models.DateTimeField(_('下单时间'))
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    tm_create=models.DateTimeField(_('创建时间'),auto_now_add=True)
    tm_edit=models.DateTimeField(_('最后修改时间'),auto_now=True)

    class Meta:
        verbose_name=_('业务通知单信息')
        verbose_name_plural=_('业务通知单信息')

    def __str__(self):
        return self.inform_code

class DownloadLog(models.Model):
    factory=models.ForeignKey('company.Company',verbose_name=_('瓶盖厂名称'),on_delete=models.CASCADE,related_name='downloadlog_factory')
    inform_id=models.IntegerField(_('业务通知单ID'))
    user=models.ForeignKey('company.User',verbose_name=_('下载人'),on_delete=models.CASCADE,related_name='downloadlog_user')
    file_name=models.CharField(_('zip文件名称'),max_length=100)
    file_size=models.DecimalField(_('zip文件大小'),   max_digits=12, decimal_places=2, blank=True, null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt=models.DateField(_('日期'),auto_now_add=True)
    tm=models.DateTimeField(_('时间'),auto_now_add=True)


    class Meta:
        verbose_name=_('码数据下载日志')
        verbose_name_plural=_('码数据下载日志')

    def __str__(self):
        return self.file_name
