#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD019
@NAME: AnsjerFormal
@software: PyCharm
@DATE: 2019/12/17 15:50
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: AliPayObject.py
@Contact: pzb3076@163.com
"""

import re
import time
import simplejson as json
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View

from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
from Model.models import UID_Bucket, UID_Preview, OssCrdModel
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Service.CommonService import CommonService
from Service.ModelService import ModelService
# 设备证书
class OssCrdView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(OssCrdView, self).dispatch(*args, **kwargs)

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

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

    def validation(self, request_dict, request, operation):
        response = ResponseObject()
        if operation is None:
            return response.json(444, 'error path')
        token = request_dict.get('token', None)
        # 设备主键uid
        tko = TokenObject(token)
        response.lang = tko.lang
        if tko.code != 0:
            return response.json(tko.code)
        userID = tko.userID
        if operation == 'adminDelete':
            return self.do_admin_delete(request_dict, userID, response)
        elif operation == 'adminQuery':
            return self.do_admin_query(request_dict, userID, response)
        else:
            return response.json(444, 'error path')

    # 管理员删除
    def do_admin_delete(self, request_dict, userID, response):
        own_perm = ModelService.check_perm(userID, 10)
        if own_perm is True:
            id = request_dict.getlist('id', None)
            uid_user_qs = OssCrdModel.objects.filter(id__in=id)
            if uid_user_qs.exists():
                uid_user_qs.delete()
                return response.json(0)
            else:
                return response.json(173)
        else:
            return response.json(404)

    # 管理员查询接口
    def do_admin_query(self, request_dict, userID, response):
        own_perm = ModelService.check_perm(userID, 20)
        if own_perm is True:
            page = request_dict.get('page', None)
            line = request_dict.get('line', None)
            page = int(page)
            line = int(line)
            deviceContent = request_dict.get('content', None)
            if deviceContent:
                try:
                    searchCondition = json.loads(deviceContent)
                except Exception as e:
                    return response.json(10, repr(e))
                else:
                    kwargs = CommonService.get_kwargs(data=searchCondition)
                    omqs = OssCrdModel.objects.filter(**kwargs).order_by('-id')
                    if not omqs.exists():
                        return response.json(0, [])
                    count = omqs.count()
                    order_ql = omqs[(page - 1) * line:page * line]
                    uidset_json = CommonService.qs_to_dict(order_ql)
                    uidset_json['count'] = count
                    return response.json(0, uidset_json)
            else:
                omqs = OssCrdModel.objects.all().order_by('-id')
                if not omqs.exists():
                    return response.json(0, [])
                count = omqs.count()
                order_ql = omqs[(page - 1) * line:page * line]
                uidset_json = CommonService.qs_to_dict(order_ql)
                uidset_json['count'] = count
                return response.json(0, uidset_json)
        else:
            return response.json(404)

