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

"""
case_tag:cmdc_api,cmdc通过Excel复制添加商品-添加成功,2252,2252-15,sit,bs
主数据平台:多采商城购物车列表通过Excel复制添加商品接口 
"""

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


module = "cmdc_buyercar_list"

# 第一步登录多采商城获取token
# 获取登录所需账号密码
username1 = commonFuc().get_business_data(module, "username")
password1 = commonFuc().get_business_data(module, "password")
# 获取登录后Cmdc_access_token
cmdc_access_token = CmdcMaiiLogin(username1, password1).get_token()
headers = commonFuc().get_business_data(module, "json_headers1", cmdc_access_token)

# 第二步查询用户对应的站点
url_user_websites = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_user_websites")
# 组装请求报文
request_body_user_websites = {}
# 发送请求
result_body_user_websites = requests.post(url_user_websites, json=request_body_user_websites, headers=headers)
result_body_user_websites = json.loads(result_body_user_websites.content)

# 获取站点对应的id
company_id = result_body_user_websites["data"][2]["companyId"]
# print(company_id)

# 第三步切换至站点
url_switch_website = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_switch_website")
# 组装请求报文
request_body_switch_website = commonFuc().get_business_data(module, "payload_switch_website", company_id)
# 发送请求
result_body_switch_website = requests.post(url_switch_website, json=request_body_switch_website, headers=headers)

# 第四步获取站点对应的商品信息
url_website_product = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_website_product")
# 组装请求报文
request_body_website_product = commonFuc().get_business_data(module, "payload_website_product1")
# 发送请求
result_body_website_product = requests.post(url_website_product, json=request_body_website_product, headers=headers)
result_body_website_product = json.loads(result_body_website_product.content)
# print(result_body_website_product)
# 获取站点的商品信息
product_code = result_body_website_product["data"]["list"][0]["productcode"]


# 第五步获取通过Excel复制添加商品
# 获取通过Excel复制添加商品接口地址
url_batch = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url_batch")
# 组装请求报文
request_body = commonFuc().get_business_data(module, "payload23", product_code, company_id)
# print(request_body)

"""
 场景:通过Excel复制添加商品，选择正确的站点以及对应的产品信息，添加成功
 用例名称：通过Excel复制添加商品，选择正确的站点以及对应的产品信息，添加成功
 输出："success":false,"code":"2001","message":"商品添加完毕，本次添加商品【1】条，成功【1】条，失败【0】条。"
"""
# 发送请求
result = requests.post(url_batch, json=request_body, headers=headers)
# 获取接口响应时间
api_time = result.elapsed.total_seconds()
result = json.loads(result.content)
result["api_time"] = api_time
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict23")
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)
