Opt year data

Brightcells 6 年之前
父節點
當前提交
76d59df977
共有 2 個文件被更改,包括 86 次插入6 次删除
  1. 2 0
      requirements_pywe.txt
  2. 84 6
      statistic/views.py

+ 2 - 0
requirements_pywe.txt

@@ -1,3 +1,5 @@
1
+pywe-component==1.0.1
2
+pywe-component-preauthcode==1.0.3
1 3
 pywe-jssdk==1.1.0
2 4
 pywe-membercard==1.0.1
3 5
 pywe-miniapp==1.1.5

+ 84 - 6
statistic/views.py

@@ -2,12 +2,13 @@
2 2
 
3 3
 from django.conf import settings
4 4
 from django.db import transaction
5
+from django.db.models import Count, F, Sum
5 6
 from django_logit import logit
6 7
 from django_models_ext.provincemodels import ProvinceShortModelMixin
7 8
 from django_response import response
8 9
 from TimeConvert import TimeConvert as tc
9 10
 
10
-from mch.models import BrandInfo, DistributorInfo, ModelInfo
11
+from mch.models import BrandInfo, DistributorInfo, ModelInfo, SaleclerkInfo
11 12
 from statistic.models import (ConsumeDistributorSaleStatisticInfo, ConsumeModelSaleStatisticInfo,
12 13
                               ConsumeProvinceSaleStatisticInfo, ConsumeSaleStatisticInfo, ConsumeUserStatisticInfo,
13 14
                               DistributorSaleStatisticInfo, ModelSaleStatisticInfo, ProvinceSaleStatisticInfo,
@@ -203,6 +204,83 @@ def __tj_generate(ymd=None):
203 204
         )
204 205
 
205 206
 
207
+def ytj(brand_id):
208
+    # [消费者维度] 周期内扫描用户人数
209
+    cusis = ConsumeUserStatisticInfo.objects.filter(brand_id=brand_id, ymd__lt=9999)
210
+    users = []
211
+    for cusi in cusis:
212
+        users += cusi.users
213
+    scan_user_count = sum(users)
214
+
215
+    # [消费者维度] 周期内镜头销售支数
216
+    sell_volume_count = ConsumeSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd__lt=9999).aggregate(Sum('num')).get('num__sum', 0) or 0
217
+
218
+    startd = tc.local_string(months=-12, format='%Y%m%d')
219
+    endd = tc.local_string(format='%Y%m%d')
220
+
221
+    # [消费者维度] 统计周期内型号扫描排行数据,请按顺序返回
222
+    models = ConsumeModelSaleStatisticInfo.objects.filter(
223
+        brand_id=brand_id,
224
+        ymd__gt=startd,
225
+        ymd__lte=endd,
226
+        status=True
227
+    ).order_by(
228
+        'model_name'
229
+    ).values(
230
+        'model_id', 'model_name'
231
+    ).annotate(
232
+        num=Sum('num')
233
+    ).order_by(
234
+        '-num'
235
+    )[:20]
236
+
237
+    # [经销商维度] 统计周期内销售员排行数据,请按顺序返回
238
+    salesmen = SaleclerkSaleStatisticInfo.objects.filter(
239
+        brand_id=brand_id,
240
+        ymd__gt=startd,
241
+        ymd__lte=endd,
242
+        status=True
243
+    ).order_by(
244
+        'clerk_id'
245
+    ).values(
246
+        'clerk_id'
247
+    ).annotate(
248
+        num=Sum('num')
249
+    ).order_by(
250
+        '-num'
251
+    )[:20]
252
+    clerks = SaleclerkInfo.objects.filter(brand_id=brand_id, status=True)
253
+    clerks = {clerk.clerk_id: {'distributor_id': clerk.distributor_id, 'distributor_name': clerk.distributor_name, 'clerk_name': clerk.clerk_name} for clerk in clerks}
254
+    salesmen = [dict(sm, **clerks.get(sm.get('clerk_id', ''), {})) for sm in salesmen]
255
+
256
+    # [收费者维度] 统计周期内省份销量排行数据,请按顺序返回
257
+    provinces = ConsumeProvinceSaleStatisticInfo.objects.filter(
258
+        brand_id=brand_id,
259
+        ymd__gt=startd,
260
+        ymd__lte=endd,
261
+        status=True
262
+    ).order_by(
263
+        'province_code'
264
+    ).values(
265
+        'province_code'
266
+    ).annotate(
267
+        num=Sum('num')
268
+    ).order_by(
269
+        '-num'
270
+    )
271
+    provinces = [dict(p, **{'province_name': ''}) for p in provinces]
272
+
273
+    return {
274
+        'scan_user_count': scan_user_count,
275
+        'sell_volume_count': sell_volume_count,
276
+        'user_count_increase_pct': -1,  # 与上个统计周期数据的用户人数比例
277
+        'volume_count_increase_pct': -1,  # 与上个统计周期数据的销售支数比例
278
+        'models': list(models),
279
+        'salesmen': salesmen,
280
+        'provinces': provinces,
281
+    }
282
+
283
+
206 284
 def ymdtj(brand_id, ymd, lastymd):
207 285
     # [消费者维度] 周期内扫描用户人数
208 286
     try:
@@ -237,8 +315,8 @@ def ymdtj(brand_id, ymd, lastymd):
237 315
     models = [m.data for m in current_models[:20]]
238 316
 
239 317
     # [经销商维度] 统计周期内销售员排行数据,请按顺序返回
240
-    salesmen = SaleclerkSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')[:20]
241
-    salesmen = [s.data for s in salesmen]
318
+    salesmen = SaleclerkSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
319
+    salesmen = [s.data for s in salesmen[:20]]
242 320
 
243 321
     # [收费者维度] 统计周期内省份销量排行数据,请按顺序返回
244 322
     provinces = ConsumeProvinceSaleStatisticInfo.objects.filter(brand_id=brand_id, ymd=ymd, status=True).order_by('-num')
@@ -259,15 +337,15 @@ def ymdtj(brand_id, ymd, lastymd):
259 337
 def v2_tj_distributor(request):
260 338
     brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
261 339
 
262
-    year = tc.local_string(format='%Y')
340
+    # year = tc.local_string(format='%Y')
263 341
     month = tc.local_string(format='%Y%m')
264 342
     day = tc.local_string(format='%Y%m%d')
265 343
 
266
-    lastyear = tc.local_string(tc.several_time_ago(years=1), format='%Y')
344
+    # lastyear = tc.local_string(tc.several_time_ago(years=1), format='%Y')
267 345
     lastmonth = tc.local_string(tc.several_time_ago(months=1), format='%Y%m')
268 346
     lastday = tc.local_string(tc.several_time_ago(days=1), format='%Y%m%d')
269 347
 
270
-    year_data = ymdtj(brand_id, year, lastyear)
348
+    year_data = ytj(brand_id)
271 349
     month_data = ymdtj(brand_id, month, lastmonth)
272 350
     day_data = ymdtj(brand_id, day, lastday)
273 351