from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.postgres.fields import ArrayField

# Create your models here.


class Category(models.Model):
    company = models.ForeignKey('company.Company', verbose_name=_(
        '公司'), on_delete=models.CASCADE, related_name='banquet_company')
    name = models.CharField(_('分类名称'), max_length=100)
    remark = models.CharField(_('备注说明'), max_length=100, blank=True, null=True)
    tm = models.DateTimeField(_('生成时间'), auto_now_add=True)

    class Meta:
        verbose_name = _('宴会分类')
        verbose_name_plural = _('宴会分类')


class Apply(models.Model):
    ancestor = models.ForeignKey('company.Company', verbose_name=_(
        '体系名称'), on_delete=models.CASCADE, related_name='apply_ancestor')
    company = models.ForeignKey('company.Company', verbose_name=_(
        '申请公司'), on_delete=models.DO_NOTHING)
    applyer = models.ForeignKey('company.User', verbose_name=_(
        '申请人'), on_delete=models.DO_NOTHING)
    category = models.ForeignKey(Category, verbose_name=_(
        '宴会类别'), on_delete=models.DO_NOTHING) 
    award = models.ForeignKey('sales.Award', verbose_name=_(
        '奖品'), on_delete=models.SET_NULL, blank=True, null=True)
    product = models.ForeignKey('product.Product', verbose_name=_(
        '产品'), on_delete=models.SET_NULL, blank=True, null=True)
    policy = models.CharField(      
        _('宴会政策'), max_length=300, blank=True, null=True)
    address = models.CharField(_('宴会地址'), max_length=500)
    lng_lat = ArrayField(models.DecimalField(
        _('经纬度'), max_digits=12, decimal_places=8), blank=True, null=True)
    table_num = models.SmallIntegerField(_('宴席桌数'))
    user_name = models.CharField(_('宴会人名称'), max_length=20)
    user_phone = models.CharField(_('宴会人手机'), max_length=20)
    use_tm = models.DateTimeField(_('宴会时间'))
    tm = models.DateTimeField(_('申请时间'), auto_now_add=True)
    businessman = models.ForeignKey('company.User', verbose_name=_(
        '业务员'), blank=True, null=True, on_delete=models.DO_NOTHING, related_name='apply_businessman')
    permit_tm = models.DateTimeField(_('审核时间'), blank=True, null=True)
    permiter = models.ForeignKey('company.User', verbose_name=_(
        '审核人'), on_delete=models.DO_NOTHING, related_name='apply_permiter', blank=True, null=True)
    finish_tm = models.DateTimeField(_('完成时间'), blank=True, null=True)
    status = models.SmallIntegerField(_('状态'), choices=(
        (0, _('未审核')),
        (1, _('已通过')),
        (2, _('未通过')), ), default=0)
    reason = models.CharField(_('原因'), max_length=500, blank=True, null=True)
    annotation = models.CharField(_('批注'), max_length=500, blank=True, null=True)
    # 冗余，为提高查询速度
    channelpromotions = models.ForeignKey('sales.ChannelPromotions', verbose_name=_('所属渠道返利'),
                                          on_delete=models.CASCADE, related_name='apply_activity',
                                          blank=True, null=True)

    class Meta:
        verbose_name = _('宴会申请')
        verbose_name_plural = _('宴会申请')


class Apply_Codes(models.Model):
    apply = models.ForeignKey(Apply, verbose_name=_(
        '宴会申请记录'),  on_delete=models.CASCADE)
    code = models.CharField(_('标签序号'), max_length=20)
    product = models.ForeignKey('product.Product', verbose_name=_(
        '产品名称'), on_delete=models.SET_NULL, blank=True, null=True)
    package_id = models.SmallIntegerField(_('包装单位ID'), default=1)
    label_id = models.BigIntegerField(_('标签ID'), db_index=True,null=True,blank=True)
    label_pk_id = models.BigIntegerField(_('标签PKID'), db_index=True, null=True,blank=True)
    dt = models.DateField(_('生成日期'),auto_now_add=True,  blank=True, null=True, db_index=True)

    class Meta:
        verbose_name = _('宴会申请标签序号')
        verbose_name_plural = _('宴会申请标签序号')


class Apply_Pictures(models.Model):
    apply = models.ForeignKey(Apply, verbose_name=_(
        '宴会申请记录'),  on_delete=models.CASCADE)
    photo = models.CharField(_('标签序号'), max_length=300)

    class Meta:
        verbose_name = _('宴会申请图片')
        verbose_name_plural = _('宴会申请图片')
