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

"""
case_tag:cmdc_api,cmdc获取原订单信息接口验证_再来一单,2288,2288-22,sit,bs
涉及到接口:多采商城需求单列表、获取原订单信息等接口
"""

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

module = "cmdc_order_handle"

# 第一步登录多采商城获取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_headers1", cmdc_access_token)

order_num = ""

# 第二步获取多采商城需求单列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_demand_list")
request_body = commonFuc().get_business_data(module, "payload_demand_list", order_num)
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)

# 生成随机数字，用于后续获取需求单
random_demand = random.randint(0, len(result["data"]["list"]) - 1)
# 获取需求单信息
demand_id = result["data"]["list"][random_demand]["demandId"]
demand_code = result["data"]["list"][random_demand]["demandCode"]
# print(demand_id)
# 第三步获取原订单信息
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url18")
request_body = commonFuc().get_business_data(module, "payload18", demand_id)
# print(request_body)
"""
 场景: 验证再来一单功能获取原订单信息接口连通性
 用例名称：获取原订单信息接口验证_再来一单
 输出：{"success":true,"code":"200","message":"ok"}
"""

# 发送请求
result = requests.get(url, params=request_body, headers=headers)
# 获取接口响应时间
api_time = float(result.elapsed.total_seconds())
result = json.loads(result.content)
# 将接口影响时间添加至结果中
result["api_time"] = api_time

if result["success"]:
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict18")
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
elif result["code"] == "ERROR":
    # 获取预期结果/原需求单中商品无效获取被删除
    check_dict = commonFuc().get_business_data(module, "checkDictInvalid")
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
else:
    # 获取预期结果/原需求单为历史脏数据导致接口异常
    check_dict = commonFuc().get_business_data(module, "checkDict_code", demand_code)
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)


