#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: langer
@software: PyCharm
@DATE: 2019/9/6 11:02
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: SysSet.py
@Contact: chanjunkai@163.com
"""
import json
import time
from var_dump import var_dump

from django.views.generic import TemplateView

from model.models import SysSetModel
from object.ResponseObject import ResponseObject
from object.TokenObject import TokenObject
from object.RedisObject import RedisObject


class queryView(TemplateView):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = json.loads(request.body.decode('utf-8'))
        return self.validate(request_dict)

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

    def validate(self, request_dict):
        response = ResponseObject()
        token = request_dict.get('token', None)
        key_list = request_dict.get('key_list')
        tko = TokenObject(token)
        if tko.code == 0:
            userID = tko.userID
            if key_list:
                sys_qs = SysSetModel.objects.filter(key__in=key_list).values('id', 'key', 'val')
                res = {}
                for i in sys_qs:
                    res[i['key']] = i['val']
                return response.json(0, res)
            else:
                pass
        else:
            return response.json(tko.code)


class updateView(TemplateView):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = json.loads(request.body.decode('utf-8'))
        return self.validate(request_dict)

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

    def validate(self, request_dict):
        response = ResponseObject()
        token = request_dict.get('token', None)
        update_data = request_dict.get('update_data', None)
        # asinterval = request_dict.get('update_data', None)
        # asimgEmail = request_dict.get('asimgEmail', None)
        from var_dump import var_dump
        var_dump(update_data)
        if not update_data:
            return response.json(0)
        nowTime = int(time.time())
        tko = TokenObject(token)
        redisObj = RedisObject()
        if tko.code == 0:
            for i in update_data:
                sys_qs = SysSetModel.objects.filter(key=i['key'])
                if sys_qs.exists():
                    if i['key'] == 'asinterval':
                        redisObj.set_data(key='asinterval', val=1, expire=int(i['val']))
                    sys_qs.update(val=i['val'], updTime=nowTime)
                else:
                    if i['key'] == 'asinterval':
                        redisObj.set_data(key='asinterval', val=1, expire=int(i['val']))
                    create_dict = {
                        'addTime': nowTime,
                        'updTime': nowTime,
                        'key': i['key'],
                        'val': i['val'],
                    }
                    SysSetModel.objects.create(**create_dict)
            return response.json(0)
        else:
            return response.json(tko.code)
