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

from air_case.mdm_login.登录外网系统.登录外网系统 import login_system
from air_case.public1.public1.public1 import get_branch_id, get_hosid, process_tuple, get_dept_buy_id, reGetString, \
    get_login_user_uxid2, get_create_time, get_ext_info, get_process_list2, compare_text_index, check_mcms_pur_plan_sql, \
    get_id, get_mdm_goods_code1, get_goods_code1, get_goods_name, get_goods_gg, get_goods_mfrs_id, get_expdt_date, \
    get_supply_id, call_login, get_hosname
from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a6流程五_中心库入库结算_正结算2_外网配送,id2303,id2303-6,sit,on
主数据平台:a_a6流程五_中心库入库结算_正结算2_外网配送
python runner_test.py tag id2303-6 debug mdm3
"""

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



def  check_hdi_distr(type=16):
    from common.db.sql.sql_tools_out import check_mcms_pur_plan_sql, get_id
    branch_id = get_branch_id()
    # 验证配送单主表
    sql = "select * from hdi_distr  where branch_id = '%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        branch_id, type)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    hos_id = get_hosid()
    # 获取采购订单id
    purchase_id = get_id(
        "select id from mcms_purchase where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
    # 查询配送单主键
    distr_main_key = get_id(
        "select id from hdi_distr a where branch_id = '%s' and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
    # 查询配送单order_no
    distr_order_no = get_id(
        "select order_no from hdi_distr a where id = '%s' and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            distr_main_key, type))
    # 从采购订单获取source_id
    source_id = get_id(
        "select id from mcms_purchase where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
    pur_bill_id = source_id
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    rec_user = info['username1']
    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;" % (
        branch_id, type)
    print(sql)
    buyBillId = process_tuple(get_dept_buy_id(sql))
    bill_relation_json = get_id(
        "select bill_relation_json from hdi_distr a where id = '%s' and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            distr_main_key, type))
    print(bill_relation_json)
    purBillDate = reGetString(bill_relation_json, r'purBillDate":"', r'""distrBillId')
    distrBillId = distr_main_key
    distrOrderNo = distr_order_no
    distrUser = get_login_user_uxid2()
    create_user = distrUser
    create_user_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    create_time = get_create_time(
        "select create_time from hdi_distr where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
    last_modified_user = distrUser
    if type == 16:
        # 验证子表数据
        check_hdi_distr_detail(distr_main_key, type, purchase_id)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]

        planBillId = process_tuple(
            get_id(
                "select pid from spd3_herp_test2.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 spd3_herp_test2.mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  spd3_herp_test2.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))
        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)
        purOrderNo = process_tuple(get_dept_buy_id(sql))
        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
        pur_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
        pur_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
        rec_org_id = process_tuple(
            get_id("select id from spd3_herp_test2.sys_org where branch_id='%s' and ename='%s';" % (
                branch_id, FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1'])))
        rec_org_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
        last_modified = get_create_time(
        "select last_modified from hdi_distr where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
        confirmUser = distrUser
        ext_info = get_ext_info(branch_id, type)
        confirmTime = reGetString(ext_info, r'"confirmTime":"', r'""confirmUser')
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr_0929', distr_main_key, distr_order_no, hos_id, get_hosname(),
                                                 get_branch_id(), source_id, pur_bill_id, prov_id, prov_name,
                                                 exe_prov_id, exe_prov_name, pur_dept_id, pur_dept_name, rec_org_id,
                                                 rec_org_name, rec_user, planBillId, planOrderNo,
                                                 purOrderNo, purBillId, purBillDate, distrBillId, distrOrderNo,
                                                 confirmTime, confirmUser, distrUser, create_user, create_user_name,
                                                 create_time, last_modified,
                                                 last_modified_user
                                                 )

    elif type == 20:
        # 验证子表数据
        check_hdi_distr_detail(distr_main_key, type, purchase_id)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]

        planBillId = process_tuple(
            get_id(
                "select pid from spd3_herp_test2.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 spd3_herp_test2.mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  spd3_herp_test2.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))
        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)
        purOrderNo = process_tuple(get_dept_buy_id(sql))
        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
        pur_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
        pur_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
        rec_org_id = process_tuple(
            get_id("select id from spd3_herp_test2.sys_org where branch_id='%s' and ename='%s';" % (
                branch_id, FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2'])))
        rec_org_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
        last_modified = get_create_time(
        "select last_modified from hdi_distr where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
        confirmUser = distrUser
        ext_info = get_ext_info(branch_id, type)
        confirmTime = reGetString(ext_info, r'"confirmTime":"', r'""confirmUser')
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr1_0929', distr_main_key, distr_order_no, hos_id, get_hosname(),
                                                 get_branch_id(), source_id, pur_bill_id, prov_id, prov_name,
                                                 exe_prov_id, exe_prov_name, pur_dept_id, pur_dept_name, rec_org_id,
                                                 rec_org_name, rec_user, planBillId, planOrderNo,
                                                 purOrderNo, purBillId, purBillDate, distrBillId, distrOrderNo,
                                                 confirmTime, confirmUser, distrUser, create_user, create_user_name,
                                                 create_time, last_modified,
                                                 last_modified_user
                                                 )

    elif type == 66:
        # 验证子表数据
        check_hdi_distr_detail(distr_main_key, type, purchase_id)
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]

        planBillId = process_tuple(
            get_id(
                "select pid from spd3_herp_test2.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 spd3_herp_test2.mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
                    hos_goods_id)))
        sql = "select order_no from  spd3_herp_test2.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))
        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)
        purOrderNo = process_tuple(get_dept_buy_id(sql))
        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
        pur_dept_id = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
        pur_dept_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid1']
        rec_org_id = process_tuple(
            get_id("select id from spd3_herp_test2.sys_org where branch_id='%s' and ename='%s';" % (
                branch_id, FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2'])))
        rec_org_name = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
        last_modified = get_create_time(
        "select last_modified from hdi_distr where branch_id = '%s'  and bill_mode='%s' order by create_time desc LIMIT 1;" % (
            branch_id, type))
        confirmUser = distrUser
        ext_info = get_ext_info(branch_id, type)
        confirmTime = reGetString(ext_info, r'"confirmTime":"', r'""confirmUser')
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr2_0929', distr_main_key, distr_order_no, hos_id, get_hosname(),
                                                 get_branch_id(), source_id, pur_bill_id, prov_id, prov_name,
                                                 exe_prov_id, exe_prov_name, pur_dept_id, pur_dept_name, rec_org_id,
                                                 rec_org_name, rec_user, planBillId, planOrderNo,
                                                 purOrderNo, purBillId, purBillDate, distrBillId, distrOrderNo,
                                                 confirmTime, confirmUser, distrUser, create_user, create_user_name,
                                                 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_hdi_distr_detail(distr_main_key, type, purchase_id):
    sql = "select * from hdi_distr_detail where pid = '%s';" % distr_main_key
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    # 获取子表主键
    distr_detail_main_key = get_id(
        "select id from hdi_distr_detail where pid = '%s';" % (
            distr_main_key))
    source_id = purchase_id
    source_detail_id = get_id("select id from mcms_purchase_detail where pid='%s'" % purchase_id)
    pur_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)
    goods_name = get_goods_name(hos_goods_id)
    goods_gg = get_goods_gg(hos_goods_id)
    mfrs_id = get_goods_mfrs_id(hos_goods_id)
    expdt_date = get_expdt_date(
        "select expdt_date from hdi_distr_detail where pid = '%s';" % (
            distr_main_key))
    product_date = get_expdt_date(
        "select product_date from hdi_distr_detail where pid = '%s';" % (
            distr_main_key))
    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 = get_supply_id(hos_goods_id)
    sub_prov_id = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    sub_prov_name = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    planBillId = process_tuple(
        get_id(
            "select pid from spd3_herp_test2.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)
    # planBillId = process_tuple(
    #     get_id(
    #         "select pid from spd3_herp_test2.mcms_pur_plan_detail  where hos_goods_id='%s' order by create_time desc  LIMIT 1;" % (
    #             hos_goods_id)))
    planDetailId = get_id(
        "select id from spd3_herp_test2.mcms_pur_plan_detail where pid='%s';" % planBillId)
    purDetailId = pur_detail_id
    sql = "select id from  mcms_purchase where branch_id='%s' and bill_mode='%s' order by create_time desc  LIMIT 1;" % (
        get_branch_id(), type)
    print(sql)
    id1 = process_tuple(get_dept_buy_id(sql))
    purBillId = id1
    distrDetailId = distr_detail_main_key
    distrBillId = distr_main_key
    erpCode = mdm_goods_code  # 11661563
    create_user = get_login_user_uxid2()
    create_time = get_create_time(
        "select create_time from hdi_distr_detail where id = '%s';" % (
            distr_detail_main_key))
    last_modified = get_create_time(
        "select last_modified from hdi_distr_detail where id = '%s';" % (
            distr_detail_main_key))
    last_modified_user = create_user
    if type == 16:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr_detail_0929', distr_detail_main_key, distr_main_key, source_id,
                                                 source_detail_id, pur_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, goods_name, goods_gg,
                                                 mfrs_id, expdt_date, product_date
                                                 , pkg_def_id, supply_id, sub_prov_id, sub_prov_name, planBillId,
                                                  planDetailId, purDetailId, purBillId, distrDetailId,
                                                 distrBillId, goods_gg, goods_code, goods_name, erpCode, create_user,
                                                 create_time, last_modified, last_modified_user)
    elif type == 20:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr_detail2_0929', distr_detail_main_key, distr_main_key, source_id,
                                                 source_detail_id, pur_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, goods_name, goods_gg,
                                                 mfrs_id, expdt_date, product_date
                                                 , pkg_def_id, supply_id, sub_prov_id, sub_prov_name, planBillId,
                                                  planDetailId, purDetailId, purBillId, distrDetailId,
                                                 distrBillId, goods_gg, goods_code, goods_name, erpCode, create_user,
                                                 create_time, last_modified, last_modified_user)
    elif type == 66:
        expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                                 'hdi_distr_detail3_0929', distr_detail_main_key, distr_main_key, source_id,
                                                 source_detail_id, pur_detail_id
                                                 , hos_goods_id, mdm_goods_code, goods_code, goods_name, goods_gg,
                                                 mfrs_id, expdt_date, product_date
                                                 , pkg_def_id, supply_id, sub_prov_id, sub_prov_name, planBillId,
                                                  planDetailId, purDetailId, purBillId, distrDetailId,
                                                 distrBillId, goods_gg, goods_code, goods_name, erpCode, create_user,
                                                 create_time, last_modified, last_modified_user)
    else:
        expected = None

    # 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')


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_a6流程五_中心库入库结算_正结算2_外网配送.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a6流程五_中心库入库结算_正结算2_外网配送.air')
from a_a6流程五_中心库入库结算_正结算2_外网配送 import order_dp



try:
    token, projectCode1, uxid1, corpId1, info1 = call_login(1)

    # order_dp(1, token, projectCode1, uxid1)  # 1低值
    # order_dp(2, token, projectCode1, uxid1)  # 2高值
    # order_dp(3, token, projectCode1, uxid1)  # 3试剂

    list_data = ['16', '20', '66']
    for i in list_data:
        time.sleep(6)
        check_hdi_distr(int(i))  # 检查配送单（外网）
except Exception as e:
    print('订单配送发生错误,请检查脚本或者数据')
    print("发生异常：", str(e))
    traceback.print_exc()
else:
    commonFuc().check_text_exist_result_text('succees','succees')