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

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from air_case.public1.public1.public1 import *
from common.dateUtils import dateUtils
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_a7流程五_中心库入库结算_正结算3_内网验收入库结算,id2303,id2303-7,sit,on
主数据平台:a_a7流程五_中心库入库结算_正结算3_内网验收入库结算
python runner_test.py tag id2303-7 debug mdm3
"""

from common.common_func import commonFuc
from airtest.core.api import using
import sys
import time

def check_mcms_check(type=1):
    if type == 1:
        bill_mode = 16
    elif type == 2:
        bill_mode = 20
    else:
        bill_mode = 66
    print('检查验收单数据库写值')
    check_no = FileUtils().r_info8('b5_spd3_core_business_flow', '验收单号', 'message15')['CHECK_NO1']
    sql = "select * from mcms_check where id = '%s';" % check_no
    order_no = get_id("select order_no from mcms_check where id = '%s';" % check_no)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    if type == 1:
        source_id = FileUtils().r_info8('b5_spd3_core_business_flow', '低值配送单号', 'message10')['distrBillId1']
    elif type == 2:
        source_id = FileUtils().r_info8('b5_spd3_core_business_flow', '高值配送单号', 'message10')['distrBillId1']
    else:
        source_id = FileUtils().r_info8('b5_spd3_core_business_flow', '试剂配送单号', 'message10')['distrBillId1']
    source_no = source_id
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    rec_org_id = get_rec_org_id(1)
    rec_org_name = get_rec_org_name(1)
    dept_id = rec_org_id
    dept_name = rec_org_name
    branch_id = get_branch_id()
    buyBillId = get_buyBillId(branch_id, bill_mode)
    buyOrderNo = get_buyOrderNo(branch_id, bill_mode)
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    planBillId = get_planBillId(hos_goods_id)
    planOrderNo = get_planOrderNo(hos_goods_id)
    purOrderNo = get_purOrderNo(branch_id, bill_mode)
    purBillId = get_purBillId(branch_id, bill_mode)
    bill_relation_json = get_mcms_chek_purBillDate(branch_id, bill_mode)
    purBillDate = reGetString(bill_relation_json, r'purBillDate":"', r'""distrBillId')
    distrBillId = get_distrBillId(branch_id, bill_mode)
    distrOrderNo = get_distrOrderNo(branch_id, bill_mode)
    checkBillId = check_no
    checkOrderNo = order_no
    create_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    sql = "select create_time from mcms_check where id='%s';" % check_no
    create_time = get_create_time(sql)
    sql = "select last_modified from mcms_check where id='%s';" % check_no
    last_modified = get_create_time(sql)
    last_modified_user = create_user
    if type == 1:
        check_mcms_check_batch(type, check_no, order_no, hos_goods_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check_1008', check_no,
                                                 order_no, get_hosid(), get_branch_id(), source_id, source_no, prov_id,
                                                 prov_name, rec_org_id, rec_org_name, dept_id, dept_name,
                                                 planBillId, planOrderNo, purOrderNo, purBillId,
                                                 purBillDate,
                                                 distrBillId, distrOrderNo, checkBillId, checkOrderNo, create_user,
                                                 create_time, last_modified,
                                                 last_modified_user)
    elif type == 2:
        check_mcms_check_batch(type, check_no, order_no, hos_goods_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check1_1008', check_no,
                                                 order_no, get_hosid(), get_branch_id(), source_id, source_no, prov_id,
                                                 prov_name, rec_org_id, rec_org_name, dept_id, dept_name,
                                                 planBillId, planOrderNo, purOrderNo, purBillId,
                                                 purBillDate,
                                                 distrBillId, distrOrderNo, checkBillId, checkOrderNo, create_user,
                                                 create_time, last_modified,
                                                 last_modified_user)
    else:
        check_mcms_check_batch(type, check_no, order_no, hos_goods_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check2_1008', check_no,
                                                 order_no, get_hosid(), get_branch_id(), source_id, source_no, prov_id,
                                                 prov_name, rec_org_id, rec_org_name, dept_id, dept_name,
                                                 planBillId, planOrderNo, purOrderNo, purBillId,
                                                 purBillDate,
                                                 distrBillId, distrOrderNo, checkBillId, checkOrderNo, create_user,
                                                 create_time, last_modified,
                                                 last_modified_user)
    # 对比预期值和实际值是否一致
    # comparison_result(actual, expected)
    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_mcms_check_batch(type, check_no, order_no, hos_goods_id):
    print('数据库检查验收单子表')
    if type == 1:
        bill_mode = 16
    elif type == 2:
        bill_mode = 20
    else:
        bill_mode = 66
    sql = "select * from mcms_check_batch where pid='%s' order by create_time desc LIMIT 1;" % check_no
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    id = get_id("select id from mcms_check_batch where pid='%s' order by create_time desc LIMIT 1;" % check_no)
    pid = check_no
    branch_id = get_branch_id()
    distr_detail_id = get_distr_detail_id(branch_id, bill_mode)
    source_detail_id = distr_detail_id
    mdm_goods_code = get_mdm_goods_code1(hos_goods_id)
    goods_code = get_goods_code1(hos_goods_id)
    sql = "select barcode_id from mcms_check_batch  where pid='%s' order by create_time desc LIMIT 1;" % (
        check_no)
    barcode_id = get_id(sql)
    if check_barcode_id(barcode_id)==True:
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        commonFuc().check_text_exist_result_text('error', 'succees')


    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_check_batch where pid='%s'" % check_no)
    last_modified = get_create_time("select last_modified from mcms_check_batch where pid='%s'" % check_no)
    last_modified_user = create_user
    if type == 1:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check_batch', id,
                                                 pid, order_no, source_detail_id, distr_detail_id, hos_goods_id,
                                                 mdm_goods_code,
                                                 goods_code, barcode_id, create_user, create_time, last_modified,
                                                 last_modified_user
                                                 )
    elif type == 2:

        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check_batch1', id,
                                                 pid, order_no, source_detail_id, distr_detail_id, hos_goods_id,
                                                 mdm_goods_code,
                                                 goods_code, barcode_id, create_user, create_time, last_modified,
                                                 last_modified_user
                                                 )
    else:

        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'mcms_check_batch2', id,
                                                 pid, order_no, source_detail_id, distr_detail_id, hos_goods_id,
                                                 mdm_goods_code,
                                                 goods_code, barcode_id, create_user, create_time, last_modified,
                                                 last_modified_user
                                                 )
    # 对比预期值和实际值是否一致
    # comparison_result(actual, expected)
    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_mcms_psi(type=1):
    print('检查中心库入库单')
    if type == 1:
        bill_mode = 16
    elif type == 2:
        bill_mode = 20
    else:
        bill_mode = 66
    branch_id = get_branch_id()
    sql = "select * from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_YS' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id, bill_mode)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_YS' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id, bill_mode)
    id = get_id(sql)
    sql = "select order_no from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_YS' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id, bill_mode)
    order_no = get_id(sql)
    target_corp_id = get_hosid()
    target_branch_id = get_branch_id()
    target_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    target_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    stock_name = get_branch_name() + "中心库房"
    target_stock_id = get_stockId_fromdb(stock_name)
    target_area_code = get_stockAreaId_new_fromDb(target_dept_name, 1)
    # source_id = FileUtils().r_info8('b5_spd3_core_business_flow', '验收单号', 'message15')['CHECK_NO1']
    sql = "select source_id from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_YS' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id, bill_mode)
    source_id=get_id(sql)
    # source_no = get_distrBillId(branch_id, bill_mode)
    source_no=None
    source_repl_id = source_no
    source_corp_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    source_dept_id = source_corp_id
    source_dept_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    source_stock_id = source_corp_id
    accounter = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    account_date = get_create_time("select account_date from  mcms_psi where id='%s';" % id)

    buyBillId = get_buyBillId(branch_id, bill_mode)
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    planBillId = get_planBillId(hos_goods_id)
    planOrderNo = get_planOrderNo(hos_goods_id)
    buyOrderNo = get_buyOrderNo(branch_id, bill_mode)
    purOrderNo = get_purOrderNo(branch_id, bill_mode)
    purBillId = get_purBillId(branch_id, bill_mode)
    bill_relation_json = get_purBillDate2(branch_id, bill_mode)
    purBillDate = reGetString(bill_relation_json, r'purBillDate":"', r'""distrBillId')
    distrBillId = get_distrBillId(branch_id, bill_mode)
    distrOrderNo = get_distrOrderNo(branch_id, bill_mode)
    checkBillId = source_id
    sql = "select order_no from mcms_check where id='%s';" % checkBillId
    checkOrderNo = get_id(sql)
    psiInBillId = id
    psiInOrderNo = order_no
    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_psi where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_psi where id='%s';" % id)
    last_modified_user = create_user
    if type == 1:
        # 检查子表
        check_mcms_psi_batch_in(id, type, order_no, source_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi1_1008', id,
                                                 order_no, target_corp_id, target_branch_id, target_dept_id,
                                                 target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_no, source_repl_id,
                                                 source_corp_id, source_dept_id, source_dept_name
                                                 , source_stock_id, accounter, account_date, planBillId,
                                                 planOrderNo, purOrderNo, purBillId
                                                 , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
                                                 psiInBillId, psiInOrderNo, create_user
                                                 , create_time, last_modified, last_modified_user)
    elif type == 2:
        check_mcms_psi_batch_in(id, type, order_no, source_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi2_1008', id,
                                                 order_no, target_corp_id, target_branch_id, target_dept_id,
                                                 target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_no, source_repl_id,
                                                 source_corp_id, source_dept_id, source_dept_name
                                                 , source_stock_id, accounter, account_date, planBillId,
                                                 planOrderNo, purOrderNo, purBillId
                                                 , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
                                                 psiInBillId, psiInOrderNo, create_user
                                                 , create_time, last_modified, last_modified_user)
    else:
        check_mcms_psi_batch_in(id, type, order_no, source_id)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi3_1008', id,
                                                 order_no, target_corp_id, target_branch_id, target_dept_id,
                                                 target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_no, source_repl_id,
                                                 source_corp_id, source_dept_id, source_dept_name
                                                 , source_stock_id, accounter, account_date, planBillId,
                                                 planOrderNo, purOrderNo, purBillId
                                                 , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
                                                 psiInBillId, psiInOrderNo, create_user
                                                 , create_time, 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')
    # # 检查中心库出库单====================================================================
    # sql = "select * from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='CK_QL' order by   CREATE_TIME desc LIMIT 1;" % (
    #     branch_id, bill_mode)
    # # 获取实际值
    # actual = check_mcms_pur_plan_sql(sql)
    # print('actual', actual)
    # sql = "select id from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='CK_QL' order by   CREATE_TIME desc LIMIT 1;" % (
    #     branch_id, bill_mode)
    # id_out = get_id(sql)
    # sql = "select order_no from  mcms_psi  where target_branch_id='%s' and bill_mode='%s' and stock_kind='CK_QL' order by   CREATE_TIME desc LIMIT 1;" % (
    #     branch_id, bill_mode)
    # order_no_out = get_id(sql)
    # target_corp_id = get_hosid()
    # target_branch_id = get_branch_id()
    # target_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    # target_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    # stock_name = target_dept_name + "库房"
    # target_stock_id = get_stockId_fromdb(stock_name)
    # target_area_code = get_stockAreaId_new_fromDb(target_dept_name, 2)
    # source_id_out = id
    # source_no_out = order_no
    # source_repl_id = source_no
    # source_corp_id = get_hosid()
    # 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() + '中心库房')
    # accounter = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    # account_date = get_create_time("select account_date from  mcms_psi where id='%s';" % id_out)
    #
    # buyBillId = get_buyBillId(branch_id, bill_mode)
    # if type == 1:
    #     hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    # elif type == 2:
    #     hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    # else:
    #     hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    # planBillId = get_planBillId(hos_goods_id)
    # planOrderNo = get_planOrderNo(hos_goods_id)
    # buyOrderNo = get_buyOrderNo(branch_id, bill_mode)
    # purOrderNo = get_purOrderNo(branch_id, bill_mode)
    # purBillId = get_purBillId(branch_id, bill_mode)
    # bill_relation_json = get_purBillDate2(branch_id, bill_mode)
    # purBillDate = reGetString(bill_relation_json, r'purBillDate":"', r'""distrBillId')
    # distrBillId = get_distrBillId(branch_id, bill_mode)
    # distrOrderNo = get_distrOrderNo(branch_id, bill_mode)
    # checkBillId = source_id
    # sql = "select order_no from mcms_check where id='%s';" % checkBillId
    # checkOrderNo = get_id(sql)
    # psiInBillId = id
    # psiInOrderNo = order_no
    # psiOutBillId = id_out
    # psiOutOrderNo = order_no_out
    # 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_psi where id='%s';" % id_out)
    # last_modified = get_create_time("select last_modified from  mcms_psi where id='%s';" % id_out)
    # last_modified_user = create_user
    # source_branch_id = get_branch_id()
    # source_area_code = get_stockAreaId_new_fromDb(source_dept_name)
    # if type == 1:
    #     check_mcms_psi_batch_out(id, type, order_no, source_id)
    #     expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi_out1',
    #                                              id_out,
    #                                              order_no_out, target_corp_id, target_branch_id, target_dept_id,
    #                                              target_dept_name, target_stock_id
    #                                              , target_area_code, source_id_out, source_no_out, source_repl_id,
    #                                              source_corp_id, source_branch_id, source_dept_id, source_dept_name
    #                                              , source_stock_id, source_area_code, buyBillId, planBillId,
    #                                              planOrderNo, buyOrderNo, purOrderNo, purBillId
    #                                              , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
    #                                              psiInBillId, psiInOrderNo, psiOutBillId, psiOutOrderNo, create_user
    #                                              , create_time, last_modified, last_modified_user)
    # elif type == 2:
    #     check_mcms_psi_batch_out(id, type, order_no, source_id)
    #     expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi_out2',
    #                                              id_out,
    #                                              order_no_out, target_corp_id, target_branch_id, target_dept_id,
    #                                              target_dept_name, target_stock_id
    #                                              , target_area_code, source_id_out, source_no_out, source_repl_id,
    #                                              source_corp_id, source_branch_id, source_dept_id, source_dept_name
    #                                              , source_stock_id, source_area_code, buyBillId, planBillId,
    #                                              planOrderNo, buyOrderNo, purOrderNo, purBillId
    #                                              , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
    #                                              psiInBillId, psiInOrderNo, psiOutBillId, psiOutOrderNo, create_user
    #                                              , create_time, last_modified, last_modified_user)
    # else:
    #     check_mcms_psi_batch_out(id, type, order_no, source_id)
    #     expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check', 'check_mcms_psi_out3',
    #                                              id_out,
    #                                              order_no_out, target_corp_id, target_branch_id, target_dept_id,
    #                                              target_dept_name, target_stock_id
    #                                              , target_area_code, source_id_out, source_no_out, source_repl_id,
    #                                              source_corp_id, source_branch_id, source_dept_id, source_dept_name
    #                                              , source_stock_id, source_area_code, buyBillId, planBillId,
    #                                              planOrderNo, buyOrderNo, purOrderNo, purBillId
    #                                              , purBillDate, distrBillId, distrOrderNo, checkBillId, checkOrderNo,
    #                                              psiInBillId, psiInOrderNo, psiOutBillId, psiOutOrderNo, create_user
    #                                              , create_time, 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_mcms_psi_batch_in(DR_ID, type, order_no, source_id1):
    if type == 1:
        bill_mode = 16
    elif type == 2:
        bill_mode = 20
    else:
        bill_mode = 66
    branch_id = get_branch_id()
    sql = " select * from  mcms_psi_batch where pid='%s' order by create_time desc LIMIT 1;" % (
        DR_ID)
    actual = check_mcms_dept_buy_sql(sql)
    print('actual', actual)
    batch_main_key = get_id("select id from mcms_psi_batch where pid='%s' order by create_time desc LIMIT 1;" % DR_ID)
    pid = DR_ID
    # order_no = get_id("select order_no from mcms_psi_dept_batch where pid='%s';" % DR_ID)
    source_detail_id = get_id("select source_detail_id from mcms_psi_batch where id='%s';" % batch_main_key)
    pick_detail_id = None
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    mdm_goods_code = get_mdm_goods_code1(hos_goods_id)
    goods_code = get_goods_code1(hos_goods_id)
    pkg_def_id = get_pkg_def_id(hos_goods_id)
    picker_name = FileUtils().r_info8("b2_herp3_bs", "用户名信息", 'message3')['username1']
    picker = get_login_user_uxid_bydb(picker_name)
    source_batch_id = get_id("select source_batch_id from mcms_psi_batch where pid='%s';" % DR_ID)
    distrBillId = get_distrBillId(branch_id, bill_mode)
    target_batch_id = get_id(
        "select id from mcms_hos_batch where hos_goods_id='%s' order by create_time desc LIMIT 1;" % hos_goods_id)
    if check_batch_id(source_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        commonFuc().check_text_exist_result_text('error', 'succees')
    barcode_id = get_id("select barcode_id from mcms_psi_batch where id='%s';" % batch_main_key)
    if check_barcode_id(barcode_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        commonFuc().check_text_exist_result_text('error', 'succees')
    dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    shelf_code = get_stockAreaId_new_fromDb(get_branch_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_psi_batch where id='%s';" % batch_main_key)
    last_modified = get_create_time("select last_modified from  mcms_psi_batch where id='%s';" % batch_main_key)
    last_modified_user = create_user
    if type == 1:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_batch_in1', batch_main_key, pid, order_no, source_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, source_batch_id,
                                                 target_batch_id, barcode_id, shelf_code, create_user, create_time,
                                                 last_modified, last_modified_user

                                                 )
    elif type == 2:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_batch_in2', batch_main_key, pid, order_no, source_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, source_batch_id,
                                                 target_batch_id, barcode_id, shelf_code, create_user, create_time,
                                                 last_modified, last_modified_user

                                                 )
    else:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_batch_in3', batch_main_key, pid, order_no, source_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, source_batch_id,
                                                 target_batch_id, barcode_id, shelf_code, 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_out_balance(type=1, OBh_ID=""):
    print('===检查结算数据======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance  where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_out_balance where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    id = get_id(sql)
    if id == OBh_ID:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')

    order_no = get_id(
        "select order_no from  mcms_out_balance where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            branch_id))
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_un_balance where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_un_balance where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = commonFuc().analysis_json('id', list_a)
    bar_code = commonFuc().analysis_json('pkgCodeSee', list_a)
    from_id = get_id(
        "select id from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (hos_goods_id))
    from_order_no = get_id(
        "select order_no from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            hos_goods_id))
    settle_start_time = get_account_date1("select settle_start_time from mcms_out_balance where id='%s';" % id)
    settle_end_time = get_account_date1("select settle_end_time from mcms_out_balance where id='%s';" % id)
    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_out_balance where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_out_balance where id='%s';" % id)
    last_modified_user = create_user
    goods_code = get_goods_code1(hos_goods_id)
    charge_dept_id = settle_dept_id
    settle_month = get_id("select settle_month from mcms_out_balance where id='%s';" % id)
    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    year_month = str(year) + str(month)
    if year_month == settle_month:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_1008', id,
                                             order_no, branch_id, hos_id, prov_id, prov_name, settle_month,
                                             settle_start_time, settle_end_time, create_user, create_time,
                                             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')

    # 检查结算表子表数据
    check_mcms_out_balance_detail(type, id)
    check_mcms_out_balance_detail2(type, id)


def check_mcms_out_balance_detail(type=1, pid=""):
    print('===检查结算数据子表======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance_detail  where pid='%s' order by id LIMIT 1;;" % (
        pid)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
        pid)
    id = get_id(sql)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    id2 = get_id(sql)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_balance_detail where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_balance_detail where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = get_id("select bar_id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
        pid))
    bar_code = get_id("select bar_code from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
        pid))
    from_id = get_id(
        "select from_id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (pid))
    from_order_no = get_id(
        "select order_no from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            hos_goods_id))

    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_out_balance_detail where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_out_balance_detail where id='%s';" % id)
    last_modified_user = create_user
    # goods_code = get_goods_code1(hos_goods_id)
    # charge_dept_id = settle_dept_id
    # settle_month = get_id("select settle_month from mcms_out_balance where id='%s';" % id)
    # year, month = dateUtils().get_year_month(dateUtils().get_current_time())

    out_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    out_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    sub_prov_name = prov_name
    account_date = get_account_date1("select account_date from mcms_out_balance_detail where id='%s';" % id)
    un_settle_id = get_id(
        "select id from mcms_out_un_balance where branch_id='%s' and from_kind='RK_YS' and hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            branch_id, hos_goods_id))
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_detail_1008_1', id,
                                             pid, prov_id, prov_name, sub_prov_id, sub_prov_name, out_dept_id,
                                             out_dept_name, account_date, from_id, from_order_no, hos_goods_id, bar_id,
                                             bar_code, distr_detail_id, hos_batch_id, un_settle_id, create_user,
                                             create_time, last_modified, last_modified_user, hos_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_mcms_out_balance_detail2(type=1, pid=""):
    print('===检查结算数据子表======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance_detail  where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    # sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
    #     pid)
    # id = get_id(sql)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    id = get_id(sql)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_balance_detail where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_balance_detail where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = get_id("select bar_id from mcms_out_balance_detail where id='%s';" % (id))
    bar_code = get_id("select bar_code from mcms_out_balance_detail where id='%s';" % (id))
    from_id = get_id(
        "select from_id from mcms_out_balance_detail where id='%s';" % (id))
    if check_batch_id(from_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    from_order_no = get_id(
        "select order_no from mcms_psi_batch where id='%s';" % (from_id))

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

    out_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    out_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    sub_prov_name = prov_name
    account_date = get_account_date1("select account_date from mcms_out_balance_detail where id='%s';" % id)
    un_settle_id = get_id(
        "select un_settle_id from  mcms_out_balance_detail where id='%s';" % id)
    if check_un_balance_id(un_settle_id):
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_detail_1008_2', id,
                                             pid, prov_id, prov_name, sub_prov_id, sub_prov_name, out_dept_id,
                                             out_dept_name, account_date, from_id, from_order_no, hos_goods_id, bar_id,
                                             bar_code, distr_detail_id, hos_batch_id, un_settle_id, create_user,
                                             create_time, last_modified, last_modified_user, hos_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_mcms_out_balance_approve_after(type=1, OBh_ID=""):
    print('===检查结算数据======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance  where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_out_balance where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
        branch_id)
    id = get_id(sql)
    if id == OBh_ID:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')

    order_no = get_id(
        "select order_no from  mcms_out_balance where branch_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            branch_id))
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_un_balance where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_un_balance where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = commonFuc().analysis_json('id', list_a)
    bar_code = commonFuc().analysis_json('pkgCodeSee', list_a)
    from_id = get_id(
        "select id from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (hos_goods_id))
    from_order_no = get_id(
        "select order_no from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            hos_goods_id))
    settle_start_time = get_account_date1("select settle_start_time from mcms_out_balance where id='%s';" % id)
    settle_end_time = get_account_date1("select settle_end_time from mcms_out_balance where id='%s';" % id)
    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_out_balance where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_out_balance where id='%s';" % id)
    last_modified_user = create_user
    goods_code = get_goods_code1(hos_goods_id)
    charge_dept_id = settle_dept_id
    settle_month = get_id("select settle_month from mcms_out_balance where id='%s';" % id)
    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    year_month = str(year) + str(month)
    if year_month == settle_month:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    check_user = create_user
    check_time = get_create_time("select check_time from  mcms_out_balance where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_1009', id,
                                             order_no, branch_id, hos_id, prov_id, prov_name, settle_month,
                                             settle_start_time, settle_end_time, create_user, create_time,
                                             last_modified, last_modified_user, check_user, check_time
                                             )

    # 对比预期值和实际值是否一致
    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')

    # 检查结算表子表数据
    check_mcms_out_balance_detail_approve_after(type, id)
    check_mcms_out_balance_detail_approve_after2(type, id)


def check_mcms_out_balance_detail_approve_after(type=1, pid=""):
    print('===检查结算数据子表======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance_detail  where pid='%s' order by id LIMIT 1;;" % (
        pid)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
        pid)
    id = get_id(sql)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    id2 = get_id(sql)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_balance_detail where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_balance_detail where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = get_id("select bar_id from  mcms_out_balance_detail where id='%s';" % (
        id))
    bar_code = get_id("select bar_code from  mcms_out_balance_detail where id='%s';" % (
        id))
    # bar_id = commonFuc().analysis_json('id', list_a)
    # bar_code = commonFuc().analysis_json('pkgCodeSee', list_a)
    # from_id = get_id(
    #     "select id from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (hos_goods_id))
    # from_order_no = get_id(
    #     "select order_no from mcms_psi_batch where hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
    #         hos_goods_id))
    from_id = get_id(
        "select from_id from  mcms_out_balance_detail where id='%s';" % (id))
    from_order_no = get_id(
        "select from_order_no from mcms_out_balance_detail where id='%s';" % (
            id))
    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_out_balance_detail where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_out_balance_detail where id='%s';" % id)
    last_modified_user = create_user
    # goods_code = get_goods_code1(hos_goods_id)
    # charge_dept_id = settle_dept_id
    # settle_month = get_id("select settle_month from mcms_out_balance where id='%s';" % id)
    # year, month = dateUtils().get_year_month(dateUtils().get_current_time())

    out_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    out_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    sub_prov_name = prov_name
    account_date = get_account_date1("select account_date from mcms_out_balance_detail where id='%s';" % id)
    un_settle_id = get_id(
        "select id from mcms_out_un_balance where branch_id='%s' and from_kind='RK_YS' and hos_goods_id='%s' order by   CREATE_TIME desc LIMIT 1;" % (
            branch_id, hos_goods_id))
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_detail_1009_1', id,
                                             pid, prov_id, prov_name, sub_prov_id, sub_prov_name, out_dept_id,
                                             out_dept_name, account_date, from_id, from_order_no, hos_goods_id, bar_id,
                                             bar_code, distr_detail_id, hos_batch_id, un_settle_id, create_user,
                                             create_time, last_modified, last_modified_user, hos_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_mcms_out_balance_detail_approve_after2(type=1, pid=""):
    print('===检查结算数据子表======')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    else:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    branch_id = get_branch_id()
    sql = "select * from mcms_out_balance_detail  where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    # sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1;" % (
    #     pid)
    # id = get_id(sql)
    sql = "select id from  mcms_out_balance_detail where pid='%s' order by id LIMIT 1 OFFSET 1;" % (
        pid)
    id = get_id(sql)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    prov_name = get_prov_name()
    sub_prov_id = prov_id
    settle_dept_id = None
    distr_detail_id = get_id("select distr_detail_id from mcms_out_balance_detail where id='%s';" % id)
    if check_distr_detail_id(distr_detail_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    hos_batch_id = get_id("select hos_batch_id from mcms_out_balance_detail where id='%s';" % id)
    if check_batch_id(hos_batch_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    # 从文件中获取条码信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "条码信息", 'message18')
    list_a = info['list_a']
    bar_id = get_id("select bar_id from mcms_out_balance_detail where id='%s';" % (id))
    bar_code = get_id("select bar_code from mcms_out_balance_detail where id='%s';" % (id))
    from_id = get_id(
        "select from_id from mcms_out_balance_detail where id='%s';" % (id))
    if check_batch_id(from_id) == True:
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    from_order_no = get_id(
        "select order_no from mcms_psi_batch where id='%s';" % (from_id))

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

    out_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    out_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    sub_prov_name = prov_name
    account_date = get_account_date1("select account_date from mcms_out_balance_detail where id='%s';" % id)
    un_settle_id = get_id(
        "select un_settle_id from  mcms_out_balance_detail where id='%s';" % id)
    if check_un_balance_id(un_settle_id):
        print('ok')
        commonFuc().check_text_exist_result_text('success', 'success')
    else:
        commonFuc().check_text_exist_result_text('error', 'success')
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_out_balance_detail_0919_2', id,
                                             pid, prov_id, prov_name, sub_prov_id, sub_prov_name, out_dept_id,
                                             out_dept_name, account_date, from_id, from_order_no, hos_goods_id, bar_id,
                                             bar_code, distr_detail_id, hos_batch_id, un_settle_id, create_user,
                                             create_time, last_modified, last_modified_user, hos_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_a7流程五_中心库入库结算_正结算3_内网验收入库结算.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a7流程五_中心库入库结算_正结算3_内网验收入库结算.air')
from a_a7流程五_中心库入库结算_正结算3_内网验收入库结算 import check_accept_order,putaway
try:
    # # 验收
    check_accept_order(3) # 试剂
    time.sleep(6)
    check_mcms_check(3)
    time.sleep(3)
    check_accept_order(2)  # 高值
    time.sleep(6)
    check_mcms_check(2)
    time.sleep(3)
    check_accept_order(1)   # 低值
    time.sleep(6)
    check_mcms_check(1)
    # 入库上架
    putaway()
    time.sleep(6)
    check_mcms_psi(1)
    putaway()
    time.sleep(6)
    check_mcms_psi(2)
    putaway()
    time.sleep(6)
    check_mcms_psi(3)
    time.sleep(3)
    OBD_ID = settle()# 生成结算单
    print(OBD_ID)
    time.sleep(6)
    check_mcms_out_balance(1, OBD_ID)  # 检查审批前的结算数据
    # # 调用审批结算单
    approval_center(OBD_ID)
    time.sleep(6)
    check_mcms_out_balance_approve_after(1, OBD_ID)  # 检查审批后的结算数据
    commonFuc().check_text_exist_result_text('succees', 'succees')

except Exception as e:
    print('脚本出现异常，请检查脚本或者数据')
    print("发生异常：", str(e))
    traceback.print_exc()
else:
    commonFuc().check_text_exist_result_text('succees', 'succees')
