# -*- coding: utf-8 -*-
# 基础接口参数信息
# 作者: 陈磊
# 时间: 2019-10-31


from WorkUtils.UtilsLog import UtilsLog


class GetBase:
    """
    公共查询请求参数
    """
    def __init__(self):
        self.log = UtilsLog()
        self.log.info("调用公共查询请求参数方法")
        self.log.info(self.__class__)

        self.base_json = {}
        self.countOutput = None
        self.editable = None
        self.excludeSearch = None
        self.filter = None
        self.limit = None
        self.output = None
        self.preservekeys = None
        self.search = None
        self.searchByAny = None
        self.searchWildcardsEnabled = None
        self.sortfield = None
        self.sortorder = None
        self.startSearch = None

    def get_base_json(self):
        """
        组装公共查询请求参数
        :return:
        """
        base = ApiBase()
        base.dict_add_key(_key="countOutput", value=self.countOutput)
        base.dict_add_key(_key="editable", value=self.editable)
        base.dict_add_key(_key="excludeSearch", value=self.excludeSearch)
        base.dict_add_key(_key="filter", value=self.filter)
        base.dict_add_key(_key="limit", value=self.limit)
        base.dict_add_key(_key="output", value=self.output)
        base.dict_add_key(_key="preservekeys", value=self.preservekeys)
        base.dict_add_key(_key="search", value=self.search)
        base.dict_add_key(_key="searchByAny", value=self.searchByAny)
        base.dict_add_key(_key="searchWildcardsEnabled", value=self.searchWildcardsEnabled)
        base.dict_add_key(_key="sortfield", value=self.sortfield)
        base.dict_add_key(_key="sortorder", value=self.sortorder)
        base.dict_add_key(_key="startSearch", value=self.startSearch)
        self.base_json = base._json


class ApiBase:
    def __init__(self):
        self.log = UtilsLog()
        self.log.info("调用公共接口方法")
        self.log.info(self.__class__)

        self._json = {}

    def dict_add_key(self, _key, value, num=None):
        """
        根据value判断key是否为空
        根据num是否为空 value按照list取对应序列
        :param _key:
        :param value:
        :param num:
        :return:
        """
        if value is None:
            pass
        elif num is None:
            self._json[_key] = value
        elif num is not None and value is not None:
            self._json[_key] = value[num]
        else:
            pass

    def dict_add_dict(self, _dict_1, _dict_2):
        """
        两个字典相加
        :param _dict_1: 1号字典
        :param _dict_2: 2号字典
        :return:
        """
        if _dict_1 == {}:
            if _dict_2 == {}:
                self._json = {}
            else:
                self._json = _dict_2
        else:
            self._json = {**_dict_1, **_dict_2}
