|
|
@@ -11,8 +11,8 @@ from TimeConvert import TimeConvert as tc
|
11
|
11
|
from django.utils import timezone
|
12
|
12
|
from django.db.models import Sum
|
13
|
13
|
|
14
|
|
-from mch.models import AdministratorInfo
|
15
|
|
-from statistic.models import ConsumeModelSaleStatisticInfo, ConsumeSaleStatisticInfo, ConsumeUserStatisticInfo
|
|
14
|
+from mch.models import AdministratorInfo, ConsumeInfoSubmitLogInfo, SaleclerkInfo
|
|
15
|
+from statistic.models import ConsumeModelSaleStatisticInfo, ConsumeSaleStatisticInfo, ConsumeUserStatisticInfo, ModelSaleStatisticInfo
|
16
|
16
|
from account.models import UserInfo
|
17
|
17
|
from integral.models import SaleclerkSubmitLogInfo
|
18
|
18
|
from utils.error.errno_utils import (AdministratorStatusCode, ProductBrandStatusCode, ProductCouponStatusCode,
|
|
|
@@ -20,6 +20,7 @@ from utils.error.errno_utils import (AdministratorStatusCode, ProductBrandStatus
|
20
|
20
|
|
21
|
21
|
from collections import defaultdict
|
22
|
22
|
import json
|
|
23
|
+from itertools import groupby
|
23
|
24
|
|
24
|
25
|
|
25
|
26
|
WECHAT = settings.WECHAT
|
|
|
@@ -215,7 +216,7 @@ def statistic_userprofile(request):
|
215
|
216
|
admin_id = request.POST.get('admin_id', '')
|
216
|
217
|
start_time = request.POST.get('start_time', '')
|
217
|
218
|
end_time = request.POST.get('end_time', '')
|
218
|
|
- model_uni_name = request.POST.get('model_uni_name', '')
|
|
219
|
+ model_name = request.POST.get('model_name', '')
|
219
|
220
|
|
220
|
221
|
if brand_id != settings.KODO_DEFAULT_BRAND_ID:
|
221
|
222
|
return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
|
|
|
@@ -225,8 +226,8 @@ def statistic_userprofile(request):
|
225
|
226
|
except AdministratorInfo.DoesNotExist:
|
226
|
227
|
return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
|
227
|
228
|
|
228
|
|
- if model_uni_name:
|
229
|
|
- logs = ConsumeModelSaleStatisticInfo.objects.filter(model_name=model_uni_name, ymd__gte=start_time, ymd__lte=end_time)
|
|
229
|
+ if model_name:
|
|
230
|
+ logs = ConsumeModelSaleStatisticInfo.objects.filter(model_name=model_name, ymd__gte=start_time, ymd__lte=end_time)
|
230
|
231
|
else:
|
231
|
232
|
logs = ConsumeModelSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
|
232
|
233
|
|
|
|
@@ -297,4 +298,56 @@ def statistic_model(request):
|
297
|
298
|
logs = [{'model_name': log[0], 'num': log[1]} for log in logs]
|
298
|
299
|
return response(200, 'Get Model Statistic Success', u'获取型号统计成功', data={
|
299
|
300
|
'logs': logs
|
|
301
|
+ })
|
|
302
|
+
|
|
303
|
+def statistic_distributor(request):
|
|
304
|
+ brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
|
|
305
|
+ admin_id = request.POST.get('admin_id', '')
|
|
306
|
+ start_time = request.POST.get('start_time', '')
|
|
307
|
+ end_time = request.POST.get('end_time', '')
|
|
308
|
+ model_name = request.POST.get('model_name', '')
|
|
309
|
+ distributor_name = request.POST.get('distributor_name', '')
|
|
310
|
+
|
|
311
|
+ if brand_id != settings.KODO_DEFAULT_BRAND_ID:
|
|
312
|
+ return response(ProductBrandStatusCode.BRAND_NOT_MATCH)
|
|
313
|
+
|
|
314
|
+ try:
|
|
315
|
+ administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
|
|
316
|
+ except AdministratorInfo.DoesNotExist:
|
|
317
|
+ return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)
|
|
318
|
+
|
|
319
|
+ logs = ModelSaleStatisticInfo.objects.filter(ymd__gte=start_time, ymd__lte=end_time)
|
|
320
|
+
|
|
321
|
+ if model_name:
|
|
322
|
+ logs = logs.filter(model_name=model_name)
|
|
323
|
+
|
|
324
|
+ distributor_logs = []
|
|
325
|
+ distributor_names = []
|
|
326
|
+
|
|
327
|
+ logs = logs.values('model_name', 'ymd', 'num', 'saleclerks')
|
|
328
|
+ for log in logs:
|
|
329
|
+ for saleclerk in json.loads(log['saleclerks']):
|
|
330
|
+ saleclerk_info = SaleclerkInfo.objects.get(clerk_id=saleclerk)
|
|
331
|
+ if distributor_name and saleclerk_info.distributor_name != distributor_name:
|
|
332
|
+ log['num'] -= 1
|
|
333
|
+ else:
|
|
334
|
+ if saleclerk_info.distributor_name not in distributor_names:
|
|
335
|
+ distributor_logs.append({'distributor_name': saleclerk_info.distributor_name, 'num': 1})
|
|
336
|
+ distributor_names.append(saleclerk_info.distributor_name)
|
|
337
|
+ else:
|
|
338
|
+ i = distributor_names.index(saleclerk_info.distributor_name)
|
|
339
|
+ distributor_logs[i]['num'] += 1
|
|
340
|
+
|
|
341
|
+ daily_logs = []
|
|
342
|
+ for k, v in groupby(logs, lambda log:log['ymd']):
|
|
343
|
+ daily_logs.append(reduce(lambda dict1, dict2: {'ymd': k, 'num': dict1['num'] + dict2['num']}, v))
|
|
344
|
+
|
|
345
|
+ model_logs = []
|
|
346
|
+ for k, v in groupby(sorted(logs, key=lambda log:log['model_name']), lambda log:log['model_name']):
|
|
347
|
+ model_logs.append(reduce(lambda dict1, dict2: {'model_name': k, 'num': dict1['num'] + dict2['num']}, v))
|
|
348
|
+
|
|
349
|
+ return response(200, 'Get Distributor Statistic Success', u'获取经销商统计成功', data={
|
|
350
|
+ 'daily_logs': daily_logs,
|
|
351
|
+ 'model_logs': model_logs,
|
|
352
|
+ 'distributor_logs': distributor_logs
|
300
|
353
|
})
|