# -*- encoding=utf8 -*-
import sys

from airtest.core.helper import using

from common.db.sql.sql_del_branch_info import delData
from common.db.sql.sql_del_kind_goodsinfo import delKindGoodsinfo
from common.db.sql.sql_del_notices_info import delByNoticeName
from common.fileUtls import FileUtils
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,r_d8库房管理_库区设置_货位管理_绑定探针,id2263,id2263-241,sit,on
内网:r_d8库房管理_库区设置_货位管理_绑定探针
"""

from common.common_func import commonFuc
from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system

module = "b2_herp3_bs"


def test1(dept_id='d69a68df361a4289b2262e4ef75b55a5', stockId='c66b6ed38dbb4fbc8603dc00cdabb572'):
    """
     场景:r_d8库房管理_库区设置_货位管理_绑定探针
     用例名称：r_d8库房管理_库区设置_货位管理_绑定探针
     输入：""
     输出："rtn_msg": {"code": 0, "msg": null,    "data":[]}
    """
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)

    # 获取库区id
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "basic_stockShelf_page_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, "payload62_6", dept_id, stockId)
    result = commonFuc().http_post(url, request_body, headers)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    id1 = commonFuc().analysis_json('id', data1)  #

    print(id1)  # stockAreaId  库区id
    print('resultaaaaaaaaaaaaaaaaaaaaaaaaaaa', result)

    # 获取货位id
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "basic_stockShelf_page_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, "payload62_13", dept_id, id1)

    print('request_body', request_body)
    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultbbbbbbbbbbb', result)

    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    id2 = commonFuc().analysis_json('id', data1[0])  #
    parentId = commonFuc().analysis_json('parentId', data1[0])
    shelfCode = commonFuc().analysis_json('shelfCode', data1[0])
    shelfNo= commonFuc().analysis_json('shelfNo', data1[0])
    # 货位id
    print(id2, parentId, shelfCode, shelfNo,'ccccccccccccccccccccccccccccc')
    # r_d7库房管理_库区设置_货位管理_绑定探针_列表页面查询
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "basic_probeInfo_probe_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, "payload62_16", shelfCode)

    print('request_body', request_body)
    # # # # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('resultccccccccccccccc', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    probeId = commonFuc().analysis_json('id', data1[0])  # 探针id
    if code == 0 and len(data) >= 1:
        commonFuc().check_text_exist(0, result)
        print(commonFuc().analysis_json('id', data1[0]))
    else:
        commonFuc().check_text_exist('error', result)

    #绑定探针
    url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "basic_probeInfo_shelf_save_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, "payload62_18", probeId,shelfCode,shelfNo)
    request_body = '[' + str(request_body) + ']'
    print('request_body', request_body)
    new_request_body = request_body.replace("'", '"')
    new_request_body = new_request_body.replace("None", 'null')
    print('new_request_body', new_request_body)
    import json
    request_body_json = json.loads(new_request_body)

    print('request_body_json', request_body_json)
    # # # # 发送请求
    result = commonFuc().http_post(url2, request_body_json, headers)
    print('resultdddddddddddddddddddddddddd', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    data1 = commonFuc().analysis_json('data', data)
    if code == 0 and data== 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
test1()
