# -*- encoding=utf8 -*-
from air_case.public1.public1.public1 import updateUserLoginDefaultRange, get_listUserMgrRangePage, call_login, \
    get_branch_id, get_dept_buy_id, check_mcms_pur_plan_sql, get_hosid, get_id, \
    get_stockAreaId_new_fromDb, get_login_user_uxid, get_expdt_date, get_create_time, get_process_list2, \
    compare_text_index, check_mcms_pur_sql, check_source_id, get_mdm_goods_code1, get_goods_code1, get_supply_id, \
    get_pkg_def_id, get_goods_gg, get_goods_name, process_tuple, get_prov_id, get_prov_id1
from common.db.sql.sql_tools_out import get_login_user_uxid_bydb
from common.fileUtls import FileUtils

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a2_流程二供货关系申请提交审批,id2303,id2303-2,sit,on
主数据平台:a_a2_流程二供货关系申请提交审批 
python runner_test.py tag id2303-2 debug mdm3
"""

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


def check_supply_relation():
    print('检查供应关系表数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.supply_relation 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.supply_relation order by create_time desc limit 1;")
    hos_id=get_id("select hos_id from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    prov_id = get_prov_id1()
    sub_prov_id=prov_id
    contact_way=get_id("select contact_way from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    contact_user=get_id("select contact_user from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    create_time = get_create_time("select create_time from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    # username = commonFuc().get_business_data('b2_herp3_bs', "username")
    auditor=get_id("select auditor from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    # create_user = get_login_user_uxid_bydb(
    #     FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"])
    # last_modified_user = create_user
    audit_time= get_create_time("select audit_time from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_supply_relation', id,hos_id,prov_id,sub_prov_id,contact_way
                                             ,contact_user,auditor,audit_time, create_time, 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_supply_relation2():
    print('检查供应关系表数据')
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.supply_relation 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.supply_relation order by create_time desc limit 1;")
    hos_id=get_id("select hos_id from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    prov_id = get_prov_id()
    sub_prov_id=get_id("select sub_prov_id from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    contact_way=get_id("select contact_way from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    contact_user=get_id("select contact_user from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    create_time = get_create_time("select create_time from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    # last_modified = get_create_time("select last_modified from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    # username = commonFuc().get_business_data('b2_herp3_bs', "username")
    auditor=get_id("select auditor from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"])
    last_modified_user = get_id("select id from spd_ybtest_hdi.sys_user where user_code='test_1';")
    audit_time= get_create_time("select audit_time from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.supply_relation where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_supply_relation2', id,hos_id,prov_id,sub_prov_id,contact_way
                                             ,contact_user,auditor,audit_time, 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')

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_a2_流程二供货关系申请提交审批.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a2_流程二供货关系申请提交审批.air')
from a_a2_流程二供货关系申请提交审批 import h_login, e_login, supply_and_marketing_apply, submitSupplyRelation_pass, \
    supply_and_marketing_apply2

try:
    module = "b5_spd3_core_business_flow"
    h_login()  # 医院登录
    e_login(1)  # 企业登录
    # 供应关系申请-客户类型医院
    FileUtils().w_info1(supply_and_marketing_apply(), module, '供货关系申请')
    submitSupplyRelation_pass()
    check_supply_relation()
    # 供应关系申请-客户类型厂商
    FileUtils().w_info1(supply_and_marketing_apply2(), module, '供货关系申请2')
    submitSupplyRelation_pass()
    check_supply_relation2()
    commonFuc().check_text_exist_result_text('succees', 'succees')
    print('=================流程二结束=================')
except Exception as e:
    # 打印错误信息
    print(f"发生错误: {e}")
    print("发生异常：", str(e))
    print('a_a2_流程二供货关系申请提交审批,出现异常,请检查')
