from django.db import models
from django.utils.translation import gettext_lazy as _


# Create your models here.


class Collector(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('所属企业'), related_name='CollectorCompany',
                                on_delete=models.PROTECT)
    member = models.ForeignKey('sales.Member', verbose_name=_('消费者帐号'), on_delete=models.PROTECT)
    name = models.CharField(_('姓名'), max_length=20, blank=True, null=True)
    phone = models.CharField(_('手机'), max_length=11, blank=True, null=True)
    gender = models.SmallIntegerField(_('性别'), choices=((1, _('男')), (2, _('女')), (0, _('未知'))), blank=True,
                                      null=True)
    address = models.CharField(_('详细地址'), max_length=100, blank=True, null=True)
    dt = models.DateField(_('日期'), auto_now_add=True, db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)

    class Meta:
        verbose_name = _('收藏人信息')
        verbose_name_plural = _('收藏人信息')

    def __str__(self):
        return self.name


class Artical(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('所属企业'), related_name='ArticalCompany',
                                on_delete=models.PROTECT)
    product = models.ForeignKey('product.Product', verbose_name=_('产品名称'), related_name='Articalproduct',
                                on_delete=models.PROTECT, blank=True, null=True)
    collector = models.ForeignKey(Collector, verbose_name=_('收藏人'), related_name='ArticalCollector',
                                  on_delete=models.PROTECT)
    code = models.CharField(_('编号或标签'), max_length=20, blank=True, null=True, unique=True)
    qty = models.IntegerField(_('数量'), blank=True, null=True)
    unit = models.CharField(_('单位'), max_length=20, blank=True, null=True)
    dt = models.DateField(_('日期'), auto_now_add=True, db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)

    class Meta:
        verbose_name = _('藏品信息')
        verbose_name_plural = _('藏品信息')
        unique_together = ("company", "product", "code")

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

#消费者操作日志
class ConsumeLog(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('所属企业'), related_name='ConsumeLogCompany',
                                on_delete=models.PROTECT)
    artical = models.ForeignKey(Artical, verbose_name=_('藏品'), related_name='ConsumeLogArtical',
                                on_delete=models.PROTECT)
    collector = models.ForeignKey(Collector, verbose_name=_('收藏人'), related_name='ConsumeLogCollector',
                                  on_delete=models.PROTECT)
    direction = models.SmallIntegerField(_('库存影响'), choices=(
        (1, _('增加')),
        (-1, _('减少')),
        (0, _('不变'))
    ))
    qty = models.IntegerField(_('数量'), blank=True, null=True)
    unit = models.SmallIntegerField(_('单位'), blank=True, null=True, choices=(
        (0, _('毫升')),
        (3, _('箱')),
        (2, _('盒')),
        (1, _('瓶'))),default=0)
    dt = models.DateField(_('日期'), auto_now_add=True, db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)

    class Meta:
        verbose_name = _('消费日志')
        verbose_name_plural = _('消费日志')

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

#管理员操作日志
class AdjLog(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_('所属企业'), related_name='AdjLogCompany',
                                on_delete=models.PROTECT)
    artical = models.ForeignKey(Artical, verbose_name=_('藏品'), related_name='AdjLogArtical', on_delete=models.PROTECT)
    user = models.ForeignKey('company.User', verbose_name=_('操作人'), related_name='AdjLogUser', on_delete=models.PROTECT)

    direction = models.SmallIntegerField(_('库存影响'), choices=(
        (1, _('增加')),
        (-1, _('减少')),
        (0, _('不变'))
    ))
    qty = models.IntegerField(_('数量'), blank=True, null=True)
    unit = models.SmallIntegerField(_('单位'), blank=True, null=True, choices=(
        (0, _('毫升')),
        (3, _('箱')),
        (2, _('盒')),
        (1, _('瓶'))),default=0)
    dt = models.DateField(_('日期'), auto_now_add=True, db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True)
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)

    class Meta:
        verbose_name = _('后台修改日志')
        verbose_name_plural = _('后台修改日志')

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



# 2023-06-12
class Binding(models.Model):
    company=models.ForeignKey('company.Company',verbose_name=_('所属企业'),related_name='binding_company', on_delete=models.PROTECT)
    product = models.ForeignKey('product.Product',verbose_name=_('产品名称'),related_name='binding_product', on_delete=models.PROTECT, blank=True, null=True) 
    artical=models.ForeignKey(Artical,verbose_name=_('藏品'),related_name='binding_artical',on_delete=models.PROTECT,blank=True,null=True)
    label_code=models.CharField(_('标签号码'),max_length=20,db_index=True)   
    nfc_code=models.CharField(_('NFC编码'),max_length=30,unique=True,blank=True,null=True,db_index=True)
    item_code=models.CharField(_('藏品编码'),max_length=30,blank=True,null=True,db_index=True)      
    label_pkg=models.SmallIntegerField(_('标签类型'),choices=(
        (3, _('箱')),
        (2, _('盒')),
        (1, _('瓶'))),
        default=2,db_index=True
    )
    label_id=models.BigIntegerField(_('标签id'),blank=True,null=True)
    label_pk_id=models.BigIntegerField(_('主箱标id'),blank=True,null=True,db_index=True)  
    dt = models.DateField(_('日期'), auto_now_add=True, db_index=True)
    tm = models.DateTimeField(_('时间'), auto_now_add=True) 
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)
    
    class Meta:
        verbose_name = _('产品标签/NFC数据绑定')
        verbose_name_plural = _('产品标签/NFC数据绑定')
        # 每个企业单个藏品的收藏码唯一
        unique_together=('company','item_code')

   
