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

"""
case_tag:cmdc_api,cmdc返利规则新增验证,2271,2271-12,sit,bs
主数据平台:后台运营系统返利规则新增接口
"""

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

module = "cmdc_rebate"

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

# 生成一个随机数字，用于后续随机选择某一条客户信息
customer_random = random.randint(0, len(result["data"]["list"]) - 1)
# 获取返利规则中客户范围列表中某一条客户信息
customer_code = result["data"]["list"][customer_random]["customerCode"]
customer_name = result["data"]["list"][customer_random]["customerName"]

# 第三步获取商品信息
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_product")
request_body = commonFuc().get_business_data(module, "payload2011_product")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 生成随机数
product_random = random.randint(0, len(result["data"]["list"]) - 1)
# 获取商品code
product_code = result["data"]["list"][product_random]["relevanceCode"]
product_name = result["data"]["list"][product_random]["relevanceName"]
specifications = result["data"]["list"][product_random]["specifications"]
manufacturer = result["data"]["list"][product_random]["manufacturer"]

# 第四步进行返利规则新增
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url21")
request_body = commonFuc().get_business_data(module, "payload21", customer_name, customer_code, product_code,
                                             product_name, specifications, manufacturer)

"""
 场景: 返利规则新增验证
 用例名称：返利规则新增验证
 输出：{"success":true,"code":"200","message":"OK"}
"""

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