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

from air_case.mdm_login.登录外网系统.登录外网系统 import login_system
from air_case.public1.public1.public1 import call_login
from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,spd_core,a_a6流程五_中心库入库结算_正结算2_外网配送,id2302,id2302-6,sit,on
主数据平台:a_a6流程五_中心库入库结算_正结算2_外网配送
python runner_test.py tag id2302-6 debug mdm3
"""

from common.common_func import commonFuc
from airtest.core.api import using
import sys
import time

module = "b5_spd3_core_business_flow"


def order_dp(type=1,token='token',projectCode='projectCode',uxid='uxid'):
    # execute_command("python runner_test.py tag id2302-5 debug mdm3")
    # # # 登录获取用户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_system(username, password).get_token()
    # 登录获取用户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, "purchase_page_url")
    #
    # request_body = commonFuc().get_business_data(module, "payload3_1_1",exeProvId,hos_id)
    # # 发送请求
    # 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)
    # data1 = commonFuc().analysis_json('data', data)
    # id1=commonFuc().analysis_json('id', data1[0])
    # print('id1',id1)
    # ==================a3获取订单详细信息中的子表id_3_0医院
    # a1订单配送列表数据查询_全部
    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)
    # print('info', info)
    list_a = info['list_a']
    # print('list_a',list_a,type(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',new_request_body1)
    #
    # list_a2 = json.loads(new_request_body1)
    # print('list_a2', list_a2)
    # print('list_a2', type(list_a2))
    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)
    username = FileUtils().r_info8('b5_spd3_core_business_flow', "供货关系申请2", 'message')['e_username']
    if type == 1:
        request_body = commonFuc().get_business_data(module, "payload9_2_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", 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", 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 main():
    token, projectCode1, uxid1, corpId1, info1 = call_login(1)
    try:
        order_dp(1,token,projectCode1,uxid1)# 1低值
        order_dp(2,token,projectCode1,uxid1)#2高值
        order_dp(3,token,projectCode1,uxid1)#3试剂
    except Exception as e:
        print('订单配送发生错误,请检查脚本或者数据')
        print("发生异常：", str(e))
        traceback.print_exc()
    else:
        commonFuc().check_text_exist_result_text('succees','succees')
# main()