# -*- encoding=utf8 -*-
import time

from air_case.public1.public1.public1 import get_branch_id, check_mcms_pur_sql, get_id, get_prov_id, get_create_time, \
    get_login_user_uxid_bydb, get_process_list2, compare_text_index, get_prov_name
from common.db.sql.sql_get_info import sql_get_info
from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a1_流程一外网企业注册审批,id2303,id2303-1,sit,on
内网:a_a1_流程一外网企业注册审批
python runner_test.py tag id2303-1 debug mdm3
"""

from common.common_func import commonFuc
from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from airtest.core.api import using
import sys

def  check_company_regist_bill(type=1):
    if type==1:
        print('检查企业注册信息数据')
        branch_id = get_branch_id()
        sql1 = "select * from spd_ybtest_hdi.company_regist_bill order by last_modified desc limit 1;"
        actual = check_mcms_pur_sql(sql1)
        print('actual', actual)
        id = get_id("select id from spd_ybtest_hdi.company_regist_bill order by last_modified desc limit 1;")
        user_login_code=get_id("select user_login_code from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        user_login_psw=get_id("select user_login_psw from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        user_name=user_login_code
        email=user_name
        erp_code=user_name
        cname=user_login_code
        # audit_end_time = get_create_time("select audit_end_time from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        audit_end_time=None
        regist_source_project=user_name
        create_time= get_create_time("select create_time from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        last_modified= get_create_time("select last_modified from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        auditor = get_id("select auditor from spd_ybtest_hdi.supply_relation where id='%s';" % id)
        supply_relation_id=get_id("select supply_relation_id from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        comp_function=get_id("select comp_function from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'company_regist_bill', id, user_login_code, user_login_psw, user_name, cname,comp_function
                                                 , email, erp_code, audit_end_time, regist_source_project, create_time,
                                                 last_modified, supply_relation_id)

        actual_value1 = get_process_list2(actual)
        expected_value1 = get_process_list2(expected)
        actual_value2 = actual_value1.replace(" ", "")
        expected_value2 = expected_value1.replace(" ", "")
        # print('actual_value2',actual_value2)
        print('expected_value2')
        print(expected_value2)
        print('actual_value2')
        print(actual_value2)
        result = (compare_text_index(actual_value2, expected_value2))
        print('result', result)
        if actual_value2 == expected_value2:
            print('ok')
            commonFuc().check_text_exist_result_text('succees', 'succees')
        else:
            print('error')
            commonFuc().check_text_exist_result_text('error', 'succees')
    else:
        print('检查医院注册信息数据')
        branch_id = get_branch_id()
        sql1 = "select * from spd_ybtest_hdi.company_regist_bill order by create_time desc limit 1;"
        actual = check_mcms_pur_sql(sql1)
        print('actual', actual)
        id = get_id("select id from spd_ybtest_hdi.company_regist_bill order by create_time desc limit 1;")
        user_login_code = get_id("select user_login_code from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        user_login_psw = get_id("select user_login_psw from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        user_name = user_login_code
        email = user_name
        erp_code = user_name
        cname = user_login_code
        # audit_end_time = get_create_time("select audit_end_time from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        audit_end_time = None
        regist_source_project = user_name
        create_time = get_create_time("select create_time from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        last_modified = get_create_time(
            "select last_modified from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        auditor = get_id("select auditor from spd_ybtest_hdi.supply_relation where id='%s';" % id)
        supply_relation_id = get_id(
            "select supply_relation_id from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        comp_function = get_id("select comp_function from spd_ybtest_hdi.company_regist_bill where id='%s';" % id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'company_regist_bill', id, user_login_code, user_login_psw, user_name,
                                                 cname, comp_function
                                                 , email, erp_code, audit_end_time, regist_source_project, create_time,
                                                 last_modified, supply_relation_id)

        actual_value1 = get_process_list2(actual)
        expected_value1 = get_process_list2(expected)
        actual_value2 = actual_value1.replace(" ", "")
        expected_value2 = expected_value1.replace(" ", "")
        # print('actual_value2',actual_value2)
        print('expected_value2')
        print(expected_value2)
        print('actual_value2')
        print(actual_value2)
        result = (compare_text_index(actual_value2, expected_value2))
        print('result', result)
        if actual_value2 == expected_value2:
            print('ok')
            commonFuc().check_text_exist_result_text('succees', 'succees')
        else:
            print('error')
            commonFuc().check_text_exist_result_text('error', 'succees')
def check_bas_company_info():
    print('检查bas_company_info数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.bas_company_info order by last_modified desc limit 1;"
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id =FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    code=id
    cname=FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    short_pinyin=get_id("select short_pinyin from spd_ybtest_hdi.bas_company_info where id='%s';" % id)
    email=cname
    erp_code=cname
    fill_date=get_create_time("select fill_date from spd_ybtest_hdi.bas_company_info where id='%s';" % id)
    last_update_datetime=get_create_time("select last_update_datetime from spd_ybtest_hdi.bas_company_info where id='%s';" % id)
    last_modified =get_create_time("select last_modified from spd_ybtest_hdi.bas_company_info where id='%s';" % id)
    last_modified_user=get_id("select last_modified_user from spd_ybtest_hdi.bas_company_info where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_bas_company_info', id, code, cname, short_pinyin, email
                                             , erp_code, fill_date, last_update_datetime, last_modified, last_modified_user
                                             )

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')
def check_sys_org():
    print('检查sys_org数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.sys_org  where corp_id='%s';"% FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = get_id("select id from spd_ybtest_hdi.sys_org  where corp_id='%s';"% FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"])
    code=FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    corp_id=FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    last_update_datetime = get_create_time("select last_update_datetime from spd_ybtest_hdi.sys_org where id='%s';" % id)
    create_time= get_create_time("select create_time from spd_ybtest_hdi.sys_org where id='%s';" % id)
    create_user= get_id("select create_user from spd_ybtest_hdi.sys_org where id='%s';" % id)
    last_modified_user= get_id("select last_modified_user from spd_ybtest_hdi.sys_org where id='%s';" % id)

    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.sys_org where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_sys_org', id, code, corp_id, last_update_datetime,create_time, create_user
                                             , last_modified_user, last_modified)

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')
def check_sys_user():
    print('检查sys_user数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.sys_user order by create_time desc limit 1;"
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = get_id("select id from spd_ybtest_hdi.sys_user order by create_time desc limit 1;")
    user_code =  get_id("select user_code from spd_ybtest_hdi.sys_user where id='%s';" % id)
    corp_id = get_id("select corp_id from spd_ybtest_hdi.sys_user where id='%s';" % id)
    last_update_datetime = get_create_time(
        "select last_update_datetime from spd_ybtest_hdi.sys_user where id='%s';" % id)
    create_time = get_create_time("select create_time from spd_ybtest_hdi.sys_user where id='%s';" % id)
    create_user = get_id("select create_user from spd_ybtest_hdi.sys_user where id='%s';" % id)
    last_modified_user = get_id("select last_modified_user from spd_ybtest_hdi.sys_user where id='%s';" % id)

    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.sys_user where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_sys_user', id, user_code, last_update_datetime, create_time,
                                             create_user
                                             , last_modified, last_modified_user,corp_id)

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')

def check_sys_user_role():
    print('检查sys_user_role数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd3_platform_test2.sys_user_role order by create_time desc limit 1;"
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = get_id("select id from spd3_platform_test2.sys_user_role order by create_time desc limit 1;")
    mgr_range_id = get_id("select mgr_range_id from spd3_platform_test2.sys_user_role where id='%s';" % id)
    user_id = get_id("select user_id from spd3_platform_test2.sys_user_role where id='%s';" % id)
    role_code= get_id("select role_code from spd3_platform_test2.sys_user_role where id='%s';" % id)
    create_user= get_id("select create_user from spd3_platform_test2.sys_user_role where id='%s';" % id)
    create_time=get_create_time("select create_time from spd3_platform_test2.sys_user_role where id='%s';" % id)
    last_modified_user = get_id("select last_modified_user from spd3_platform_test2.sys_user_role where id='%s';" % id)
    last_modified=get_create_time("select last_modified from spd3_platform_test2.sys_user_role where id='%s';" % id)

    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_sys_user_role', id, mgr_range_id, user_id, role_code, create_user
                                             , create_time, last_modified_user, last_modified)

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')
def check_sys_user_org():
    print('检查sys_user_org数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd3_platform_test2.sys_user_org order by create_time desc limit 1;"
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = get_id("select id from spd3_platform_test2.sys_user_org order by create_time desc limit 1;")
    user_id = get_id("select user_id from spd3_platform_test2.sys_user_org where id='%s';" % id)
    org_id = get_id("select org_id from spd3_platform_test2.sys_user_org where id='%s';" % id)
    org_code = get_id("select org_code from spd3_platform_test2.sys_user_org where id='%s';" % id)
    ename = get_id("select ename from spd3_platform_test2.sys_user_org where id='%s';" % id)
    cname = ename
    email=cname
    uxid= get_id("select uxid from spd3_platform_test2.sys_user_org where id='%s';" % id)
    last_update_datetime= get_create_time("select last_update_datetime from spd3_platform_test2.sys_user_org where id='%s';" % id)
    create_time = get_create_time("select create_time from spd3_platform_test2.sys_user_org where id='%s';" % id)
    create_user=get_id("select create_user from spd3_platform_test2.sys_user_org where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd3_platform_test2.sys_user_org where id='%s';" % id)
    last_modified_user=create_user
    corp_id=get_id("select corp_id from spd3_platform_test2.sys_user_org where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_sys_user_org', id, user_id, org_id, org_code
                                             , ename, cname, email, uxid, last_update_datetime,
                                             create_time, create_user,last_modified,last_modified_user,corp_id)

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')


def check_sys_user_login():
    print('检查sys_user_login数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd3_platform_test2.sys_user_login order by create_time desc limit 1;"
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = get_id("select id from spd3_platform_test2.sys_user_login order by create_time desc limit 1;")
    uxid = get_id("select uxid from spd3_platform_test2.sys_user_login where id='%s';" % id)
    user_login_code = get_id("select user_login_code from spd3_platform_test2.sys_user_login where id='%s';" % id)
    last_update_datetime=get_create_time("select last_update_datetime from spd3_platform_test2.sys_user_login where id='%s';" % id)
    pwd_encryption= get_id("select pwd_encryption from spd3_platform_test2.sys_user_login where id='%s';" % id)
    create_time=get_create_time("select create_time from spd3_platform_test2.sys_user_login where id='%s';" % id)
    create_user= get_id("select create_user from spd3_platform_test2.sys_user_login where id='%s';" % id)
    last_modified=get_create_time("select last_modified from spd3_platform_test2.sys_user_login where id='%s';" % id)
    last_modified_user=create_user
    corp_id=get_id("select corp_id from spd3_platform_test2.sys_user_login where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_sys_user_login', id, uxid, user_login_code, last_update_datetime, pwd_encryption
                                             , create_time, create_user, last_modified, last_modified_user, corp_id)

    actual_value1 = get_process_list2(actual)
    expected_value1 = get_process_list2(expected)
    actual_value2 = actual_value1.replace(" ", "")
    expected_value2 = expected_value1.replace(" ", "")
    # print('actual_value2',actual_value2)
    print('expected_value2')
    print(expected_value2)
    print('actual_value2')
    print(actual_value2)
    result = (compare_text_index(actual_value2, expected_value2))
    print('result', result)
    if actual_value2 == expected_value2:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error')
        commonFuc().check_text_exist_result_text('error', 'succees')
pro_path = commonFuc().get_pro_path2()
print('pro_path', pro_path)
sys.path.append(pro_path + r'/air_case/b5_spd3_core_business_flow/a_a1_流程一外网企业注册审批.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a1_流程一外网企业注册审批.air')
from a_a1_流程一外网企业注册审批 import test1,test2,test3

def main():
    test1()#企业注册
    time.sleep(6)
    check_company_regist_bill(1)
    check_bas_company_info()
    test2()#企业审核
    time.sleep(6)
    check_sys_org()
    check_sys_user()
    check_sys_user_role()
    check_sys_user_org()
    check_sys_user_login()
    test3()
    time.sleep(6)
    check_sys_org()
    check_sys_user()
    check_sys_user_role()
    check_sys_user_org()
    check_sys_user_login()
try:
    main()
    commonFuc().check_text_exist_result_text('succees','succees')
except Exception as e:
    # 打印错误信息
    print(f"发生错误: {e}")
    commonFuc().check_text_exist_result_text('error','succees')