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

"""
case_tag:cmdc_api,cmdc列表查询条件验证_发货时间列表,2293,2293-035,sit,bs
主数据平台:运营后台管理系统发货时间列表接口
"""

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

module = "cmdc_product"

# 获取登录所需账号密码
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)

# 获取发货时间列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url34")
request_body = commonFuc().get_business_data(module, "payload34")

# 发送请求
result = requests.get(url, params=request_body, headers=headers)
result = json.loads(result.content)

# 生成一个随机数
delivery_time_random = random.randint(0, len(result["data"]["list"]) - 1)
# 判断商品圈定方式
if result["data"]["list"][delivery_time_random]["productRangeType"]:

    # 当商品圈定方式为按商品时，进行商品相关字段查询验证
    delivery_time_id = result["data"]["list"][delivery_time_random]["deliveryTimeId"]
    # 通过查看发货时间详情获取商品信息
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url36")
    request_body = commonFuc().get_business_data(module, "payload36", delivery_time_id)

    # 发送请求
    result1 = requests.get(url, params=request_body, headers=headers)
    result1 = json.loads(result1.content)

    # 获取商品信息
    line_codes = ""
    name = result1["data"]["list"][delivery_time_random]["name"]
    status = result1["data"]["list"][delivery_time_random]["status"]
    product_code = result1["data"]["list"][delivery_time_random]["productList"][0]["productCode"]
    product_name = result1["data"]["list"][delivery_time_random]["productList"][0]["productName"]

    # 进行查询条件验证
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url35")
    request_body = commonFuc().get_business_data(module, "payload35", line_codes, name, status, product_code,
                                                 product_name)

    # 发送请求
    result = requests.get(url, params=request_body, headers=headers)
    # 获取接口响应时间
    api_time = float(result.elapsed.total_seconds())
    result = json.loads(result.content)
    # print(result)
    # 获取查询结果中规则名称
    result = {"name": result["data"]["list"][0]["name"]}
    result["api_time"] = api_time
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict35", name)
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
else:
    # 当商品圈定方式为按产品线时，进行产品线相关字段查询验证
    line_codes = result["data"]["list"][delivery_time_random]["lineCodes"]
    name = result["data"]["list"][delivery_time_random]["name"]
    status = result["data"]["list"][delivery_time_random]["status"]
    product_code = ""
    product_name = ""

    # 进行查询条件验证
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url35")
    request_body = commonFuc().get_business_data(module, "payload35", line_codes, name, status, product_code,
                                                 product_name)

    """
    场景: 列表查询条件验证_发货时间列表
    用例名称：列表查询条件验证_发货时间列表
    输出：{"name":"%s"}
    """

    # 发送请求
    result = requests.get(url, params=request_body, headers=headers)
    # 获取接口响应时间
    api_time = float(result.elapsed.total_seconds())
    result = json.loads(result.content)
    # print(result)
    # 获取查询结果中规则名称
    result = {"name": result["data"]["list"][0]["name"]}
    result["api_time"] = api_time
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict35", name)
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
