Sin Descripción

models.py 11KB

    # -*- coding: utf-8 -*- from django.conf import settings from django.db import models from django.utils.translation import ugettext_lazy as _ from django_models_ext import BaseModelMixin, upload_file_url, upload_path from shortuuidfield import ShortUUIDField from jsonfield import JSONField from datetime import datetime from django.utils import timezone class SalesResponsibilityInfo(BaseModelMixin): REFUSED = -1 UNVERIFIED = 0 ACTIVATED = 1 DISABLED = 2 DELETED = 3 ASSIGN = 10 USER_STATUS_TUPLE = ( (REFUSED, u'已拒绝'), (UNVERIFIED, u'未验证'), (ACTIVATED, u'已激活'), (DISABLED, u'已禁用'), (DELETED, u'已删除'), (ASSIGN, u'已分配'), ) brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True) brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称') sr_id = ShortUUIDField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True, unique=True) user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True) unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 UnionID', db_index=True) openid = models.CharField(_(u'openid'), max_length=32, blank=True, null=True, help_text=u'微信 OpenID', db_index=True) name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'销售担当姓名') phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'销售担当联系电话') avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'销售担当头像') user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=UNVERIFIED, help_text=u'用户状态', db_index=True) refused_reason = models.TextField(_(u'refused_reason'), blank=True, null=True, help_text=u'审核拒绝原因') is_auth = models.BooleanField(_(u'is_auth'), default=False, help_text=u'是否已授权') is_super = models.BooleanField(_(u'is_super'), default=False, help_text=u'是否超级销售担当') class Meta: verbose_name = _(u'销售担当信息') verbose_name_plural = _(u'销售担当信息') unique_together = ( ('unionid', 'brand_id'), ) def __unicode__(self): return u'{}-{}'.format(self.name, self.phone) @property def final_avatar(self): return self.avatar and self.avatar.replace(settings.QINIU_FILE_URL_BEFORE, settings.QINIU_FILE_URL_AFTER).replace(settings.QINIU_FILE_URL_BEFORE2, settings.QINIU_FILE_URL_AFTER) @property def base_data(self): return { 'sr_id': self.sr_id, 'is_sr': True, 'is_super': self.is_super, } @property def admindata(self): return { 'brand_id': self.brand_id, 'brand_name': self.brand_name, 'sr_id': self.sr_id, 'name': self.name, 'phone': self.phone, 'status': self.user_status, 'refused_reason': self.refused_reason, 'is_auth': self.is_auth, } data = admindata class SalesResponsibilityInfoModelsSaleStatisticInfo(BaseModelMixin): brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True) # 销售担当,供 SuperSalesResponsibilityInfoModelsSaleStatisticInfo 统计使用 sr_id = models.CharField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True) # 经销商 distributor_id = models.CharField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True) distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称') # 型号 model_id = models.CharField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True) model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称', db_index=True) is_important = models.BooleanField(_(u'is_important'), default=True, help_text=u'是否重要型号') ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True) # 例:20171208, tc.local_string(format='%Y%m%d'), 0 为全部 today_num = models.IntegerField(_(u'today_num'), default=0, help_text=u'支数(今日)') yesterday_num = models.IntegerField(_(u'yesterday_num'), default=0, help_text=u'支数(昨日)') current_month = models.IntegerField(_(u'current_month'), default=0, help_text=u'支数(本月)') last_month = models.IntegerField(_(u'last_month'), default=0, help_text=u'支数(上月)') position = models.IntegerField(_(u'position'), default=1, help_text=u'排序') class Meta: verbose_name = _(u'[销售担当]型号销量统计') verbose_name_plural = _(u'[销售担当]型号销量统计') def __unicode__(self): return '%d' % self.pk @property def data(self): return { 'model_id': self.model_id, 'model_name': self.model_name, 'is_important': self.is_important, 'today_num': self.today_num, 'yesterday_num': self.yesterday_num, 'current_month': self.current_month, 'last_month': self.last_month, } class SuperSalesResponsibilityInfoModelsSaleStatisticInfo(BaseModelMixin): brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True) # 销售担当 sr_id = models.CharField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True) sr_name = models.CharField(_(u'sr_name'), max_length=255, blank=True, null=True, help_text=u'销售担当名称') sr_avatar = models.CharField(_(u'sr_avatar'), max_length=255, blank=True, null=True, help_text=u'销售担当头像') ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True) # 例:20171208, tc.local_string(format='%Y%m%d'), 0 为全部 today_num = models.IntegerField(_(u'today_num'), default=0, help_text=u'支数(今日)') yesterday_num = models.IntegerField(_(u'yesterday_num'), default=0, help_text=u'支数(昨日)') current_month = models.IntegerField(_(u'current_month'), default=0, help_text=u'支数(本月)') last_month = models.IntegerField(_(u'last_month'), default=0, help_text=u'支数(上月)') position = models.IntegerField(_(u'position'), default=1, help_text=u'排序') class Meta: verbose_name = _(u'[超级销售担当]销售担当销量统计') verbose_name_plural = _(u'[超级销售担当]销售担当销量统计') def __unicode__(self): return '%d' % self.pk @property def data(self): return { 'sr_id': self.sr_id, 'sr_name': self.sr_name, 'sr_avatar': self.sr_avatar, 'today_num': self.today_num, 'yesterday_num': self.yesterday_num, 'current_month': self.current_month, 'last_month': self.last_month, } class LotteryCampaignInfo(BaseModelMixin): """抽奖活动""" campaign_id = ShortUUIDField(_(u'campaign_id'), max_length=32, blank=True, null=True, help_text=u'活动唯一标识', db_index=True, unique=True) title = models.CharField(_(u'title'), max_length=255, help_text=u'活动标题') start_at = models.DateTimeField(_(u'start_at'), blank=True, null=True, help_text=u'参与开始时间') end_at = models.DateTimeField(_(u'end_at'), blank=True, null=True, help_text=u'参与结束时间') draw_at = models.DateTimeField(_(u'draw_at'), blank=True, null=True, help_text=u'开奖时间', db_index=True) model_ids = JSONField(_(u'model_ids'), blank=True, null=True, help_text=u'参与型号列表') class Meta: verbose_name = _(u'抽奖活动') verbose_name_plural = _(u'抽奖活动') ordering = ('-created_at',) def __unicode__(self): return u'{}'.format(self.title or self.campaign_id or '') def is_active(self): now = timezone.now() # ← aware,和 DB 值可比 return bool(self.start_at and self.end_at and self.start_at <= now <= self.end_at) @property def data(self): return { 'campaign_id': self.campaign_id, 'title': self.title, 'start_at': self.start_at and self.start_at.isoformat(), 'end_at': self.end_at and self.end_at.isoformat(), 'draw_at': self.draw_at and self.draw_at.isoformat(), 'model_ids': self.model_ids, } class LotteryEntryInfo(BaseModelMixin): """抽奖名单""" entry_id = ShortUUIDField(_(u'entry_id'), max_length=32, blank=True, null=True, help_text=u'名额唯一标识', db_index=True, unique=True) campaign_id = models.CharField(_(u'campaign_id'), max_length=32, help_text=u'所属活动唯一标识', db_index=True) user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True) title = models.CharField(_(u'title'), max_length=255, blank=True, null=True, help_text=u'用户姓名', db_index=True) phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'用户手机', db_index=True) serial_no = models.CharField(_(u'serial_no'), max_length=64, help_text=u'镜头序列号', db_index=True) model_id = models.CharField(_(u'model_id'), max_length=64, blank=True, null=True, help_text=u'型号唯一标识', db_index=True) model_name = models.CharField(_(u'model_id'), max_length=64, blank=True, null=True, help_text=u'型号名称', db_index=True) submit_pk = models.CharField(_(u'submit_pk'), max_length=32, blank=True, null=True, help_text=u'提交记录唯一标识', db_index=True) class Meta: verbose_name = _(u'抽奖名单') verbose_name_plural = _(u'抽奖名单') ordering = ('-created_at',) unique_together = ( ('campaign_id', 'serial_no', 'model_id'), ) def __unicode__(self): return u'{}-{}'.format(self.campaign_id, self.serial_no)