# -*- encoding=utf8 -*-
import json
import traceback

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from air_case.public1.public1.public1 import *
from common.db.sql.sql_del_branch_info import delData
from common.db.sql.sql_tools import process_tuple, get_id, get_last_auditor

from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command
from common.timeUtils import timeUtils
from airtest.core.api import using

__author__ = "xiaohesheng"

"""
case_tag:api,spd_core,a_b4流程十一请领业务流_二级库向直送,id2302,id2302-14,sit,on
核心业务流:a_b4流程十一请领业务流_二级库向直送
python runner_test.py tag id2302-14 debug mdm3
"""


def SERACH_goods_id(GoodsId):
    # 在请领出库渠道边仓中搜索产品
    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等信息，使用创建的用户登录============结束\
    # 获取请求头信息
    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,
                                                                             "herpService_mcmsBranchGoodsInfo_page_url")
    dept1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    # hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message2')['hosGoodsId']
    # GoodsId = hosGoodsId.replace(get_hosid(), '')
    request_body = commonFuc().get_business_data(module, "payload53_2", GoodsId, get_hosid(), get_branch_id(), dept1)
    result = commonFuc().http_post(url, request_body, headers)
    branchGoodsId = commonFuc().analysis_json('id', commonFuc().analysis_json('data', commonFuc().analysis_json('data',
                                                                                                                result)))
    return branchGoodsId


def goods_move2():  # 设置请领出库渠道为直送
    # # 登录获取用户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等信息，使用创建的用户登录============结束
    module = "b2_herp3_bs"
    # 设置请领出库渠道为直送
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "updateSettleType_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"))
    for i in range(1, 4):
        info = FileUtils().r_info8('b5_spd3_core_business_flow', "产品审核信息", 'message%s' % i)
        hosGoodsId = info['hosGoodsId']
        GoodsId = hosGoodsId.replace(get_hosid(), '')
        branchGoodsId = SERACH_goods_id(GoodsId)
        # #请求体
        request_body = commonFuc().get_business_data(module, "payload47_2", branchGoodsId, hosGoodsId, get_branch_id())
        print(request_body)
        # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('goods_move,result', result)


def second_dept_Request(hosGoodsId, type):
    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等信息，使用创建的用户登录============结束
    # 我要请领_手工请领_列表查询===========================
    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,
                                                                             "herpService_buy_queryGoodsInfoPage_url")
    print('我要请领_手工请领_列表查询=================', url)
    deptid2 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    deptname2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    # 高值
    # hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message1')['hosGoodsId']
    GoodsId = hosGoodsId.replace(get_hosid(), '')
    # areaCode = get_stockAreaId_new(deptid2, deptname2, 2)
    areaCode = process_tuple(get_id(
        "select shelf_code from mcms_stock_shelf where address='二级检验科002'and shelf_name='默认库区'order by create_time desc LIMIT 1;"))
    request_body = commonFuc().get_business_data(module, "payload39", GoodsId, get_hosid(), deptid2, areaCode)
    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))
    goodsName = commonFuc().analysis_json('goodsName', data1)
    goodsSpec = commonFuc().analysis_json('goodsSpec', data1)
    mdmGoodsCode = commonFuc().analysis_json('mdmGoodsCode', data1)
    goodsCode = commonFuc().analysis_json('goodsCode', data1)
    unit = commonFuc().analysis_json('unit', data1)
    purMode = commonFuc().analysis_json('purMode', data1)
    goodsMfrsName = commonFuc().analysis_json('goodsMfrsName', data1)
    unitName = commonFuc().analysis_json('unitName', data1)
    price = commonFuc().analysis_json('price', data1)
    deptId = commonFuc().analysis_json('deptId', data1)
    # areaCode=commonFuc().analysis_json('areaCode',data1)
    areaName = '默认库区'
    targetDeptId = commonFuc().analysis_json('targetDeptId', data1)
    targetDeptName = commonFuc().analysis_json('targetDeptName', data1)
    targetStockId = commonFuc().analysis_json('targetStockId', data1)
    targetStockName = commonFuc().analysis_json('targetStockName', data1)
    purModeText = commonFuc().analysis_json('purModeText', data1)
    goodsGeneralName = commonFuc().analysis_json('goodsGeneralName', data1)
    provName = commonFuc().analysis_json('provName', data1)
    pkgDefId = commonFuc().analysis_json('pkgDefId', data1)
    pkgDefName = commonFuc().analysis_json('pkgDefName', data1)
    # 信息写入文件
    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, module, '二级库请领产品信息', titlename, 'message21')
    # 我要请领_手工请领_列表查询===========================
    # 添加到请领车=============================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_buy_createBuyCar_url")
    print('添加到请领车=================', url)
    if type == 1:
        request_body = commonFuc().get_business_data(module, "payload40", hosGoodsId, purMode, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, goodsMfrsName, pkgDefName, pkgDefId, unitName,
                                                     price,
                                                     deptId,
                                                     areaCode, areaName, targetDeptId, targetDeptName, targetStockId,
                                                     targetStockName, purModeText, goodsGeneralName, provName)
    elif type == 2:
        request_body = commonFuc().get_business_data(module, "payload41", hosGoodsId, purMode, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, goodsMfrsName, unitName, price, deptId,
                                                     areaCode, areaName, targetDeptId, targetDeptName, targetStockId,
                                                     targetStockName, purModeText, goodsGeneralName, provName)
    elif type == 3:
        request_body = commonFuc().get_business_data(module, "payload42", hosGoodsId, purMode, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, goodsMfrsName, pkgDefName, pkgDefId, unitName,
                                                     price,
                                                     deptId,
                                                     areaCode, areaName, targetDeptId, targetDeptName, targetStockId,
                                                     targetStockName, purModeText, goodsGeneralName, provName)
    request_body = get_process_list(request_body)
    print('添加到请领车', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('添加到请领车,result', result)
    # 添加到请领车=============================================


def BuyCarSubmit():
    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等信息，使用创建的用户登录============结束
    # 我要请领_手工请领_列表查询===========================
    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,
                                                                             "herpService_buy_queryBuyCarPage_url")
    print('请领车查询=================', url)
    deptid2 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2']
    deptname2 = FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2']
    areaCode = get_stockAreaId_new_fromDb(deptname2, 2)
    request_body = commonFuc().get_business_data(module, "payload43", areaCode, get_hosid(), deptid2)

    print('请领车查询', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('请领车查询,result', result)
    data = commonFuc().analysis_json('data', result)
    list_id = []
    for i in data:
        id = commonFuc().analysis_json('id', i)
        list_id.append(id)
    # 请领车查询===============================================
    # 请领车提交===============================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_buy_validBuyCarSubmit_url")
    print('请领车提交=================', url)

    billExpdtDate = timeUtils().get_time_add(30, 2)
    print('billExpdtDate', billExpdtDate)
    request_body = commonFuc().get_business_data(module, "payload44", billExpdtDate, get_branch_id(), list_id[0],
                                                 list_id[1], list_id[2])

    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_buy_createBuyBill_url")
    print('创建请领车交易=================', url)

    billExpdtDate = timeUtils().get_time_add(30, 2)
    request_body = commonFuc().get_business_data(module, "payload44", billExpdtDate, get_branch_id(), list_id[0],
                                                 list_id[1], list_id[2])

    print('创建请领车交易', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('创建请领车交易,result', result)
    currentTaskList = commonFuc().analysis_json('currentTaskList', commonFuc().analysis_json('data', result))


def add_dept2():
    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)
    # c3_新增科室_特殊科室_二级库房
    branch_id = FileUtils().r_info(module, '院区新增')["branch_id"]
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "dept_insert_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, "payload15_2", get_branch_id(), get_hosid())
    print(request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)


def add_new_deptinfo():
    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 == 5:
        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', 'deptid5']

    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 purchase_module_process():
    updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是一级库
    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,
                                                                             "herpService_purPlan_listVoPage_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"))
    # #请求体
    request_body = commonFuc().get_business_data(module, "payload56", get_hosid())
    print('采购计划列表查询暂存数据', request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('采购计划列表查询暂存数据,result', result)
    data = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    for i in data:
        pid = commonFuc().analysis_json('id', i)
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "herpService_purPlan_listVo_url")
        # #请求体
        request_body = commonFuc().get_business_data(module, "payload57", pid)
        result = commonFuc().http_post(url, request_body, headers)
        print('herpService_purPlan_listVo_url_result', result)
        data1 = commonFuc().analysis_json('data', result)
        id1 = commonFuc().analysis_json('pid', data1)
        closeStatus = commonFuc().analysis_json('closeStatus', data1)
        id2 = commonFuc().analysis_json('id', data1)
        inSettlement = 1
        # planPkgQty = commonFuc().analysis_json('planPkgQty', data1)
        # planPkgQty = int(float(planPkgQty))
        # pkgDefQty = commonFuc().analysis_json('pkgDefQty', data1)
        # pkgDefQty = int(float(pkgDefQty))
        provId = commonFuc().analysis_json('provId', data1)
        provName = commonFuc().analysis_json('provName', data1)
        subProvId = commonFuc().analysis_json('subProvId', data1)
        subProvName = commonFuc().analysis_json('subProvName', data1)
        hosGoodsId = commonFuc().analysis_json('hosGoodsId', data1)
        purMode = commonFuc().analysis_json('purMode', data1)
        version = commonFuc().analysis_json('version', data1)
        pkgDefId = commonFuc().analysis_json('pkgDefId', data1)
        purTaskId = commonFuc().analysis_json('purTaskId', data1)
        sourceId = commonFuc().analysis_json('sourceId', data1)
        codingType = commonFuc().analysis_json('codingType', data1)
        tempFlag = commonFuc().analysis_json('tempFlag', data1)
        orderNo = commonFuc().analysis_json('orderNo', data1)
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "herpService_purPlan_submitPurPlan_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"))
        # # 请求体
        if purMode == 60:
            request_body = commonFuc().get_business_data(module, "payload58", id1, closeStatus, id2, provId, provName,
                                                         subProvId,
                                                         subProvName, hosGoodsId, purMode, pkgDefId, sourceId)
            titlename = ('orderNo1', 'orderNo2')
            info = (orderNo, orderNo)
            FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '采购计划单号试剂', titlename, 'message12')
            # =====================
        elif purMode == 10:
            request_body = commonFuc().get_business_data(module, "payload58", id1, closeStatus, id2, provId, provName,
                                                         subProvId,
                                                         subProvName, hosGoodsId, purMode, pkgDefId, sourceId)
            print('orderNo', orderNo)
            titlename = ('orderNo1', 'orderNo2')
            info = (orderNo, orderNo)
            FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '采购计划单号', titlename, 'message7')
        else:  # purMode=20
            request_body = commonFuc().get_business_data(module, "payload58_1", id1, id2, provId, provName,
                                                         subProvId,
                                                         subProvName, hosGoodsId, sourceId)
            titlename = ('orderNo1', 'orderNo2')
            info = (orderNo, orderNo)
            FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '采购计划单号高值', titlename, 'message11')

        print('采购计划提交request_body', request_body)
        # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('采购计划提交', result)


def order_dp(type=1):
    # # 登录获取用户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 = "a6_mdm3_dp"
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "purchase_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"]

    if type == 1:
        orderNo1 = FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号', 'message7')["orderNo1"]
        print(orderNo1)
    elif type == 2:
        orderNo1 = FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号高值', 'message11')["orderNo1"]
        print(orderNo1)
    elif type == 3:
        orderNo1 = FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号试剂', 'message12')["orderNo1"]
        print(orderNo1)
    else:
        orderNo1 = 'orderNo1'
    request_body = commonFuc().get_business_data(module, "payload1_1_1", orderNo1, hos_id, exeProvId)
    # 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

    # =========================查询待确认订单======================
    # ==============================确认订单==================
    module = "a6_mdm3_dp"

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

    request_body = commonFuc().get_business_data(module, "payload2", 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)
    # ==============================确认订单==================
    # ==============================配送==================

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

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

    # print('request_body',request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, 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医院
    # ==================订单配送========================
    module = "a6_mdm3_dp"
    # 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)
    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)
    if type == 1:
        request_body = commonFuc().get_business_data(module, "payload9_2_1_1_1", id1, id1, hosId, provId, billMode,
                                                     createUser, username, createUser, id2, hosGoodsId, mdmGoodsCode,
                                                     goodsCode, pkgDefId, productDate, expdtDate, erpCode, id2)
    elif type == 2:
        request_body = commonFuc().get_business_data(module, "payload9_2_1_2_1", id1, id1, hosId, provId, billMode,
                                                     createUser, username, createUser, id2, hosGoodsId, mdmGoodsCode,
                                                     goodsCode, productDate, expdtDate, erpCode, id2)
    elif type == 3:
        request_body = commonFuc().get_business_data(module, "payload9_2_1_3_1", id1, id1, hosId, provId, billMode,
                                                     createUser, username, createUser, id2, hosGoodsId, mdmGoodsCode,
                                                     goodsCode, pkgDefId, 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 check_accept_order(type=1):
    # # 登录获取用户id等信息，使用创建的用户登录===========开始
    info = FileUtils().r_info8('b2_herp3_bs', "用户名信息", 'message3')
    # print(info)
    username = info['username1']

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

    # =========================查询待验收订单======================
    module = "b5_spd3_core_business_flow"
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "check_wait_page_url")
    print('url', 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]
    if type == 1:
        sourceOrderNo = FileUtils().r_info8(module, '低值配送单号', 'message10')["distrBillId1"]
    elif type == 2:
        sourceOrderNo = FileUtils().r_info8(module, '高值配送单号', 'message10')["distrBillId1"]
    elif type == 3:
        sourceOrderNo = FileUtils().r_info8(module, '试剂配送单号', 'message10')["distrBillId1"]
    else:
        sourceOrderNo = FileUtils().r_info8(module, '低值配送单号', 'message10')["distrBillId1"]
    request_body = commonFuc().get_business_data(module, "payload", hos_id, branch_id, sourceOrderNo)
    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)

    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, 'message13')
    # 从message13文件中获取信息
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "验收单信息", 'message13')
    # print('info', info)
    list_a = info['list_a']
    print('list_a', list_a)
    sourceType = "sourceType"
    billMode = 'billMode'
    recOrgId = 'recOrgId'
    recOrgName = 'recOrgName'
    sourceOrgId = 'sourceOrgId'
    sourceOrgName = 'sourceOrgName'
    sourceVersion = 'sourceVersion'
    tbStatus = 'tbStatus'
    for i in list_a:
        print('i', i)
        for j in i:
            sourceType = j["sourceType"]
            billMode = j["billMode"]
            recOrgId = j['recOrgId']
            recOrgName = j['recOrgName']
            sourceOrgId = j['sourceOrgId']
            sourceOrgName = j['sourceOrgName']
            sourceVersion = j['sourceVersion']
            tbStatus = j['tbStatus']
            break

    # =========================查询待验收订单======================
    # ==============================查看订单详情==================

    sourceId = sourceOrderNo
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "check_wait_detail_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload1", sourceId, sourceType, billMode, branch_id)

    # print('request_body',request_body)
    # 发送请求
    while True:
        time.sleep(6)
        result = commonFuc().http_post(url, request_body, headers)
        code = commonFuc().analysis_json('code', result)
        if code == 0:
            break
        else:
            print('验收单数据未同步完成，等待6秒')
    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, 'b5_spd3_core_business_flow', '验收单详情信息', titlename, 'message14')
    # 读取viewId
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "验收单详情信息", 'message14')
    # print('info', info)
    list_a = info['list_a']
    print('list_a', list_a)
    viewId = "viewId"
    settlement = 'settlement'
    recTemperature = 'recTemperature'
    recHumidity = 'recHumidity'
    hosGoodsId = 'hosGoodsId'
    distrQty = 'distrQty'
    distrPkgQty = 'distrPkgQty'
    for i in list_a:
        for j in i:
            viewId = j["viewId"]
            settlement = j["settlement"]
            recTemperature = j['recTemperature']
            recHumidity = j['recHumidity']
            hosGoodsId = j['hosGoodsId']
            distrQty = j['distrQty']
            distrPkgQty = j['distrPkgQty']

        break
    # ==============================查看订单详情==================
    # ========设置验收全部合格
    url2 = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "check_barcode_save_url")
    print('check_barcode_save_url', url2)
    sourceBillId = sourceId
    request_body2 = commonFuc().get_business_data(module, "payload2", sourceBillId, viewId)

    print('设置验收全部合格request_body2', request_body2)
    # 发送请求
    time.sleep(6)

    while True:
        result2 = commonFuc().http_post(url2, request_body2, headers)
        print('result', result2)
        data = commonFuc().analysis_json('data', result2)
        code = commonFuc().analysis_json('code', result2)
        msg = commonFuc().analysis_json('msg', result2)
        print(code)
        if code == 0 and len(data) >= 1:
            break
        else:
            print('设置产品合格出现问题')
            time.sleep(6)
            continue

    # ========设置验收全部合格
    # =============提交验收单========================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "check_submit_url")
    deptId = recOrgId
    deptName = recOrgName
    provId = sourceOrgId
    provName = sourceOrgName
    createUser = uxid
    sourceNo = sourceId
    checkQty = distrQty
    checkPkgQty = distrPkgQty
    time.sleep(6)
    request_body = commonFuc().get_business_data(module, "payload3", billMode, branch_id, deptId, deptName, hos_id,
                                                 provId, provName, recOrgId, recOrgName, sourceId, sourceVersion,
                                                 sourceType, tbStatus, createUser, sourceNo, viewId, hosGoodsId, viewId,
                                                 settlement,
                                                 checkQty, distrPkgQty, checkPkgQty, recTemperature, recHumidity)

    print('request_body', '提交验收单', request_body)
    # 发送请求
    while True:
        time.sleep(3)
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        CHECK_NO = commonFuc().analysis_json('data', result)
        code = commonFuc().analysis_json('code', result)
        msg = commonFuc().analysis_json('msg', result)
        if code == 0:
            break
        elif code == 14031083 and "不允许验收" in msg:
            result2 = commonFuc().http_post(url2, request_body2, headers)
            print('result', result2)
            data = commonFuc().analysis_json('data', result2)
            code = commonFuc().analysis_json('code', result2)
            print(code)
            if code == 0 and len(data) >= 1:
                break
            else:
                print('设置产品合格出现问题')
                time.sleep(6)
            continue

    info = (CHECK_NO, CHECK_NO)
    titlename = ('CHECK_NO1', 'CHECK_NO2')
    FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '验收单号', titlename, 'message15')


def main():
    try:
        # add_dept2()  # 新增一个二级科室
        # add_new_deptinfo()  # 写入科室信息到文件中
        # grants_user_new()   #新增授权
        # 设置部门用户
        # set_dept_user(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid5'],FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid5'],2)
        # 设置部门负责人
        # set_dept_lead_new(FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid5"])  # 设置二级科室负责人
        # 从下面开始,上面的暂时注释
        goods_move2()  # 设置产品出库渠道为直送
        updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
        for i in range(6):
            # # 低值
            second_dept_Request(FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message2')['hosGoodsId'],
                                1)
            # # 高值
            second_dept_Request(FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message1')['hosGoodsId'],
                                2)
            # # 试剂
            second_dept_Request(FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message3')['hosGoodsId'],
                                3)
            BuyCarSubmit()  # 请领车提交
            for i in range(6):  # 三个单据共审批6次
                approval_center()
            # 采购模块处理
            purchase_module_process()
            # 三个单据共审批3次
            approval_center(FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号试剂', 'message12'))
            approval_center(FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号', 'message7'))
            approval_center(FileUtils().r_info8('b5_spd3_core_business_flow', '采购计划单号高值', 'message11'))

            order_dp(1)  # 1低值
            order_dp(2)  # 2高值
            order_dp(3)  # 3试剂
            check_accept_order(1)  # 低值
            check_accept_order(2)  # 高值
            check_accept_order(3)  # 试剂
            second_dept_one_key_in(2)
            second_dept_one_key_in(2)
            second_dept_one_key_in(2)
        commonFuc().check_text_exist_result_text('succees', 'succees')
        print('==========================流程十一结束==============================')
    except:
        commonFuc().check_text_exist_result_text('error', '1')
# main()
