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

from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from air_case.public1.public1.public1 import *
from common.db.sql.sql_del_branch_info import delData

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_b8流程十五退库业务_三级库向二级库退库,id2302,id2302-18,sit,on
核心业务流:a_b8流程十五退库业务_三级库向二级库退库
python runner_test.py tag id2302-18 debug mdm3
"""


def second_dept_return_Out_Stock(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"))

    if type == 2:
        udi_code = get_udi3(2)  # 获取条码 低值1 高值2 试剂3

        # 高值退库出库
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "herpService_stock_pick_scan_url")
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        stockId = commonFuc().analysis_json('stockId', list_a)
        stockName = commonFuc().analysis_json('stockName', list_a)
        areaCode = commonFuc().analysis_json('areaCode', list_a)
        areaName = commonFuc().analysis_json('areaName', list_a)
        deptId3= FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid3"]
        targetDeptId= FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        deptName = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        targetStockId =get_stockId(deptName+'库房',2)

        request_body = commonFuc().get_business_data(module, "payload65", udi_code, stockId, deptId3,areaCode, areaName,
                                                     targetStockId,targetDeptId)
        print('request_body',request_body)
        result = commonFuc().http_post(url, request_body, headers)
        print('退库出库扫码result',result)
        # 获取必要信息
        deptid2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]

        data1 = commonFuc().analysis_json('data', result)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        UTid = commonFuc().analysis_json('id', data1)
        createTime = commonFuc().analysis_json('id', data1)
        stockId = commonFuc().analysis_json('stockId', data1)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        areaName = commonFuc().analysis_json('areaName', data1)
        shelfCode = commonFuc().analysis_json('shelfCode', data1)
        codeType = commonFuc().analysis_json('codeType', data1)
        tagType = commonFuc().analysis_json('tagType', data1)
        hosGoodsId = commonFuc().analysis_json('hosGoodsId', data1)
        mdmGoodsCode = commonFuc().analysis_json('mdmGoodsCode', data1)
        goodsCode = commonFuc().analysis_json('goodsCode', data1)
        goodsDi = commonFuc().analysis_json('goodsDi', data1)
        barName = commonFuc().analysis_json('barName', data1)
        batchCode = commonFuc().analysis_json('batchCode', data1)
        expdtDate = commonFuc().analysis_json('expdtDate', data1)
        productDate = commonFuc().analysis_json('productDate', data1)
        pkgCode = commonFuc().analysis_json('pkgCode', data1)
        pkgCodeSee = commonFuc().analysis_json('pkgCodeSee', data1)
        ssccCode = commonFuc().analysis_json('ssccCode', data1)
        tbStatus = commonFuc().analysis_json('tbStatus', data1)
        ext = commonFuc().analysis_json('ext', data1)
        distrDetailId = commonFuc().analysis_json('distrDetailId', ext)
        distrBillId = commonFuc().analysis_json('distrBillId', ext)
        purBillId = commonFuc().analysis_json('purBillId', ext)
        pkgDefQty = commonFuc().analysis_json('pkgDefQty', ext)
        pkgDefId = commonFuc().analysis_json('pkgDefId', ext)
        pkgDefName = commonFuc().analysis_json('pkgDefName', ext)
        purMode = commonFuc().analysis_json('purMode', data1)
        goodsMfrsName = commonFuc().analysis_json('goodsMfrsName', data1)
        goodsSpec = commonFuc().analysis_json('goodsSpec', data1)
        unit = commonFuc().analysis_json('unit', data1)
        goodsName = commonFuc().analysis_json('goodsName', data1)
        pkgDefId = commonFuc().analysis_json('pkgDefId', data1)
        # pkgDefQty=commonFuc().analysis_json('pkgDefQty',data1)
        pkgDefName = commonFuc().analysis_json('pkgDefName', data1)
        printNum = commonFuc().analysis_json('printNum', data1)
        made = commonFuc().analysis_json('made', data1)
        hosGoodsCode = commonFuc().analysis_json('hosGoodsCode', data1)
        sourceId = commonFuc().analysis_json('sourceId', data1)
        price = commonFuc().analysis_json('price', data1)
        purModeText = commonFuc().analysis_json('purModeText', data1)
        version = commonFuc().analysis_json('version', data1)
        unitName = commonFuc().analysis_json('unitName', data1)
        # ==============================获取条码信息
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        extInfo = commonFuc().analysis_json('extInfo', list_a)
        print('extInfo', extInfo)
        # inStockTime = commonFuc().analysis_json('inStockTime', extInfo)
        inStockTime = reGetString(extInfo, r'inStockTime": "', r'",')
        print('inStockTime', inStockTime)
        # 从message18文件中获取信息
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "dept_stock_out_submit_url")


        deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
        deptName2 = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        stockName2 = deptName2 + "库房"
        createTime = timeUtils().get_time_hms(1)
        request_body = "request_body"
        mgrCorpName = get_hosname()
        mgrRangeId = deptid2
        mgrRangeName = deptName2
        extStr1 = get_branch_name()
        extStr3 = get_branch_name()
        snCode = UTid
        userLoginCode = username
        cname = username
        orgCode1 = get_orgCode()
        orgName1 = get_orgName()
        # print(orgCode1, orgName1)
        # import pdb
        # pdb.set_trace()

        request_body = commonFuc().get_business_data(module, "payload50_2_3", stockId, deptId3, areaCode,
                                                     areaName,targetStockId,targetDeptId,
                                                     uxid, userLoginCode, cname, get_hosid(), get_branch_id(),
                                                     deptid1,
                                                     orgCode1, orgName1, get_hosid(), get_hosid(), mgrCorpName,
                                                     get_branch_id(),
                                                     get_branch_name(), mgrRangeId, mgrRangeName, extStr1, extStr3,
                                                     get_hosid(), stockId,deptId3,
                                                     areaCode, areaName, targetStockId, targetDeptId, UTid,createTime,snCode,
                                                     get_hosid(), stockId, areaCode, areaName, shelfCode,
                                                     codeType, hosGoodsId, mdmGoodsCode, goodsCode, goodsDi,
                                                     barName,
                                                     batchCode, expdtDate, productDate, pkgCode, pkgCodeSee,
                                                     ssccCode,
                                                     tbStatus, purBillId,
                                                     distrBillId,
                                                     inStockTime, distrDetailId, purMode, price, goodsMfrsName,
                                                     goodsSpec, unit, goodsName,made, unitName,
                                                     hosGoodsCode, purModeText)
        print('request_body', '高值退库出库', request_body)
        # import pdb
        # pdb.set_trace()
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
    elif type == 1:
        print('低值耗材或者试剂退库出库')
        udi_code = get_udi3(1)  # 获取条码 低值1 高值2 试剂3
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "herpService_stock_pick_scan_url")
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        stockId = commonFuc().analysis_json('stockId', list_a)
        stockName = commonFuc().analysis_json('stockName', list_a)
        areaCode = commonFuc().analysis_json('areaCode', list_a)
        areaName = commonFuc().analysis_json('areaName', list_a)
        deptId3 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid3"]
        targetDeptId = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        deptName = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        targetStockId = get_stockId(deptName + '库房', 2)

        request_body = commonFuc().get_business_data(module, "payload65", udi_code, stockId, deptId3, areaCode,
                                                     areaName,
                                                     targetStockId, targetDeptId)
        print('request_body', request_body)
        result = commonFuc().http_post(url, request_body, headers)
        print('退库出库扫码result', result)

        # 获取必要信息
        deptid2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        data1 = commonFuc().analysis_json('data', result)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        UTid = commonFuc().analysis_json('id', data1)
        createTime = commonFuc().analysis_json('id', data1)
        stockId = commonFuc().analysis_json('stockId', data1)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        areaName = commonFuc().analysis_json('areaName', data1)
        shelfCode = commonFuc().analysis_json('shelfCode', data1)
        codeType = commonFuc().analysis_json('codeType', data1)
        tagType = commonFuc().analysis_json('tagType', data1)
        hosGoodsId = commonFuc().analysis_json('hosGoodsId', data1)
        mdmGoodsCode = commonFuc().analysis_json('mdmGoodsCode', data1)
        goodsCode = commonFuc().analysis_json('goodsCode', data1)
        goodsDi = commonFuc().analysis_json('goodsDi', data1)
        barName = commonFuc().analysis_json('barName', data1)
        batchCode = commonFuc().analysis_json('batchCode', data1)
        expdtDate = commonFuc().analysis_json('expdtDate', data1)
        productDate = commonFuc().analysis_json('productDate', data1)
        pkgCode = commonFuc().analysis_json('pkgCode', data1)
        pkgCodeSee = commonFuc().analysis_json('pkgCodeSee', data1)
        ssccCode = commonFuc().analysis_json('ssccCode', data1)
        tbStatus = commonFuc().analysis_json('tbStatus', data1)
        ext = commonFuc().analysis_json('ext', data1)
        distrDetailId = commonFuc().analysis_json('distrDetailId', ext)
        distrBillId = commonFuc().analysis_json('distrBillId', ext)
        purBillId = commonFuc().analysis_json('purBillId', ext)
        pkgDefQty = commonFuc().analysis_json('pkgDefQty', ext)
        pkgDefId = commonFuc().analysis_json('pkgDefId', ext)
        pkgDefName = commonFuc().analysis_json('pkgDefName', ext)
        purMode = commonFuc().analysis_json('purMode', data1)
        goodsMfrsName = commonFuc().analysis_json('goodsMfrsName', data1)
        goodsSpec = commonFuc().analysis_json('goodsSpec', data1)
        unit = commonFuc().analysis_json('unit', data1)
        goodsName = commonFuc().analysis_json('goodsName', data1)
        pkgDefId = commonFuc().analysis_json('pkgDefId', data1)
        # pkgDefQty=commonFuc().analysis_json('pkgDefQty',data1)
        pkgDefName = commonFuc().analysis_json('pkgDefName', data1)
        printNum = commonFuc().analysis_json('printNum', data1)
        made = commonFuc().analysis_json('made', data1)
        hosGoodsCode = commonFuc().analysis_json('hosGoodsCode', data1)
        sourceId = commonFuc().analysis_json('sourceId', data1)
        price = commonFuc().analysis_json('price', data1)
        purModeText = commonFuc().analysis_json('purModeText', data1)
        version = commonFuc().analysis_json('version', data1)
        unitName = commonFuc().analysis_json('unitName', data1)
        # ==============================获取条码信息
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        extInfo = commonFuc().analysis_json('extInfo', list_a)
        print('extInfo', extInfo)
        # inStockTime = commonFuc().analysis_json('inStockTime', extInfo)
        inStockTime = reGetString(extInfo, 'inStockTime\"\: \"', '\"\, \"distrDetailId')
        print('inStockTime', inStockTime)
        # 从message18文件中获取信息
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "dept_stock_out_submit_url")

        deptid2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
        deptName2 = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        stockName2 = deptName2 + "库房"
        createTime = timeUtils().get_time_hms(1)
        request_body = "request_body"
        mgrCorpName = get_hosname()
        mgrRangeId = deptid2
        mgrRangeName = deptName2
        extStr1 = get_branch_name()
        extStr3 = get_branch_name()
        snCode = UTid
        userLoginCode = username
        cname = username
        orgCode1 = get_orgCode()
        orgName1 = get_orgName()

        request_body = commonFuc().get_business_data(module, "payload50_2_4", stockId, deptId3, areaCode,
                                                     areaName,targetStockId,targetDeptId,
                                                     uxid, userLoginCode, cname, get_hosid(), get_branch_id(),
                                                     deptid1,
                                                     orgCode1, orgName1, get_hosid(), get_hosid(), mgrCorpName,
                                                     get_branch_id(),
                                                     get_branch_name(), mgrRangeId, mgrRangeName, extStr1, extStr3,
                                                     get_hosid(), stockId,deptId3,
                                                     areaCode, areaName, targetStockId, targetDeptId, UTid,createTime,snCode,
                                                     get_hosid(), stockId, areaCode, areaName, shelfCode,
                                                     codeType, hosGoodsId, mdmGoodsCode, goodsCode, goodsDi,
                                                     barName,
                                                     batchCode, expdtDate, productDate, pkgCode, pkgCodeSee,
                                                     ssccCode,
                                                     tbStatus, pkgDefId,purBillId,
                                                     distrBillId,
                                                     inStockTime, distrDetailId,purMode, price, goodsMfrsName,
                                                     goodsSpec, unit, goodsName,pkgDefId,pkgDefName,made, unitName,
                                                     hosGoodsCode, purModeText)

        print('request_body', request_body)
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('低值产品退库出库，result', result)
    elif type == 3:
        print('试剂产品退库出库')
        udi_code = get_udi3(3)  # 获取条码 低值1 高值2 试剂3
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "herpService_stock_pick_scan_url")
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        stockId = commonFuc().analysis_json('stockId', list_a)
        stockName = commonFuc().analysis_json('stockName', list_a)
        areaCode = commonFuc().analysis_json('areaCode', list_a)
        areaName = commonFuc().analysis_json('areaName', list_a)
        deptId3 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid3"]
        targetDeptId = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        deptName = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        targetStockId = get_stockId(deptName + '库房', 2)

        request_body = commonFuc().get_business_data(module, "payload65", udi_code, stockId, deptId3, areaCode,
                                                     areaName,
                                                     targetStockId, targetDeptId)
        print('request_body', request_body)
        result = commonFuc().http_post(url, request_body, headers)
        print('退库出库扫码result', result)

        # 获取必要信息
        deptid2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        data1 = commonFuc().analysis_json('data', result)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        UTid = commonFuc().analysis_json('id', data1)
        createTime = commonFuc().analysis_json('id', data1)
        stockId = commonFuc().analysis_json('stockId', data1)
        areaCode = commonFuc().analysis_json('areaCode', data1)
        areaName = commonFuc().analysis_json('areaName', data1)
        shelfCode = commonFuc().analysis_json('shelfCode', data1)
        codeType = commonFuc().analysis_json('codeType', data1)
        tagType = commonFuc().analysis_json('tagType', data1)
        hosGoodsId = commonFuc().analysis_json('hosGoodsId', data1)
        mdmGoodsCode = commonFuc().analysis_json('mdmGoodsCode', data1)
        goodsCode = commonFuc().analysis_json('goodsCode', data1)
        goodsDi = commonFuc().analysis_json('goodsDi', data1)
        barName = commonFuc().analysis_json('barName', data1)
        batchCode = commonFuc().analysis_json('batchCode', data1)
        expdtDate = commonFuc().analysis_json('expdtDate', data1)
        productDate = commonFuc().analysis_json('productDate', data1)
        pkgCode = commonFuc().analysis_json('pkgCode', data1)
        pkgCodeSee = commonFuc().analysis_json('pkgCodeSee', data1)
        ssccCode = commonFuc().analysis_json('ssccCode', data1)
        tbStatus = commonFuc().analysis_json('tbStatus', data1)
        ext = commonFuc().analysis_json('ext', data1)
        distrDetailId = commonFuc().analysis_json('distrDetailId', ext)
        distrBillId = commonFuc().analysis_json('distrBillId', ext)
        purBillId = commonFuc().analysis_json('purBillId', ext)
        pkgDefQty = commonFuc().analysis_json('pkgDefQty', ext)
        pkgDefId = commonFuc().analysis_json('pkgDefId', ext)
        pkgDefName = commonFuc().analysis_json('pkgDefName', ext)
        purMode = commonFuc().analysis_json('purMode', data1)
        goodsMfrsName = commonFuc().analysis_json('goodsMfrsName', data1)
        goodsSpec = commonFuc().analysis_json('goodsSpec', data1)
        unit = commonFuc().analysis_json('unit', data1)
        goodsName = commonFuc().analysis_json('goodsName', data1)
        pkgDefId = commonFuc().analysis_json('pkgDefId', data1)
        # pkgDefQty=commonFuc().analysis_json('pkgDefQty',data1)
        pkgDefName = commonFuc().analysis_json('pkgDefName', data1)
        printNum = commonFuc().analysis_json('printNum', data1)
        made = commonFuc().analysis_json('made', data1)
        hosGoodsCode = commonFuc().analysis_json('hosGoodsCode', data1)
        sourceId = commonFuc().analysis_json('sourceId', data1)
        price = commonFuc().analysis_json('price', data1)
        purModeText = commonFuc().analysis_json('purModeText', data1)
        version = commonFuc().analysis_json('version', data1)
        unitName = commonFuc().analysis_json('unitName', data1)
        # ==============================获取条码信息
        # 从message18文件中获取信息
        info = FileUtils().r_info8(module, "条码信息", 'message18')
        # print('info', info)
        list_a = info['list_a']
        extInfo = commonFuc().analysis_json('extInfo', list_a)
        print('extInfo', extInfo)
        # inStockTime = commonFuc().analysis_json('inStockTime', extInfo)
        inStockTime = reGetString(extInfo, 'inStockTime\"\: \"', '\"\, \"distrDetailId')
        print('inStockTime', inStockTime)
        # 从message18文件中获取信息
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module,
                                                                                 "dept_stock_out_submit_url")

        deptid2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        deptid1 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
        deptName2 = FileUtils().r_info8('b2_herp3_bs', '所有科室name', 'message6')["deptid2"]
        stockName2 = deptName2 + "库房"
        createTime = timeUtils().get_time_hms(1)
        request_body = "request_body"
        mgrCorpName = get_hosname()
        mgrRangeId = deptid2
        mgrRangeName = deptName2
        extStr1 = get_branch_name()
        extStr3 = get_branch_name()
        snCode = UTid
        userLoginCode = username
        cname = username
        orgCode1 = get_orgCode()
        orgName1 = get_orgName()

        request_body = commonFuc().get_business_data(module, "payload50_2_4", stockId, deptId3, areaCode,
                                                     areaName, targetStockId, targetDeptId,
                                                     uxid, userLoginCode, cname, get_hosid(), get_branch_id(),
                                                     deptid1,
                                                     orgCode1, orgName1, get_hosid(), get_hosid(), mgrCorpName,
                                                     get_branch_id(),
                                                     get_branch_name(), mgrRangeId, mgrRangeName, extStr1, extStr3,
                                                     get_hosid(), stockId, deptId3,
                                                     areaCode, areaName, targetStockId, targetDeptId, UTid, createTime,
                                                     snCode,
                                                     get_hosid(), stockId, areaCode, areaName, shelfCode,
                                                     codeType, hosGoodsId, mdmGoodsCode, goodsCode, goodsDi,
                                                     barName,
                                                     batchCode, expdtDate, productDate, pkgCode, pkgCodeSee,
                                                     ssccCode,
                                                     tbStatus, pkgDefId, purBillId,
                                                     distrBillId,
                                                     inStockTime, distrDetailId, purMode, price, goodsMfrsName,
                                                     goodsSpec, unit, goodsName, pkgDefId, pkgDefName, made, unitName,
                                                     hosGoodsCode, purModeText)

        print('request_body', request_body)
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('试剂产品退库出库，result', result)

def main():
    try:
        updateUserLoginDefaultRange(get_listUserMgrRangePage1(3))  # 设置登录账号默认权限是三级库
        second_dept_return_Out_Stock(2)  # 高值耗材退货出库
        second_dept_return_Out_Stock(1)  # 低值耗材退货出库
        second_dept_return_Out_Stock(3)  # 试剂耗材退货出库
        second_dept_one_key_in(2)
        second_dept_one_key_in(2)
        second_dept_one_key_in(2)
        commonFuc().check_text_exist_result_text('succees', 'succees')
        print('================流程十五执行完毕===================')

    except:
        commonFuc().check_text_exist_result_text('error', '1')
# main()