# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test67
# FileName:      read_excel.py
# Author:       lao_zhao
# Datetime:     2025/6/13 14:47
# Description:
# 
# ---------------------------------------------------------------------------
from typing import Optional

import openpyxl

from APIAutoTest_v3 import log_decorator
from APIAutoTest_v3.utils.read_json import read_json
from APIAutoTest_v3.utils.read_system_ini import ReadSystemIni
from APIAutoTest_v3.utils.read_user_ini import ReadUserIni


class ReadExcel:
    @log_decorator
    def __init__(self, username):
        """读取所有的json文件，获取excel的工作簿和工作表"""
        ini = ReadUserIni(username)

        self.case_data_dict = read_json(ini.get_file_path("case"))
        self.expect_data_dict = read_json(ini.get_file_path("expect"))
        self.sql_data_dict = read_json(ini.get_file_path("sql"))

        wb = openpyxl.load_workbook(ini.get_file_path("excel"))
        self.ws = wb[ini.get_table_name("name")]

    @log_decorator
    def __get_cell_value(self, column: str, row: int) -> Optional[str]:
        """获取指定单元格数据"""
        cell_value = self.ws[column + str(row)].value
        if isinstance(cell_value, str) and len(cell_value.strip()) > 0:
            return cell_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_url(self, row):
        """根据行，获取用例的url"""
        path = self.__get_cell_value("f", row)
        if path:
            return ReadSystemIni().get_host("bpm") + path

    @log_decorator
    def case_mime(self, row):
        """根据行获取用例的媒体类型"""
        value = self.__get_cell_value("g", row)
        if value:
            return value.lower()

    @log_decorator
    def case_req(self, row):
        """根据行，获取用例的请求方法"""
        return self.__get_cell_value("h", row)

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

    @log_decorator
    def update_key(self, row):
        """根据行，获取更新的key"""
        return self.__get_cell_value("m", row)

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

    @log_decorator
    def case_level(self, row):
        """根据行，获取用例的等级"""
        return self.__get_cell_value("e", 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)
            url = self.case_url(row)
            mime = self.case_mime(row)
            req = self.case_req(row)
            case_data = self.case_data(row)
            expect_data = self.expect_data(row)
            sql_type = self.sql_type(row)
            sql_data = self.sql_data(row)
            update_key = self.update_key(row)
            if url and req and expect_data:
                list_data.append(
                    [module, api, url, title, level, mime, req, case_data, expect_data, sql_type, sql_data, update_key])
        else:
            return list_data


if __name__ == '__main__':
    excel = ReadExcel(ReadSystemIni().get_user_data_name("user2"))
    print(excel.module_name(3))
    print(excel.get_data())
