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

from air_case.public1.public1.public1 import updateUserLoginDefaultRange, get_listUserMgrRangePage, call_login, \
    get_branch_id, get_dept_buy_id, check_mcms_pur_plan_sql, get_hosid, get_id, \
    get_stockAreaId_new_fromDb, get_login_user_uxid, get_expdt_date, get_create_time, get_process_list2, \
    compare_text_index, check_mcms_pur_sql, check_source_id, get_mdm_goods_code1, get_goods_code1, get_supply_id, \
    get_pkg_def_id, get_goods_gg, get_goods_name, get_login_user_uxid_bydb, process_tuple
from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a5流程五_中心库入库结算_正结算,id2303,id2303-5,sit,on
主数据平台:a_a5流程五_中心库入库结算_正结算 
python runner_test.py tag id2303-5 debug mdm3
"""

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



# 检查采购计划主表
def check_mcms_mcms_pur_plan(type=16):
    branch_id = get_branch_id()
    # 要验证的数据sql
    sql1 = "select * from mcms_pur_plan where branch_id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql1)
    print('actual', actual)
    # 获取请领单主键id
    sql = "select id from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        branch_id, type)
    print(sql)
    id1 = process_tuple(get_dept_buy_id(sql))
    sql = "select order_no from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc LIMIT 1;" % (
        branch_id, type)
    print(sql)
    buyOrderNo1 = process_tuple(get_dept_buy_id(sql))
    # 根据请领单的id获取采购计划单主表主键
    sql = "select id from mcms_pur_plan where branch_id='%s'  order by  create_time desc LIMIT 1;" % (
        branch_id)
    id2 = process_tuple(get_dept_buy_id(sql))  # 采购计划主表主键
    sql = "select order_no from mcms_pur_plan where branch_id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id)
    PLAN_order_no = process_tuple(get_dept_buy_id(sql))  # 采购计划主表order_no


    hos_id = get_hosid()
    dept_id1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    dept_name1 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    dept_name2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    rec_org_id = process_tuple(
        get_id("select id from sys_org where branch_id='%s' and ename='%s';" % (branch_id, dept_name1)))
    rec_org_name = dept_name1
    info = FileUtils().r_info8("b5_spd3_core_business_flow", "库区信息", 'message22')
    list_a = info['list_a']
    area_code =get_stockAreaId_new_fromDb(dept_name1, 1)
    sql1 = "select area_name from mcms_pur_plan where branch_id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id)
    area_name = process_tuple(get_id(sql1))
    source_id = None
    rec_user =  get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    sql1 = "select rec_addr from mcms_pur_plan where branch_id='%s'  order by  create_time desc LIMIT 1;" % (
        branch_id)
    rec_addr = process_tuple(get_id(sql1))
    sql = "select bill_expdt_date from  mcms_pur_plan where branch_id='%s' order by create_time desc  LIMIT 1;" % (
        branch_id)
    print(sql)
    bill_expdt_date = None
    buyBillId = id1
    planBillId = id2
    buyOrderNo = buyOrderNo1
    create_user = rec_user
    create_time = get_create_time(
        "select create_time from mcms_pur_plan where branch_id='%s'  order by  create_time desc LIMIT 1;" % (
            branch_id))
    last_modified = get_create_time(
        "select last_modified from mcms_pur_plan where branch_id='%s'  order by  create_time desc LIMIT 1;" % (
            branch_id))
    last_modified_user = create_user
    planOrderNo = PLAN_order_no
    if type == 16:
        # 验证子表数据
        check_mcms_mcms_pur_plan_detail(id2, type, planOrderNo)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan_0927', id2, PLAN_order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, source_id, rec_user,
                                                 rec_addr, bill_expdt_date, planBillId, planOrderNo,

                                                 create_user, create_time, last_modified, last_modified_user)
    elif type == 20:
        check_mcms_mcms_pur_plan_detail(id2, type, planOrderNo)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan2_0927', id2, PLAN_order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, source_id, rec_user,
                                                 rec_addr, bill_expdt_date, planBillId, planOrderNo,

                                                 create_user, create_time, last_modified, last_modified_user)
    elif type == 66:
        check_mcms_mcms_pur_plan_detail(id2, type, planOrderNo)
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan3_0927', id2, PLAN_order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, source_id, rec_user,
                                                 rec_addr, bill_expdt_date, planBillId, planOrderNo,
                                                 create_user, create_time, last_modified, last_modified_user)
    else:
        expected = None
    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_mcms_pur_plan_detail(id, type, planOrderNo):
    branch_id = get_branch_id()
    # 根据采购计划主表的id获取采购计划单子表主键
    sql = "select id from mcms_pur_plan_detail where pid='%s' order by  create_time desc LIMIT 1;" % (
        id)
    id2 = process_tuple(get_dept_buy_id(sql))  # 采购计划单子表主键
    # # 将采购计划子表主键写入文件中
    # info = (id2, id2)
    # titlename = ("id2", 'id2')
    # FileUtils().w_info8(info, 'b6_spd3_core_business_flow_database_check', '采购计划单子表主键', titlename, 'message')
    sql = "select order_no from mcms_pur_plan where id='%s' order by  create_time desc LIMIT 1;" % (
        id2)
    PLAN_order_no = process_tuple(get_dept_buy_id(sql))  # 采购计划单子表order_no
    # 要验证的数据sql
    sql1 = "select * from mcms_pur_plan_detail where id='%s' order by  create_time desc LIMIT 1;" % (
        id2)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql1)
    print('actual', actual)
    hos_id = get_hosid()
    dept_id1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    dept_name1 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    dept_name2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    rec_org_id = process_tuple(
        get_id("select id from sys_org where branch_id='%s' and ename='%s';" % (branch_id, dept_name2)))
    rec_user =  get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = rec_user
    # last_modified = create_time
    last_modified_user = create_user
    pid = id
    if type == 16:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
        mdm_goods_code = process_tuple(
            get_id("select mdm_goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        # print(mdm_goods_code)
        goods_code = process_tuple(
            get_id("select goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        pkg_def_id = process_tuple(
            get_id("select pkg_def_id from mcms_pkg where hos_goods_id='%s' LIMIT 1;" % hos_goods_id))
        supply_id = process_tuple(
            get_id("select supply_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        sub_prov_id = prov_id
        create_time = get_create_time(
            "select create_time from mcms_pur_plan_detail where id='%s';" % (
                id2))
        last_modified = get_create_time(
            "select last_modified from mcms_pur_plan_detail where id='%s';" % (
                id2))
        sub_prov_name = prov_name
        source_id = process_tuple(
            get_id("select source_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan_detail_0927', id2, pid, planOrderNo, source_id, hos_goods_id,
                                                 mdm_goods_code, goods_code, pkg_def_id, supply_id, prov_id, prov_name,
                                                 sub_prov_id, sub_prov_name,
                                                 create_user, create_time, last_modified, last_modified_user
                                                 )
    elif type == 20:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
        mdm_goods_code = process_tuple(
            get_id("select mdm_goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        # print(mdm_goods_code)
        goods_code = process_tuple(
            get_id("select goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        pkg_def_id = process_tuple(
            get_id("select pkg_def_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        supply_id = process_tuple(
            get_id("select supply_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        sub_prov_id = prov_id
        create_time = get_create_time(
            "select create_time from mcms_pur_plan_detail where id='%s';" % (
                id2))
        last_modified =  get_create_time(
            "select last_modified from mcms_pur_plan_detail where id='%s';" % (
                id2))
        sub_prov_name = prov_name
        source_id = process_tuple(
            get_id("select source_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan_detail2_0927', id2, pid, planOrderNo, source_id,
                                                 hos_goods_id,
                                                 mdm_goods_code, goods_code, supply_id, prov_id, prov_name,
                                                 sub_prov_id, sub_prov_name,
                                                 create_user, create_time, last_modified, last_modified_user
                                                 )

    else:

        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
        mdm_goods_code = process_tuple(
            get_id("select mdm_goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        # print(mdm_goods_code)
        goods_code = process_tuple(
            get_id("select goods_code from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        pkg_def_id = process_tuple(
            get_id("select pkg_def_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        supply_id = process_tuple(
            get_id("select supply_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        sub_prov_id = prov_id
        create_time = get_create_time(
            "select create_time from mcms_pur_plan_detail where id='%s';" % (
                id2))
        last_modified = get_create_time(
            "select last_modified from mcms_pur_plan_detail where id='%s';" % (
                id2))
        sub_prov_name = prov_name
        source_id = process_tuple(
            get_id("select source_id from mcms_pur_plan_detail where hos_goods_id='%s' and  id='%s';" % (
                hos_goods_id, id2)))
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_pur_plan_detail3_0927', id2, pid, planOrderNo, source_id,
                                                 hos_goods_id,
                                                 mdm_goods_code, goods_code, pkg_def_id, supply_id, prov_id, prov_name,
                                                 sub_prov_id, sub_prov_name,
                                                 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_purchase(type=16):
    branch_id = get_branch_id()
    # 获取采购订单主键id
    sql = "select id from  mcms_purchase where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        branch_id, type)
    print(sql)
    id1 = process_tuple(get_dept_buy_id(sql))
    sql = "select order_no from  mcms_purchase where branch_id='%s' and bill_mode='%s'and id='%s' order by create_time desc LIMIT 1;" % (
        branch_id, type, id1)
    print(sql)
    order_no = process_tuple(get_dept_buy_id(sql))
    sql = "select id from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        branch_id, type)
    print(sql)
    buyBillId = process_tuple(get_dept_buy_id(sql))

    # 要验证的数据sql
    sql1 = "select * from mcms_purchase where branch_id='%s' and bill_mode='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id, type, id1)
    print(sql1)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql1)
    print('actual', actual)
    hos_id = get_hosid()

    dept_id1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    dept_name1 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    dept_name2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    rec_org_id = process_tuple(
        get_id("select id from sys_org where branch_id='%s' and ename='%s';" % (branch_id, dept_name1)))
    rec_org_name = dept_name1
    info = FileUtils().r_info8("b5_spd3_core_business_flow", "库区信息", 'message22')
    list_a = info['list_a']
    area_code =get_stockAreaId_new_fromDb(dept_name1, 1)
    sql1 = "select area_name from mcms_purchase where branch_id='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id, id1)
    area_name = process_tuple(get_id(sql1))
    rec_user = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1']
    # rec_user =  get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    sql1 = "select rec_addr from mcms_purchase where branch_id='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
        branch_id, id1)
    rec_addr = process_tuple(get_id(sql1))
    sql = "select bill_expdt_date from  mcms_pur_plan where branch_id='%s' and source_id='%s' order by create_time desc  LIMIT 1;" % (
        branch_id, buyBillId)
    print(sql)
    bill_expdt_date = get_expdt_date(sql)

    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_purchase where branch_id='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
            branch_id, id1))
    last_modified = get_create_time(
        "select last_modified from mcms_purchase where branch_id='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
            branch_id, id1))
    last_modified_user = create_user
    last_auditor = rec_user
    last_audit_time = get_create_time(
        "select last_audit_time from mcms_purchase where branch_id='%s' and id='%s' order by  create_time desc LIMIT 1;" % (
            branch_id, id1))

    if type == 16:
        # 验证子表数据
        check_mcms_purchase_detail(id1, type, order_no)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
        planBillId = process_tuple(
            get_id(
                "select pid from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        planOrderNo = process_tuple(
            get_id(
                "select order_no from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
            branch_id, type)
        print(sql)
        buyOrderNo = process_tuple(get_dept_buy_id(sql))
        purOrderNo = order_no
        purBillId = id1
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        exe_prov_id = prov_id
        exe_prov_name = prov_name
        close_date = get_create_time(
            "select close_date from mcms_purchase where id='%s';" % (
                id1))
        source_id = planBillId
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_purchase_0927', id1, order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, bill_expdt_date, source_id, rec_user,
                                                 rec_addr, prov_id, prov_name, exe_prov_id, exe_prov_name,
                                                 planBillId, planOrderNo,
                                                  purOrderNo, purBillId, close_date, create_user,
                                                 create_time, last_modified, last_auditor, last_audit_time
                                                 )

    elif type == 20:
        # 检查采购订单子表
        check_mcms_purchase_detail(id1, type, order_no)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
        planBillId = process_tuple(
            get_id(
                "select pid from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        planOrderNo = process_tuple(
            get_id(
                "select order_no from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
            branch_id, type)
        print(sql)
        buyOrderNo = process_tuple(get_dept_buy_id(sql))
        purOrderNo = order_no
        purBillId = id1
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        exe_prov_id = prov_id
        exe_prov_name = prov_name
        close_date = get_create_time(
            "select close_date from mcms_purchase where id='%s';" % (
                id1))
        source_id = planBillId
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_purchase2_0927', id1, order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, bill_expdt_date, source_id, rec_user,
                                                 rec_addr, prov_id, prov_name, exe_prov_id, exe_prov_name,
                                                 planBillId, planOrderNo,
                                                  purOrderNo, purBillId, close_date, create_user,
                                                 create_time, last_modified, last_auditor, last_audit_time
                                                 )
    elif type == 66:
        # 检查采购订单子表数据
        check_mcms_purchase_detail(id1, type, order_no)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
        planBillId = process_tuple(
            get_id(
                "select pid from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        planOrderNo = process_tuple(
            get_id(
                "select order_no from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
            branch_id, type)
        print(sql)
        buyOrderNo = process_tuple(get_dept_buy_id(sql))
        purOrderNo = order_no
        purBillId = id1
        prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
        prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
        exe_prov_id = prov_id
        exe_prov_name = prov_name
        close_date = get_create_time(
            "select close_date from mcms_purchase where id='%s';" % (
                id1))
        source_id = planBillId
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_purchase3_0927', id1, order_no, hos_id,
                                                 get_branch_id(), dept_id1, dept_name1, rec_org_id, rec_org_name,
                                                 area_code, area_name, bill_expdt_date, source_id, rec_user,
                                                 rec_addr, prov_id, prov_name, exe_prov_id, exe_prov_name,
                                                 planBillId, planOrderNo,
                                                  purOrderNo, purBillId, close_date, create_user,
                                                 create_time, last_modified, last_auditor, last_audit_time
                                                 )
    else:
        expected = None
    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_purchase_detail(id, type, OrderNo):
    branch_id = get_branch_id()
    # 根据采购订单主表的id获取采购订单子表主键
    sql = "select id from mcms_purchase_detail where pid='%s' order by  create_time desc LIMIT 1;" % (
        id)
    detail_main_key = process_tuple(get_dept_buy_id(sql))  # 采购订单单子表主键
    sql = "select order_no from mcms_purchase_detail where pid='%s' order by  create_time desc LIMIT 1;" % (
        id)
    PLAN_order_no = process_tuple(get_dept_buy_id(sql))  # 采购计划单子表order_no
    # 要验证的数据sql
    sql1 = "select * from mcms_purchase_detail where pid='%s' order by  create_time desc LIMIT 1;" % (
        id)
    # 获取实际值
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    hos_id = get_hosid()
    dept_id1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    dept_name1 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
    dept_name2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    rec_org_id = process_tuple(
        get_id("select id from sys_org where branch_id='%s' and ename='%s';" % (branch_id, dept_name2)))
    rec_user =  get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = rec_user
    # last_modified = create_time
    last_modified_user = create_user
    pid = 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"]
    source_id = get_id(
        "select source_id from mcms_purchase_detail where pid='%s' order by  create_time desc LIMIT 1;" % id)
    if check_source_id(hos_goods_id, source_id) == True:
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        commonFuc().check_text_exist_result_text('error', 'succees')
    mdm_goods_code = get_mdm_goods_code1(hos_goods_id)
    goods_code = get_goods_code1(hos_goods_id)
    supply_id = get_supply_id(hos_goods_id)
    pkg_def_id = get_pkg_def_id(hos_goods_id)
    planBillId = process_tuple(
        get_id(
            "select pid from mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                hos_goods_id)))
    sql = "select id from  spd3_herp_test2.mcms_dept_buy where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        get_branch_id(), type)
    print(sql)
    buyBillId = process_tuple(get_dept_buy_id(sql))
    buyDetailId = get_id(
        "select id from mcms_dept_buy_detail where pid='%s';" % buyBillId)

    planDetailId = get_id(
        "select id from spd3_herp_test2.mcms_pur_plan_detail where pid='%s';" % planBillId)
    source_id = planDetailId
    goodsGg = get_goods_gg(hos_goods_id)
    goodsCode = get_goods_code1(hos_goods_id)
    goodsName = get_goods_name(hos_goods_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_purchase_detail where id='%s';" % detail_main_key)
    last_modified = create_time
    last_modified_user = create_user

    if type == 16:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_purchase_detail_0927', detail_main_key, pid, OrderNo, source_id,
                                                 hos_goods_id,
                                                 mdm_goods_code, goods_code, supply_id, pkg_def_id, planBillId,
                                                 planDetailId,
                                                 goodsGg, goodsCode, goodsName, 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_purchase_detail1_0927', detail_main_key, pid, OrderNo, source_id,
                                                 hos_goods_id,
                                                 mdm_goods_code, goods_code, supply_id, pkg_def_id, planBillId,
                                                 planDetailId,
                                                 goodsGg, goodsCode, goodsName, create_user, create_time, last_modified,
                                                 last_modified_user
                                                 )

    else:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'mcms_purchase_detail2_0927', detail_main_key, pid, OrderNo, source_id,
                                                 hos_goods_id,
                                                 mdm_goods_code, goods_code, supply_id, pkg_def_id, planBillId,
                                                 planDetailId,
                                                 goodsGg, goodsCode, goodsName, 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')

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_a5流程五_中心库入库结算_正结算1_提交采购计划.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a5流程五_中心库入库结算_正结算1_提交采购计划.air')
from a_a5流程五_中心库入库结算_正结算1_提交采购计划 import goods_move3,get_GoodsPkgDef, low_vaule_purchase_plan, approval_center, hight_vaule_purchase_plan,reagent_purchase_plan

def main():
    try:
        token, projectCode1, uxid1, corpId1, info1 = call_login(2)
        updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是1级库
        goods_move3(token)
        # 获取低值的hosGoodsId
        hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
        get_GoodsPkgDef(hosGoodsId,token)  # 获取单元包id
        low_vaule_purchase_plan(token,uxid1)#低值采购计划提交
        time.sleep(6)
        check_mcms_mcms_pur_plan(16)#低值采购计划数据检查
        #审批
        approval_center(token)
        time.sleep(6)
        check_mcms_purchase(16)#检查低值采购订单数据
        hight_vaule_purchase_plan(token,uxid1)#高值采购计划提交
        time.sleep(6)
        check_mcms_mcms_pur_plan(20)  # 高值采购计划数据检查
        approval_center(token)
        time.sleep(6)
        check_mcms_purchase(20) #检查高值采购订单数据
        time.sleep(6)
        reagent_purchase_plan(token,uxid1)#试剂采购计划提交
        time.sleep(6)
        check_mcms_mcms_pur_plan(66) #试剂采购计划数据检查
        approval_center(token)
        time.sleep(6)
        check_mcms_purchase(66)  # 检查试剂采购订单数据
        commonFuc().check_text_exist_result_text('succees', 'succees')
    except Exception as e:
        # print('出现错误,请检查脚本或者数据')
        print(f"发生错误: {e}")
        print("发生异常：", str(e))

main()