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

"""
case_tag:cmdc_api,cmdc用户购物车商品移除,2252,2252-4,sit,bs
主数据平台:多彩商城购物车列表商品删除接口 
"""

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

module = "cmdc_buyercar_list"

# 第一步进行用户登录获取Cmdc_access_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()
print(cmdc_access_token)
headers = commonFuc().get_business_data(module, "json_headers3", cmdc_access_token)
print(headers)
# 第二步在购物车中新增商品
url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url2")
print(url1)
request_body = commonFuc().get_business_data(module, "payload2")
print(request_body)
result = requests.post(url1, json=request_body, headers=headers)
result = json.loads(result.content)
print(result)
# 第三步查询购物车中新增的商品信息
url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
print(url2)
request_body = commonFuc().get_business_data(module, "payload1")
print(request_body)
result = requests.post(url2, json=request_body, headers=headers)
result = json.loads(result.content)
print(result)
# 获取所添加商品id
product_id = commonFuc().get_business_data(module, "payload2")["productId"]
print(product_id, type(product_id))
result_product_id = result["data"]["list"][0]["list"][0]["productId"]
print(result_product_id, type(result_product_id))
if result_product_id == product_id:
    buyer_cart_id = result["data"]["list"][0]["list"][0]["buyerCartId"]
    print(buyer_cart_id)
    request_body = commonFuc().get_business_data(module, "payload3", buyer_cart_id)
    print(request_body)
    # 第三步删除新增加至购物车列表的商品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url3")
    print(url)
    """
    场景:从用户购物车列表移除商品
    用例名称：从用户购物车列表移除商品
    输出：{"success":true,"code":"200","message":"OK","data":"ok"}
    """
    # 发送请求
    result = requests.post(url, json=request_body, headers=headers)
    result = json.loads(result.content)
    print(result)
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict2")
    print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
