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

"""
case_tag:cmdc_api,cmdc站点满足快速下单,2250,2250-2,sit,bs
主数据平台:多采商城快速下单权限判定接口 
"""

from common.common_func import commonFuc
from air_case.cmdc_login.多采商城登录.多采商城登录 import CmdcMaiiLogin
import requests
import json

module = "cmdc-qos"

# 第一步登录多采商城获取cmdc_access_token
# 获取登录所需账号密码
username = commonFuc().get_business_data(module, "username")
password = commonFuc().get_business_data(module, "password")
# 获取登录后Cmdc_access_token
cmdc_access_token = CmdcMaiiLogin(username, password).get_token()
headers = commonFuc().get_business_data(module, "json_headers", cmdc_access_token)

# 第二步查询站点对应的companyId
url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
request_body1 = commonFuc().get_business_data(module, "payload1")
# 发送请求
result1 = requests.post(url1, json=request_body1, headers=headers)
result1 = json.loads(result1.text)
# print(result1)
# 获取站点对应的快速下单标识字段quickOrderSign
quick_order_sign = result1["data"][1]["quickOrderSign"]

"""
 场景:站点拥有快速下单权限入口
 用例名称：快速下单权限判定接口-站点满足条件显示快速下单入口
 输入：companyId
 输出："quickOrderSign": 1
"""

# 第三步验证站点是否具有快速下单权限
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url")

if quick_order_sign == 1:
    # 获取companyId
    company_id = result1["data"][1]["companyId"]
    request_body = commonFuc().get_business_data(module, "payload", company_id)
    # 发送请求
    result = requests.get(url, params=request_body)
    # 获取接口响应时间
    api_time = result.elapsed.total_seconds()
    result = json.loads(result.text)
    result["api_time"] = api_time
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict1")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
else:
    # 获取companyId
    company_id = result1["data"][1]["companyId"]
    request_body = commonFuc().get_business_data(module, "payload", company_id)
    # 发送请求
    result = requests.get(url, params=request_body)
    # 获取接口响应时间
    api_time = result.elapsed.total_seconds()
    result = json.loads(result.text)
    result["api_time"] = api_time
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict2")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)

