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

from common.fileUtls import FileUtils
from common.run_cmd_script import execute_command

__author__ = "xiaohesheng"

"""
case_tag:api,b6_spd3_core_business_flow_database_check,a_a5流程五_中心库入库结算_正结算,id2303,id2303-5,sit,on
主数据平台:a_a5流程五_中心库入库结算_正结算 
python runner_test.py tag id2303-5 debug mdm3
"""

from common.common_func import commonFuc
from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from airtest.core.api import using
import sys
import time

module = "b6_spd3_core_business_flow_database_check"


def login():
    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()
    return token, projectCode, uxid, corpId, info
    # 登录获取用户id等信息，使用创建的用户登录============结束


# 采购计划_低值
def low_vaule_purchase_plan():
    module = "b3_herp3_purchase"
    # 登录获取用户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, "herpService_purPlan_insert_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)
    hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    pkgDefId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品包装信息', 'message6')["unitPackageid1"]
    # 获取院区id
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hosId = branch_id[0:5]
    deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
    hospital_name = commonFuc().get_business_data(module, 'hospital_name')
    areaCode = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["shelfCode"]
    areaName = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["areaName"]
    recUser = uxid

    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload1_1_1", hosGoodsId, pkgDefId, hosId, branch_id,
                                                 hospital_name, deptid1, deptid1, areaCode, areaName, recUser)
    print('request_body', request_body)
    # new_request_body = request_body.replace("'", '"')
    # new_request_body = new_request_body.replace("None", 'null')
    # # print(new_request_body)
    # import json
    # request_body_json = json.loads(new_request_body)
    # print('request_body_json', request_body_json)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    pid = commonFuc().analysis_json('data', result)  # 采购计划号
    # 获取暂存的单号==========================================================

    # 获取采购计划详情中的id
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_purPlan_listVo_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)
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload11", pid)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    data = commonFuc().analysis_json('data', result)
    id1 = commonFuc().analysis_json('id', data[0])
    orderNo = commonFuc().analysis_json('orderNo', data[0])
    print('orderNo',orderNo)
    titlename=('orderNo1','orderNo2')
    info=(orderNo,orderNo)
    FileUtils().w_info8(info,'b5_spd3_core_business_flow', '采购计划单号', titlename, 'message7')
    # =====================================

    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"))
    print('headers', headers)
    provId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    provName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    subProvId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    subProvName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload12_1_1", pid, id1, provId, provName, subProvId,
                                                 subProvName, hosGoodsId, pkgDefId)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultbbbbbbbbbbbbbbbbbbbb', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0 and data == None:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)

def approval_center():
    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)
    # 断言
    # 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 get_GoodsPkgDef(hosGoodsId):
    # 获取院区产品id==================开始
    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, "mcmsBranchGoodsInfo_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"))
    # 获取院区id
    branch_id = FileUtils().r_info(module, '院区新增')["branch_id"]
    hos_id = branch_id[0:5]
    print(hos_id)
    # hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
    GoodsId = hosGoodsId.replace(hos_id, '')
    print(GoodsId)
    deptid1 = FileUtils().r_info8(module, '所有科室id', 'message6')["deptid1"]
    # #请求体
    request_body = commonFuc().get_business_data(module, "payload39_1_1", branch_id, hos_id, GoodsId, deptid1)
    print(request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    total = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result))

    data = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    branchGoodsId = commonFuc().analysis_json('branchGoodsId', data)
    hosGoodsId = commonFuc().analysis_json('hosGoodsId', data)

    # 获取院区产品id ===========结束

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "branchGoodsPkgDef_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"))
    # 获取院区id
    branch_id = FileUtils().r_info(module, '院区新增')["branch_id"]

    # #请求体
    request_body = commonFuc().get_business_data(module, "payload42_2", hos_id, branch_id, branchGoodsId)
    print('request_body', request_body)
    # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # 断言
    # checkDict=commonFuc().get_business_data(module,'checkDict10')
    # commonFuc().check_result(checkDict,result)
    unitPackageid = commonFuc().analysis_json('id', commonFuc().analysis_json('data', result))
    print('unitPackageid', unitPackageid)
    info = (unitPackageid, unitPackageid)
    titlename = ('unitPackageid1', 'unitPackageid2')
    FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '产品包装信息', titlename, 'message6')
#高值采购计划
def hight_vaule_purchase_plan():
    module = "b3_herp3_purchase"
    # 登录获取用户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, "herpService_purPlan_insert_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)
    hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message1')["hosGoodsId"]
    pkgDefId = None
    # 获取院区id
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hosId = branch_id[0:5]
    deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
    hospital_name = commonFuc().get_business_data(module, 'hospital_name')
    areaCode = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["shelfCode"]
    areaName = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["areaName"]
    recUser = uxid

    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload1_1_2", hosGoodsId, hosId, branch_id,
                                                 hospital_name, deptid1, deptid1, areaCode, areaName, recUser)
    print('request_body', request_body)
    # new_request_body = request_body.replace("'", '"')
    # new_request_body = new_request_body.replace("None", 'null')
    # # print(new_request_body)
    # import json
    # request_body_json = json.loads(new_request_body)
    # print('request_body_json', request_body_json)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    pid = commonFuc().analysis_json('data', result)  # 采购计划号
    # 获取暂存的单号==========================================================

    # 获取采购计划详情中的id
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_purPlan_listVo_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)
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload11", pid)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    data = commonFuc().analysis_json('data', result)
    id1 = commonFuc().analysis_json('id', data[0])
    orderNo = commonFuc().analysis_json('orderNo', data[0])
    print('orderNo',orderNo)
    titlename=('orderNo1','orderNo2')
    info=(orderNo,orderNo)
    FileUtils().w_info8(info,'b5_spd3_core_business_flow', '采购计划单号高值', titlename, 'message11')
    # =====================================

    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"))
    print('headers', headers)
    provId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    provName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    subProvId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    subProvName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload12_1_2", pid, id1, provId, provName, subProvId,
                                                 subProvName, hosGoodsId)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultbbbbbbbbbbbbbbbbbbbb', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0 and data == None:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
#试剂采购计划
def reagent_purchase_plan():
    module = "b3_herp3_purchase"
    # 登录获取用户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, "herpService_purPlan_insert_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)
    hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message3')["hosGoodsId"]

    get_GoodsPkgDef(hosGoodsId)  # 获取单元包id
    pkgDefId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品包装信息', 'message6')["unitPackageid1"]

    # 获取院区id
    branch_id = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_id"]
    hosId = branch_id[0:5]
    deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
    hospital_name = commonFuc().get_business_data(module, 'hospital_name')
    areaCode = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["shelfCode"]
    areaName = FileUtils().r_info8('b2_herp3_bs', 'shelfinfo', 'message7')["areaName"]
    recUser = uxid

    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload1_1_3", hosGoodsId,pkgDefId, hosId, branch_id,
                                                 hospital_name, deptid1, deptid1, areaCode, areaName, recUser)
    print('request_body', request_body)
    # new_request_body = request_body.replace("'", '"')
    # new_request_body = new_request_body.replace("None", 'null')
    # # print(new_request_body)
    # import json
    # request_body_json = json.loads(new_request_body)
    # print('request_body_json', request_body_json)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    pid = commonFuc().analysis_json('data', result)  # 采购计划号
    # 获取暂存的单号==========================================================

    # 获取采购计划详情中的id
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_purPlan_listVo_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)
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload11", pid)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)
    data = commonFuc().analysis_json('data', result)
    id1 = commonFuc().analysis_json('id', data[0])
    orderNo = commonFuc().analysis_json('orderNo', data[0])
    print('orderNo', orderNo)
    titlename = ('orderNo1', 'orderNo2')
    info = (orderNo, orderNo)
    FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '采购计划单号试剂', titlename, 'message12')
    # =====================================

    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"))
    print('headers', headers)
    provId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    provName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    subProvId = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_corpId"]
    subProvName = FileUtils().r_info8('b5_spd3_core_business_flow', '供货关系申请2', 'message')["e_username"]
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload12_1_3", pid, id1, provId, provName, subProvId,
                                                 subProvName, hosGoodsId,pkgDefId)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultbbbbbbbbbbbbbbbbbbbb', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0 and data == None:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
def main():
    try:
        # 获取低值的hosGoodsId
        hosGoodsId = FileUtils().r_info8('b5_spd3_core_business_flow', '产品审核信息', 'message2')["hosGoodsId"]
        get_GoodsPkgDef(hosGoodsId)  # 获取单元包id
    except:
        print('流程五获取hosGoodsId出现错误,请检查脚本或者数据')
    try:
        low_vaule_purchase_plan()
    except Exception as e:
        print('低值采购计划提交出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
        traceback.print_exc()
    #审批
    try:
        approval_center()
    except Exception as e:
        print('审批采购计划出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
    try:
        hight_vaule_purchase_plan()
    except Exception as e:
        print('高值采购计划出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
    try:
        approval_center()
    except Exception as e:
        print('审批采购计划出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
    try:
        reagent_purchase_plan()
    except Exception as e:
        print('试剂采购计划出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
    try:
        approval_center()
    except Exception as e:
        print('审批采购计划出现错误,请检查脚本或者数据')
        print("发生异常：", str(e))
    commonFuc().check_text_exist_result_text('succees','succees')

main()