# -*- encoding=utf8 -*-
__author__ = "liguangyu"

"""
case_tag:cmdc_api,cmdc代客下单_套包清单_查询条件验证,2264,2264-78,sit,bs
主数据平台:运营后台管理系统套包清单获取接口
"""

from common.common_func import commonFuc
from air_case.cmdc_login.后台管理系统登录.后台管理系统登录 import CmdcDoLogin
import requests
import json

module = "cmdc_special_fresenius_group"

# 第一步登录后台运营平台获取cmdc_access_token
# 获取登录所需账号密码
username = commonFuc().get_business_data(module, "username1")
password = commonFuc().get_business_data(module, "password1")
# 获取登录后Cmdc_access_token
cmdc_access_token = CmdcDoLogin(username, password).get_token()
headers = commonFuc().get_business_data(module, "json_headers2", cmdc_access_token)

# 第二步套包清单列表查询
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url50")
request_body = commonFuc().get_business_data(module, "payload50")
# 发送请求
result = requests.get(url, params=request_body, headers=headers)
result = json.loads(result.content)


# 此函数用于当x为none时，进行空字符串替换
def func(x):
    if x is None:
        x = ""
        return x
    else:
        return x


# 获取套包信息
group_name = result["data"][0]["groupName"][:4]
group_product_code = result["data"][0]["subList"][0]["product"]["productCode"]
group_product_name = result["data"][0]["subList"][0]["product"]["productName"]
group_option_str = result["data"][0]["subList"][0]["product"]["specifications"]
document_no = func(result["data"][0]["documentNo"])
product_line_code = result["data"][0]["productLineCode"]
material_code_split = result["data"][0]["subList"][0]["product"]["materialCode"]

# 第三步进行列表查询条件验证
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url50")
request_body = commonFuc().get_business_data(module, "payload51", group_name, group_product_code, group_product_name,
                                             group_option_str, document_no, product_line_code, material_code_split)

"""
 场景: 代客下单_套包清单_查询条件验证
 用例名称：代客下单_套包清单_查询条件验证
 输出：{"productLineCode": "%s"}
"""

# 发送请求
result = requests.get(url, params=request_body, headers=headers)
# 获取接口响应时间
api_time = float(result.elapsed.total_seconds())
result = json.loads(result.content)

# 获取结果中ref单号
result = {"productLineCode": result["data"][0]["productLineCode"]}
result["api_time"] = api_time
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict51", product_line_code)
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)
