# -*- 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_core1,a_c1流程十八中心库业务_拆包功能,id2302,id2302-21,sit,on
核心业务流:a_c1流程十八中心库业务_拆包功能
python runner_test.py tag id2302-21 debug mdm3
"""


def center_Stock_unpack(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 == 1:
        print('低值耗材扫码发放')
        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码
        udi_code = get_udi1(1)
        # 扫码====================
        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)
        deptId2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        targetDeptId = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
        deptName = get_branch_name()
        targetStockId = get_stockId(deptName + '中心库房', 1)

        request_body = commonFuc().get_business_data(module, "payload67", udi_code, stockId, stockName, areaCode,
                                                     areaName
                                                     )
        print('herpService_stock_pick_scan,request_body', request_body)
        result = commonFuc().http_post(url, request_body, headers)
        print('扫码发放result', result)
        price = commonFuc().analysis_json('price', commonFuc().analysis_json('data', result))
        createTime = commonFuc().analysis_json('createTime', commonFuc().analysis_json('data', result))
        hosId = commonFuc().analysis_json('hosId', commonFuc().analysis_json('data', result))
        tbStatus = commonFuc().analysis_json('tbStatus', commonFuc().analysis_json('data', result))
        printNum = commonFuc().analysis_json('printNum', commonFuc().analysis_json('data', result))
        # 扫码=======================
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_centCbSubmit_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'
        pkgDefId= 'pkgDefId'
        pkgDefQty= 'pkgDefQty'
        purBillId= 'purBillId'
        pkgDefName= 'pkgDefName'
        distrBillId= 'distrBillId'
        inStockTime= 'inStockTime'
        distrDetailId= 'distrDetailId'
        purMode= 'purMode'
        goodsMfrsName= 'goodsMfrsName'
        goodsSpec= 'goodsSpec'
        unit= 'unit'
        goodsName= 'goodsName'
        made= 'made'
        unitName= 'unitName'
        hosGoodsCode= 'hosGoodsCode'
        purModeText= 'purModeText'
        for i in list_a:
            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['extInfo']
            print('ext', ext)
            distrDetailId = reGetString(ext, r'distrDetailId": "', r'", ')
            distrBillId = reGetString(ext, r'distrBillId": "', r'", ')
            purBillId = reGetString(ext, r'purBillId": "', r'", ')
            pkgDefQty = reGetString(ext, r'pkgDefQty": "', r'", ')
            pkgDefId = reGetString(ext, r'pkgDefId": "', r'", ')
            pkgDefName = reGetString(ext, r'pkgDefName": "', r'", ')
            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']
            unitName = i['unitName']
            purModeText = i['purModeText']
            extInfo = i['extInfo']
            inStockTime = reGetString(extInfo, r'inStockTime": "', r'",')
            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, "payload68", stockId, stockName, areaCode, areaName, UTid,
                                                     createTime, UTid, hosId,
                                                     stockId,
                                                     areaCode, areaName, shelfCode, codeType, hosGoodsId,
                                                     mdmGoodsCode, goodsCode, goodsDi, barName, batchCode, expdtDate,
                                                     productDate, pkgCode, pkgCodeSee, ssccCode, tbStatus, pkgDefId,
                                                     pkgDefQty,
                                                     purBillId, pkgDefName, distrBillId, inStockTime,distrDetailId,
                                                     purMode, price,
                                                     goodsMfrsName, goodsSpec, unit, goodsName, pkgDefId, pkgDefQty,
                                                     pkgDefName, printNum, made, unitName, hosGoodsCode, purModeText,tbStatus)
        print('request_body', 'ccccccc', request_body)
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        code = commonFuc().analysis_json('code', result)
        if code == 1010:
            commonFuc().check_text_exist_result_text('error', 'succees')
    elif type == 3:
        print('试剂产品扫码发放')
        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码
        udi_code = get_udi1(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)
        deptId2 = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid2"]
        targetDeptId = FileUtils().r_info8('b2_herp3_bs', '所有科室id', 'message6')["deptid1"]
        deptName = get_branch_name()
        targetStockId = get_stockId(deptName + '中心库房', 1)

        request_body = commonFuc().get_business_data(module, "payload67", udi_code, stockId, stockName, areaCode,
                                                     areaName
                                                     )
        print('request_body', request_body)
        result = commonFuc().http_post(url, request_body, headers)
        print('扫码发放result', result)
        price = commonFuc().analysis_json('price', commonFuc().analysis_json('data', result))
        createTime = commonFuc().analysis_json('createTime', commonFuc().analysis_json('data', result))
        hosId = commonFuc().analysis_json('hosId', commonFuc().analysis_json('data', result))
        tbStatus = commonFuc().analysis_json('tbStatus', commonFuc().analysis_json('data', result))
        printNum = commonFuc().analysis_json('printNum', commonFuc().analysis_json('data', result))
        # 扫码=======================
        url = commonFuc().get_api_add_port_url() + commonFuc().get_business_data(module, "herpService_centCbSubmit_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'
        pkgDefId = 'pkgDefId'
        pkgDefQty = 'pkgDefQty'
        purBillId = 'purBillId'
        pkgDefName = 'pkgDefName'
        distrBillId = 'distrBillId'
        inStockTime = 'inStockTime'
        distrDetailId = 'distrDetailId'
        purMode = 'purMode'
        goodsMfrsName = 'goodsMfrsName'
        goodsSpec = 'goodsSpec'
        unit = 'unit'
        goodsName = 'goodsName'
        made = 'made'
        unitName = 'unitName'
        hosGoodsCode = 'hosGoodsCode'
        purModeText = 'purModeText'
        for i in list_a:
            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['extInfo']
            print('ext', ext)
            distrDetailId = reGetString(ext, r'distrDetailId": "', r'", ')
            distrBillId = reGetString(ext, r'distrBillId": "', r'", ')
            purBillId = reGetString(ext, r'purBillId": "', r'", ')
            pkgDefQty = reGetString(ext, r'pkgDefQty": "', r'", ')
            pkgDefId = reGetString(ext, r'pkgDefId": "', r'", ')
            pkgDefName = reGetString(ext, r'pkgDefName": "', r'", ')
            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']
            unitName = i['unitName']
            purModeText = i['purModeText']
            extInfo = i['extInfo']
            inStockTime = reGetString(extInfo, r'inStockTime": "', r'",')
            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, "payload69", stockId, stockName, areaCode, areaName, UTid,
                                                     createTime, UTid, hosId,
                                                     stockId,
                                                     areaCode, areaName, shelfCode, codeType, hosGoodsId,
                                                     mdmGoodsCode, goodsCode, goodsDi, barName, batchCode, expdtDate,
                                                     productDate, pkgCode, pkgCodeSee, ssccCode, tbStatus, pkgDefId,
                                                     pkgDefQty,
                                                     purBillId, pkgDefName, distrBillId, inStockTime, distrDetailId,
                                                     purMode, price,
                                                     goodsMfrsName, goodsSpec, unit, goodsName, pkgDefId, pkgDefQty,
                                                     pkgDefName, printNum, made, unitName, hosGoodsCode, purModeText,tbStatus)
        print('request_body', 'ccccccc', request_body)
        # # # # 发送请求
        result = commonFuc().http_post(url, request_body, headers)
        print('result', result)
        code=commonFuc().analysis_json('code',result)
        if code==1010:
            commonFuc().check_text_exist_result_text('error', 'succees')

def main():
    try:
        updateUserLoginDefaultRange(get_listUserMgrRangePage1(1))  # 设置登录账号默认权限是二级库
        center_Stock_unpack(1)  # 低值耗材扫码发放
        center_Stock_unpack(3)  # 试剂耗材扫码发放
        commonFuc().check_text_exist_result_text('succees', 'succees')
        print('================流程十八执行完毕===================1')

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