# -*- encoding=utf8 -*-
import json
import traceback

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from air_case.public1.public1.public1 import *
from common.db.sql.sql_tools import check_mcms_dept_buy_sql, get_login_user_uxid_bydb, get_create_time

from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command
from common.timeUtils import timeUtils
from airtest.core.api import using

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_b1流程八_高值跟台业务流程,id2303,id2303-11,sit,on
主数据平台:a_b1流程八_高值跟台业务流程
python runner_test.py tag id2303-11 debug mdm3
"""


def check_mcms_operation_apply():#检查跟台申请单
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_apply where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    #获取主表主键ID
    sql = " select id from  mcms_operation_apply where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id)
    ID = get_id(sql)
    #获取order_no
    sql = " select order_no from  mcms_operation_apply where id='%s';" % (ID)
    order_no = get_id(sql)
    rec_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    rec_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    target_stock_id = get_stockId_fromdb(rec_dept_name + '库房')
    rec_address_id = get_stockAreaId_new_fromDb(rec_dept_name, 2)


    source_corp_id = get_hosid()
    source_branch_id = get_branch_id()
    source_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    source_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    source_stock_id = get_stockId_fromdb(get_branch_name() + '中心库房')
    source_area_code = get_stockAreaId_new_fromDb(source_dept_name, 1)

    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply where id='%s';" % ID)
    oper_date= get_create_time("select oper_date from  mcms_operation_apply where id='%s';" % ID)
    expire_date= get_create_time("select expire_date from  mcms_operation_apply where id='%s';" % ID)
    last_modified_user = create_user
    hos_id=get_hosid()
    hos_name=get_hosname()
    branch_name=get_branch_name()
    #子表检查
    check_mcms_operation_apply_list(ID)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'check_mcms_operation_apply', ID, hos_id,hos_name,branch_id,branch_name,order_no,
                                             rec_dept_id,rec_dept_name,rec_address_id,oper_date,expire_date
                                                 , create_user, create_time, last_modified, last_modified_user

                                                 )


    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_mcms_operation_apply_list(pid):
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_apply_list where pid='%s';" % (
        pid)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    # 获取子表主键ID
    sql = " select id from  mcms_operation_apply_list where pid='%s';" % (
        pid)
    ID = get_id(sql)

    hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    prov_id=get_prov_id()
    sub_prov_id=get_prov_id()


    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified_user = create_user

    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_operation_apply_list', ID, pid, hos_goods_id, prov_id, sub_prov_id,
                                           create_user, create_time, last_modified, last_modified_user

                                             )

    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_mcms_operation_apply_approval_after():#检查跟台申请单审批后的
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_apply where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    #获取主表主键ID
    sql = " select id from  mcms_operation_apply where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id)
    ID = get_id(sql)
    #获取order_no
    sql = " select order_no from  mcms_operation_apply where id='%s';" % (ID)
    order_no = get_id(sql)
    rec_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    rec_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    target_stock_id = get_stockId_fromdb(rec_dept_name + '库房')
    rec_address_id = get_stockAreaId_new_fromDb(rec_dept_name, 2)


    source_corp_id = get_hosid()
    source_branch_id = get_branch_id()
    source_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    source_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    source_stock_id = get_stockId_fromdb(get_branch_name() + '中心库房')
    source_area_code = get_stockAreaId_new_fromDb(source_dept_name, 1)

    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply where id='%s';" % ID)
    oper_date= get_create_time("select oper_date from  mcms_operation_apply where id='%s';" % ID)
    expire_date= get_create_time("select expire_date from  mcms_operation_apply where id='%s';" % ID)
    last_modified_user = create_user
    hos_id=get_hosid()
    hos_name=get_hosname()
    branch_name=get_branch_name()
    #子表检查
    check_mcms_operation_apply_list(ID)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'check_mcms_operation_apply_approval_after', ID, hos_id,hos_name,branch_id,branch_name,order_no,
                                             rec_dept_id,rec_dept_name,rec_address_id,oper_date,expire_date
                                                 , create_user, create_time, last_modified, last_modified_user

                                                 )


    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_mcms_operation_apply_list_apply_approval_after(pid):
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_apply_list where pid='%s';" % (
        pid)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    # 获取子表主键ID
    sql = " select id from  mcms_operation_apply_list where pid='%s';" % (
        pid)
    ID = get_id(sql)

    hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    prov_id=get_prov_id()
    sub_prov_id=get_prov_id()


    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified_user = create_user

    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_operation_apply_list', ID, pid, hos_goods_id, prov_id, sub_prov_id,
                                           create_user, create_time, last_modified, last_modified_user

                                             )

    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_mcms_operation_notice():
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_notice where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    # 获取主表主键ID
    sql = " select id from  mcms_operation_notice where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    ID = get_id(sql)
    # 获取order_no
    sql = " select order_no from  mcms_operation_notice where id='%s';" % (ID)
    order_no = get_id(sql)
    source_bill_id=get_id("select id from mcms_operation_apply order by create_time desc  LIMIT 1;")
    source_order_no=get_id("select order_no from mcms_operation_apply order by create_time desc  LIMIT 1;")

    rec_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    rec_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    target_stock_id = get_stockId_fromdb(rec_dept_name + '库房')
    rec_address_id = get_stockAreaId_new_fromDb(rec_dept_name, 2)

    source_corp_id = get_hosid()
    source_branch_id = get_branch_id()
    source_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    source_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    source_stock_id = get_stockId_fromdb(get_branch_name() + '中心库房')
    source_area_code = get_stockAreaId_new_fromDb(source_dept_name, 1)

    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply where id='%s';" % ID)
    oper_date = get_create_time("select oper_date from  mcms_operation_apply where id='%s';" % ID)
    expire_date = get_create_time("select expire_date from  mcms_operation_apply where id='%s';" % ID)
    last_modified_user = create_user
    hos_id = get_hosid()
    hos_name = get_hosname()
    branch_name = get_branch_name()
    # 子表检查
    check_mcms_operation_notice_list(ID,order_no)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_operation_notice', ID, hos_id, hos_name,
                                             branch_id, branch_name, order_no,
                                             rec_dept_id, rec_dept_name, rec_address_id, oper_date, expire_date
                                             , create_user, create_time, last_modified, last_modified_user

                                             )

    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_mcms_operation_notice_list(pid,p_order_no):
    branch_id = get_branch_id()
    sql = " select * from  mcms_operation_notice_list where pid='%s';" % (
        pid)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)

    # 获取子表主键ID
    sql = " select id from  mcms_operation_notice_list where pid='%s';" % (
        pid)
    ID = get_id(sql)
    source_bill_detail_id=get_id("select id from mcms_operation_apply_list order by create_time desc  LIMIT 1;")
    pkg_code=p_order_no
    hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    prov_id=get_prov_id()
    sub_prov_id=get_prov_id()


    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_time = get_create_time("select create_time from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified = get_create_time("select last_modified from  mcms_operation_apply_list where id='%s';" % ID)
    last_modified_user = create_user

    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_operation_notice_list', ID, pid, hos_goods_id, prov_id, sub_prov_id,
                                           create_user, create_time, last_modified, last_modified_user

                                             )

    print('expected', expected)
    print('aaaaaaaaaaaaaaaaa')
    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)
    print(compare_text_index(actual_value2, expected_value2))
    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_b1流程八_高值跟台业务流程.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_b1流程八_高值跟台业务流程.air')
from a_b1流程八_高值跟台业务流程 import operationApply,second_dept_in_warehouse,out_return_gt
try:
    # set_dept_lead()  # 设置二级科室负责人
    # # # # # 设置默认科室,切换到二级科室,使用科室管理功能
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
    # herpService_mcmsDeptGoodsInfo_getByProvIdPage()
    # time.sleep(6)
    # operationApply()  # 手术跟台申请单提交
    # time.sleep(6)
    # check_mcms_operation_apply()# 手术跟台申请单数据库检查
    # approval_center()  # 二级科室审批审批手术跟台申请单
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是中心库
    # approval_center1()  # 中心库再次审批手术跟台申请单
    # time.sleep(6)
    # check_mcms_operation_apply_approval_after()  # 手术跟台申请单审批后的数据库检查
    # # #内网查询手术通知单号
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是中心库
    # orderno = Search_OrderNO()  # 手术通知单接口查询单号
    # time.sleep(6)
    check_mcms_operation_notice()# 手术通知单数据库检查

    height_value_order_dp(orderno)  # 高值跟台外网配送
    check_accept_order()  # 内网验收
    updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
    second_dept_in_warehouse()  # 二级科室入库
    updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是中心库
    out_return_gt()  # 跟台退货出库_中心库处理
    commonFuc().check_text_exist_result_text('succees', 'succees')
except Exception as e:
    # 打印错误信息
    print(f"发生错误: {e}")
    commonFuc().check_text_exist_result_text('error', 'succees')

