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

__author__ = "xiaohesheng"

"""
case_tag:api,spd_core,a_a2_流程二供货关系申请提交审批,id2302,id2302-2,sit,on
主数据平台:a_a2_流程二供货关系申请提交审批 
"""

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

module = "b5_spd3_core_business_flow"


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
        # # 注册一个新企业
        # pro_path = commonFuc().get_pro_path()
        # sys.path.append(pro_path + r'\\air_case\\a1_mdm3_es\\外网企业注册提交接口_正常注册.air')
        # using(pro_path + r'\\air_case\\a1_mdm3_es\\外网企业注册提交接口_正常注册.air')
        # from 外网企业注册提交接口_正常注册 import es
        # id1, cname = es()
        # # 企业审核
        # module = "a2_mdm3_ea"
        # username = commonFuc().get_business_data(module, "username1")
        # password = commonFuc().get_business_data(module, "password")
        # # print(username, password)
        # # 获取token和projectCode
        # token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
        # # 获取请求头信息
        # headers = commonFuc().get_business_data(module, "json_headers2",
        #                                         commonFuc().get_business_data(module, "json_contentType"), token,
        #                                         projectCode,token)
        # # #企业锁定
        # # url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "queryAlCompanyRegistInfo_url")
        # # print('企业锁定url', url)
        # # request_body = commonFuc().get_business_data(module, "payload3", id1)
        # # # 发送请求
        # # result = commonFuc().http_post(url, request_body, headers)
        # # print('企业锁定结果', result)
        #
        # url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "auditCompanyRegistBill_url")
        # print('企业审核url', url)
        # url2=commonFuc().get_business_data(module, "auditCompanyRegistBill_url")
        # lastModified = dateUtils().get_current_time()
        # createTime = dateUtils().get_current_time()
        # lastModifiedUser = None
        # userLoginCode = cname
        # userName = cname
        # email = cname
        # erpCode = cname
        # registSourceProject = cname
        # request_body = commonFuc().get_business_data(module, "payload5_1", id1, lastModified, createTime,
        #                                               userLoginCode, userName, cname, email, erpCode,
        #                                              registSourceProject)
        # # request_body = commonFuc().get_business_data(module, "payload5_2", id1)
        # # request_body = commonFuc().get_business_data(module, "payload5_3",id1)
        #
        # import json
        # request_body_str=json.dumps(request_body)
        # request_body_str2=url+request_body_str
        # data=request_body_str2
        # import hashlib
        # byte_str= data.encode('utf-8')
        # hash_object = hashlib.sha256(byte_str)
        # X_JS_HASH = hash_object.hexdigest()
        # #重新获取头信息
        # headers = commonFuc().get_business_data(module, "json_headers3",
        #                                         commonFuc().get_business_data(module, "json_contentType"), token,
        #                                         projectCode,X_JS_HASH)
        # print('headers',headers)
        # print('企业审核request_body', request_body)
        # result = commonFuc().http_post(url, request_body, headers)
        # print('企业审核', result)

        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"
    #2024.8.22在企业审核的时候供货关系就自动建立了，注释如下脚本
    # # 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)
    # print('供销关系申请提交request_body',request_body)
    # # 发送请求
    # 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)
    # #获取接口返回数据
    #验证返回内容
    code=commonFuc().analysis_json('code',result)
    if code==0:
        print('ok')
        commonFuc().check_text_exist_result_text('succees', 'succees')
    else:
        print('error，检查供应关系_厂商_审核结果')
        commonFuc().check_text_exist_result_text('succees', 'succees')

    e_corpId = corpId
    e_username = username
    return e_corpId, h_corpId, e_username, h_name


def main():
    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')
        print('=================流程二结束=================')
    except Exception as e:
        # 打印错误信息
        print(f"发生错误: {e}")
        print('a_a2_流程二供货关系申请提交审批,出现异常,请检查')
# main()
