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

"""
case_tag:cmdc_api,cmdc套包商品管理查询条件验证,2264,2264-43,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_headers", cmdc_access_token)

# 套包状态-全部
group_status = ""

# 第二步获取套包列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "group_list")
request_body = commonFuc().get_business_data(module, "group_payload", group_status)
# 发送请求
result = requests.post(url, json=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_code = result["data"]["list"][0]["groupCode"]
group_name = result["data"]["list"][0]["groupName"]
product_code = result["data"]["list"][0]["products"][0]["productCode"]
material_code = result["data"]["list"][0]["products"][0]["materialCode"]
manufacturer_list = result["data"]["list"][0]["products"][0]["manufacturer"]
productLine_name = func(result["data"]["list"][0]["products"][0]["productLineName"])
description = result["data"]["list"][0]["products"][0]["description"]
manufacturer_product_desc = func(result["data"]["list"][0]["products"][0]["manufacturerProductDesc"])
manufacturer_product_no = func(result["data"]["list"][0]["products"][0]["manufacturerProductNo"])
product_info = result["data"]["list"][0]["products"][0]["productName"]
status = result["data"]["list"][0]["status"]

# 第三步套包商品管理查询条件验证
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "group_list")
request_body = commonFuc().get_business_data(module, "payload9", group_code, group_name,
                                             product_code, material_code, manufacturer_list,
                                             productLine_name, description, manufacturer_product_desc,
                                             manufacturer_product_no, product_info, status)

"""
 场景:套包商品管理查询条件验证
 用例名称：套包商品管理查询条件验证
 输出：{"groupCode":"%s"}
"""

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

# 获取套包code
result = {"groupCode": result["data"]["list"][0]["groupCode"]}
result["api_time"] = api_time
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict9", group_code)
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)
