# -*- 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_del_branch_info import delData
from common.db.sql.sql_tools import check_mcms_dept_buy_sql, get_PICK_id, get_buyBillId, get_buyOrderNo, \
    get_PICK_order_no, get_login_user_uxid_bydb, get_create_time, get_pick_detail_id, get_mdm_goods_code1, \
    get_goods_code1, get_pkg_def_id, check_batch_id, check_barcode_id, get_DRId, get_DN_no

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_b8流程十五退库业务_三级库向二级库退库,id2303,id2303-18,sit,on
核心业务流:a_b8流程十五退库业务_三级库向二级库退库
python runner_test.py tag id2303-18 debug mdm3
"""
def check_mcms_psi_dept_out(type1=16):
    branch_id = get_branch_id()

    sql = " select * from  mcms_psi_dept where target_branch_id='%s' and bill_mode='%s' and stock_kind='CK_LYTK' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id, type1)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)
    DR_ID=get_id(" select id from  mcms_psi_dept where target_branch_id='%s' and bill_mode='%s' and stock_kind='CK_LYTK' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id, type1))
    sql = "select order_no from mcms_psi_dept where id='%s';" % DR_ID
    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')['deptid2']
    target_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    target_stock_id = get_stockId_fromdb(target_dept_name + '库房')
    target_area_code = get_stockAreaId_new_fromDb(target_dept_name, 2)
    PICK_id = get_PICK_id(branch_id, type1)
    source_id = PICK_id
    source_corp_id = get_hosid()
    source_branch_id = get_branch_id()
    source_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid3']
    source_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid3']
    source_stock_id = get_stockId_fromdb(source_dept_name + '库房')
    source_area_code = get_stockAreaId_new_fromDb(source_dept_name, 2)
    buyBillId = get_buyBillId(branch_id, type1)
    pickOrderNo = get_PICK_order_no(branch_id, type1)
    buyOrderNo = get_buyOrderNo(branch_id, type1)
    pickBillId = get_PICK_id(branch_id, type1)
    psiDeptOutBillId = DR_ID
    psiDeptOutOrderNo = 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_dept where id='%s';" % DR_ID)
    last_modified = get_create_time("select last_modified from  mcms_psi_dept where id='%s';" % DR_ID)
    last_modified_user = create_user
    accounter=create_user
    account_date = process_tuple(get_create_time("select account_date from  mcms_psi_dept where id='%s';" % DR_ID))
    if type1 == 16:
        # 子表检查
        check_mcms_psi_dept_batch(DR_ID,type1,order_no)
        target_area_code = None
        accounter = None
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_tkck1', DR_ID, order_no, target_corp_id, target_branch_id,
                                                 target_dept_id, target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo
                                                 , create_user, create_time, last_modified, last_modified_user

                                                 )
    elif type1 == 20:
        # 子表检查
        check_mcms_psi_dept_batch(DR_ID,type1,order_no)
        target_area_code=None
        accounter=None
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_tkck2', DR_ID, order_no, target_corp_id, target_branch_id,
                                                 target_dept_id, target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo
                                                 , create_user, create_time, last_modified, last_modified_user)
    else:
        # 子表检查
        check_mcms_psi_dept_batch(DR_ID, type1, order_no)
        target_area_code = None
        accounter = None
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_tkck3', DR_ID, order_no, target_corp_id, target_branch_id,
                                                 target_dept_id, target_dept_name, target_stock_id
                                                 , target_area_code, source_id, source_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo
                                                 , 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_psi_dept_batch(DR_ID,type,order_no):
    branch_id = get_branch_id()
    sql = " select * from  mcms_psi_dept_batch where pid='%s';" % (
        DR_ID)
    actual = check_mcms_dept_buy_sql(sql)
    print('actual', actual)
    batch_main_key = get_id("select id from mcms_psi_dept_batch where pid='%s';" % 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_pick_detail_id(branch_id,type)
    pick_detail_id=source_detail_id

    if type == 16:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 20:
        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_dept_batch where pid='%s';" % DR_ID)
    target_batch_id=source_batch_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_dept_batch where pid='%s';" % DR_ID)
    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')['deptid3']
    shelf_code= get_stockAreaId_new_fromDb(dept_name, 2)
    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_dept_batch where pid='%s';" % DR_ID)
    last_modified = get_create_time("select last_modified from  mcms_psi_dept_batch where pid='%s';" % DR_ID)
    last_modified_user = create_user
    if type == 16:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_batch1', batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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 == 20:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_batch2', batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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_dept_batch3_1',batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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_psi_dept_batch_in(DR_ID,type,order_no):
    branch_id = get_branch_id()
    sql = " select * from  mcms_psi_dept_batch where pid='%s';" % (
        DR_ID)
    actual = check_mcms_dept_buy_sql(sql)
    print('actual', actual)
    batch_main_key = get_id("select id from mcms_psi_dept_batch where pid='%s';" % 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_DRId(branch_id,type)+"0001"
    pick_detail_id=None

    if type == 16:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 20:
        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_dept_batch where pid='%s';" % DR_ID)
    target_batch_id=source_batch_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_dept_batch where pid='%s';" % DR_ID)
    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(dept_name, 2)
    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_dept_batch where pid='%s';" % DR_ID)
    last_modified = get_create_time("select last_modified from  mcms_psi_dept_batch where pid='%s';" % DR_ID)
    last_modified_user = create_user
    if type == 16:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_batch_tkrk1', batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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 == 20:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_batch_tkrk2', batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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_dept_batch_tkrk3',batch_main_key, pid, order_no,source_detail_id
                                            ,pick_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_psi_dept_in(type1=16):
    branch_id = get_branch_id()
    sql = " select * from  mcms_psi_dept where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_LYTK' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id, type1)
    actual = check_mcms_dept_buy_sql(sql)
    print(actual)
    DR_ID=get_id(" select id from  mcms_psi_dept where target_branch_id='%s' and bill_mode='%s' and stock_kind='RK_LYTK' order by   CREATE_TIME desc LIMIT 1;" % (
    branch_id, type1))
    sql = "select order_no from mcms_psi_dept where id='%s';" % DR_ID
    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')['deptid2']
    target_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    target_stock_id = get_stockId_fromdb(target_dept_name + '库房')
    target_area_code = get_stockAreaId_new_fromDb(target_dept_name, 2)

    source_id = get_DRId(branch_id,type1)
    source_no=get_DN_no(branch_id,type1)
    source_corp_id = get_hosid()
    source_branch_id = get_branch_id()
    source_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid3']
    source_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid3']
    source_stock_id = get_stockId_fromdb(source_dept_name + '库房')
    source_area_code = get_stockAreaId_new_fromDb(source_dept_name, 2)
    buyBillId = get_buyBillId(branch_id, type1)
    pickOrderNo = get_PICK_order_no(branch_id, type1)
    buyOrderNo = get_buyOrderNo(branch_id, type1)
    pickBillId = get_PICK_id(branch_id, type1)
    psiDeptOutBillId = source_id
    psiDeptOutOrderNo = source_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_dept where id='%s';" % DR_ID)
    last_modified = get_create_time("select last_modified from  mcms_psi_dept where id='%s';" % DR_ID)
    last_modified_user = create_user
    accounter=create_user
    account_date = process_tuple(get_create_time("select account_date from  mcms_psi_dept where id='%s';" % DR_ID))
    psiDeptInBillId=DR_ID
    psiDeptInOrderNo=order_no
    if type1 == 16:
        # 子表检查
        check_mcms_psi_dept_batch_in(DR_ID,type1,order_no)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_psi_dept_tkrk1', DR_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_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo,psiDeptInBillId,psiDeptInOrderNo
                                                 , create_user, create_time, last_modified, last_modified_user

                                                 )
    elif type1 == 20:
        # 子表检查
        check_mcms_psi_dept_batch_in(DR_ID,type1,order_no)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                   'mcms_psi_dept_tkrk2', DR_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_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo,psiDeptInBillId,psiDeptInOrderNo
                                                 , create_user, create_time, last_modified, last_modified_user
                                                 )
    else:
        # 子表检查
        check_mcms_psi_dept_batch_in(DR_ID, type1, order_no)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                   'mcms_psi_dept_tkrk3', DR_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_corp_id, source_branch_id,
                                                 source_dept_id, source_dept_name, source_stock_id, source_area_code,accounter,account_date,
                                                 pickOrderNo, pickBillId, psiDeptOutBillId, psiDeptOutOrderNo,psiDeptInBillId,psiDeptInOrderNo
                                                 , 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_b8流程十五退库业务_三级库向二级库退库.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_b8流程十五退库业务_三级库向二级库退库.air')
from a_b8流程十五退库业务_三级库向二级库退库 import second_dept_return_Out_Stock
try:
    updateUserLoginDefaultRange(get_listUserMgrRangePage1(3))  # 设置登录账号默认权限是三级库
    second_dept_return_Out_Stock(2)  # 高值耗材退货出库
    time.sleep(6)
    check_mcms_psi_dept_out(20)#检查出库单（包括主表和子表）
    time.sleep(6)
    second_dept_return_Out_Stock(1)  # 低值耗材退货出库
    time.sleep(6)
    check_mcms_psi_dept_out(16)  # 检查出库单（包括主表和子表）
    second_dept_return_Out_Stock(3)  #试剂耗材退货出库
    check_mcms_psi_dept_out(66)  # 检查出库单（包括主表和子表）
    second_dept_one_key_in(2)
    second_dept_one_key_in(2)
    second_dept_one_key_in(2)
    time.sleep(6)
    check_mcms_psi_dept_in(16)  # 检查入库单（包括主表和子表）
    check_mcms_psi_dept_in(20)  # 检查入库单（包括主表和子表）
    check_mcms_psi_dept_in(66)  # 检查入库单（包括主表和子表）
    commonFuc().check_text_exist_result_text('succees', 'succees')
except Exception as e:
    # 打印错误信息
    print(f"发生错误: {e}")
    commonFuc().check_text_exist_result_text('error', 'SUCCESS')
