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

"""
case_tag:cmdc_api,cmdc列表查询条件验证_三方需求单待发货列表,00,00-28,sit,be
主数据平台:后台运营系统获取待发货列表_三方需求单接口
"""

from common.common_func import commonFuc
from air_case.cmdc_login.后台管理系统登录.后台管理系统登录 import CmdcDoLogin
import datetime
import requests
import random
import json

module = "cmdc_special_version"

# 第一步登录后台运营系统获取token
username = commonFuc().get_business_data(module, "username_do_be")
password = commonFuc().get_business_data(module, "password_do_be")
# 获取登录后Cmdc_access_token
cmdc_access_token = CmdcDoLogin(username, password).get_token()
headers = commonFuc().get_business_data(module, "json_headers", cmdc_access_token)

# 第二步获取待发货列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url27")
request_body = commonFuc().get_business_data(module, "payload27")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 生成随机数
so_random = random.randint(0, len(result["data"]["list"]) - 1)
# 获取三方需求单信息
soCode = result["data"]["list"][so_random]["soCode"]
logisticsOrderNo = (result["data"]["list"][so_random]["logisticsOrderNo"] if result["data"]["list"][so_random][
    "logisticsOrderNo"] else "")
logisticsCompany = (result["data"]["list"][so_random]["logisticsCompany"] if result["data"]["list"][so_random][
    "logisticsCompany"] else "")
customerCode = result["data"]["list"][so_random]["customerCode"]
customerName = result["data"]["list"][so_random]["customerName"]
demandParentCode = result["data"]["list"][so_random]["demandParentCode"]
demandCode = result["data"]["list"][so_random]["demandCode"]
projectName = (
    result["data"]["list"][so_random]["projectName"] if result["data"]["list"][so_random]["projectName"] else "")
productLineName = (result["data"]["list"][so_random]["productLineName"] if result["data"]["list"][so_random][
    "productLineName"] else "")
createtime = result["data"]["list"][so_random]["createTime"][:10] + " 00:00:00"
audittime = (result["data"]["list"][so_random]["auditTime"][:10] + " 00:00:00" if result["data"]["list"][so_random][
    "auditTime"] else "")
paidtime = (result["data"]["list"][so_random]["paidTime"][:10] + " 00:00:00" if result["data"]["list"][so_random][
    "paidTime"] else "")
nowtime = datetime.datetime.now().strftime("%Y-%m-%d") + " 00:00:00"
settlementStatus = result["data"]["list"][so_random]["settlementStatus"]
paymentType = result["data"]["list"][so_random]["paymentType"]

# 第二步进行列表条件查询
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url28")
request_body = commonFuc().get_business_data(module, "payload28", soCode, logisticsOrderNo, logisticsCompany,
                                             customerCode,
                                             customerName, demandParentCode, demandCode, projectName, productLineName,
                                             createtime, nowtime, audittime, nowtime,
                                             settlementStatus, paymentType)
# print(request_body)
"""
 场景: 列表查询条件验证_三方需求单待发货列表
 用例名称：列表查询条件验证_三方需求单待发货列表
 输出：{"soCode":"%s"}
"""

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

# 获取三方需求单信息
result = json.loads(result.content)
# print(result)
result = {"soCode": result["data"]["list"][0]["soCode"]}
# 将接口响应时间添加至result
result["api_time"] = api_time

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