# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test
# FileName:      read_excel.py
# Author:       mwx
# Datetime:     2024/11/5 20:21
# Description:
# 
# ---------------------------------------------------------------------------
import openpyxl
from urllib3 import request

from Interface_automation.APIAutoText_v3_1_1 import log
from Interface_automation.APIAutoText_v3_1_1.commn.read_basic_ini import ReadBasicIni
from Interface_automation.APIAutoText_v3_1_1.commn.read_json import read_json
from Interface_automation.APIAutoText_v3_1_1.commn.read_user_ini import ReadUserIni


class ReadExcel:
    def __init__(self,user):
        ini = ReadUserIni(user)
        excel_path = ini.file_name("excel")
        table_name = ini.table_name("name")
        case_path = ini.file_name("case")
        expect_path = ini.file_name("expect")
        s1l_path = ini.file_name("sql")
        self.case_data = read_json(case_path)
        self.expect_data = read_json(expect_path)
        self.sql_data = read_json(s1l_path)
        wb = openpyxl.load_workbook(excel_path)
        self.ws = wb[table_name]

    def __get_column_row(self,column,row):
        log.info(f"获取excel表格指定表格的数据，列{column},行{row}")
        try:
            res = self.ws[column + str(row)].value
        except Exception as e:
            log.error(f"获取excel表格指定表格的数据出错，列{column},行{row}，错误类型为{type(e)}")
            raise e
        else:
            if res is None:
                return None
            elif res.strip():
                return res.strip()

    def module_name(self,row):
        log.info(f"根据b列获取模块名称,行{row}")
        return self.__get_column_row("b",row)

    def api_name(self,row):
        log.info(f"根据c列获取接口名称,行{row}")
        return self.__get_column_row("c", row)

    def request_method(self, row):
        log.info(f"根据f列获取请求方式,行{row}")
        res = self.__get_column_row("f", row)
        if res:
            return res.lower()

    def url_name(self,row):
        log.info(f"根据g列获取路径,行{row}")
        res = self.__get_column_row("g", row)
        if res:
            return ReadBasicIni().get_host("bpm") + res

    def mime_type(self, row):
        log.info(f"根据h列获取媒体类型,行{row}")
        res = self.__get_column_row("h", row)
        if res:
            return res.lower()

    def data_case(self,row):
        case = self.__get_column_row("i",row)
        module_name = self.module_name(row)
        api_name = self.api_name(row)
        if module_name and api_name and case:
            return self.case_data[module_name][api_name][case]

    def data_expect(self,row):
        expect = self.__get_column_row("j",row)
        module_name = self.module_name(row)
        api_name = self.api_name(row)
        if module_name and api_name and expect:
            return self.expect_data[module_name][api_name][expect]

    def sql_type(self,row):
        log.info(f"获取k列下的sql语句类型")
        res = self.__get_column_row("k",row)
        if res:
            return res.lower()

    def data_sql(self,row):
        sql = self.__get_column_row("l",row)
        module_name = self.module_name(row)
        api_name = self.api_name(row)
        if module_name and api_name and sql:
            return self.sql_data[module_name][api_name][sql]

    def update_sql(self, row):
        log.info(f"执行的方法为：update_key，功能描述为：根据行号，获取用例的更新的key，传入的参数为：{row}行")
        return self.__get_column_row("m", row)

    def get_data(self):
        list1 = []
        for row in range(2,self.ws.max_row+1):
            request_m = self.request_method(row)
            url_name = self.url_name(row)
            mime_type = self.mime_type(row)
            data_case = self.data_case(row)
            data_expect = self.data_expect(row)
            sql_type = self.sql_type(row)
            data_sql = self.data_sql(row)
            update_sql = self.update_sql(row)
            if request_m and url_name and data_expect:
                list1.append([request_m,url_name,mime_type,data_case,data_expect,sql_type,data_sql,update_sql])
        else:
            return list1

if __name__ == '__main__':
    r = ReadExcel("mw")
    print(r.get_data())
