# -*- encoding=utf8 -*-
import json
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_a8流程五_中心库入库结算_正结算3_外网开票,id2302,id2302-8,sit,on
主数据平台:a_a8流程五_中心库入库结算_正结算3_外网开票
python runner_test.py tag id2302-8 debug mdm3
"""

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

module = "b6_spd3_core_business_flow_database_check"


def check_mcms_invoice():
    print('检查发票数据')
    prov_id = get_prov_id()
    # 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"]

    sql = "select * from  mcms_invoice where prov_id='%s'  order by   CREATE_TIME desc LIMIT 1;" % (
        prov_id)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_invoice where prov_id='%s'  order by   CREATE_TIME desc LIMIT 1;" % (
        prov_id)
    id = get_id(sql)
    hos_id = get_hosid()
    prov_name = get_prov_name()

    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8("b5_spd3_core_business_flow", "供货关系申请2", 'message')['e_username'])
    # create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_invoice where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_invoice where id='%s';" % id)
    last_modified_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])

    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    year_month = str(year) + str(month)
    settle_months = year_month
    invoice_date = get_account_date1("select invoice_date from mcms_invoice where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_invoice_1009', id,
                                             hos_id, prov_id, prov_name, invoice_date, settle_months, 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_invoice_detail(1, id)
    # check_mcms_invoice_detail2(1, id)


def check_mcms_invoice_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_invoice_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_invoice_detail where pid='%s' order by id LIMIT 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')

    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8("b5_spd3_core_business_flow", "供货关系申请2", 'message')['e_username'])
    create_time = get_create_time("select create_time from  mcms_invoice_detail where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_invoice_detail where id='%s';" % id)
    last_modified_user = create_user

    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    settle_month = str(year) + str(month)
    hobd_id = get_id("select id from mcms_out_balance_detail  where prov_id='%s' order by id desc LIMIT 1 OFFSET 1;" % (
        prov_id))
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_invoice_detail_1009_1', id,
                                             pid, hobd_id, branch_id, hos_goods_id, settle_month, 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_invoice_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_invoice_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_invoice_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')

    account_user_id = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8("b5_spd3_core_business_flow", "供货关系申请2", 'message')['e_username'])
    create_time = get_create_time("select create_time from  mcms_invoice_detail where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_invoice_detail where id='%s';" % id)
    last_modified_user = create_user

    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    settle_month = str(year) + str(month)
    hobd_id = get_id("select id from mcms_out_balance_detail  where prov_id='%s' order by id desc LIMIT 1;" % (
        prov_id))
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_invoice_detail_0919_2', id,
                                             pid, hobd_id, branch_id, hos_goods_id, settle_month, 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_invoice_ext():
    print('检查付款单数据')
    prov_id = get_prov_id()
    # 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"]

    sql = "select * from  mcms_invoice_ext where prov_id='%s'  order by   CREATE_TIME desc LIMIT 1;" % (
        prov_id)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_invoice_ext where prov_id='%s'  order by   CREATE_TIME desc LIMIT 1;" % (
        prov_id)
    id = get_id(sql)
    hos_id = get_hosid()
    prov_name = get_prov_name()

    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8("b5_spd3_core_business_flow", "供货关系申请2", 'message')['e_username'])
    # create_user = account_user_id
    create_time = get_create_time("select create_time from  mcms_invoice_ext where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_invoice_ext where id='%s';" % id)
    last_modified_user = get_login_user_uxid_bydb(FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')['username1'])
    create_user = last_modified_user
    year, month = dateUtils().get_year_month(dateUtils().get_current_time())
    if month < 10:
        month = "0" + str(month)
    year_month = str(year) + str(month)
    settle_months = year_month
    pay_date = get_account_date1("select pay_date from mcms_invoice_ext where id='%s';" % id)
    pay_user = create_user
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_invoice_ext_1009', id,
                                             hos_id, prov_id, prov_name, pay_date, create_user,
                                             create_time, last_modified, last_modified_user, pay_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_invoice_ext_detail(1, id)


def check_mcms_invoice_ext_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_invoice_ext_detail  where pid='%s';" % (
        pid)
    # 获取实际值
    actual = check_mcms_pur_plan_sql(sql)
    print('actual', actual)
    sql = "select id from  mcms_invoice_ext_detail where pid='%s';" % (
        pid)
    id = get_id(sql)
    hos_id = get_hosid()
    prov_id = get_prov_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_invoice_ext_detail where id='%s';" % id)
    last_modified = get_create_time("select last_modified from  mcms_invoice_ext_detail where id='%s';" % id)
    last_modified_user = create_user

    invoice_id = get_id("select id from   mcms_invoice where prov_id='%s'  order by   CREATE_TIME desc LIMIT 1;" % (
        prov_id))
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_invoice_ext_detail_0919_1', id,
                                             pid, invoice_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')



try:
    OBH_ID = settle()
    # 调用审批结算单
    approval_center(OBH_ID)
    from air_case.public1.public1.public1 import *
    invoice(2,OBH_ID)
    time.sleep(6)
    # 检查发票数据
    check_mcms_invoice()
    time.sleep(3)
    # 检查付款单数据
    check_mcms_invoice_ext()
except Exception as e:
    print('脚本出现异常，请检查脚本或者数据')
    print("发生异常：", str(e))
    traceback.print_exc()
else:
    commonFuc().check_text_exist_result_text('succees', 'succees')
