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

"""
case_tag:cmdc_api,cmdc促销活动列表查询条件验证,2262,2262-39,sit,bs
主数据平台:运营后台管理系统促销活动列表查询接口
"""

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

module = "cmdc_commodity_activity"

# 第一步登录后台运营平台获取cmdc_access_token
# 获取登录所需账号密码
username = commonFuc().get_business_data(module, "username30")
password = commonFuc().get_business_data(module, "password30")
# 获取登录后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, "url1")
request_body = commonFuc().get_business_data(module, "payload1")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 生成随机数
activity_random = random.randint(0, len(result["data"]["list"]) - 1)

# 获取促销活动信息activityBasicId、activityName、activityStatus、activityType、startTime、endTime、cStartTime、cEndTime
activity_basic_id = result["data"]["list"][activity_random]["activityBasicId"]
activity_name = result["data"]["list"][activity_random]["activityName"]
activity_status = result["data"]["list"][activity_random]["activityStatus"]
activity_type = result["data"]["list"][activity_random]["activityType"]
start_time = result["data"]["list"][activity_random]["effectiveDate"]
end_time = result["data"]["list"][activity_random]["expirationDate"]
c_start_time = result["data"]["list"][activity_random]["createTime"]
c_end_time = result["data"]["list"][activity_random]["expirationDate"]

# 第三步促销活动管理列表查询条件验证
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
request_body = commonFuc().get_business_data(module, "payload_list", activity_basic_id, activity_name, activity_status,
                                             activity_type, start_time, c_start_time, end_time, c_end_time)
# print(request_body)
"""
 场景: 促销活动列表查询条件验证
 用例名称：促销活动列表查询条件验证
 输出：{"activityBasicId":"%s"}
"""

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

