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

"""
case_tag:cmdc_api,cmdc用户购物车商品移除,2252,2252-9,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_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()
headers = commonFuc().get_business_data(module, "json_headers3", cmdc_access_token)

# 获取登录后台管理系统所需账号密码
username1 = commonFuc().get_business_data(module, "username1")
password1 = commonFuc().get_business_data(module, "password1")
# 获取登录后Cmdc_access_token
cmdc_access_token = CmdcDoLogin(username1, password1).get_token()
headers1 = commonFuc().get_business_data(module, "json_headers", cmdc_access_token)
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url20")
request_body = commonFuc().get_business_data(module, "payload20")
# 发送请求
result = requests.post(url, json=request_body, headers=headers1)
result = json.loads(result.content)
product_id = result["data"]["list"][0]["productId"]
price = result["data"]["list"][0]["referencePrice"]
filiale_code = result["data"]["list"][0]["filialeCode"]

# 第二步在购物车中新增商品
url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url2")
request_body1 = commonFuc().get_business_data(module, "payload2", product_id, price, filiale_code)
result1 = requests.post(url1, json=request_body1, headers=headers)
result1 = json.loads(result1.content)

# 第三步查询购物车中新增的商品信息
url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
request_body2 = commonFuc().get_business_data(module, "payload1")
result2 = requests.post(url2, json=request_body2, headers=headers)
result2 = json.loads(result2.content)
# print(result2)
# 获取所添加商品id
product_id = commonFuc().get_business_data(module, "payload2")["productId"]
result_product_id = result2["data"]["list"][0]["list"][0]["productId"]
if result_product_id == product_id:
    buyer_cart_id = result2["data"]["list"][0]["list"][0]["buyerCartId"]
    request_body = commonFuc().get_business_data(module, "payload3", buyer_cart_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)
    result = json.loads(result.content)
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict2")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
