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

import json
from django.views.generic import TemplateView
from object.ResponseObject import ResponseObject
from model.models import UserModel, LangSetModel, LangAreaModel, LangValModel, LangKeyModel,skuRuleModel,skuConditionModel
import time
from object.TokenObject import TokenObject


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

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

    def validate(self, request_dict, operation):
        response = ResponseObject()
        token = request_dict.get('token', None)
        tko = TokenObject(token)
        if tko.code == 0:
            userID = tko.userID
            user_qs = UserModel.objects.filter(id=userID)
            if not user_qs.exists():
                return response.json(403)
            if user_qs[0].role_id == "admin":
                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 == 'query':
                    return self.do_query(request_dict, response, userID)
                else:
                    return response.json(414)
            else:
                if operation == 'query':
                    return self.do_query(request_dict, response, userID)
                else:
                    return response.json(414)
        else:
            return response.json(tko.code)

    def do_add(self, request_dict, response):
        name = request_dict.get('name', None)
        numName = request_dict.get('numName', None)
        cr_id= request_dict.get('id', None)
        if name:
            nowTime = int(time.time())
            create_dict = {
                'cr_id': cr_id,
                'numName':numName,
                'conditionName': name,
                'addTime': nowTime,
                'updTime': nowTime
            }
            try:
                skuConditionModel.objects.create(**create_dict)
            except Exception as e:
                return response.json(404, repr(e))
            else:
                sr_qs = skuConditionModel.objects.filter(cr_id=cr_id).values('conditionName','numName', 'id')
                return response.json(0, list(sr_qs))

        else:
            return response.json(414)

    def do_update(self, request_dict, response):

        name = request_dict.get('name', None)
        numName = request_dict.get('numName', None)
        id = request_dict.get('id', None)
        nowTime = int(time.time())
        update_dict = {
            'numName': numName,
            'conditionName': name,
            'updTime': nowTime
        }
        try:
            skuConditionModel.objects.filter(id=id).update(**update_dict)
        except Exception as e:
            return response.json(404, repr(e))
        else:
            return response.json(0)

    def do_delete(self, request_dict, response):
        id = request_dict.get('id', None)
        try:
            skuConditionModel.objects.filter(id=id).delete()
        except Exception as e:
            return response.json(404, repr(e))
        else:
            return response.json(0)

    def do_query(self, request_dict, response, userID):
        user_qs = UserModel.objects.filter(id=userID)
        if not user_qs.exists():
            return response.json(403)
        la_qs = skuConditionModel.objects.filter().values('conditionName','numName', 'id')
        return response.json(0, list(la_qs))