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

"""
case_tag:cmdc_api,cmdc快速下单-申请取消-审核,2252,2252-27,sit,bs
涉及到接口：需求单创建、需求单取消、审核等接口
"""

from common.common_func import commonFuc
from air_case.cmdc_login.多采商城登录.多采商城登录 import CmdcMaiiLogin
from air_case.cmdc_login.后台管理系统登录.后台管理系统登录 import CmdcDoLogin
import requests
import json

module = "cmdc_demand"

# 第一步登录多采商城获取token信息
# 获取登录多采商城所需账号密码
username = commonFuc().get_business_data(module, "username")
password = commonFuc().get_business_data(module, "password")
# 获取登录后Cmdc_access_token并进行参数化赋值
cmdc_access_token1 = CmdcMaiiLogin(username, password).get_token()
headers1 = commonFuc().get_business_data(module, "json_headers1", cmdc_access_token1)

# 第二步进行需求单创建
url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url10")
request_body1 = commonFuc().get_business_data(module, "payload10")
# 发送请求
result1 = requests.post(url1, json=request_body1, headers=headers1)
result1 = json.loads(result1.content)

# 获取新创建需求单对应的订单编号
order_num = result1["data"]["国药集团北京医疗器械有限公司"]["demandItems"][0]["demandCode"]

# 第三步在需求单列表根据订单编号查询出新创建的需求单
url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url20")
request_body2 = commonFuc().get_business_data(module, "payload20", order_num)
# 发送请求
result2 = requests.post(url2, json=request_body2, headers=headers1)
result2 = json.loads(result2.content)

# 第四步获取到新增需求单对应的id并进行参数化赋值处理
demand_code = result2["data"]["list"][0]["demandCode"]

# 步骤五对新创建的需求单进行取消申请操作
url3 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url30")
request_body3 = commonFuc().get_business_data(module, "payload30", demand_code)
# 发送请求
result3 = requests.post(url3, json=request_body3, headers=headers1)
result3 = json.loads(result3.content)

"""
 场景:流程验证-快速下单创建-申请取消-审核
 用例名称：快速下单创建-申请取消-审核
"""
# 步骤六在后台运营系统查询出用户提交取消申请
# 登录后台运营系统获取token信息
username = commonFuc().get_business_data(module, "username1")
password = commonFuc().get_business_data(module, "password1")
# 获取登录后Cmdc_access_token并进行参数化赋值
cmdc_access_token = CmdcDoLogin(username, password).get_token()
headers = commonFuc().get_business_data(module, "json_headers", cmdc_access_token)
# 查询用户提交的取消的需求单
url4 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url83")
request_body4 = commonFuc().get_business_data(module, "payload83", demand_code)
# 发送请求
result4 = requests.post(url4, json=request_body4, headers=headers)
result4 = json.loads(result4.content)

# 获取demandid
demand_id = result2["data"]["list"][0]["demandId"]

# 对需求单进行审核操作
url5 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url84")
request_body5 = commonFuc().get_business_data(module, "payload84", demand_id)

# 发送请求
result5 = requests.post(url5, json=request_body5, headers=headers)
# 获取接口响应时间
api_time = result5.elapsed.total_seconds()
result5 = json.loads(result5.content)
result5["api_time"] = api_time
if result5["code"] == "200":
    # 获取预期结果
    check_dict5 = commonFuc().get_business_data(module, "checkDict84")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict5, result5)
elif result5["code"] == "3007":
    # 获取预期结果
    check_dict5 = commonFuc().get_business_data(module, "checkDict_3007")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict5, result5)
else:
    # 获取预期结果
    check_dict5 = commonFuc().get_business_data(module, "checkDict_401")
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict5, result5)

