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

"""
case_tag:cmdc_api,cmdc购物车支持跨站商品下单验证,2252,2252-17,sit,bs
涉及到接口:多采商城登录、购物车列表、购物车新增商品、订单创建等接口
"""

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


module = "cmdc_buyercart_handle"

# 第一步登录多采商城获取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_headers", cmdc_access_token)

# 第二步新增跨站商品至购物车列表
url_add = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_add")
# 添加102站点对应的商品至购物车
request_body1 = commonFuc().get_business_data(module, "payload1")
result1 = requests.post(url_add, json=request_body1, headers=headers)
# 添加103站点对应的商品至购物车
request_body2 = commonFuc().get_business_data(module, "payload2")
result2 = requests.post(url_add, json=request_body2, headers=headers)
result2 = json.loads(result2.content)
# 获取预期结果
check_dict1 = commonFuc().get_business_data(module, "checkDict1")
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict1, result2)
"""
 场景:购物车支持跨站商品下单验证
 用例名称：购物车支持跨站商品下单验证
 输出：{"success":true,"code":"200","message":"OK","data":"ok"}

"""
# 获取已添加至购物车列表商品对应的id
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url")
# 获取102站点商品对应的id值
request_body = commonFuc().get_business_data(module, "payload4")
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
buyercart_id_102 = result["data"]["list"][0]["list"][0]["buyerCartId"]
# print(buyercart_id_102)
# 获取103站点商品对应的id值
request_body = commonFuc().get_business_data(module, "payload5")
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
buyercart_id_103 = result["data"]["list"][0]["list"][0]["buyerCartId"]
buyercart_its = [buyercart_id_102, buyercart_id_103]

# 第四步，进行商品的选择
url_choose = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_choose")
request_body = commonFuc().get_business_data(module, "payload6", buyercart_id_102)
# 发送请求
result = requests.post(url_choose, json=request_body, headers=headers)

request_body = commonFuc().get_business_data(module, "payload7", buyercart_id_102)
# 发送请求
result = requests.post(url_choose, json=request_body, headers=headers)

# 第五步选择已添加至购物车列表中商品进行订单提交
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
request_body = commonFuc().get_business_data(module, "payload30", buyercart_id_102, buyercart_id_102, buyercart_id_102, buyercart_id_102, buyercart_id_103, buyercart_id_103, buyercart_id_103, buyercart_id_103, buyercart_id_102, buyercart_id_103)
# print(request_body)
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
if result["data"]:
    # # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict2")
    # # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
else:
    print("下单失败")
