# coding=utf-8
from django.db import transaction
import datetime
from django.utils import timezone
from django.db.models import Count
from rest_framework.decorators import action
from django.conf import settings
from rest_framework.views import APIView
from utils import response_ok, response_error
from utils.custom_modelviewset import CustomModelViewSet
from utils.exceptions import CustomError
from utils.permission import IsTenantUser
from .serializers import RepairOrderSerializer
from .models import RepairOrder, RepairOrderRecord
from .filters import RepairOrderFilter
from apps.tenant import tenant_log
from apps.log.models import BizLog
from apps.tenant.employee.serializers import Employee, EmployeeSerializer,User
from apps.wxapp.models import CustomerWechat

class DictView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        tenant = request.user.employee.tenant
        ret = {
            'status': settings.REPAIR_STATUS_CHOICES,
        }
        return response_ok(ret)

class GetEmployeeView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        employees = EmployeeSerializer(
            Employee.objects.filter(status__lte=Employee.DAY_OFF, type=Employee.EMPLOYEE, tenant=request.user.employee.tenant), many=True).data
        data = []
        for emp in employees:
            dict = {
                'id': emp['user'],
                'name': '{0}-{1}-{2}-{3}'.format(emp['name'], emp['tel'], emp['position'], emp['status_text'])
            }
            data.append(dict)
        return response_ok(data)

class OrderRecordView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        order_id = request.GET.get('order_id')
        records = RepairOrderRecord.objects.filter(repair_order_id=order_id).order_by('-operation_time')
        data = []
        for record in records:
            dict = record.get_records(order_id)
            data.append(dict)
        return response_ok(data)

class RepairOrderViewSet(CustomModelViewSet):
    permission_classes = [IsTenantUser, ]
    queryset = RepairOrder.objects.filter(delete=False)
    serializer_class = RepairOrderSerializer

    def filter_queryset(self, queryset):
        queryset = queryset.filter(tenant=self.request.user.employee.tenant)
        f = RepairOrderFilter(self.request.GET, queryset=queryset)
        return f.qs

    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            if instance.tenant != request.user.employee.tenant:
                raise CustomError(u'禁止跨企业操作！')
            super(RepairOrderViewSet, self).destroy(self, request, *args, **kwargs)
            tenant_log(self.request.user.employee, BizLog.DELETE, u'删除报修单[%s]，id=%d' % (instance.no, instance.id))
        return response_ok()

    @action(methods=['post'], detail=True)
    def check(self, request, pk):
        # 审核
        try:
            with transaction.atomic():
                user = self.request.user
                instance = RepairOrder.get_instance_by_id(pk, user.employee.tenant)
                instance.check_repair_order(user)
        except CustomError as e:
            return response_error(e.get_error_msg())
        except Exception as e:
            return response_error(str(e))
        return response_ok('审核完成！')

    @action(methods=['post'], detail=True)
    def dispatch_employee(self, request, pk):
        # 派工
        try:
            user_ids = self.request.data.get('user_ids')
            if not user_ids:
                return response_error('请选择维修工人！')
            user = self.request.user
            with transaction.atomic():
                instance = RepairOrder.get_instance_by_id(pk, user.employee.tenant)
                if not instance.status == settings.CHECKED:
                    return response_error('当前报修工单状态非待派单状态，不能进行派单！')
                instance.dispatch_repair_order(user, user_ids)
            if user.id != user_ids:
                user = User.objects.filter(id=user_ids).first()
                CustomerWechat.sendDispatchMsg(user, instance.user.employee.name, instance.device_address,
                                            instance.fault_des, instance.create_time, instance.no)
        except CustomError as e:
            return response_error(e.get_error_msg())
        except Exception as e:
            import traceback
            traceback.print_exc()
            return response_error(str(e))
        return response_ok('派工完成！')

    @action(methods=['post'], detail=True)
    def turn_employee(self, request, pk):
        # 转单
        try:
            user_ids = request.data.get('user_id')
            if not user_ids:
                return response_error('请选择维修工人！')
            user = self.request.user
            with transaction.atomic():
                instance = RepairOrder.get_instance_by_id(pk, user.employee.tenant)
                if not instance.status == settings.DISPATCH:
                    return response_error('当前报修单状态还未派单，不能进行转单！')
                instance.repair_users_id = user_ids
                instance.save()
                # 记录操作
                RepairOrderRecord.objects.create(repair_order=instance, status=settings.TURN_EMPLOYEE, user=user,
                                                 repair_users=user_ids)
        except CustomError as e:
            return response_error(e.get_error_msg())
        except Exception as e:
            return response_error(str(e))
        return response_ok('派单完成！')

    @action(methods=['post'], detail=True)
    def cancel(self, request, pk):
        # 作废
        try:
            if not self.request.user.has_perm('repair_order.invalid_repair_order'):
                raise CustomError(u"您没有[报修工单-作废]权限，无法执行该操作，请联系管理员分配权限！")
            with transaction.atomic():
                user = self.request.user
                instance = RepairOrder.get_instance_by_id(pk, user.employee.tenant)
                instance.invalid(user)
        except CustomError as e:
            return response_error(e.get_error_msg())
        except Exception as e:
            return response_error(str(e))
        return response_ok('工单作废完成！')

class HomeStatisticsView(APIView):
    '''首页统计数据'''
    permission_classes = [IsTenantUser, ]
    def get(self, request):
        statistics = {
            'wait_check':0, #待审核
            'is_check':0, # 待接单
            'working':0, #维修中
            'hang_up':0, #挂起中
            'complete':0, #已完成
            'appraise':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_check'] = rows.filter(status=RepairOrder.NOT_CHECKED).count()
            statistics['is_check'] = rows.filter(status=RepairOrder.CHECKED).count()
            statistics['working'] = rows.filter(status=RepairOrder.DISPATCH).count()
            statistics['hang_up'] = rows.filter(status=RepairOrder.HANG_UP).count()
            statistics['complete'] = rows.filter(status=RepairOrder.FINISH).count()
            statistics['appraise'] = rows.filter(status=RepairOrder.APPRAISE).count()
            return response_ok(statistics)
        else:
            return response_ok(statistics)

class RepairTypeView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        tenant = request.user.employee.tenant
        order_records = RepairOrder.objects.filter(tenant=tenant,status__gte=RepairOrder.CHECKED)
        order_records = order_records.values('repair_type').annotate(count=Count('id')).order_by('repair_type').values(
            'repair_type__name', 'count')
        return response_ok(
            [{'name': order_record['repair_type__name'], 'value': order_record['count']} for order_record in order_records])

class RepairOrderView(APIView):
    permission_classes = [IsTenantUser, ]

    def get(self, request):
        order_mouth = []  # 日期
        repair_counts = []  # 报修量
        finish_counts = []  # 完工量
        tenant = request.user.employee.tenant
        now = timezone.now()

        for d in range(30):
            date = (now + datetime.timedelta(days=-d)).strftime('%Y-%m-%d')
            order_mouth.append(date[5:])
            rows = RepairOrder.objects.filter(tenant=tenant,create_time__gte=date, create_time__lte=date + ' 23:59:59', status__in=[RepairOrder.CHECKED, RepairOrder.FINISH])
            check_counts = rows.filter(status__gte=RepairOrder.CHECKED).count()
            finish_count = rows.filter(status__gte=RepairOrder.FINISH).count()
            repair_counts.append(check_counts)
            finish_counts.append(finish_count)
        return response_ok({
            'finish_counts': sorted(finish_counts),
            'repair_counts': sorted(repair_counts),
            'order_mouth': sorted(order_mouth),
        })
