# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Selenium_Project
# FileName:      read_excel.py
# Author:       xxxxxxx
# Datetime:     2024/5/20 19:01
# Description:
# 命名规范：文件名全小写+下划线，类名大驼峰，方法和变量小写+下划线连接，
# 常量大写，变量和常量用名词，方法用动词
# ---------------------------------------------------------------------------
import openpyxl

from api_test_0520 import log_decorator
from api_test_0520.common.readini import ReadIni
from api_test_0520.common.readjson import read_json


class ReadExcel:
    @log_decorator
    def __init__(self, username="user_01", table_name="BPM"):
        self.ini = ReadIni()
        case_path = self.ini.get_file_path("case", username)
        expect_path = self.ini.get_file_path("expect", username)
        sql_path = self.ini.get_file_path("sql", username)

        self.case_value_dict = read_json(case_path)
        self.expect_value_dict = read_json(expect_path)
        self.sql_value_dict = read_json(sql_path)

        excel_path = self.ini.get_file_path("excel", username)
        # table_name = self.ini.get_table_name("table_name")

        try:
            wd = openpyxl.load_workbook(excel_path)
            self.ws = wd[table_name]
        except Exception as e:
            print(e)
            raise e

    @log_decorator
    def __get_call_value(self, cloumn:str, row:int) -> str:
        try:
            call_value = self.ws[cloumn + str(row)].value
        except Exception as e:
            print(e)
            raise e
        else:
            if call_value is None:
                return None
            elif call_value.strip():
                return call_value.strip()

    @log_decorator
    def module_name(self, row):
        return self.__get_call_value("b", row)

    @log_decorator
    def api_name(self, row):
        return self.__get_call_value("c", row)

    @log_decorator
    def req_method(self, row):
        try:
            req_method = self.__get_call_value("F", row)
        except Exception as e:
            print(e)
            raise e
        else:
            if req_method:
                return req_method.lower()

    @log_decorator
    def req_url(self, row):
        req_url = self.__get_call_value("g", row)
        if req_url:
            return self.ini.get_host("bpm") + req_url

    @log_decorator
    def req_mime(self, row):
        try:
            mime = self.__get_call_value("h", row)
        except Exception as e:
            print(e)
            raise e
        else:
            if mime:
                return mime.lower()

    @log_decorator
    def case_data_key(self, row):
        value = self.__get_call_value("i", row)
        if value:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.case_value_dict[module_name][api_name][value]

    @log_decorator
    def expect_data_key(self, row):
        value = self.__get_call_value("j", row)
        if value:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.expect_value_dict[module_name][api_name][value]

    @log_decorator
    def sql_data_key(self, row):
        value = self.__get_call_value("k", row)
        if value:
            module_name = self.module_name(row)
            api_name = self.api_name(row)
            return self.sql_value_dict[module_name][api_name][value]

    @log_decorator
    def sql_type(self, row):
        value = self.__get_call_value("l", row)
        if value:
            return value.lower()

    @log_decorator
    def sql_update(self, row):
        return self.__get_call_value("m", row)

    @log_decorator
    def get_data(self):
        list_data = []
        for row in range(2, self.ws.max_row+1):
            mothed = self.req_method(row)
            url = self.req_url(row)
            mime = self.req_mime(row)
            case_data = self.case_data_key(row)
            expect_data = self.expect_data_key(row)
            sql_data = self.sql_data_key(row)
            sql_type = self.sql_type(row)
            sql_update = self.sql_update(row)
            if mothed and url:
                list_data.append([mothed, url, mime, case_data, expect_data, sql_data, sql_type, sql_update])
        else:
            return list_data

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