# -*- coding: utf-8 -*-
# 基础接口返回信息解析类
# 作者: 陈磊
# 时间: 2019-10-21

import json
from WorkUtils.UtilsLog import UtilsLog


class UtilsResponse:
    def __init__(self):
        self.log = UtilsLog()
        self.log.info("调用基础接口返回信息解析类")
        self.log.info(self.__class__)

    def get_code(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中code:")
        try:
            value = json.loads(response)["code"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_status(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中status:")
        try:
            value = json.loads(response)["status"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_msg(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中msg:")
        try:
            value = json.loads(response)["msg"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_result(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中result:")
        try:
            value = json.loads(response)["result"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_values_from_key(self, _dict, key):
        """
        根据key得到所有value
        :return:
        """
        values = []
        self.log.debug("查询")
        self.log.debug("key=" + str(key))
        for x, y in enumerate(_dict):
            values.append(y[key])
        self.log.debug("values=" + str(values))
        return values

    def get_values_from_sql(self, _sql, key):
        """
        根据key得到所有value
        :return:
        """
        values = []
        self.log.debug("查询")
        self.log.debug("key=" + str(key))
        for x, y in enumerate(_sql):
            # self.log.debug(y.__dict__)
            values.append(y.__dict__.get(key))
        self.log.debug("values=" + str(values))
        return values

    def check_sort(self, _list, key, possible):
        self.log.debug("校验排序")
        if possible == "ASC":
            self.log.debug("校验升序排序...")
            start = 0
            for x, y in enumerate(_list):
                assert y[key] > start
                start = y[key]
        elif possible == "DESC":
            self.log.debug("校验降序排序...")
            start = float("inf")
            for x, y in enumerate(_list):
                assert y[key] < start
                start = y[key]
        else:
            self.log.debug("校验默认排序...")
            start = 0
            for x, y in enumerate(_list):
                assert y[key] > start
                start = y[key]
        self.log.debug("校验成功")

    def get_data(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中data:")
        try:
            value = json.loads(response)["data"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_data_list(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中data的list:")
        try:
            value = json.loads(response)["data"]["list"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error

    def get_data_list_records(self, response):
        """
        :param response: 返回信息
        :return: 返回value
        """
        self.log.debug(u"解析返回信息中data的list:")
        try:
            value = json.loads(response)["data"]["list"]["records"]
            self.log.debug(value)
            return value
        except KeyError as error:
            self.log.error(u"json解析出错缺少Key:")
            self.log.error(error)
            return error
