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

"""
case_tag:cmdc_api,cmdc商品下架拦截验证_订单修改,2289,2289-37,sit,bs
涉及到接口:多采商城需求单修改接口
"""

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

module = "cmdc_demand_mall"

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

# 数据库操作
mysql_handle = mySql()
# 获取conf.ini文件中配置的数据库信息
host, port, user, pwd = mysql_handle.getConf(db="cmdc_db")

# 进行商品下架操作
sql = "UPDATE `cmdc-product`.ic_product t SET t.status = 102 WHERE t.productId = 314"
mysql_handle.executeUpdate(host, port, user, pwd, "cmdc-product", sql)

# 第二步需求单编辑修改操作
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url39")
request_body = commonFuc().get_business_data(module, "payload39")

"""
 场景: 验证需求单编辑提交时，所含商品下架，是否被拦截成功
 用例名称：商品下架拦截验证_订单修改
 输出：{"errMessage": "红外测温仪商品已被下架，请返回重新提交。"}
"""

# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)
# 获取message
result = {"errMessage": result["data"][0]["errMessage"]}
# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict39")
# print(check_dict)
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)

# 进行商品上架架操作
sql = "UPDATE `cmdc-product`.ic_product t SET t.status = 101 WHERE t.productId = 314"
mysql_handle.executeUpdate(host, port, user, pwd, "cmdc-product", sql)