# -*- encoding=utf8 -*-
from common.db.sql.sql_del_branch_info import delData
from common.fileUtls import FileUtils
from air_case.public1.public1.public1 import *

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a3_流程三_产品导入和审批,id2303,id2303-3,sit,on
主数据平台:a_a3_流程三_产品导入和审批
python runner_test.py tag id2303-3 debug mdm3
"""

from common.common_func import commonFuc
from air_case.mdm_login.登录外网系统.登录外网系统 import login_system
from airtest.core.api import using
import sys


def check_mcms_goods_info(type=1):
    print('检查审批前的产品信息')
    try:
        if type == 1:
            hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
        elif type == 2:
            hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
        elif type == 3:
            hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
        elif type == 4:
            hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message4')["hosGoodsId"]
        elif type == 5:
            hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message5')["hosGoodsId"]
        else:
            hos_goods_id = 'hos_goods_id'
        if hos_goods_id==None:
            print('产品信息还未写入文件，请检查脚本')
            sys.exit(0)
        print('产品id是',hos_goods_id)
    except Exception as e:
        print("发生异常：", str(e))
        hos_goods_id = 'hos_goods_id'
        sys.exit(0)
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.mcms_goods_info where id='%s';" % hos_goods_id
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = hos_goods_id
    hos_goods_code = get_id(
        "select hos_goods_code from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    mdm_goods_code = get_id(
        "select mdm_goods_code from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    prov_hos_goods_id= get_id(
        "select prov_hos_goods_id from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    mdm_goods_spec_code=mdm_goods_code
    spd_goods_code=mdm_goods_code
    goods_name=get_id(
        "select goods_name from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    short_pinyin=get_id(
        "select short_pinyin from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    create_time = get_create_time("select create_time from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    # username = commonFuc().get_business_data('b2_herp3_bs', "username")
    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"])
    last_modified_user = create_user
    parent_id = get_id("select parent_id from mcms_stock_shelf where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_goods_info'+str(type), id, hos_goods_code, hos_id, prov_id,
                                             prov_hos_goods_id, mdm_goods_code, mdm_goods_spec_code, spd_goods_code,
                                             goods_name, short_pinyin
                                             , create_user, create_time, last_modified_user, last_modified)

    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_goods_info_after_approval(type=1):
    print('检查审批后的产品信息')
    if type == 1:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    elif type == 2:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    elif type == 3:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]
    elif type == 4:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message4')["hosGoodsId"]
    elif type == 5:
        hos_goods_id = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message5')["hosGoodsId"]
    else:
        hos_goods_id = 'hos_goods_id'
    branch_id = get_branch_id()
    sql1 = "select * from spd_ybtest_hdi.mcms_goods_info where id='%s';" % hos_goods_id
    actual = check_mcms_pur_sql(sql1)
    print('actual', actual)
    id = hos_goods_id
    hos_goods_code = get_id(
        "select hos_goods_code from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    hos_id = get_hosid()
    prov_id = get_prov_id()
    mdm_goods_code = get_id(
        "select mdm_goods_code from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    prov_hos_goods_id= get_id(
        "select prov_hos_goods_id from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    mdm_goods_spec_code=mdm_goods_code
    spd_goods_code=mdm_goods_code
    goods_name=get_id(
        "select goods_name from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    short_pinyin=get_id(
        "select short_pinyin from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    create_time = get_create_time("select create_time from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    last_modified = get_create_time("select last_modified from spd_ybtest_hdi.mcms_goods_info where id='%s';" % id)
    # username = commonFuc().get_business_data('b2_herp3_bs', "username")
    create_user = get_login_user_uxid_bydb(
        FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"])
    last_modified_user = create_user
    parent_id = get_id("select parent_id from mcms_stock_shelf where id='%s';" % id)
    expected = commonFuc().get_business_data('b6_spd3_core_business_flow_database_check',
                                             'check_mcms_goods_info_after_approval'+str(type), id, hos_goods_code, hos_id, prov_id,
                                             prov_hos_goods_id, mdm_goods_code, mdm_goods_spec_code, spd_goods_code,
                                             goods_name, short_pinyin
                                             , create_user, create_time, last_modified_user, last_modified)

    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_a3_流程三_产品导入和审批.air')
using(pro_path + r'//air_case/b5_spd3_core_business_flow/a_a3_流程三_产品导入和审批.air')
from a_a3_流程三_产品导入和审批 import add_goods, select1, apply, add_goods1, add_goods2, add_goods3, add_goods4, \
    supplyProvHosGoods_add_DI

try:
    # ==============分割线 暂时注释===============
    add_goods()  # 直销高值
    select1('message1')
    time.sleep(6)
    # check_mcms_goods_info(1)#审核前的产品信息
    apply()#审批产品
    time.sleep(6)
    # check_mcms_goods_info_after_approval(1) #审核后的产品信息
    add_goods1()  # 直销低值
    select1('message2')
    time.sleep(6)
    # check_mcms_goods_info(2)  # 审核前的产品信息
    apply()
    time.sleep(6)
    # check_mcms_goods_info_after_approval(2) #审核后的产品信息
    add_goods2()  # 直销试剂
    select1('message3')
    time.sleep(6)
    # check_mcms_goods_info(3)  # 审核前的产品信息
    apply()
    time.sleep(6)
    # check_mcms_goods_info_after_approval(3)  # 审核后的产品信息
    add_goods3()  # 集配
    select1('message4')
    apply()
    add_goods4()  # 定向 "isSubprov": "1"
    select1('message5')
    apply()
    # 20240618外网在配送的时候增加了DI验证，加入维护di的脚本
    supplyProvHosGoods_add_DI()
    # 202410.18因脚本运行时到检查产品数据库数据时，未知原因脚本出现错乱问题
    #修改为检查审批后的产品信息
    # 审核后的产品信息
    check_mcms_goods_info_after_approval(1)
    check_mcms_goods_info_after_approval(2)
    check_mcms_goods_info_after_approval(3)
    # ==============分割线 暂时注释===============
    commonFuc().check_text_exist_result_text('succees', 'succees')
except  Exception as e:
    print("发生异常：", str(e))
    print('新增产品信息出现异常,后续的脚本必然无法走通,需要检查程序或者脚本或者环境是否出现问题')
    commonFuc().check_text_exist_result_text('产品新增失败', '产品新增成功')
    sys.exit(0)
else:
    # 删除今天之前的产品信息(包含内外网)
    # delData().Del_goods_info_all()
    commonFuc().check_text_exist_result_text('succees', 'succees')
    print('=================流程三结束=================')
