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字段
    code=models.CharField(_('代码'),max_length=20)

    schema=models.CharField(_('生号模式名'),max_length=20,blank=True,null=True)
    fname= models.CharField(_('生号字段名'),max_length=20,blank=True,null=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

class SyncLog(models.Model):
    '''
    从内网同步业务通知单码数据日志
    '''
    inform_id=models.IntegerField(_('业务通知单ID'),db_index=True)
    qty_tt=models.IntegerField(_('码总数量'),blank=True,null=True)
    qty_sync=models.IntegerField(_('本次同步数量'),blank=True,null=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt=models.DateField(_('日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('时间'),auto_now_add=True)

    class Meta:
        verbose_name=_('同步内网码数据日志')
        verbose_name_plural=_('同步内网码数据日志')

    #def __str__(self):
    #    return self.file_name


class UploadLog(models.Model):
    '''
    瓶盖厂上传内外码绑定日志
    '''
    batch_id=models.IntegerField(_('瓶盖厂绑定batch_id'),db_index=True)
    segment=models.CharField(_('内码号段'),max_length=10)
    cap_inform_id=models.IntegerField(_('内码所属业务通知单id'),db_index=True)
    cap_qty_tt=models.IntegerField(_('内码总数量'))
    cap_qty_ok=models.IntegerField(_('内码写入数量'))
    outter_inform_id=models.IntegerField(_('外码所属业务通知单id'),db_index=True)
    outter_qty_tt=models.IntegerField(_('外码总数量'))
    outter_qty_ok=models.IntegerField(_('外码写入数量'))
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)
    dt=models.DateField(_('日期'),auto_now_add=True,db_index=True)
    tm=models.DateTimeField(_('时间'),auto_now_add=True)

    class Meta:
        verbose_name=_('瓶盖厂上传内码数据日志')
        verbose_name_plural=_('瓶盖厂上传内码数据日志')


# 2024-07-17
class FctyUploadErr(models.Model):
    batch_id=models.IntegerField(_('瓶盖厂绑定batch_id'))
    codetype=models.ForeignKey(CodeType,verbose_name=_('码类型'),on_delete=models.CASCADE,related_name='fctyuploaderr_codetype')    
    e_type=models.SmallIntegerField(_('错误类型'),choices=(
        (0,_('无对应业务通知单')),    
        (1,_('本批次内重复')),
        (2,_('本批次内不一致')),
        (10,_('与往批次重复')),
        (20,_('与往批次不一致')), 
    ))
    qty=models.IntegerField(_('数量'))
    dt=models.DateField(_('日期'),auto_now_add=True)
    tm=models.DateTimeField(_('时间'),auto_now_add=True)
    remark=models.CharField(_('备注说明'),max_length=50,blank=True,null=True)

    class Meta:
        verbose_name=_('瓶盖厂数据错误日志')
        verbose_name_plural=_('瓶盖厂数据错误日志')


class FctyUploadErrDetails(models.Model):
    fctyuploaderr=models.ForeignKey(FctyUploadErr,verbose_name=('错误日志'),on_delete=models.CASCADE)
    f_sn=models.SmallIntegerField(_('txt序号'))
    line=models.IntegerField(_('所在行'))    
    code=models.CharField(_('码值'),max_length=20,db_index=True)
    code_match= models.CharField(_('当前对应码'),max_length=20,blank=True,null=True,db_index=True)
    code_match0=models.CharField(_('以往对应码'),max_length=20,blank=True,null=True,db_index=True)
    tm=models.DateTimeField(_('时间'),auto_now_add=True)

    class Meta:
        verbose_name=_('瓶盖厂数据错明细')
        verbose_name_plural=_('瓶盖厂数据错明细')
