# -*- 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_core,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)
    deptid = commonFuc().analysis_json('deptId',
                                       commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    sourceId = commonFuc().analysis_json('sourceId',
                                         commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    sourceType = commonFuc().analysis_json('sourceType',
                                           commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    billMode = commonFuc().analysis_json('billMode',
                                         commonFuc().analysis_json('data', commonFuc().analysis_json('data', result)))
    deptName = commonFuc().analysis_json('deptName',
                                         commonFuc().analysis_json('data', commonFuc().analysis_json('data', 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", deptid, uxid)
    print('request_body', '科室入库页面获取库区信息', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('科室入库页面获取库区信息result', result)
    data = commonFuc().analysis_json('data', result)
    areaCode = commonFuc().analysis_json('areaCode', data)
    areaName = commonFuc().analysis_json('areaName', data)
    # 科室入库页面获取库区信息=================================================

    # 科室入库页面获取单据详细信息=================================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_in_detail_url")
    print('科室入库页面获取单据详细信息,url', url)
    targetAreaCode = areaCode
    request_body = commonFuc().get_business_data(module, "payload30", deptid, targetAreaCode, get_hosid(), sourceId,
                                                 sourceType, billMode)
    print('request_body', '科室入库页面获取单据详细信息', request_body)

    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    data = commonFuc().analysis_json('data', result)
    viewId = commonFuc().analysis_json('viewId', data)
    shelfCode = commonFuc().analysis_json('shelfCode', data)
    # 科室入库页面获取单据详细信息====================================================
    # 一键入库====================================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_in_submit_url")
    print('一键入库,url', url)
    targetAreaCode = areaCode
    targetAreaName = areaName
    request_body = commonFuc().get_business_data(module, "payload31", viewId, shelfCode, sourceId,
                                                 sourceType, deptid, deptName, targetAreaCode, targetAreaName)
    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_stock_barcode_page_url")
    print('条码库存查询,url', url)

    request_body = commonFuc().get_business_data(module, "payload32", get_branch_id(), deptid)
    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))
    data1 = []
    for i in data:
        lockStatus = commonFuc().analysis_json('lockStatus', i)
        if lockStatus == 0:
            data1 = i
            break
    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, 'message20')
    # # 将二级库库存条码信息写入文件=================================================
    # 跟台消耗提交================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_out_submit_url")
    print('跟台消耗提交,url', url)
    chargeDeptId = deptid
    operDate = timeUtils().get_time_add(3, 2)
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "二级库条码信息", 'message20')
    list_a = info['list_a']
    stockId = commonFuc().analysis_json('stockId', list_a)
    sourceType = 'CK_XH'
    pickType = 'KS_XH'
    barCodeList_id = commonFuc().analysis_json('id', list_a)
    hosGoodsId = commonFuc().analysis_json('hosGoodsId', list_a)
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "配送订单详细信息2", 'message9')
    list22 = info['list_a']
    mdmGoodsCode = commonFuc().analysis_json('spdGoodsCode', list22)
    goodsCode = commonFuc().analysis_json('goodsCode', list_a)
    batchCode = commonFuc().analysis_json('batchCode', list_a)
    productDate = commonFuc().analysis_json('productDate', list_a)
    expdtDate = commonFuc().analysis_json('expdtDate', list_a)
    purMode = commonFuc().analysis_json('purMode', list_a)
    pkgCodeSee = commonFuc().analysis_json('pkgCodeSee', list_a)
    recDeptName = commonFuc().analysis_json('recDeptName', list_a)
    request_body = commonFuc().get_business_data(module, "payload33", areaCode, areaName, deptid, stockId, get_hosid(),
                                                 sourceType, pickType, chargeDeptId, operDate, pickType, areaCode,
                                                 areaName, deptid, stockId, get_hosid(), get_branch_id(),
                                                 barCodeList_id, hosGoodsId, mdmGoodsCode, goodsCode, batchCode,
                                                 productDate, expdtDate, purMode, shelfCode)
    print('request_body', '跟台消耗提交', request_body)

    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('跟台消耗提交,result', result)
    DRh_id = ' '.join(commonFuc().analysis_json('data', result))
    # 跟台消耗提交================================
    # 跟台消耗扫条码== == == == == == == == == == == == == == == ==

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_scan_url")
    print('跟台消耗扫条码,url', url)
    stockName = recDeptName
    request_body = commonFuc().get_business_data(module, "payload34", pkgCodeSee, stockId, stockName,
                                                 areaCode, areaName)
    print('跟台消耗扫条码', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('跟台消耗扫条码,result', result)
    viewId2 = commonFuc().analysis_json('id', commonFuc().analysis_json('data', result))
    # 跟台消耗扫条码 == == == == == == == == == == == == == == == ==
    # 跟台消耗退回 == == == == == == == == == == == == == == == ==
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "dept_stock_rtn_submit_url")
    print('跟台消耗退回,url', url)
    billMode = '20'
    request_body = commonFuc().get_business_data(module, "payload35", viewId2, hosGoodsId, DRh_id,
                                                 barCodeList_id, billMode, deptid, targetAreaCode, targetAreaName)
    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_herpService_operationConfirm_submit_url")
    print('台使用确认——跟台退货,url', url)
    info = FileUtils().r_info8('b5_spd3_core_business_flow', "二级库条码信息", 'message20')
    list_a = info['list_a']
    extInfo = commonFuc().analysis_json('extInfo', list_a)
    distrOrderNo = reGetString(extInfo, r'distrBillId": "', r'", ')
    print('distrOrderNo', distrOrderNo)
    provId = commonFuc().analysis_json('provId', list_a)
    provName = commonFuc().analysis_json('provName', list_a)
    hosGoodsCode = commonFuc().analysis_json('hosGoodsCode', list_a)
    goodsName = commonFuc().analysis_json('goodsName', list_a)
    goodsSpec = commonFuc().analysis_json('goodsSpec', list_a)
    unit = commonFuc().analysis_json('unit', list_a)
    qty = commonFuc().analysis_json('qty', list_a)
    request_body = commonFuc().get_business_data(module, "payload36", areaCode, areaName, viewId2, distrOrderNo, provId,
                                                 provName, pkgCodeSee, hosGoodsId, hosGoodsCode, goodsName, goodsSpec,
                                                 unit, batchCode, productDate, expdtDate, qty, stockId, shelfCode)
    print('台使用确认——跟台退货', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('台使用确认——跟台退货,result', result)

    # 跟台使用确认——跟台退货== == == == == == == == == == == == == == == ==


def out_return_gt():
    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_out_return_gt_url")
    print('跟台退货出库列表查询——中心库=================', url)
    request_body = commonFuc().get_business_data(module, "payload37", get_branch_id())
    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))
    DR_Id = 'DR_Id'
    for i in data1:
        status = commonFuc().analysis_json('status', i)
        if status == '待处理':
            DR_Id = commonFuc().analysis_json('id', i)
            break
    try:
        print('DR_Id', DR_Id)
    except:
        print('无待处理数据')
    # 跟台退货出库列表查询——中心库==================
    # 跟台退货出库确认========================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_out_return_confirm_url")
    print('#跟台退货出库确认——中心库=================', url)
    deptid1 = FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid1']
    request_body = commonFuc().get_business_data(module, "payload38", DR_Id, deptid1)
    print('跟台退货出库确认——中心库', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('跟台退货出库确认——中心库==,result', result)


# 跟台退货出库确认========================
def main():
    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()  # 内网验收
        updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
        second_dept_in_warehouse()  # 二级科室入库
        updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  # 设置登录账号默认权限是中心库
        out_return_gt()  # 跟台退货出库_中心库处理
        commonFuc().check_text_exist_result_text('succees', 'succees')
        print('================流程八结束===========================')
    except:
        commonFuc().check_text_exist_result_text('error', '1')
# main()