# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test60
# FileName:      read_excel.py
# Author:       lao_zhao
# Datetime:     2024/5/20 14:15
# Description:
# 
# ---------------------------------------------------------------------------
import openpyxl

from InterfaceAutoTest import log_decorator
from InterfaceAutoTest.common.read_ini import ReadIni
from InterfaceAutoTest.common.read_json import read_json
from InterfaceAutoTest.data_config.settings import *


class ReadExcel:
    @log_decorator
    def __init__(self, username="demo", table_name="BPM"):
        """获取所有json文件的路径，再读取json文件，再获取excel的路径和工作表名称，加载工作簿，获取工作表"""
        self.ini = ReadIni()

        case_data_path = self.ini.get_file_path(CASE_KEY, username)
        expect_data_path = self.ini.get_file_path(EXPECT_KEY, username)
        sql_data_path = self.ini.get_file_path(SQL_KEY, username)

        self.case_data_dict = read_json(case_data_path)
        self.expect_data_dict = read_json(expect_data_path)
        self.sql_data_dict = read_json(sql_data_path)

        excel_path = self.ini.get_file_path(EXCEL_KEY, username)
        # table_name = self.ini.get_table_name("table_name")
        try:
            wb = openpyxl.load_workbook(excel_path)
            self.ws = wb[table_name]
        except Exception as e:
            raise e

    @log_decorator
    def __get_cell_value(self, column: str, row: int) -> str:
        """获取指定单元格数据"""
        try:
            value = self.ws[column+str(row)].value
        except Exception as e:
            raise e
        else:
            if value is None:
                return None
            elif value.strip():
                return value.strip()

    @log_decorator
    def module_name(self, row):
        """获取模块名称"""
        return self.__get_cell_value(MODULE, row)

    @log_decorator
    def api_name(self, row):
        """获取接口名称"""
        return self.__get_cell_value(API, row)

    @log_decorator
    def case_req_method(self, row):
        """获取请求方法"""
        return self.__get_cell_value(METHOD, row)

    @log_decorator
    def case_req_url(self, row):
        """获取请求url"""
        value = self.__get_cell_value(URL, row)
        if value:
            return self.ini.get_host(BPM_KEY) + value

    @log_decorator
    def case_req_mime(self, row):
        """获取请求的媒体类型"""
        value = self.__get_cell_value(MIME, row)
        if value:
            return value.lower()

    @log_decorator
    def case_data(self, row):
        """获取用例数据"""
        case_data_key = self.__get_cell_value(CASE, row)
        if case_data_key:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.case_data_dict[module_name][api_name][case_data_key]

    @log_decorator
    def expect_data(self, row):
        """获取用例数据"""
        expect_data_key = self.__get_cell_value(EXPECT, row)
        if expect_data_key:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.expect_data_dict[module_name][api_name][expect_data_key]

    @log_decorator
    def sql_data(self, row):
        """获取用例数据"""
        sql_data_key = self.__get_cell_value(SQL, row)
        if sql_data_key:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.sql_data_dict[module_name][api_name][sql_data_key]

    @log_decorator
    def sql_type(self, row):
        """获取sql语句类型"""
        value = self.__get_cell_value(SQL_TYPE, row)
        if value:
            return value.lower()

    @log_decorator
    def update_key(self, row):
        """获取更新的key"""
        return self.__get_cell_value(UPDATE_KEY, row)

    @log_decorator
    def case_title(self, row):
        """根据行号，获取用例的标题"""
        return self.__get_cell_value(TITLE, row)

    @log_decorator
    def case_level(self, row):
        """根据行，获取用例等级"""
        return self.__get_cell_value(LEVEL, row)

    @log_decorator
    def get_data(self):
        """获取excel中的数据存放在二维列表中"""
        list_data = []
        for row in range(2, self.ws.max_row+1):
            method = self.case_req_method(row)
            url = self.case_req_url(row)
            mime = self.case_req_mime(row)
            case_data = self.case_data(row)
            expect_data = self.expect_data(row)
            sql_data = self.sql_data(row)
            sql_type = self.sql_type(row)
            update_key = self.update_key(row)
            # 用例模块，接口，标题，等级
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            case_title = self.case_title(row)
            case_level = self.case_level(row)
            if method and url:
                list_data.append([module_name, api_name, case_title, case_level, method, url, mime, case_data, expect_data, sql_data, sql_type, update_key])
        else:
            return list_data


if __name__ == '__main__':
    excel = ReadExcel()
    print(excel.get_data())