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

"""
case_tag:cmdc_api,cmdc购物车数量修改验证,2252,2252-10,sit,bs
主数据平台:多采商城修改购物车中商品数量接口 
"""

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


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_headers1", 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)
# 获取商品信息
invalid_sign = result["data"]["list"][0]["list"][0]["invalidSign"]
buyercart_id = result["data"]["list"][0]["list"][0]["buyerCartId"]
product_name = result["data"]["list"][0]["list"][0]["productName"]
quantity = result["data"]["list"][0]["list"][0]["quantity"]
modify_quantity = random.randint(2, 10)

if invalid_sign == 0:
    # 组装修改购物车报文
    url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url83")
    request_body1 = commonFuc().get_business_data(module, "payload83", buyercart_id, modify_quantity)
    # print(request_body1)
    # 发送请求
    result1 = requests.post(url1, json=request_body1, headers=headers)
    # 获取接口响应时间
    api_time = result1.elapsed.total_seconds()
    result1 = json.loads(result1.content)
    result1["api_time"] = api_time
    # 获取预期结果
    check_dict1 = commonFuc().get_business_data(module, "checkDict80")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict1, result1)
    # 获取购物车列表
    url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url1")
    request_body2 = commonFuc().get_business_data(module, "payload81", product_name)
    # 发送请求
    result2 = requests.post(url2, json=request_body2, headers=headers)
    result2 = json.loads(result2.content)
    for i in result2["data"]["list"][0]["list"]:
        if i["buyerCartId"] == buyercart_id:
            result3 = i["quantity"]
            check_dict3 = commonFuc().get_business_data(module, "checkDict81", result3)
            check_dict4 = commonFuc().get_business_data(module, "checkDict82", modify_quantity)
            commonFuc().check_result(check_dict3, check_dict4)

"""
 场景:对购物车列表中商品进行数量的修改
 用例名称：对购物车列表中商品进行数量的修改
 输出：{"success":true,"code":"200","message":"OK","data":"ok"}
"""

