Bladeren bron

统计分析

wushaodong 4 jaren geleden
bovenliggende
commit
63bb87b4c9
2 gewijzigde bestanden met toevoegingen van 83 en 26 verwijderingen
  1. 2 1
      apps/wxapp/urls.py
  2. 81 25
      apps/wxapp/views.py

+ 2 - 1
apps/wxapp/urls.py

@@ -12,7 +12,8 @@ urlpatterns = [
     url(r'^wxbind/$', WxBindView.as_view()),
 
     url(r'^home/statistics/$', HomeStatisticsView.as_view()),
-    url(r'^statistics/$', StatisticsView.as_view()),
+    url(r'^statistics/repair/$', StatisticsRepairView.as_view()),
+    url(r'^statistics/evaluate/$', StatisticsEvaluateView.as_view()),
     url(r'^poster/$', PosterView.as_view()),
     url(r'^dict/$', DictView.as_view()),
     url(r'^get_device/$', DeviceView.as_view()),

+ 81 - 25
apps/wxapp/views.py

@@ -5,16 +5,13 @@ from rest_framework.exceptions import NotFound
 from django.db import transaction
 from rest_framework.views import APIView
 from rest_framework import generics
-from rest_framework.decorators import action
-from django.db.models import Q
+import datetime
+from django.db.models import Sum
 from rest_framework_jwt.views import ObtainJSONWebToken,VerifyJSONWebToken,RefreshJSONWebToken
 from rest_framework.serializers import ValidationError
-from apps.WechatApplet.models import WechatApplet
 from utils import response_ok, response_error
-from apps.tenant.config.serializers import Config,ConfigSerializer
 from apps.tenant.option.serializers import OptionSerializer,Option
-from apps.tenant.area.serializers import AreaSerializer,Area
-from apps.tenant.building.models import Building
+from django.utils import timezone
 from apps.tenant.notices.models import Notices
 from apps.tenant.notices.serializers import NoticesWXSerializer
 from apps.tenant.notices.filters import NoticesFilter
@@ -22,8 +19,7 @@ from utils.permission import isLogin
 from utils.wx.WXBizDataCrypt import WXBizDataCrypt
 from apps.tenant.poster.serializer import PosterSerializer, Poster
 from apps.tenant.device.models import DeviceModel
-from apps.tenant.repair_order.models import RepairOrder
-
+from apps.tenant.repair_order.models import RepairOrder,RepairOrderComment
 from .serializers import *
 
 class CustomerRefreshTokenView(RefreshJSONWebToken):
@@ -119,26 +115,86 @@ class HomeStatisticsView(APIView):
         else:
             return response_ok(statistics)
 
-class StatisticsView(APIView):
+class StatisticsRepairView(APIView):
     '''小程序统计数据'''
-
+    permission_classes = [isLogin, ]
     def get(self, request):
-        statistics = {
-            'total':0, #总报修
-            'wait':0, #待维修
-            'working':0, #维修中
-            'complete':0, #已完工
-        }
-        if request.user or request.user.is_authenticated:
-            tenant = request.user.employee.tenant
-            rows = RepairOrder.objects.filter(tenant=tenant)
-            statistics['total'] = rows.filter(status__gte=RepairOrder.CHECKED).count()
-            statistics['wait'] = rows.filter(status=RepairOrder.CHECKED).count()
-            statistics['working'] = rows.filter(status=RepairOrder.DISPATCH).count()
-            statistics['complete'] = rows.filter(status__in=[RepairOrder.FINISH,RepairOrder.APPRAISE,]).count()
-            return response_ok(statistics)
+        days = int(request.GET.get('days')) # 7 30 365
+        data = []# 报修数据
+        tenant = request.user.employee.tenant
+        now = timezone.now()
+        rows = RepairOrder.objects.filter(tenant=tenant,status__gte=RepairOrder.CHECKED)
+        if days < 31:
+            for d in range(days):
+                date = (now + datetime.timedelta(days=-d)).strftime('%Y-%m-%d')
+                item = {
+                    'title':date,
+                    'data':rows.filter(create_time__gte=date,create_time__lte=date+' 23:59:59').count(),
+                }
+                data.append(item)
         else:
-            return response_ok(statistics)
+            # 得到今年的的时间 (年份) 得到的today_year等于2016年
+            today_year = now.year
+            # 今年的时间减去1,得到去年的时间。last_year等于2015
+            last_year = int(now.year) - 1
+            # 得到今年的每个月的时间。today_year_months等于1 2 3 4 5 6 7 8 9,
+            today_year_months = range(1, now.month + 1)
+            # 得到去年的每个月的时间  last_year_months 等于10 11 12
+            last_year_months = range(now.month + 1, 13)
+            # 定义列表去年的数据
+            data_list_lasts = []
+            # 通过for循环,得到去年的时间夹月份的列表
+            # 先遍历去年每个月的列表
+            for last_year_month in last_year_months:
+                # 定义date_list 去年加上去年的每个月
+                date_list = '%s-%s' % (last_year, last_year_month)
+                # 通过函数append,得到去年的列表
+                data_list_lasts.append(date_list)
+
+            data_list_todays = []
+            # 通过for循环,得到今年的时间夹月份的列表
+            # 先遍历今年每个月的列表
+            for today_year_month in today_year_months:
+                # 定义date_list 去年加上今年的每个月
+                data_list = '%s-%s' % (today_year, today_year_month)
+                # 通过函数append,得到今年的列表
+                data_list_todays.append(data_list)
+            # 去年的时间数据加上今年的时间数据得到年月时间列表
+            data_year_month = data_list_lasts + data_list_todays
+            for year_months in data_year_month:
+                year_month = year_months.split('-')
+                year = int(year_month[0])
+                month = int(year_month[1])
+                item = {
+                    'title': year_months,
+                    'data':rows.filter(create_time__year=year, create_time__month=month).count(),
+                }
+                data.append(item)
+
+        return response_ok(data)
+
+class StatisticsEvaluateView(APIView):
+    '''小程序评价统计数据'''
+    permission_classes = [isLogin, ]
+    def get(self, request):
+        days = int(request.GET.get('days')) # 7 30 365
+        data = []# 评价数据
+        tenant = request.user.employee.tenant
+        now = timezone.now()
+        date = (now + datetime.timedelta(days=-days)).strftime('%Y-%m-%d')
+        rows = RepairOrderComment.objects.filter(repair_order__tenant=tenant,create_time__gte=date,) \
+            .values('user__employee__name') \
+            .annotate(count=Sum('start')) \
+            .values('user__employee__name', 'count') \
+            .order_by('user__employee__name')
+        for row in rows:
+            item = {
+                'title':row['user__employee__name'],
+                'data':row['count'],
+            }
+            data.append(item)
+
+        return response_ok(data)
 
 class PosterView(generics.ListAPIView):
     '''小程序首页数据'''