# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test66
# FileName:      read_excel.py
# Author:       lao_zhao
# Datetime:     2025/5/9 14:01
# 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
from APIAutoTest_v3.data_config.settings import *


class ReadExcel:
    @log_decorator
    def __init__(self, username):
        """获取excel的路径及工作表的名称，加载工作簿获取工作表；获取所以json文件的路径，获取json文件内容的python对象"""
        self.ini = ReadUserIni(username)
        # 创建系统配置文件的对象
        self.system_ini = ReadSystemIni()

        self.case_data_dict = read_json(self.ini.get_file_path(FILE_CASE))
        self.expect_data_dict = read_json(self.ini.get_file_path(FILE_EXPECT))
        self.sql_data_dict = read_json(self.ini.get_file_path(FILE_SQL))

        wb = openpyxl.load_workbook(self.ini.get_file_path(FILE_EXCEL))
        self.ws = wb[self.ini.get_table_name(TABLE_NAME_KEY)]

    @log_decorator
    def __get_cell_value(self, column, row) 
        """根据行或列，获取指定单元格数据"""
        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(MODULE, row)

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

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

    @log_decorator
    def case_url(self, row):
        """根据行，获取用例的url"""
        value = self.__get_cell_value(PATH, row)
        if value:
            return self.system_ini.get_host(HOST_KEY) + value

    @log_decorator
    def case_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)
        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(EXPECT, 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(SQL, 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(SQLTYPE, row)
        if value:
            return value.lower()

    @log_decorator
    def update_key(self, row):
        """根据行，获取更新的key"""
        return self.__get_cell_value(UPDATEKEY, 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):
        """获取所以的测试数据，存放在一个二维列表中"""
        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)
            req = self.case_req(row)
            url = self.case_url(row)
            mime = self.case_mime(row)
            case = self.case_data(row)
            expect = self.expect_data(row)
            sql_type = self.sql_type(row)
            sql_data = self.sql_data(row)
            update_key = self.update_key(row)

            if req and url and expect:
                list_data.append([module, api, title, level,req, url, mime, case, expect, sql_type, sql_data, update_key])
        else:
            return list_data


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