#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time

from django.views import View

from Model.models import EquipmentVersionLimitModel, UidSetModel
from Object.RedisObject import RedisObject
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject


class EquipmentVersionLimitView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.GET
        return self.validate(request_dict, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.POST
        return self.validate(request_dict, operation)

    def validate(self, request_dict, operation):
        token = request_dict.get('token', None)
        token = TokenObject(token)
        print('ind')
        response = ResponseObject()
        if token.code != 0:
            return response.json(token.code)

        if operation == 'add':
            return self.do_add(request_dict, response)
        elif operation == 'update':
            return self.do_update(request_dict, response)
        elif operation == 'delete':
            return self.do_delete(request_dict, response)
        elif operation == 'uid':
            return self.do_query(request_dict, response)
        else:
            return response.json(404)

    def do_add(self, request_dict, response):
        type = request_dict.get('type', None)
        content = request_dict.get('content', None)
        eid = request_dict.get('eid', None)

        if type and content and eid:
            now_time = int(time.time())
            data = EquipmentVersionLimitModel()
            data.type = type
            data.content = content
            data.equipment_version_id = eid
            data.status = 1
            data.add_time = now_time
            data.update_time = now_time
            data.save()
            return response.json(0)
        else:
            return response.json(444)

    def do_update(self, request_dict, response):
        print('in ')
        id = request_dict.get('id', None)
        content = request_dict.get('content', None)
        status = request_dict.get('status', None)

        if id is None:
            return response.json(444)

        update = {}
        if content:
            update['content'] = content

        if status:
            update['status'] = status

        evl_qs = EquipmentVersionLimitModel.objects.filter(id=id)
        if evl_qs.exists():
            eid = evl_qs[0].equipment_version_id
            evl_qs.update(**update)
            redisObject = RedisObject()
            redisObject.del_data(key='limit_{eid}'.format(eid=eid))
        return response.json(0)

    def do_delete(self, request_dict, response):
        id = request_dict.get('id', None)

        if id is None:
            return response.json(0)

        evl_qs = EquipmentVersionLimitModel.objects.filter(id=id)
        if evl_qs.exists():
            eid = evl_qs[0].equipment_version_id
            redisObject = RedisObject()
            redisObject.del_data(key='limit_{eid}'.format(eid=eid))
            evl_qs.delete()
        return response.json(0)

    def do_query(self, request_dict, response):
        uid_qs = UidSetModel.objects.filter()[0:5000].values('uid')
        data = []
        for uid in uid_qs:
            data.append(uid['uid'])
            data.append(uid['uid'])
            data.append(uid['uid'])
        return response.json(0, data)
