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

"""
case_tag:cmdc_api,cmdc产品线制单员删除功能验证,2293,2293-085,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, "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)

# 第二步获取产品线列表
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url71")
request_body = commonFuc().get_business_data(module, "payload74_1")

# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)
# print(result)

for i in result["data"]["list"]:
    # 只对包含多个制单员的产品线进行制单员删除操作
    if len(i["memberIdsList"]) > 1:
        # 获取产品线信息
        product_line_code = i["productLineCode"]
        # 生成随机数
        member_random = random.randint(0, len(i["memberIdsList"]) - 1)
        # 获取制单员id
        member_id = i["memberIdsList"][member_random]

        # 第三步对产品已分配制单员进行删除操作
        url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url79")
        request_body = commonFuc().get_business_data(module, "payload79", product_line_code, member_id)

        """
        
         场景: 产品线制单员删除功能验证
         用例名称：产品线制单员删除功能验证
         输出：{"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, "checkDict79")
        # print(check_dict)
        # 断言实际结果中是否包含预期结果的内容
        commonFuc().check_result(check_dict, result)

        # 跳出循环
        break
