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

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from air_case.public1.public1.public1 import *

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_core1,a_b1流程八_高值跟台业务流程,id2302,id2302-11,sit,on
主数据平台:a_b1流程八_高值跟台业务流程
python runner_test.py tag id2302-11 debug mdm3
"""


def operationApply():  # 高值跟台申请
    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_operationApply_listPage_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, "payload22", corpId, corpId)
    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_operationApply_submit_url")
    print('url', url)
    stockAreaId = get_stockAreaId_new(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2'],
                                      FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2'], 2)
    expireDate = timeUtils.get_time_add(1, 2)
    operDate = timeUtils.get_time_add(1, 2)
    # 从文件中获取必要信息
    info = FileUtils().r_info8(module, "产品信息", 'message19')
    # print('info', info)
    list_a = info['list_a']
    print('list_a', type(list_a), len(list_a))
    provId = commonFuc().analysis_json('provId', list_a)
    provCode = provId
    provName = commonFuc().analysis_json('provName', list_a)
    hosGoodsId = commonFuc().analysis_json('hosGoodsId', list_a)
    hosGoodsCode = commonFuc().analysis_json('hosGoodsCode', list_a)

    subProvId = commonFuc().analysis_json('subProvId', list_a)
    subProvName = commonFuc().analysis_json('subProvName', list_a)
    goodsName = commonFuc().analysis_json('goodsName', list_a)
    goodsSpec= commonFuc().analysis_json('goodsSpec', list_a)
    goodsMfrsId= commonFuc().analysis_json('goodsMfrsId', list_a)
    goodsMfrsName= commonFuc().analysis_json('goodsMfrsName', list_a)
    unit= commonFuc().analysis_json('unit', list_a)
    price= commonFuc().analysis_json('price', list_a)
    request_body = commonFuc().get_business_data(module, "payload24", stockAreaId, expireDate, operDate, provId,
                                                 provCode, provName, hosGoodsId, hosGoodsCode, provId, subProvId,
                                                 subProvName, goodsName,goodsSpec,goodsMfrsId,goodsMfrsName,unit,price)
    result = commonFuc().http_post(url, request_body, headers)
    print('跟台手术申请提交result', result)

    # 跟台手术申请提交============

def second_dept_in_warehouse():
    updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
    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,
                                                                             "dept_stock_in_page_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, "payload28", get_branch_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,
                                                                             "dept_stockUser_queryStockShelfList_url")
    print('科室入库页面获取单据详细信息,url', url)
    request_body = commonFuc().get_business_data(module, "payload29", get_branch_id(), get_hosid())
    print('request_body', '跟台手术申请', request_body)

# 室入库页面获取库区信息=================================================

    #科室入库页面获取单据详细信息=================================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_in_detail_url")
    print('科室入库页面获取单据详细信息,url', url)
    request_body = commonFuc().get_business_data(module, "payload29", get_branch_id(), get_hosid())
    print('request_body', '跟台手术申请', request_body)

    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
#科室入库页面获取单据详细信息=========

try:
    # set_dept_lead()#设置科室负责人
    # # # # # 设置默认科室,切换到二级科室,使用科室管理功能
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(2))#设置登录账号默认权限是二级库
    # herpService_mcmsDeptGoodsInfo_getByProvIdPage()
    # operationApply()#手术跟台申请单提交
    # approval_center()#审批手术跟台申请单
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(1))#设置登录账号默认权限是中心库
    # approval_center1()#再次审批手术跟台申请单
    # # # #内网查询手术通知单号
    # updateUserLoginDefaultRange(get_listUserMgrRangePage(1))#设置登录账号默认权限是中心库
    # orderno=Search_OrderNO()#手术通知单接口查询单号
    # print(orderno)
    # height_value_order_dp(orderno)#高值跟台外网配送
    # check_accept_order()#内网验收
    second_dept_in_warehouse()#二级科室入库
    commonFuc().check_text_exist_result_text('succees', 'succees')
except:
    commonFuc().check_text_exist_result_text('error', '1')
