|
|
@@ -8,7 +8,7 @@ from django_logit import logit
|
8
|
8
|
from django_response import response
|
9
|
9
|
from paginator import pagination
|
10
|
10
|
from TimeConvert import TimeConvert as tc
|
11
|
|
-from django.db.models import Sum
|
|
11
|
+from django.db.models import Sum, Count
|
12
|
12
|
from datetime import datetime
|
13
|
13
|
|
14
|
14
|
from mch.models import AdministratorInfo, ConsumeInfoSubmitLogInfo, SaleclerkInfo, ModelInfo
|
|
|
@@ -21,7 +21,8 @@ from utils.error.errno_utils import (AdministratorStatusCode, ProductBrandStatus
|
21
|
21
|
|
22
|
22
|
from collections import defaultdict
|
23
|
23
|
import json
|
24
|
|
-from itertools import groupby
|
|
24
|
+from itertools import groupby,chain
|
|
25
|
+
|
25
|
26
|
|
26
|
27
|
|
27
|
28
|
WECHAT = settings.WECHAT
|
|
|
@@ -315,6 +316,7 @@ def statistic_distributor(request):
|
315
|
316
|
end_time = request.POST.get('end_time', '')
|
316
|
317
|
model_name = request.POST.get('model_name', '')
|
317
|
318
|
distributor_name = request.POST.get('distributor_name', '')
|
|
319
|
+ code_version = request.POST.get('code_version', '')
|
318
|
320
|
|
319
|
321
|
if brand_id != settings.KODO_DEFAULT_BRAND_ID:
|
320
|
322
|
return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
|
|
|
@@ -324,35 +326,37 @@ def statistic_distributor(request):
|
324
|
326
|
except AdministratorInfo.DoesNotExist:
|
325
|
327
|
return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
|
326
|
328
|
|
327
|
|
- logs = ModelSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
|
328
|
|
-
|
329
|
|
- if model_name:
|
330
|
|
- logs = logs.filter(model_name=model_name)
|
331
|
|
-
|
332
|
|
- distributor_logs = []
|
333
|
|
- distributor_names = []
|
334
|
|
-
|
335
|
|
- logs = logs.values('model_name', 'ymd', 'num', 'saleclerks')
|
336
|
|
- for log in logs:
|
337
|
|
- for saleclerk in json.loads(log['saleclerks']):
|
338
|
|
- saleclerk_info = SaleclerkInfo.objects.get(clerk_id=saleclerk)
|
339
|
|
- if distributor_name and saleclerk_info.distributor_name != distributor_name:
|
340
|
|
- log['num'] -= 1
|
341
|
|
- else:
|
342
|
|
- if saleclerk_info.distributor_name not in distributor_names:
|
343
|
|
- distributor_logs.append({'distributor_name': saleclerk_info.distributor_name, 'num': 1})
|
344
|
|
- distributor_names.append(saleclerk_info.distributor_name)
|
345
|
|
- else:
|
346
|
|
- i = distributor_names.index(saleclerk_info.distributor_name)
|
347
|
|
- distributor_logs[i]['num'] += 1
|
348
|
|
-
|
349
|
|
- daily_logs = []
|
350
|
|
- for k, v in groupby(logs, lambda log: log['ymd']):
|
351
|
|
- daily_logs.append(reduce(lambda dict1, dict2: {'ymd': k, 'num': dict1['num'] + dict2['num']}, v))
|
352
|
|
-
|
353
|
|
- model_logs = []
|
354
|
|
- for k, v in groupby(sorted(logs, key=lambda log: log['model_name']), lambda log: log['model_name']):
|
355
|
|
- model_logs.append(reduce(lambda dict1, dict2: {'model_name': k, 'num': dict1['num'] + dict2['num']}, v))
|
|
329
|
+ logs = SaleclerkSubmitLogInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time, model_name__contains=model_name, code_version__contains=code_version, test_user=False, dupload=False, test_sn=False)
|
|
330
|
+
|
|
331
|
+ daily_logs = list(logs.values('ymd').annotate(num=Count('pk')).order_by('ymd'))
|
|
332
|
+ daily_hasScan_logs = list(logs.filter(has_scan=True).values('ymd').annotate(has_scan_num=Count('pk')).order_by('ymd'))
|
|
333
|
+
|
|
334
|
+ j = 0
|
|
335
|
+ for i in range(0, len(daily_logs)):
|
|
336
|
+ daily_logs[i]['has_scan_num'] = 0
|
|
337
|
+ if j < len(daily_hasScan_logs) and daily_logs[i]['ymd'] == daily_hasScan_logs[j]['ymd']:
|
|
338
|
+ daily_logs[i]['has_scan_num'] = daily_hasScan_logs[j]['has_scan_num']
|
|
339
|
+ j += 1
|
|
340
|
+
|
|
341
|
+ model_logs = list(logs.values('model_name').annotate(num=Count('pk')).order_by('model_name'))
|
|
342
|
+ model_hasScan_logs = list(logs.filter(has_scan=True).values('model_name').annotate(has_scan_num=Count('pk')).order_by('model_name'))
|
|
343
|
+
|
|
344
|
+ j = 0
|
|
345
|
+ for i in range(0, len(model_logs)):
|
|
346
|
+ model_logs[i]['has_scan_num'] = 0
|
|
347
|
+ if j < len(model_hasScan_logs) and model_logs[i]['model_name'] == model_hasScan_logs[j]['model_name']:
|
|
348
|
+ model_logs[i]['has_scan_num'] = model_hasScan_logs[j]['has_scan_num']
|
|
349
|
+ j += 1
|
|
350
|
+
|
|
351
|
+ distributor_logs = list(logs.values('distributor_name').annotate(num=Count('pk')).order_by('distributor_name'))
|
|
352
|
+ distributor_hasScan_logs = list(logs.filter(has_scan=True).values('distributor_name').annotate(has_scan_num=Count('pk')).order_by('distributor_name'))
|
|
353
|
+
|
|
354
|
+ j = 0
|
|
355
|
+ for i in range(0, len(distributor_logs)):
|
|
356
|
+ distributor_logs[i]['has_scan_num'] = 0
|
|
357
|
+ if j < len(distributor_hasScan_logs) and distributor_logs[i]['distributor_name'] == distributor_hasScan_logs[j]['distributor_name']:
|
|
358
|
+ distributor_logs[i]['has_scan_num'] = distributor_hasScan_logs[j]['has_scan_num']
|
|
359
|
+ j += 1
|
356
|
360
|
|
357
|
361
|
return response(200, 'Get Distributor Statistic Success', u'获取经销商统计成功', data={
|
358
|
362
|
'daily_logs': daily_logs,
|