# -*- 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,b6_spd3_core_business_flow_database_check,a_b0流程七_中心库入库结算_正负结算,id2303,id2302-10,sit,on
主数据平台:a_b0流程七_中心库入库结算_正负结算
python runner_test.py tag id2303-10 debug mdm3
"""


def test1():
    # 提交采购计划
    execute_command("python runner_test.py tag id2302-5 debug mdm3")
    # 外网配送
    execute_command("python runner_test.py tag id2302-6 debug mdm3")
    # 内网验收入库结算
    execute_command("python runner_test.py tag id2302-7 debug mdm3")


# 中心库退货
def center_warehouse_return():
    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等信息，使用创建的用户登录============结束
    # 到库房明细查询，查询产品的udi码
    get_udi(1)
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_pick_submit_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"))
    #从文件中获取必要信息
    info = FileUtils().r_info8(module, "条码信息", 'message18')
    # print('info', info)
    list_a = info['list_a']
    print('list_a', list_a[0], type(list_a), len(list_a))
    UTid = 'UT'
    areaCode = 'areaCode'
    createTime = 'createTime'
    hosId = 'hosId'
    stockId = 'stockId'
    areaCode = 'areaCode'
    areaName = 'areaName'
    shelfCode = 'shelfCode'
    codeType = 'codeType'
    tagType = 'tagType'
    hosGoodsId = 'hosGoodsId'
    mdmGoodsCode = 'mdmGoodsCode'
    goodsCode = 'goodsCode'
    goodsDi = 'goodsDi'
    barName = 'barName'
    batchCode = 'batchCode'
    expdtDate = 'expdtDate'
    productDate = 'productDate'
    pkgCode = 'pkgCode'
    pkgCodeSee = 'pkgCodeSee'
    ssccCode = 'ssccCode'
    tbStatus = 'tbStatus'
    pkgDefQty = 'pkgDefQty'
    purBillId = 'purBillId'
    distrBillId = 'distrBillId'
    inStockTime = 'inStockTime'
    distrDetailId = 'distrDetailId'
    purMode = 'purMode'
    goodsMfrsName = 'goodsMfrsName'
    goodsSpec = 'goodsSpec'
    unit = 'unit'
    goodsName = 'goodsName'
    printNum = 'printNum'
    made = 'made'
    provId = 'provId'
    provName = 'provName'
    subProvId = 'subProvId'
    subProvName = 'subProvName'
    hosGoodsCode = 'hosGoodsCode'
    subSendFlag = 'subSendFlag'
    onlyKey = 'onlyKey'
    pkgDefId = 'pkgDefId'
    pkgDefName = 'pkgDefName'
    print('aaaaaaaaaaaaaaaaaaaaaaaaa', list_a[0])
    list_b = list_a[0]
    for i in list_b:
        print(i)
        UTid = i['id']
        areaCode = i['areaCode']
        createTime = i['createTime']
        hosId = i['hosId']
        stockId = i['stockId']
        areaCode = i['areaCode']
        areaName = i['areaName']
        shelfCode = i['shelfCode']
        codeType = i['codeType']
        tagType = i['tagType']
        hosGoodsId = i['hosGoodsId']
        mdmGoodsCode = i['mdmGoodsCode']
        goodsCode = i['goodsCode']
        goodsDi = i['goodsDi']
        barName = i['barName']
        batchCode = i['batchCode']
        expdtDate = i['expdtDate']
        productDate = i['productDate']
        pkgCode = i['pkgCode']
        pkgCodeSee = i['pkgCodeSee']
        ssccCode = i['ssccCode']
        tbStatus = i['tbStatus']
        onlyKey = i['hosGoodsId']
        ext = i['ext']
        print('ext', ext)
        distrDetailId = ext['distrDetailId']
        distrBillId = ext['distrBillId']
        purBillId = ext['purBillId']
        pkgDefQty = ext['pkgDefQty']
        pkgDefId = ext['pkgDefId']
        pkgDefName = ext['pkgDefName']
        purMode = i['purMode']
        goodsMfrsName = i['goodsMfrsName']
        goodsSpec = i['goodsSpec']
        unit = i['unit']
        goodsName = i['goodsName']
        pkgDefId = i['pkgDefId']
        pkgDefQty = i['pkgDefQty']
        pkgDefName = i['pkgDefName']
        printNum = i['printNum']
        made = i['made']
        provId = i['provId']
        provName = i['provName']
        subProvId = i['subProvId']
        subProvName = i['subProvName']
        hosGoodsCode = i['hosGoodsCode']
        subSendFlag = i['subSendFlag']
        areaName = i['areaName']
        hosId = i['hosId']
        break
    deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
    deptName = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid1"]
    # # 请求体
    request_body = commonFuc().get_business_data(module, "payload19", areaCode, UTid, createTime, UTid, 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, printNum, made, provId, provName, subProvId, subProvName,
                                                 hosGoodsCode, subSendFlag, onlyKey, areaName, hosId, get_branch_id(),
                                                 deptid1, deptName, stockId)
    print('request_body', 'ccccccc', request_body)

    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    PICK_ID = commonFuc().analysis_json('data', result)
    # =====================出库复核列表查询获取pickid===============
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_pick_return_page_url")
    print('url', url)
    request_body = commonFuc().get_business_data(module, "payload20", get_branch_id(), get_hosid(), deptid1)
    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)

    # =====================出库复核列表查询获取pickid===============
    # =======================查看出库单详细信息===============
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_pick_checkReview_url")
    url = url + ' '.join(PICK_ID) + '?pickedFlag=true'
    print('url', url)
    # # # # 发送请求
    result = commonFuc().http_get(url, headers)
    print('查看出库单详细信息result', result)
    # =======================查看出库单详细信息===============
    # ========================扫码复核
    url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                             "herpService_stock_hdiBarcode_scan_url")
    url = url + pkgCode
    print('url', url)
    # # # # 发送请求
    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)
    tbStatus=commonFuc().analysis_json('tbStatus', 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)
    # 请求体
    request_body = commonFuc().get_business_data(module, "payload21", 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:
    # test1() # 提交采购计划   # 外网配送   # 内网验收入库结算
    # 先查询所有的部门信息写入文件
    dept_query_all()
    # 设置库房人员，根据传入的科室id和科室名称来设置库房
    # 中心库type=1 二级type=2三级type=3
    set_dept_user(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid2'],FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid2'],2)
    set_dept_user(FileUtils().r_info8("b2_herp3_bs", "所有科室id", 'message6')['deptid3'],FileUtils().r_info8("b2_herp3_bs", "所有科室name", 'message6')['deptid3'],3)
    center_warehouse_return()#中心库退货
    OBh_ID=settle()  # 生成结算单
    # print(OBh_ID)
    # 调用审批结算单
    approval_center(OBh_ID)
    invoice(3,OBh_ID)#type=2 传递结算单号，外网查询开票
    commonFuc().check_text_exist_result_text('succees', 'succees')
except:
    commonFuc().check_text_exist_result_text('error', '1')
