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

"""
case_tag:cmdc_api,cmdc商品属性修改功能验证,2293,2293-005,sit,bs
主数据平台:后台运营系统商品属性修改接口
"""

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

module = "cmdc_product"

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


# 此函数用于当x为none时，进行空字符串替换
def func(x):
    if x is None:
        x = ""
        return x
    else:
        return x


# 生成一个随机数字，用于后续随机选择某一条商品属性信息
rebate_random = random.randint(0, len(result["data"]["list"]) - 1)
# 获取商品属性信息
product_id = result["data"]["list"][rebate_random]["productId"]
product_name = func(result["data"]["list"][rebate_random]["productName"])
property_id = func(result["data"]["list"][rebate_random]["propertyId"])
property_name = func(result["data"]["list"][rebate_random]["propertyName"])
property_note = func(result["data"]["list"][rebate_random]["propertyNote"])
property_val = func(result["data"]["list"][rebate_random]["propertyVal"])

# 第三步对商品属性进行修改功能验证
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url5")
request_body = commonFuc().get_business_data(module, "payload5", product_id, property_id, property_name, property_note,
                                             property_val, property_name)
# print(request_body)

"""
 场景: 商品属性修改功能验证
 用例名称：商品属性修改功能验证
 输出：{"data":"%s"}
"""

# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)
# 获取查询结果中data信息
result = {"data": result["data"]}
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict5", property_id)
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)
