# -*-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


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", username)
        expect_data_path = self.ini.get_file_path("expect", username)
        sql_data_path = self.ini.get_file_path("sql", 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", 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("b", row)

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

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

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

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

    @log_decorator
    def case_data(self, row):
        """获取用例数据"""
        case_data_key = self.__get_cell_value("i", 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("j", 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("k", 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("l", row)
        if value:
            return value.lower()

    @log_decorator
    def update_key(self, row):
        """获取更新的key"""
        return self.__get_cell_value("m", 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)
            if method and url:
                list_data.append([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())