# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test65
# FileName:      read_excel.py
# Author:       lao_zhao
# Datetime:     2025/3/12 11:35
# Description:
# 
# ---------------------------------------------------------------------------
import openpyxl
from APIAutoTest_v3 import log_decorator
from APIAutoTest_v3.common.read_json import read_json
from APIAutoTest_v3.common.read_system_ini import ReadSystemIni
from APIAutoTest_v3.common.read_user_ini import ReadUserIni


class ReadExcel:
    @log_decorator
    def __init__(self, username):
        """获取所有json文件及excel文件的路径，再获取工作表的名称，读取所有的json文件，加载工作簿，获取工作表"""
        # 创建ReadUserIni用户配置文件类对象。
        ini = ReadUserIni(username)
        case_data_path = ini.get_file_path("case")
        expect_data_path = ini.get_file_path("expect")
        sql_data_path = ini.get_file_path("sql")

        excel_path = ini.get_file_path("excel")
        table_name = ini.get_table_name("tableName")

        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)

        wb = openpyxl.load_workbook(excel_path)
        self.ws = wb[table_name]

    @log_decorator
    def __get_cell_value(self, column: str, row: int) -> str:
        """根据行和列，获取指定单元格数据"""
        value = self.ws[column+str(row)].value

        if isinstance(value, str) and 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_title(self, row):
        """根据行，获取用例的title"""
        return self.__get_cell_value("d", row)

    @log_decorator
    def case_level(self, row):
        """根据行，获取用例的level"""
        return self.__get_cell_value("e", row)


    @log_decorator
    def req_method(self, row):
        """根据行，获取接口请求方法"""
        return self.__get_cell_value("f", row)

    @log_decorator
    def case_url(self, row):
        """根据行，获取接口url"""
        value = self.__get_cell_value("g", row)
        if value:
            return ReadSystemIni().get_server_value("host")+value

    @log_decorator
    def case_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语句"""
        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):
        """获取所有的测试数据，存放在一个二维列表中，在返回"""
        list_data = []
        for row in range(2, self.ws.max_row+1):
            module = self.module_name(row)
            api = self.api_name(row)
            title = self.case_title(row)
            level = self.case_level(row)
            method = self.req_method(row)
            url = self.case_url(row)
            mime = self.case_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 and expect_data:
                list_data.append([module, api, title, 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())