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

"""
case_tag:cmdc_api,cmdc产品线分配制单员功能验证,2293,2293-082,sit,bs
主数据平台:运营后台管理系统产品线分配制单员接口
"""

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

module = "cmdc_product"

# 第一步登录获取登录所需账号密码
username = commonFuc().get_business_data(module, "username2")
password = commonFuc().get_business_data(module, "password2")
# 获取登录后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, "url71")
request_body = commonFuc().get_business_data(module, "payload71")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 生成随机数
line_random = random.randint(0, len(result["data"]["list"]) - 1)
# 获取产品线信息
product_line_code = result["data"]["list"][line_random]["productLineCode"]
member_ids_list = (result["data"]["list"][line_random]["memberIdsList"] if result["data"]["list"][line_random][
    "memberIdsList"] else [])

# 第三步获取制单员列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url75")
request_body = {"productLineCode": product_line_code, "realName": "", "memberIdsList": member_ids_list, "pageNumber": 1,
                "pageSize": 5}
# print(request_body)
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)

# 判断是否存在制单员
if result["data"]["list"]:
    # 生成随机数
    marker_random = random.randint(0, len(result["data"]["list"]) - 1)
    # 获取制单员信息
    marker_list = result["data"]["list"][marker_random]

    # 第四步分配制单员
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url76")
    request_body = {"productLineCode": product_line_code, "userList": [marker_list]}

    """
     场景: 产品线分配制单员功能验证
     用例名称：产品线分配制单员功能验证
     输出：{"success":false,"code":"200","message":"操作成功!","data":null,"freshToken":null}
    """

    # 发送请求
    result = requests.post(url, json=request_body, headers=headers)
    # 获取接口响应时间
    api_time = float(result.elapsed.total_seconds())
    result = json.loads(result.content)
    result["api_time"] = api_time
    # 获取预期结果
    check_dict = commonFuc().get_business_data(module, "checkDict76")
    # print(check_dict)
    # 断言实际结果中是否包含预期结果的内容
    commonFuc().check_result(check_dict, result)
