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

"""
case_tag:cmdc_api,cmdc验证特价单品_加入购物车_购物车列表展示,2252,2252-71,sit,bs
涉及到接口:特价单品加入购物车接口
"""

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

module = "cmdc_special_fresenius_group"

# 第一步登录多采商城获取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, "url_website")
request_body = commonFuc().get_business_data(module, "payload_website")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)

# 第四步特价单品加入购物车
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url43")
request_body = commonFuc().get_business_data(module, "payload43")

"""
 场景:验证特价单品加入购物车功能，并且在购物车列表正常展示
 用例名称：验证特价单品_加入购物车_购物车列表展示
 输出：{"groupPrice": 101.0, "timeStr":"2023-11-09至2031-12-31", "documentNo":"特价单品验证毋动"}
"""

# 发送请求
result = requests.post(url, json=request_body, headers=headers)
# 获取接口响应时间
api_time = float(result.elapsed.total_seconds())

result = json.loads(result.content)
# print(result)
result["api_time"] = api_time
# print(result)
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict43")
# # 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)

# 查询购物车列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_buyercart")
request_body = commonFuc().get_business_data(module, "payload_buyercart")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)


# 过滤查找添加的单品
for i in result["data"]["list"]:
    if i["companyCode"] == "00111":
        for j in i["list"]:
            if j["buyerCartId"] == 19177:
                result = {"groupPrice": j["groupPrice"], "timeStr": j["timeStr"], "documentNo": j["documentNo"],
                          "buyCartNum": 5}

                # print(result)
                # 获取预期结果
                check_dict = commonFuc().get_business_data(module, "checkDict_buyercart")
                # # 断言实际结果中是否包含预期结果的内容
                commonFuc().check_result(check_dict, result)
