# -*- 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_b2流程九请领业务流_二级库向中心库,id2302,id2302-12,sit,on
核心业务流:a_b2流程九请领业务流_二级库向中心库
python runner_test.py tag id2302-12 debug mdm3
"""


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)
    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)
    # 信息写入文件
    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, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, purMode, goodsMfrsName, 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, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, purMode, 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, goodsName, goodsSpec,
                                                     mdmGoodsCode,
                                                     goodsCode, unit, purMode, goodsMfrsName, 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(deptid2, 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))
    # 创建领车交易=================================================
    # 一键审批=================================================
    # url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
    #                                                                          "herpService_approvalBatch_url")
    # print('一键审批=================', url)
    #
    # request_body = commonFuc().get_business_data(module, "payload45", currentTaskList)
    #
    # print('一键审批', 'request_body', request_body)
    # result = commonFuc().http_post(url, request_body, headers)
    # print('一键审批,result', result)
    # 一键审批=================================================
def centeStock_pick():
    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"))
    # 生成拣货单列表查询===============================================
    url1 = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_BuyPick_buyPage_url")
    print('生成拣货单列表查询=================', url1)
    request_body1 = commonFuc().get_business_data(module, "payload46")

    print('生成拣货单列表查询', 'request_body', request_body1)
    result1 = commonFuc().http_post(url1, request_body1, headers)
    print('生成拣货单列表查询,result1', result1)
    total1=commonFuc().analysis_json('total',commonFuc().analysis_json('data',result1))
    Bh_id=commonFuc().analysis_json('id',commonFuc().analysis_json('data',commonFuc().analysis_json('data',result1)))
    # 生成拣货单列表查询===============================================
    #生成拣货单===============================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_BuyPick_savePick_url")
    print('生成拣货单=================', url)
    request_body = commonFuc().get_business_data(module, "payload47",Bh_id)

    print('生成拣货单', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('生成拣货单,result', result)
    while True:
        result = commonFuc().http_post(url, request_body, headers)
        remark=commonFuc().analysis_json('remark',commonFuc().analysis_json('data',result))
        time.sleep(3)
        if remark==None:
            break
    #生成拣货单===============================================
    while True:
        result1 = commonFuc().http_post(url1, request_body1, headers)
        total2 = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result1))
        if total1-total2==1:
            print('==================生成拣货单完成============================')
            break

def get_centeStock_pick_list_counts():
    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"))
    url1 = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                              "herpService_BuyPick_buyPage_url")
    print('生成拣货单列表查询=================', url1)
    request_body1 = commonFuc().get_business_data(module, "payload46")

    print('生成拣货单列表查询', 'request_body', request_body1)
    result1 = commonFuc().http_post(url1, request_body1, headers)
    print('生成拣货单列表查询,result1', result1)
    total1 = commonFuc().analysis_json('total', commonFuc().analysis_json('data', result1))
    return total1
def pick_execute(GoodsId,type=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等信息，使用创建的用户登录============结束
    # 生成拣货单列表查询===============================================
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))
    url1 = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                              "herpService_BuyPick_pickPage_url")
    print('拣货执行列表页面=================', url1)
    request_body1 = commonFuc().get_business_data(module, "payload48",GoodsId)

    print('拣货执行列表页面', 'request_body', request_body1)
    result1 = commonFuc().http_post(url1, request_body1, headers)
    print('拣货执行列表页面,result1', result1)
    #获取id
    PICK_ID=commonFuc().analysis_json('id',commonFuc().analysis_json('data',commonFuc().analysis_json('data',result1)))
    version=commonFuc().analysis_json('version',commonFuc().analysis_json('data',commonFuc().analysis_json('data',result1)))
 # 生成拣货单列表查询===============================================
 # 生成拣货单列表锁定===============================================

    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                              "herpService_BuyPick_updateLock_url")
    print('生成拣货单列表锁定=================', url)
    # version=int(version)+1
    request_body = commonFuc().get_business_data(module, "payload49", PICK_ID,version)

    print('生成拣货单列表锁定', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('生成拣货单列表锁定,result1', result)
 # 生成拣货单列表锁定===============================================
 #拣货==============================
    if type==1:
        udi_code=get_udi1(1) #获取条码 低值1 高值2 试剂3
    elif type==2:
        udi_code = get_udi1(2)  # 获取条码 低值1 高值2 试剂3
    elif type==3:
        udi_code = get_udi1(3)  # 获取条码 低值1 高值2 试剂3
    else:
        udi_code='udi_code'
    print('udi_code',udi_code)
    pickScan(PICK_ID,udi_code,type)
    #提交拣货执行单
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_BuyPick_updateStatus_url")
    url=url+PICK_ID
    print('提交拣货执行单=================', url)
    request_body = commonFuc().get_business_data(module, "payload51",PICK_ID)
    print('提交拣货执行单', 'request_body', request_body)
    result = commonFuc().http_post(url, request_body, headers)
    print('提交拣货执行单,result1', result)

 #拣货===================================
 #复核===================================
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_hdiBarcode_scan_url")
    url = url + udi_code
    print('复核=================', url)
    result = commonFuc().http_get(url, headers)
    print('复核,result1', result)
    result = commonFuc().http_get(url, headers)
    print('扫码复核result', result)
    data = commonFuc().analysis_json('data', result)
    UT_id = commonFuc().analysis_json('id', data)
    createTime = commonFuc().analysis_json('createTime', data)
    snCode = commonFuc().analysis_json('snCode', data)
    tagType = commonFuc().analysis_json('tagType', data)
    goodsDi = commonFuc().analysis_json('goodsDi', data)
    hosId= commonFuc().analysis_json('hosId', data)
    stockId= commonFuc().analysis_json('stockId', data)
    areaCode= commonFuc().analysis_json('areaCode', data)
    areaName= commonFuc().analysis_json('areaName', data)
    shelfCode= commonFuc().analysis_json('shelfCode', data)
    codeType= commonFuc().analysis_json('codeType', data)
    mdmGoodsCode= commonFuc().analysis_json('mdmGoodsCode', data)
    goodsCode= commonFuc().analysis_json('goodsCode', data)
    barName= commonFuc().analysis_json('barName', data)
    batchCode= commonFuc().analysis_json('batchCode', data)
    expdtDate= commonFuc().analysis_json('expdtDate', data)
    productDate= commonFuc().analysis_json('productDate', data)
    pkgCode= commonFuc().analysis_json('pkgCode', data)
    pkgCodeSee= commonFuc().analysis_json('pkgCodeSee', data)
    ssccCode= commonFuc().analysis_json('ssccCode', data)
    tbStatus= commonFuc().analysis_json('tbStatus', data)
    purMode= commonFuc().analysis_json('purMode', data)
    goodsMfrsName= commonFuc().analysis_json('goodsMfrsName', data)
    goodsSpec= commonFuc().analysis_json('goodsSpec', data)
    unit= commonFuc().analysis_json('unit', data)
    goodsName= commonFuc().analysis_json('goodsName', data)
    made= commonFuc().analysis_json('made', data)
    hosGoodsCode= commonFuc().analysis_json('hosGoodsCode', data)
    distrBillId = commonFuc().analysis_json('distrBillId', commonFuc().analysis_json('ext', data))
    purBillId = commonFuc().analysis_json('purBillId', commonFuc().analysis_json('ext', data))
    distrDetailId = commonFuc().analysis_json('distrDetailId', commonFuc().analysis_json('ext', data))
    pkgDefId = commonFuc().analysis_json('pkgDefId', commonFuc().analysis_json('ext', data))
    pkgDefQty = commonFuc().analysis_json('pkgDefQty', commonFuc().analysis_json('ext', data))
    pkgDefName =commonFuc().analysis_json('pkgDefName', commonFuc().analysis_json('ext', data))
    print(UT_id)
    # ========================扫码复核
    # ========出库复核提交==========
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_stock_review_pass_submit_url")

    print('url', url)
    # sourceId = ' '.join(PICK_ID)
    sourceId = PICK_ID
    # 请求体
    request_body = commonFuc().get_business_data(module, "payload52", UT_id, createTime, snCode, hosId, stockId,
                                                 areaCode, areaName, shelfCode, codeType, tagType, hosGoodsId,
                                                 mdmGoodsCode, goodsCode, goodsDi, barName, batchCode, expdtDate,
                                                 productDate, pkgCode, pkgCodeSee, ssccCode, tbStatus, distrDetailId,
                                                 distrBillId, purBillId, pkgDefQty, pkgDefId, pkgDefName, purMode,
                                                 goodsMfrsName, goodsSpec, unit, goodsName, pkgDefId, pkgDefQty,
                                                 pkgDefName, made, hosGoodsCode, sourceId)
    print('request_body', request_body)
    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('herpService_stock_review_pass_submit_urlresult', result)
 #复核===================================
try:
    updateUserLoginDefaultRange(get_listUserMgrRangePage(2))  # 设置登录账号默认权限是二级库
    # 二级库区信息写入文件
    get_stockAreaId_new(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2'],
                        FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2'], 2)
    # get_stockAreaId_new(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid3'],FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid3'],3)
    # 设置库区联系人
    set_stockShelf_Linkman()
    # 低值

    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()
    updateUserLoginDefaultRange(get_listUserMgrRangePage(1))  #设置登录账号默认权限是中心库
    #获取拣货单列表个数
    for i in range(get_centeStock_pick_list_counts()):
        centeStock_pick()#中心库拣货
    #低值拣货执行
    hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message2')['hosGoodsId']
    GoodsId = hosGoodsId.replace(get_hosid(), '')
    pick_execute(GoodsId,1)
    # 高值拣货执行
    hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message1')['hosGoodsId']
    GoodsId = hosGoodsId.replace(get_hosid(), '')
    pick_execute(GoodsId,2)
    # 试剂拣货执行
    hosGoodsId = FileUtils().r_info8("b5_spd3_core_business_flow", "产品审核信息", 'message3')['hosGoodsId']
    GoodsId = hosGoodsId.replace(get_hosid(), '')
    pick_execute(GoodsId, 3)
    second_dept_one_key_in()
    second_dept_one_key_in()
    second_dept_one_key_in()
    commonFuc().check_text_exist_result_text('succees', 'succees')
except:
    commonFuc().check_text_exist_result_text('error', '1')
