# -*- encoding=utf8 -*-
from common.fileUtls import FileUtils

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a2_流程二供货关系申请提交审批,id2303,id2303-2,sit,on
主数据平台:a_a2_流程二供货关系申请提交审批 
python runner_test.py tag id2303-2 debug mdm3
"""

from common.common_func import commonFuc
from air_case.mdm_login.登录外网系统.登录外网系统 import login_system
from airtest.core.api import using
import sys
module = "b6_spd3_core_business_flow_database_check"
def h_login():

    #供货关系申请_客户类型_医院_登录_医院登录获取医院id
    module = "a4_mdm3_cm"
    #如果需要h0347医院账号登录,这里就从data文件获取登录医院的用户名
    #username = commonFuc().get_business_data(module, "username")


    username =  commonFuc().get_business_data(module, "username")
    password = commonFuc().get_business_data(module, "password1")
    print('医院名称', username)
    print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    import time
    while True:
        try:
            token,projectCode,uxid,corpId,info= login_system(username, password).get_token()
        except:
            time.sleep(6)
            continue
        else:
            return token,projectCode,uxid,corpId,username
def e_login(type=1):
    module = "a4_mdm3_cm"
    username='username'
    if type==1:#注册一个新企业，并审批
        pro_path=commonFuc().get_pro_path()
        sys.path.append(pro_path+r'\\air_case\\a2_mdm3_ea\\企业审核.air')
        using(pro_path+r'\\air_case\\a2_mdm3_ea\\企业审核.air')
        from 企业审核 import test1

        info = FileUtils().r_info("a2_mdm3_ea", "企业信息")
        print(info)
        username = info['cname']
        print('aaaaaaaaaaaaaaaaaaaaaaaaaaa',username)
    else:#读取文件中存入的企业信息
        info = FileUtils().r_info("a2_mdm3_ea", "企业信息")
        print(info)
        username = info['cname']
        print('aaaaaaaaaaaaaaaaaaaaaaaaaaa', username)
    #a2供货关系申请_客户类型_医院_登录_企业登录
    # print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    print(username, password)
    # 获取token和projectCode
    import time
    i=1
    while True:
        if i==1000:
            print('6000秒过去了，新注册的企业还未登录,检查同步是否正常')
            break
        try:
            token,projectCode,uxid,corpId,info= login_system(username, password).get_token()
        except:
            print('新注册的企业登录失败,等待6秒')
            time.sleep(6)
            i=i+1
            continue
        else:
            return token, projectCode, uxid, corpId, username
            # break
def supply_and_marketing_apply():

    token, projectCode, uxid, corpId, username = e_login()
    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a4_mdm3_cm"
    # a3供货关系申请_客户类型_医院_供销关系申请提交
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "submitSupplyRelation_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    contactUser = commonFuc().randomString(5)
    contactWay = contactUser
    request_body = commonFuc().get_business_data(module, "payload", h_corpId, contactUser, contactWay)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # #获取接口返回数据
    #
    check_dict = commonFuc().get_business_data(module, "checkDict")
    commonFuc().check_result(check_dict, result)
    e_corpId = corpId
    e_username = username
    return e_corpId, h_corpId, e_username, h_name

def submitSupplyRelation_pass():
    module = "a4_mdm3_cm"
    token, projectCode, uxid, corpId, h_name = h_login()
    print(token, projectCode, uxid, corpId, h_name)
    pro_path = commonFuc().get_pro_path()
    #查询待审核数据=========================================
    # a11供货关系审核_驳回_客户类型_医院_供销关系审核_查询待审核数据1
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "listVoPage_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)

    request_body = commonFuc().get_business_data(module, "payload6", corpId)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    id1 = commonFuc().analysis_json('id', commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    print('id1', id1)
    # 查询待审核数据======================================
    #b15供货关系审核_通过_客户类型_医院
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "auditSupplyRelation_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    contactUser=commonFuc().randomString(5)
    contactWay=contactUser
    request_body = commonFuc().get_business_data(module, "payload7_2", id1,contactUser,contactWay)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    #断言
    checkDict=commonFuc().get_business_data(module,'checkDict')
    commonFuc().check_result(checkDict,result)
def supply_and_marketing_apply2():
    pro_path=commonFuc().get_pro_path()

    token,projectCode,uxid,corpId,username=e_login(2)
    #获取医院corpId

    h_token,h_projectCode,h_uxid,h_corpId,h_name=h_login()

    module = "a4_mdm3_cm"
    #a3供货关系申请_客户类型_医院_供销关系申请提交
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "submitSupplyRelation_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    contactUser=commonFuc().randomString(5)
    contactWay=contactUser
    request_body = commonFuc().get_business_data(module, "payload_2",h_corpId,contactUser,contactWay)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result',result)
    # #获取接口返回数据
    #
    check_dict= commonFuc().get_business_data(module, "checkDict")
    commonFuc().check_result(check_dict,result)
    e_corpId=corpId
    e_username=username
    return e_corpId,h_corpId,e_username,h_name
try:
    h_login()#医院登录
    e_login(1)#企业登录
    #供应关系申请-客户类型医院
    FileUtils().w_info1(supply_and_marketing_apply(), module, '供货关系申请')
    submitSupplyRelation_pass()
    #供应关系申请-客户类型厂商
    FileUtils().w_info1(supply_and_marketing_apply2(), module, '供货关系申请2')
    submitSupplyRelation_pass()
    commonFuc().check_text_exist_result_text('succees','succees')
except:
    print('a_a2_流程二供货关系申请提交审批,出现异常,请检查')