import sys

from airtest.core.helper import using

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from common.common_func import commonFuc
from common.fileUtls import FileUtils
import json
import time
from common.timeUtils import timeUtils

# 添加[]
from common.timeUtils import timeUtils


def get_process_list(request_body):
    request_body = "[" + str(request_body) + "]"
    print('request_body', request_body)
    new_request_body = request_body.replace("'", '"')
    new_request_body1 = new_request_body.replace("\\", '\\\\')
    new_request_body2 = new_request_body1.replace("None", 'null')
    print(new_request_body2)
    request_body_json = json.loads(new_request_body2, encoding='utf-8')
    return request_body_json


def get_branch_id():
    return FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]


def get_hosid():
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hosId = branch_id[0:5]
    return hosId


def approval_center(OBH_ID='OBhxxxxxxxxxxxxxxx'):
    module = "b2_herp3_bs"
    # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    print(info)
    username1 = info['username1']

    username = username1
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 登录获取用户id等信息，使用创建的用户登录============结束
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "apprCenter_url")

    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload50_6_1_2", OBH_ID)
    print(request_body)
    # # # 发送请求


    result = commonFuc().http_post(url, request_body, headers)

    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', data)
    if total == 0:
        request_body = commonFuc().get_business_data(module, "payload50_6")
    result = commonFuc().http_post(url, request_body, headers)

    # 断言
    # checkDict=commonFuc().get_business_data(module,'checkDict10')
    # commonFuc().check_result(checkDict,result)
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', result)
    data1 = commonFuc().analysis_json('data', data)

    commonFuc().check_text_exist(0, result)
    taskId = commonFuc().analysis_json('taskId', data1[0])

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "approvalBatch_url")

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload50_7", taskId)
    print(request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    # 断言
    # checkDict=commonFuc().get_business_data(module,'checkDict10')
    # commonFuc().check_result(checkDict,result)
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', result)
    if code >= 0:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)

def approval_center1():
    module = "b2_herp3_bs"
    # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    print(info)
    username1 = info['username1']

    username = username1
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 登录获取用户id等信息，使用创建的用户登录============结束
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "apprCenter_url")

    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload50_6")
    print(request_body)
    # # # 发送请求


    result = commonFuc().http_post(url, request_body, headers)

    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)

    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', result)
    data1 = commonFuc().analysis_json('data', data)

    commonFuc().check_text_exist(0, result)
    taskId = commonFuc().analysis_json('taskId', data1[0])

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "approvalBatch_url")

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload50_7", taskId)
    print(request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    # 断言
    # checkDict=commonFuc().get_business_data(module,'checkDict10')
    # commonFuc().check_result(checkDict,result)
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', result)
    if code >= 0:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)

def login(username, password, type=1):
    print('username,password', username, password)
    # 外网登录
    if type == 1:
        from air_case.mdm_login.登录外网系统.登录外网系统 import login_system
        # # 获取token和projectCode
        token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 内网登录
    elif type == 2:
        from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
        token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    else:
        from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
        token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    return token, projectCode, uxid, corpId, info


def settle():  # 结算相关
    module = "b5_spd3_core_business_flow"
    # # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    # print(info)
    username = info['username1']
    password = commonFuc().get_business_data("b2_herp3_bs", "password")

    print('username,password', username, password)
    # # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 登录获取用户id等信息，使用创建的用户登录============结束
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    # ================生成结算单列表查询===================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "mcmsOutUnBalance_listVoPage_url")
    print(url)
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hosId = branch_id[0:5]
    provId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    request_body = commonFuc().get_business_data(module, "payload7", provId, hosId, hosId)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    # 取结算单id
    list_a = []
    for i in data1:
        id = commonFuc().analysis_json('id', i)
        list_a.append(id)
    print(list_a)
    print(list_a[0], list_a[1], type(list_a[0]))
    # ===================生成结算单列表查询======================
    # ================== 生成结算单==============================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsOutUnBalance_genBalanceBill_url")
    print(url)
    request_body = commonFuc().get_business_data(module, "payload8", hosId, list_a[0], list_a[1], hosId)
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # ================== 生成结算单==============================
    # ===================结算单查询返回结算单号====================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_mcmsOutBalance_listVoPage_url")
    print(url)
    request_body = commonFuc().get_business_data(module, "payload22", hosId, hosId)
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    # print('result', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    print('data1', data1[0])
    return commonFuc().analysis_json('id', data1[0])
    # ==================结算单查询返回结算单号====================


def get_stockId(name, type=1):  # 获取仓库id参数是库房名称
    module = "b2_herp3_bs"
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login(username, password, 2)
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "basic_stockInfo_stockTree_url")
    # url=url+userid
    print(url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload62_1_1_1", get_hosid(), name)
    print('request_body', request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)

    if code == 0 and len(data) >= 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
    if type == 1:
        # 增加一段根据库房名称返回id
        print('data', data)
        for i in data:
            cname = commonFuc().analysis_json('cname', i)
            print(cname)
            if cname == name:
                print(cname, 'ccccccccccccccccccccccccccc')
                id1 = commonFuc().analysis_json('id', i)
                # print(id1)
                return id1
                # break
    elif type == 2:
        children = []
        print('data', data)
        for i in data:
            print('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', i)
            children = i['children']
            break
        for i in children:
            cname = i['cname']
            if cname == name:
                id2 = commonFuc().analysis_json('id', i)
                print(id2)
                return id2
    elif type == 3:
        children = []
        print('data', data)
        children = commonFuc().analysis_json('children', commonFuc().analysis_json('children', data))
        print('children', children)
        for i in children:
            id3 = i['id']
            print(id3)
            return id3


def dept_query_all():  # 科室查询，将科室id写入文件中
    module = "b2_herp3_bs"
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    # print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)
    # b9_科室管理_科室登记_科室查询
    branch_id = FileUtils().r_info(module, '院区新增')["branch_id"]
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "deptPage_url")

    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))
    # #请求体
    request_body = commonFuc().get_business_data(module, "payload13", branch_id)
    print(request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', data)
    if total == 4:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
    # 将deptid写入文件
    data1 = commonFuc().analysis_json('data', data)
    print('data1', data1)
    info = []
    for i in data1:
        deptid = commonFuc().analysis_json('id', i)
        info.append(deptid)
    titlename = ['deptid1', 'deptid2', 'deptid3', 'deptid4']

    FileUtils().w_info8(info, module, '所有科室id', titlename, 'message6')
    info1 = []
    for i in data1:
        ename = commonFuc().analysis_json('ename', i)
        info1.append(ename)
    FileUtils().w_info8(info1, module, '所有科室name', titlename, 'message6')


def get_stockAreaId(dept_id, stockId):  # 获取库区id,参数是部门id和仓库id
    module = "b2_herp3_bs"
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "basic_stockShelf_page_url")
    # url=url+userid
    print(url)


    # 请求体
    request_body = commonFuc().get_business_data(module, "payload62_4", dept_id, stockId)
    print('request_body查询库区id', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    # pageSize = commonFuc().analysis_json('pageSize', data)
    # print(pageSize, type(pageSize))
    if code == 0 and len(data) >= 1:
        commonFuc().check_text_exist(0, result)

    else:
        commonFuc().check_text_exist('error', result)
    # 增加一段返回库区id
    print('data1', data1)
    for i in data1:
        shelfName = commonFuc().analysis_json('shelfName', i)
        print(shelfName)
        if shelfName == '默认库区' or shelfName == '自动化测试':
            import time
            # time.sleep(60)
            print(shelfName, 'ccccccccccccccccccccccccccc')
            id1 = commonFuc().analysis_json('id', i)
            shelfCode = commonFuc().analysis_json('shelfCode', i)
            areaName = commonFuc().analysis_json('shelfName', i)
            print('get_stockAreaIdssssssssssssssssssssssssssssssssss', id1)
            return id1


def set_dept_user(dept_id, deptName, type=1):
    module = "b2_herp3_bs"
    stockId = 'stockId'
    # 获取库房id=========     开始====
    if type == 1:
        # 中心库房名称
        branch_name = FileUtils().r_info(module, '院区新增')["branch_name"]
        stockId = get_stockId(branch_name + '中心库房')
    # 二级库房名称
    elif type == 2:
        stockId = get_stockId(deptName + '库房', 2)
        print('stockId',stockId)
    elif type == 3:
        stockId = get_stockId(deptName + '库房', 3)
        print(stockId)

    # 获取库房id==========结束============

    # ====获取库区id===== 开始==========

    stockAreaId = get_stockAreaId(dept_id, stockId)
    print(stockAreaId)
    # ====获取库区id===== 结束==========
    # dept_query_all()

    # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8(module, "用户名信息", 'message3')
    print(info)
    username1 = info['username1']

    username = username1
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token1, projectCode1, uxid1, corpId1, info1 = login_system(username, password).get_token()
    # 登录获取用户id等信息，使用创建的用户登录============结束
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    print( token,projectCode,uxid,corpId,info)
    # 查询库区是否已经存在人员，不需要重复添加========
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "basic_stockUser_page_url")
    request_body = commonFuc().get_business_data(module, "payload65_3", dept_id, stockAreaId, get_hosid())
    print('查询库区是否已经存在人员', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('查询库区是否已经存在人员',result)
    userCode = commonFuc().analysis_json('userCode',commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    # print('userCode',userCode)
    if userCode !=username1:
        # 查询库区是否已经存在人员，不需要重复添加===============

        # r_d2库房管理_库区设置_人员管理_添加人员
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "basic_stockUser_insert_url")
        print('url库房管理_库区设置_人员管理_添加人员', url)
        # 获取请求头信息
        headers = commonFuc().get_business_data(module, "json_headers2",
                                                commonFuc().get_business_data(module, "json_contentType"), token,
                                                commonFuc().get_business_data(module, "X-APP-CODE"))

        # # 请求体
        request_body = commonFuc().get_business_data(module, "payload62_9_1_1", dept_id, deptName, get_hosid(), stockAreaId,
                                                     username1, uxid1, username1)
        print('url库房管理_库区设置_人员管理_添加人员request_body', request_body)

        request_body_json = get_process_list(request_body)
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body_json, headers)
        print('resultbbbbbbbbbbb', result)
        # 断言
        code = commonFuc().analysis_json('code', result)
        data = commonFuc().analysis_json('data', result)
        data1 = commonFuc().analysis_json('data', data)
        # print(type(data1))
        # pageSize = commonFuc().analysis_json('pageSize', data)
        # print(pageSize, type(pageSize))
        if code == 0 and data == 1:
            commonFuc().check_text_exist(0, result)
        else:
            commonFuc().check_text_exist('error', result)
    else:
        print('库区已经存在人员，无需重复添加')

def call_login(type=1):  # type=1外网登录 2内网登录
    module = "b2_herp3_bs"
    info = FileUtils().r_info8(module, "用户名信息", 'message3')
    print(info)
    username1 = info['username1']

    username = username1
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token1, projectCode1, uxid1, corpId1, info1 = login(username, password, 2)
    return token1, projectCode1, uxid1, corpId1, info1


def get_udi(type=1):
    # type=1低值 2高值 3试剂
    module = "b5_spd3_core_business_flow"
    token, projectCode1, uxid1, corpId1, info1 = call_login()
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "mcmsInvoice_barcode_url")
    print('get_udi', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload17", get_branch_id())
    print('request_body', request_body)

    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))

    pkgcode = data1[0]['pkgCodeSee']
    pkgcode1 = pkgcode[0:20]
    pkgcode2 = pkgcode[21:len(pkgcode)]
    print(pkgcode1, pkgcode2)
    # 013530000003170010333\x1D112404261724110221UT240426000001802
    print('aaaaaaaaaaaaaaaaa', data1[0]['pkgCodeSee'])
    # 扫条码
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_pick_return_scan_url")
    print('herpService_pick_return_scan_url', url)
    branch_name = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_name"]
    stockId = get_stockId(branch_name + '中心库房')
    areaCode = FileUtils().r_info8("b2_herp3_bs", "shelfinfo", 'message7')['shelfCode']
    request_body = commonFuc().get_business_data(module, "payload18", areaCode, pkgcode, stockId)
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # # 将条码信息写入文件
    data = commonFuc().analysis_json('data', result)
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data)
    list_b.append(data)
    list_c.append(data)
    # print('list_a', len(list_a), list_a)
    info = (list_a, list_b, list_c)
    titlename = ('list_a', 'list_b', 'list_c')
    # 将条码信息写入文件
    FileUtils().w_info8(info, module, '条码信息', titlename, 'message18')


def invoice(type=1, OBh_ID='OBhxxxxxxxxxxxxxxx'):
    # # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "供货关系申请2", 'message')
    # print(info)
    username = info['e_username']
    password = commonFuc().get_business_data("b2_herp3_bs", "password1")
    token, projectCode, uxid, corpId, info = login(username, password)
    # print(token, projectCode, uxid, corpId, info)
    # 登录获取用户id等信息，使用创建的用户登录============结束
    module = "a6_mdm3_si"
    # 发票录入列表查询=================================================
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "listGoodsPage_url")
    print('发票录入列表查询url', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    if type == 1:
        request_body = commonFuc().get_business_data(module, "payload2_1_1", get_hosid(), get_branch_id())
    elif type == 2:
        request_body = commonFuc().get_business_data(module, "payload2_1_2", get_hosid(), get_branch_id(), OBh_ID)
    else:
        request_body = commonFuc().get_business_data(module, "payload2_1_2", get_hosid(), get_branch_id(), OBh_ID)

    print('request_body_发票录入列表查询', request_body)
    # 发送请求
    i = 1
    while True:
        time.sleep(6)
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        try:
            data = commonFuc().analysis_json('data', result)
            total = commonFuc().analysis_json('total', data)
            if total == 0:
                print('数据暂时未同步过来，或者内网没有生成结算单')
                i = i + 1
                if i >= 100:
                    print('600秒过去了，数据还未查询出来，调用一下内网结算函数')
                    settle()
                    approval_center()
                continue
            else:
                break
        except:
            continue

    # 将发票信息写入文件
    data = commonFuc().analysis_json('data', result)
    totalInfo = commonFuc().analysis_json('totalInfo', result)
    print('totalInfo', totalInfo)
    data1 = commonFuc().analysis_json('data', data)
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data1[0])
    list_b.append(data1[0])
    list_c.append(data1[0])
    # print('list_a', len(list_a), list_a)
    info = (list_a, list_b, list_c)
    titlename = ('list_a', 'list_b', 'list_c')
    # 将发票信息写入文件
    FileUtils().w_info8(info, module, '结算开票信息', titlename, 'message')
    # 发票录入列表查询=================================================
    # ===========发票暂存=======================================
    if type == 2:
        leftSettleQty, maxSettleAmount = SaveCacheBatch(headers)
    elif type == 3:  # 需要暂存2条结算数据
        SaveCacheBatch2(headers)
        leftSettleQty = commonFuc().analysis_json('totalQty', totalInfo)
        maxSettleAmount = commonFuc().analysis_json('totalAmount', totalInfo)
    else:
        leftSettleQty, maxSettleAmount = SaveCacheBatch(headers)
    # url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "saveCacheBatch_url")
    # print(url)
    # # 从文件中获取必要信息
    # info = FileUtils().r_info8(module, "结算开票信息", 'message')
    # # print('info', info)
    # list_a = info['list_a']
    # # print('list_a', list_a,type(list_a),len(list_a))
    # pid='pid'
    # settleMonth = "settleMonth"
    # hosGoodsId = 'hosGoodsId'
    # batchCode = 'batchCode'
    # distrDetailId = 'distrDetailId'
    # distrId='distrId'
    # distrOrderNo='distrOrderNo';settlePrice='settlePrice';maxSettleQty='maxSettleQty'
    # maxSettleAmount='maxSettleAmount';curSettleQty='curSettleQty';curSettleAmount='curSettleAmount'
    # leftSettleQty='leftSettleQty';leftSettleAmount='leftSettleAmount';hosId='hosId';provId='provId'
    # branchId='branchId'; balanceDetId='balanceDetId'; purMode='purMode';barCode='barCode';viewId='viewId'
    # for i in list_a:
    #     pid = i['pid']
    #     settleMonth = i['settleMonth']
    #     hosGoodsId = i['hosGoodsId']
    #     detailList = i['detailList']
    #     for j in detailList:
    #         batchCode = j['batchCode']
    #         distrDetailId=j['distrDetailId']
    #         distrId = j['distrId']
    #         distrOrderNo = j['distrOrderNo']
    #         settlePrice = j['settlePrice']
    #         maxSettleQty = j['maxSettleQty']
    #         maxSettleAmount = j['maxSettleAmount']
    #         curSettleQty = j['curSettleQty']
    #         curSettleAmount = j['curSettleAmount']
    #         leftSettleQty = j['leftSettleQty']
    #         leftSettleAmount = j['leftSettleAmount']
    #         hosId = j['hosId']
    #         provId = j['provId']
    #         branchId = j['branchId']
    #         balanceDetId = j['balanceDetId']
    #         purMode = j['purMode']
    #         barCode = j['barCode']
    #         viewId=j['viewId']
    #         break
    # # list2=[]
    # # detailList=None
    # # for i in list_a:
    # #     detailList = i['detailList']
    # #     break
    # # list2=detailList
    # # for k in list2:
    # #     maxSettleQty = k['leftSettleQty']
    # #     maxSettleAmount = k['settlePrice']
    # #     curSettleQty = k['curSettleQty']
    # #     curSettleAmount = k['curSettleAmount']
    # #     leftSettleQty = k['leftSettleQty']
    #
    # request_body = commonFuc().get_business_data(module, "payload3_1_1_1", pid, settleMonth, hosGoodsId, batchCode,
    #                                              distrDetailId, distrId, distrOrderNo, settlePrice, maxSettleQty,
    #                                              maxSettleAmount, curSettleQty, curSettleAmount, leftSettleQty,leftSettleAmount, hosId,
    #                                              provId,
    #                                              branchId, balanceDetId,purMode, barCode,viewId)
    # print('request_body', request_body)
    # request_body_json=get_process_list(request_body)
    # result = commonFuc().http_post(url, request_body_json, headers)
    # print('result',result)
    # ===========发票暂存===============================================
    # ==============发票提交===========================================
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "submitCache_url")
    print('发票提交', url)
    hosVersion = 3
    hosName = commonFuc().get_business_data(module, 'hosName')
    invoiceQty = leftSettleQty
    invoiceAmount = maxSettleAmount
    invoiceDate = timeUtils().get_time_hms(5)
    request_body = commonFuc().get_business_data(module, "payload3_2_2_2", hosName, invoiceQty, invoiceAmount,
                                                 invoiceDate, get_hosid(), hosVersion)
    print('发票提交request_body', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('发票提交', result)
    time.sleep(3)

    print('result', result)
    # ==============发票提交===========================================
    # ===============发票确认列表查询
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "listVoPage_url1")

    request_body = commonFuc().get_business_data(module, "payload3_3")

    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    id1 = commonFuc().analysis_json('id', data1[0])
    print(id1)
    # ===============发票确认列表查询
    # ===============发票确认
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "invoiceUpdateStatus_url")

    request_body = commonFuc().get_business_data(module, "payload3_4", id1)

    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # ===============发票确认
    # ==============内网发票核对============
    module = "b5_spd3_core_business_flow"
    # # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    username = info['username1']
    password = commonFuc().get_business_data("b2_herp3_bs", "password")
    print('username,password', username, password)
    # # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login(username, password, 2)
    # 登录获取用户id等信息，使用创建的用户登录============结束
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "mcmsInvoice_listVoPage_url")
    print('url', 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, "payload9", timeUtils.get_time_hms(5),
                                                 timeUtils.get_time_hms(5), get_hosid(), get_hosid())
    print('request_body', request_body)
    # 发送请求
    i = 1
    while True:
        time.sleep(6)
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        try:
            data = commonFuc().analysis_json('data', result)
            total = commonFuc().analysis_json('total', data)
            if total == 0:
                print('数据暂时未同步过来')
                i = i + 1
                if i >= 100:
                    print('600秒过去了，数据还未查询出来，检查一下脚本或者被测软件')
                continue
            else:
                break
        except:
            continue

    print('result', '内网发票核对', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    VI_id = commonFuc().analysis_json('id', data1[0])
    print('VI_id', VI_id)
    # ==============发票列表查询
    # ==============发票核对提交
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_invoiceUpdateStatusList_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload10", VI_id, get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # ==============发票核对提交
    # ==============内网发票核对============
    # ==============内网发票提交开始
    # 列表查询
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_listVoPage_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload11", timeUtils.get_time_hms(5),
                                                 timeUtils.get_time_hms(5), get_hosid(), get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '内网发票提交列表查询结果', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    total = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result))
    if total == 0:
        print('检查被测程序，发票提交列表查询数据是否正确')
    else:
        for i in range(total):
            VI_id2 = commonFuc().analysis_json('id', data1[i])
            if VI_id2 == VI_id:
                break
    print('VI_id2', VI_id2)
    # ==============发票提交,下一步归档
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_invoiceUpdateStatusList_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload12", VI_id, get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '发票提交,下一步归档', result)
    # ==============发票提交，下一步归档
    # ==============发票归档列表查询
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_listVoPage_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload13", timeUtils.get_time_hms(5),
                                                 timeUtils.get_time_hms(5), get_hosid(), get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '发票归档列表查询', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    total = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result))
    if total == 0:
        print('检查被测程序，发票归档列表查询查询数据是否正确')
    else:
        for i in range(total):
            VI_id3 = commonFuc().analysis_json('id', data1[i])
            if VI_id3 == VI_id2:
                break
    print('VI_id3', VI_id3)
    # ==============发票归档列表查询
    # ==================发票归档提交
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_invoiceUpdateStatusList_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload14", VI_id, get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '发票归档提交,下一步付款', result)
    # ==================发票归档提交
    # ==================付款单列表查询
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_listVoPage_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload15", get_hosid(), get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '付款单列表查询', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    total = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result))
    if total == 0:
        print('检查被测程序，发票归档列表查询查询数据是否正确')
    else:
        for i in range(total):
            VI_id4 = commonFuc().analysis_json('id', data1[i])
            if VI_id4 == VI_id3:
                break
    print('VI_id4', VI_id4)
    # ==================付款单列表查询
    # ==================付款提交
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "mcmsInvoice_genPaymentBills_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload16", VI_id, get_hosid())
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', '付款提交', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0 and data == 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
    # ==================付款提交
    # ==============内网发票提交
    # ================发票查询列表页面验证

    # ================发票查询列表页面验证


def SaveCacheBatch(headers):
    module = "a6_mdm3_si"
    # 获取请求头信息

    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "saveCacheBatch_url")
    print(url)
    # 从文件中获取必要信息
    info = FileUtils().r_info8(module, "结算开票信息", 'message')
    # print('info', info)
    list_a = info['list_a']
    # print('list_a', list_a,type(list_a),len(list_a))
    pid = 'pid'
    settleMonth = "settleMonth"
    hosGoodsId = 'hosGoodsId'
    batchCode = 'batchCode'
    distrDetailId = 'distrDetailId'
    distrId = 'distrId'
    distrOrderNo = 'distrOrderNo';
    settlePrice = 'settlePrice';
    maxSettleQty = 'maxSettleQty'
    maxSettleAmount = 'maxSettleAmount';
    curSettleQty = 'curSettleQty';
    curSettleAmount = 'curSettleAmount'
    leftSettleQty = 'leftSettleQty';
    leftSettleAmount = 'leftSettleAmount';
    hosId = 'hosId';
    provId = 'provId'
    branchId = 'branchId';
    balanceDetId = 'balanceDetId';
    purMode = 'purMode';
    barCode = 'barCode';
    viewId = 'viewId'
    for i in list_a:
        pid = i['pid']
        settleMonth = i['settleMonth']
        hosGoodsId = i['hosGoodsId']
        detailList = i['detailList']
        for j in detailList:
            batchCode = j['batchCode']
            distrDetailId = j['distrDetailId']
            distrId = j['distrId']
            distrOrderNo = j['distrOrderNo']
            settlePrice = j['settlePrice']
            maxSettleQty = j['maxSettleQty']
            maxSettleAmount = j['maxSettleAmount']
            curSettleQty = j['curSettleQty']
            curSettleAmount = j['curSettleAmount']
            leftSettleQty = j['leftSettleQty']
            leftSettleAmount = j['leftSettleAmount']
            hosId = j['hosId']
            provId = j['provId']
            branchId = j['branchId']
            balanceDetId = j['balanceDetId']
            purMode = j['purMode']
            barCode = j['barCode']
            viewId = j['viewId']
            break
    # list2=[]
    # detailList=None
    # for i in list_a:
    #     detailList = i['detailList']
    #     break
    # list2=detailList
    # for k in list2:
    #     maxSettleQty = k['leftSettleQty']
    #     maxSettleAmount = k['settlePrice']
    #     curSettleQty = k['curSettleQty']
    #     curSettleAmount = k['curSettleAmount']
    #     leftSettleQty = k['leftSettleQty']

    request_body = commonFuc().get_business_data(module, "payload3_1_1_1", pid, settleMonth, hosGoodsId, batchCode,
                                                 distrDetailId, distrId, distrOrderNo, settlePrice, maxSettleQty,
                                                 maxSettleAmount, curSettleQty, curSettleAmount, leftSettleQty,
                                                 leftSettleAmount, hosId,
                                                 provId,
                                                 branchId, balanceDetId, purMode, barCode, viewId)
    print('request_body', request_body)
    request_body_json = get_process_list(request_body)
    result = commonFuc().http_post(url, request_body_json, headers)
    print('result', result)
    return leftSettleQty, maxSettleAmount


def SaveCacheBatch2(headers):
    module = "a6_mdm3_si"
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "saveCacheBatch_url")
    print(url)
    # 从文件中获取必要信息
    info = FileUtils().r_info8(module, "结算开票信息", 'message')
    # print('info', info)
    list_a = info['list_a']
    print('list_a', list_a)
    # print('list_a', list_a,type(list_a),len(list_a))
    pid = 'pid'
    settleMonth = "settleMonth"
    hosGoodsId = 'hosGoodsId'
    batchCode = 'batchCode'
    distrDetailId = 'distrDetailId'
    distrId = 'distrId'
    distrOrderNo = 'distrOrderNo';
    settlePrice = 'settlePrice';
    maxSettleQty = 'maxSettleQty'
    maxSettleAmount = 'maxSettleAmount';
    curSettleQty = 'curSettleQty';
    curSettleAmount = 'curSettleAmount'
    leftSettleQty = 'leftSettleQty';
    leftSettleAmount = 'leftSettleAmount';
    hosId = 'hosId';
    provId = 'provId'
    branchId = 'branchId';
    balanceDetId = 'balanceDetId';
    purMode = 'purMode';
    barCode = 'barCode';
    viewId = 'viewId'
    for i in list_a:
        pid = i['pid']
        settleMonth = i['settleMonth']
        hosGoodsId = i['hosGoodsId']
        detailList = i['detailList']
        for j in detailList:
            batchCode = j['batchCode']
            distrDetailId = j['distrDetailId']
            distrId = j['distrId']
            distrOrderNo = j['distrOrderNo']
            settlePrice = j['settlePrice']
            maxSettleQty = j['maxSettleQty']
            maxSettleAmount = j['maxSettleAmount']
            curSettleQty = j['curSettleQty']
            curSettleAmount = j['curSettleAmount']
            leftSettleQty = j['leftSettleQty']
            leftSettleAmount = j['leftSettleAmount']
            hosId = j['hosId']
            provId = j['provId']
            branchId = j['branchId']
            balanceDetId = j['balanceDetId']
            purMode = j['purMode']
            barCode = j['barCode']
            viewId = j['viewId']
            break

    # for i in list_a:
    #     pid2 = i['pid']
    #     settleMonth2 = i['settleMonth']
    #     hosGoodsId2 = i['hosGoodsId']

    detailList2 = commonFuc().analysis_json('detailList', list_a)[1]
    print('detailList2', detailList2)
    pid2 = commonFuc().analysis_json('pid', detailList2)
    settleMonth2 = commonFuc().analysis_json('settleMonth', detailList2)
    hosGoodsId2 = commonFuc().analysis_json('hosGoodsId', detailList2)
    batchCode2 = commonFuc().analysis_json('batchCode', detailList2)
    distrDetailId2 = commonFuc().analysis_json('distrDetailId', detailList2)
    distrId2 = commonFuc().analysis_json('distrId', detailList2)
    distrOrderNo2 = commonFuc().analysis_json('distrOrderNo', detailList2)
    settlePrice2 = commonFuc().analysis_json('settlePrice', detailList2)
    maxSettleQty2 = commonFuc().analysis_json('maxSettleQty', detailList2)
    maxSettleAmount2 = commonFuc().analysis_json('maxSettleAmount', detailList2)
    curSettleQty2 = commonFuc().analysis_json('curSettleQty', detailList2)
    curSettleAmount2 = commonFuc().analysis_json('curSettleAmount', detailList2)
    leftSettleQty2 = commonFuc().analysis_json('leftSettleQty', detailList2)
    leftSettleAmount2 = commonFuc().analysis_json('leftSettleAmount', detailList2)
    balanceDetId2 = commonFuc().analysis_json('balanceDetId', detailList2)
    purMode2 = commonFuc().analysis_json('purMode', detailList2)
    barCode2 = commonFuc().analysis_json('barCode', detailList2)
    viewId2 = commonFuc().analysis_json('viewId', detailList2)
    provId2 = commonFuc().analysis_json('provId', detailList2)
    branchId2 = commonFuc().analysis_json('branchId', detailList2)
    # hosId2 = j['hosId']
    # provId2 = j['provId']
    # branchId2 = j['branchId']
    # balanceDetId2 = j['balanceDetId']
    # purMode2 = j['purMode']
    # barCode2 = j['barCode']
    # viewId2 = j['viewId']
    # for j in detailList2:
    #     batchCode2 = j['batchCode']
    #     distrDetailId2 = j['distrDetailId']
    #     distrId2 = j['distrId']
    #     distrOrderNo2 = j['distrOrderNo']
    #     settlePrice2 = j['settlePrice']
    #     maxSettleQty2 = j['maxSettleQty']
    #     maxSettleAmount2 = j['maxSettleAmount']
    #     curSettleQty2 = j['curSettleQty']
    #     curSettleAmount2 = j['curSettleAmount']
    #     leftSettleQty2 = j['leftSettleQty']
    #     leftSettleAmount2 = j['leftSettleAmount']
    #     hosId2 = j['hosId']
    #     provId2 = j['provId']
    #     branchId2 = j['branchId']
    #     balanceDetId2 = j['balanceDetId']
    #     purMode2 = j['purMode']
    #     barCode2 = j['barCode']
    #     viewId2 = j['viewId']
    #     break
    # list2=[]
    # detailList=None
    # for i in list_a:
    #     detailList = i['detailList']
    #     break
    # list2=detailList
    # for k in list2:
    #     maxSettleQty = k['leftSettleQty']
    #     maxSettleAmount = k['settlePrice']
    #     curSettleQty = k['curSettleQty']
    #     curSettleAmount = k['curSettleAmount']
    #     leftSettleQty = k['leftSettleQty']

    request_body1 = commonFuc().get_business_data(module, "payload3_1_1_1", pid, settleMonth, hosGoodsId, batchCode,
                                                  distrDetailId, distrId, distrOrderNo, settlePrice, maxSettleQty,
                                                  maxSettleAmount, curSettleQty, curSettleAmount, leftSettleQty,
                                                  leftSettleAmount, hosId,
                                                  provId,
                                                  branchId, balanceDetId, purMode, barCode, viewId)

    request_body2 = commonFuc().get_business_data(module, "payload3_1_1_2", pid2, settleMonth2, hosGoodsId2, batchCode2,
                                                  distrDetailId2, distrId2, distrOrderNo2, settlePrice2, maxSettleQty2,
                                                  maxSettleAmount2, curSettleQty2, curSettleAmount2, leftSettleQty2,
                                                  leftSettleAmount2, hosId,
                                                  provId2,
                                                  branchId2, balanceDetId2, purMode2, barCode2, viewId2)
    # print('request_body1', request_body1,request_body1)
    # print('request_body2', request_body2, request_body2)
    request_body3 = str(request_body1) + ',' + str(request_body2)
    request_body4 = get_process_list(request_body3)
    print('request_body4', request_body4)
    # while True:
    #     time.sleep(3)

    result = commonFuc().http_post(url, request_body4, headers)

    print('result', result)


def set_dept_lead():  # 设置科室负责人
    module = 'b2_herp3_bs'
    username = FileUtils().r_info8(module, '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)

    # print(list_id)
    # for i in list_id:
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "deptProcOrder_update__url")
    # url=url+userid
    print(url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    dept2_id = FileUtils().r_info8(module, '所有科室id', 'message6')["deptid2"]
    # 请求体
    request_body = commonFuc().get_business_data(module, "payload61_5", uxid, dept2_id)

    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    cname = commonFuc().analysis_json('cname', data1)
    # print(len(data1))
    if code == 0 and data == 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)


def getUserOnlineLoginWithMgrRange(dept_id):  # 切换权限
    module = 'b2_herp3_bs'
    username = FileUtils().r_info8(module, '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    module = 'b5_spd3_core_business_flow'
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "platformService_getUserOnlineLoginWithMgrRange_url")
    # url=url+userid
    print(url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    request_body = commonFuc().get_business_data('b5_spd3_core_business_flow', "payload23", corpId, dept_id, uxid)
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)


def updateUserLoginDefaultRange(roleid):
    module = 'b2_herp3_bs'
    username = FileUtils().r_info8(module, '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取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,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data('b5_spd3_core_business_flow',
                                                                             "platformService_updateUserLoginDefaultRange_url")
    url = url + roleid
    print(url)
    result = commonFuc().http_get(url, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)


def get_listUserMgrRangePage(type=1):  # 查询权限id
    module = 'b2_herp3_bs'
    username = FileUtils().r_info8(module, '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取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,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data('b5_spd3_core_business_flow',
                                                                             "platformService_listUserMgrRangePage_url")

    request_body = commonFuc().get_business_data('b5_spd3_core_business_flow', "payload23", uxid)
    result = commonFuc().http_post(url, request_body, headers)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    print('data1',data1)
    list2=[]
    mgrRangeName_list=[]
    for i in data1:
        mgrRangeName=i['mgrRangeName']
        mgrRangeName_list.append(mgrRangeName)
    print('mgrRangeName_list',mgrRangeName_list)
    for i in mgrRangeName_list:
        print(i)
        if i=='设备科001':
            list2.append(i)

    for i in mgrRangeName_list:
        if i == '二级检验科002':
            list2.append(i)
        else:
            continue
    for i in mgrRangeName_list:
        if i == '三级检测组003':
            list2.append(i)
        else:
            continue
    for i in mgrRangeName_list:
        if i == '科室004':
            list2.append(i)
        else:
            continue

    print('list2',list2)

    if type == 1:
        for i in data1:
            if i['mgrRangeName']==list2[0]:
                    return commonFuc().analysis_json('id', i)
    elif type==2:
        for i in data1:
            if i['mgrRangeName']==list2[1]:
                    return commonFuc().analysis_json('id', i)
    elif type == 3:
        for i in data1:
            if i['mgrRangeName']==list2[2]:
                    return commonFuc().analysis_json('id', i)
    elif type == 4:
        for i in data1:
            if i['mgrRangeName']==list2[3]:
                    return commonFuc().analysis_json('id', i)


def get_stockAreaId_new(dept_id, deptName, type=1):
    module = "b2_herp3_bs"
    username = FileUtils().r_info8(module, '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    stockId = 'stockId'
    # 获取库房id=========     开始====
    if type == 1:
        # 中心库房名称
        branch_name = FileUtils().r_info(module, '院区新增')["branch_name"]
        stockId = get_stockId(branch_name + '中心库房')
    # 二级库房名称
    elif type == 2:
        stockId = get_stockId(deptName + '库房', 2)
        print(stockId)
    elif type == 3:
        stockId = get_stockId(deptName + '库房', 3)
        print(stockId)

    # 获取库房id==========结束============

    # ====获取库区id===== 开始==========

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "basic_stockShelf_page_url")

    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    request_body = commonFuc().get_business_data(module, "payload62_4", dept_id, stockId)
    print('request_body查询库区id', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)

    if code == 0 and len(data) >= 1:
        commonFuc().check_text_exist(0, result)

    else:
        commonFuc().check_text_exist('error', result)
    # 增加一段返回库区name
    shelfCode = "shelfCode"
    for i in data1:
        shelfName = commonFuc().analysis_json('shelfName', i)
        print(shelfName)
        if shelfName == '默认库区' or shelfName == '自动化测试':
            import time
            id1 = commonFuc().analysis_json('id', i)
            shelfCode = commonFuc().analysis_json('shelfCode', i)
            areaName = commonFuc().analysis_json('shelfName', i)
    return shelfCode


def herpService_mcmsDeptGoodsInfo_getByProvIdPage():  # 高值跟台获取产品信息
    module = "b5_spd3_core_business_flow"
    username = FileUtils().r_info8('b2_herp3_bs', '用户名信息', 'message3')["username1"]
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_mcmsDeptGoodsInfo_getByProvIdPage_url")

    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))

    # 请求体
    provId = FileUtils().r_info8("b5_spd3_core_business_flow", "供货关系申请2", 'message')['e_corpId']
    hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message1')['hosGoodsId']
    GoodsId = hosGoodsId.replace(get_hosid(), '')

    request_body = commonFuc().get_business_data(module, "payload25", GoodsId, provId)
    print('request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # 写入文件中
    data = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data)
    list_b.append(data)
    list_c.append(data)

    info = (list_a, list_b, list_c)
    titlename = ('list_a', 'list_b', 'list_c')
    # 将条码信息写入文件
    FileUtils().w_info8(info, module, '产品信息', titlename, 'message19')


def height_value_order_dp(orderno):  # 高值跟台配送
    # # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "供货关系申请2", 'message')
    # print(info)
    username = info['e_username']

    password = commonFuc().get_business_data("b2_herp3_bs", "password1")
    # username = '001f90380'
    # password='1qaz!QAZ'
    print('username,password', username, password)
    # # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login(username, password,1)
    # 登录获取用户id等信息，使用创建的用户登录============结束

    # =========================查询待确认订单======================
    module = "b5_spd3_core_business_flow"
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "hdiFusion_operation_page_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hos_id = branch_id[0:5]
    exeProvId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]


    orderNo1 = orderno

    dateValue1 = timeUtils().get_time_add(-30, 2)
    dateValue2 = timeUtils().get_time_add(30, 2)

    request_body = commonFuc().get_business_data(module, "payload1_1_1", dateValue1, dateValue2, orderNo1, exeProvId,
                                                 hos_id, dateValue1, dateValue2)
    print('request_body',request_body)

    # 发送请求
    while True:
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        # 断言
        code = commonFuc().analysis_json('code', result)
        data = commonFuc().analysis_json('data', result)
        total = commonFuc().analysis_json('total', data)
        if total == 1:
            break
        else:
            time.sleep(6)

    data1 = commonFuc().analysis_json('data', data)
    id1 = commonFuc().analysis_json('id', data1[0])
    print('id1', id1)

    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data1)
    list_b.append(data1)
    list_c.append(data1)
    # print('list_a', len(list_a), list_a)
    info = (list_a, list_b, list_c)
    titlename = ('list_a', 'list_b', 'list_c')
    # 将配送订单信息息写入到文件中
    FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '配送订单信息', titlename, 'message8')
    # 从message8文件中获取信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "配送订单信息", 'message8')
    # print('info', info)
    list_a = info['list_a']
    list_a = ','.join(map(str, list_a))
    import json
    new_request_body = list_a.replace("'", '"')
    new_request_body1 = new_request_body.replace("None", 'null')
    print(new_request_body1)
    list_a2 = json.loads(new_request_body1)
    # print('list_a', list_a)
    billMode = "billMode"
    createUser = 'createUser'
    hosId = 'hosId'
    provId = 'provId'
    for i in list_a2:
        billMode = i["billMode"]
        createUser = i["createUser"]
        hosId = i['hosId']
        provId = i['provId']
        break

    # =========================查询待确认订单======================
    # ==============================确认订单==================

    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "ensure_url")

    request_body = commonFuc().get_business_data(module, "payload26", id1, uxid)

    # print('request_body',request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist('error', result)
    # ==============================确认订单==================
    # ==================a3获取订单详细信息中的子表id_3_0医院
    # a1订单配送列表数据查询_全部
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "detail_id_url")
    url=url+id1+"/3"
    # request_body = commonFuc().get_business_data(module, "payload4", id1)

    # print('request_body',request_body)
    # 发送请求
    result = commonFuc().http_get(url,headers)
    print('resulta3获取订单详细信息中的子表id_3_0医院', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', data)

    commonFuc().check_text_exist(code, result)
    data1 = commonFuc().analysis_json('data', data)
    id2 = commonFuc().analysis_json('id', data1[0])
    print(id1, id2)
    data1 = commonFuc().analysis_json('data', data)
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data1)
    list_b.append(data1)
    list_c.append(data1)
    # print('list_a', len(list_a), list_a)
    info = (list_a, list_b, list_c)
    titlename = ('list_a', 'list_b', 'list_c')
    # 将配送订单信息息写入到文件中
    FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '配送订单详细信息', titlename, 'message9')

    # ==================a3获取订单详细信息中的子表id_3_0医院
    # ==================订单配送========================
    # a1订单配送列表数据查询_全部
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "submit_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    # 从message9文件中获取信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "配送订单详细信息", 'message9')
    print('info从message9文件中获取信息', info)
    # print('info', info)
    list_a = info['list_a']

    hosGoodsId = 'hosGoodsId'
    mdmGoodsCode = 'mdmGoodsCode'
    goodsCode = 'goodsCode'
    pkgDefId = 'pkgDefId'
    erpCode = 'erpCode'
    for i in list_a:
        print('aaaaaaaaaaaaaaaaaaaaaaaaaaaa', i)
        for j in i:
            print('bbbbbbbbbbbbbbbbbbb', j)
            hosGoodsId = j['hosGoodsId']
            mdmGoodsCode = j['mdmGoodsCode']
            goodsCode = j['goodsCode']
            pkgDefId = j['pkgDefId']
            erpCode = j['erpCode']
            break
        # # #获取日期
    productDate = timeUtils().get_time_hms(5)
    expdtDate = timeUtils().get_time_add(190, 2)


    request_body = commonFuc().get_business_data(module, "payload9_2_1_2", id1, id1, hosId, provId, billMode,
                                                     createUser, username, createUser, id2, hosGoodsId, mdmGoodsCode,
                                                     goodsCode, productDate, expdtDate, erpCode, id2)

    print('request_body', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    id1 = commonFuc().analysis_json('id', data)
    info = (id1, id1)
    titlename = ('distrBillId1', 'distrBillId2')
    # 将配送订单信息息写入到文件中
    if type == 1:
        FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '低值配送单号', titlename, 'message10')
    elif type == 2:
        FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '高值配送单号', titlename, 'message10')
    elif type == 3:
        FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '试剂配送单号', titlename, 'message10')
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist('error', result)


def Search_OrderNO():
    # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    print(info)
    username1 = info['username1']

    username = username1
    ## print('医院名称',username)
    password = commonFuc().get_business_data('b2_herp3_bs', "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login(username, password, 2)
    # 登录获取用户id等信息，使用创建的用户登录============结束

    # print( token,projectCode,uxid,corpId,info)
    module = 'b3_herp3_purchase'
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_operationNotice_url")
    print(url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))
    print('headers', headers)
    # 从文件中获取院区id
    branchId = FileUtils().r_info8('b2_herp3_bs', "院区新增", 'message')
    # # #获取日期
    date1 = timeUtils().get_time_hms(5)
    date2 = timeUtils().get_time_hms(5)
    # print(date1,date2)

    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload23_1_1", date2, date1, branchId)
    print('request_body', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('手术通知单result', result)
    data1 = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    return commonFuc().analysis_json('orderNo', data1)
