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

"""
case_tag:cmdc_api,cmdc常购清单列表商品移除,2252,2252-33,sit,bs
主数据平台:多采商城常购清单列表商品删除接口 
"""

from common.common_func import commonFuc
from air_case.cmdc_login.多采商城登录.多采商城登录 import CmdcMaiiLogin
from air_case.cmdc_login.后台管理系统登录.后台管理系统登录 import CmdcDoLogin
import requests
import json

module = "cmdc_frequent_purchase"

# 步骤一登录后台管理系统查询商品列表获取已上架商品信息
# 获取登录后台管理系统所需账号密码
username1 = commonFuc().get_business_data(module, "username1")
password1 = commonFuc().get_business_data(module, "password1")
# 获取登录后Cmdc_access_token
cmdc_access_token1 = CmdcDoLogin(username1, password1).get_token()
headers1 = commonFuc().get_business_data(module, "json_headers", cmdc_access_token1)

# 步骤二查询已已上架商品信息
url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url10")
request_body1 = commonFuc().get_business_data(module, "payload10")

# 发送请求
result1 = requests.post(url1, json=request_body1, headers=headers1)
result1 = json.loads(result1.content)

# 获取已上架商品product_id和price
product_id = result1["data"]["list"][0]["productId"]
price = result1["data"]["list"][0]["referencePrice"]

# 步骤三登录多采商城
# 获取登录所需账号密码
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)

# 步骤四添加已上架商品至常购清单列表
url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
request_body2 = commonFuc().get_business_data(module, "payload1", product_id, price)

# 发送请求
result2 = requests.post(url2, json=request_body2, headers=headers)
result2 = json.loads(result2.content)

# 第三步查询购物车中新增的商品信息
url3 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url2")
request_body3 = commonFuc().get_business_data(module, "payload2")
# 发送请求
result3 = requests.post(url3, json=request_body3, headers=headers)
result3 = json.loads(result3.content)
# 获取购物车列表中商品对应的productId
result_product_id = result3["data"]["list"][0]["list"][0]["productId"]
# 判断查询出的商品是否为新增商品
if result_product_id == product_id:
    frequent_purchase_id = result3["data"]["list"][0]["list"][0]["frequentPurchaseId"]
    request_body = commonFuc().get_business_data(module, "payload3", frequent_purchase_id)
    # 第三步删除新增加至购物车列表的商品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url3")

    """
    场景:从用户购物车列表移除商品
    用例名称：从用户购物车列表移除商品
    输出：{"success":true,"code":"200","message":"OK","data":"ok"}
    """

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