@@ -0,0 +1,83 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +import logging  | 
            |
| 4 | 
                +  | 
            |
| 5 | 
                +from django.db.models import Sum  | 
            |
| 6 | 
                +from django_six import CompatibilityBaseCommand, close_old_connections  | 
            |
| 7 | 
                +from TimeConvert import TimeConvert as tc  | 
            |
| 8 | 
                +  | 
            |
| 9 | 
                +from integral.models import SaleclerkSubmitLogInfo  | 
            |
| 10 | 
                +from mch.models import BrandInfo, DistributorInfo, ModelInfo  | 
            |
| 11 | 
                +from sales.models import SalesResponsibilityInfo, SalesResponsibilityInfoModelsSaleStatisticInfo, SuperSalesResponsibilityInfoModelsSaleStatisticInfo  | 
            |
| 12 | 
                +  | 
            |
| 13 | 
                +  | 
            |
| 14 | 
                +logger = logging.getLogger('console')
               | 
            |
| 15 | 
                +  | 
            |
| 16 | 
                +  | 
            |
| 17 | 
                +class Command(CompatibilityBaseCommand):  | 
            |
| 18 | 
                + def handle(self, *args, **options):  | 
            |
| 19 | 
                +  | 
            |
| 20 | 
                +        logger.info('Sales is dealing')
               | 
            |
| 21 | 
                +  | 
            |
| 22 | 
                + close_old_connections()  | 
            |
| 23 | 
                +  | 
            |
| 24 | 
                + # year = tc.local_string(format='%Y')  | 
            |
| 25 | 
                + month = tc.local_string(format='%Y%m')  | 
            |
| 26 | 
                + day = tc.local_string(format='%Y%m%d')  | 
            |
| 27 | 
                +  | 
            |
| 28 | 
                + # lastyear = tc.local_string(tc.several_time_ago(years=1), format='%Y')  | 
            |
| 29 | 
                + lastmonth = tc.local_string(tc.several_time_ago(months=1), format='%Y%m')  | 
            |
| 30 | 
                + lastday = tc.local_string(tc.several_time_ago(days=1), format='%Y%m%d')  | 
            |
| 31 | 
                +  | 
            |
| 32 | 
                + brands = BrandInfo.objects.filter(status=True)  | 
            |
| 33 | 
                +  | 
            |
| 34 | 
                + for b in brands:  | 
            |
| 35 | 
                + distributors = DistributorInfo.objects.filter(brand_id=b.brand_id, status=True)  | 
            |
| 36 | 
                + models = ModelInfo.objects.filter(brand_id=b.brand_id, status=True)  | 
            |
| 37 | 
                + for d in distributors:  | 
            |
| 38 | 
                + for m in models:  | 
            |
| 39 | 
                + logs = SaleclerkSubmitLogInfo.objects.filter(distributor_pk=d.pk, model_pk=m.pk, dupload=False, test_sn=False, status=True)  | 
            |
| 40 | 
                + # today_num = logs.filter(ymd=day).count()  | 
            |
| 41 | 
                + yesterday_num = logs.filter(ymd=lastday).count()  | 
            |
| 42 | 
                + current_month = logs.filter(ym=month).count()  | 
            |
| 43 | 
                + last_month = logs.filter(ym=lastmonth).count()  | 
            |
| 44 | 
                + if m.is_important or (yesterday_num or current_month or last_month):  | 
            |
| 45 | 
                + SalesResponsibilityInfoModelsSaleStatisticInfo.objects.create(  | 
            |
| 46 | 
                + brand_id=b.brand_id,  | 
            |
| 47 | 
                + sr_id=d.sr_id,  | 
            |
| 48 | 
                + distributor_id=d.distributor_id,  | 
            |
| 49 | 
                + distributor_name=d.distributor_name,  | 
            |
| 50 | 
                + model_id=m.model_id,  | 
            |
| 51 | 
                + model_name=m.model_name,  | 
            |
| 52 | 
                + is_important=m.is_important,  | 
            |
| 53 | 
                + ymd=day,  | 
            |
| 54 | 
                + yesterday_num=yesterday_num,  | 
            |
| 55 | 
                + current_month=current_month,  | 
            |
| 56 | 
                + last_month=last_month,  | 
            |
| 57 | 
                + )  | 
            |
| 58 | 
                +  | 
            |
| 59 | 
                + srs = SalesResponsibilityInfo.objects.filter(brand_id=b.brand_id, status=True)  | 
            |
| 60 | 
                + for s in srs:  | 
            |
| 61 | 
                + sums = SalesResponsibilityInfoModelsSaleStatisticInfo.objects.filter(  | 
            |
| 62 | 
                + sr_id=s.sr_id,  | 
            |
| 63 | 
                + ymd=day,  | 
            |
| 64 | 
                + status=True  | 
            |
| 65 | 
                + ).aggregate(  | 
            |
| 66 | 
                +                    Sum('yesterday_num'),
               | 
            |
| 67 | 
                +                    Sum('current_month'),
               | 
            |
| 68 | 
                +                    Sum('last_month'),
               | 
            |
| 69 | 
                + )  | 
            |
| 70 | 
                +                yesterday_num = sums.get('yesterday_num__sum', 0) or 0
               | 
            |
| 71 | 
                +                current_month = sums.get('current_month__sum', 0) or 0
               | 
            |
| 72 | 
                +                last_month = sums.get('last_month__sum', 0) or 0
               | 
            |
| 73 | 
                + SuperSalesResponsibilityInfoModelsSaleStatisticInfo.objects.create(  | 
            |
| 74 | 
                + brand_id=b.brand_id,  | 
            |
| 75 | 
                + sr_id=s.sr_id,  | 
            |
| 76 | 
                + sr_name=s.name,  | 
            |
| 77 | 
                + ymd=day,  | 
            |
| 78 | 
                + yesterday_num=yesterday_num,  | 
            |
| 79 | 
                + current_month=current_month,  | 
            |
| 80 | 
                + last_month=last_month,  | 
            |
| 81 | 
                + )  | 
            |
| 82 | 
                +  | 
            |
| 83 | 
                + close_old_connections()  | 
            
                @@ -99,6 +99,9 @@ class SaleclerkSubmitLogInfo(BaseModelMixin):  | 
            ||
| 99 | 99 | 
                test_user = models.BooleanField(_(u'test_user'), default=False, help_text=_(u'是否为测试用户'), db_index=True)  | 
            
| 100 | 100 | 
                test_sn = models.BooleanField(_(u'test_sn'), default=False, help_text=_(u'是否为测试序列号'), db_index=True)  | 
            
| 101 | 101 | 
                 | 
            
| 102 | 
                + ym = models.IntegerField(_(u'ym'), default=0, help_text=u'年月', db_index=True) # 例:201712, tc.local_string(format='%Y%m')  | 
            |
| 103 | 
                + ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True) # 例:20171208, tc.local_string(format='%Y%m%d')  | 
            |
| 104 | 
                +  | 
            |
| 102 | 105 | 
                class Meta:  | 
            
| 103 | 106 | 
                verbose_name = _(u'saleclerksubmitloginfo')  | 
            
| 104 | 107 | 
                verbose_name_plural = _(u'saleclerksubmitloginfo')  | 
            
                @@ -2,8 +2,7 @@ CodeConvert==2.0.5  | 
            ||
| 2 | 2 | 
                MySQL-python==1.2.5  | 
            
| 3 | 3 | 
                Pillow==5.0.0  | 
            
| 4 | 4 | 
                StatusCode==1.0.0  | 
            
| 5 | 
                -TimeConvert==1.4.4  | 
            |
| 6 | 
                -cryptography==1.5.2  | 
            |
| 5 | 
                +TimeConvert==1.5.0  | 
            |
| 7 | 6 | 
                furl==2.0.0  | 
            
| 8 | 7 | 
                isoweek==1.3.3  | 
            
| 9 | 8 | 
                jsonfield==2.0.2  | 
            
                @@ -80,6 +80,9 @@ class SalesResponsibilityInfo(BaseModelMixin):  | 
            ||
| 80 | 80 | 
                class SalesResponsibilityInfoModelsSaleStatisticInfo(BaseModelMixin):  | 
            
| 81 | 81 | 
                brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)  | 
            
| 82 | 82 | 
                 | 
            
| 83 | 
                + # 销售担当,供 SuperSalesResponsibilityInfoModelsSaleStatisticInfo 统计使用  | 
            |
| 84 | 
                + sr_id = models.CharField(_(u'sr_id'), max_length=32, help_text=u'销售担当唯一标识', db_index=True)  | 
            |
| 85 | 
                +  | 
            |
| 83 | 86 | 
                # 经销商  | 
            
| 84 | 87 | 
                distributor_id = models.CharField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True)  | 
            
| 85 | 88 | 
                distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')  |