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

"""
case_tag:cmdc_api,cmdc单个新增验证_套包协议特价,2264,2264-56,sit,bs
主数据平台:后台运营系统客户列表查询、套包列表查询、套包协议特价新增等接口
"""

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

module = "cmdc_special_fresenius_group"

# 第一步登录后台运营平台获取cmdc_access_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)

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

# 生成随机数
customer_quantity = random.randint(1, len(result["data"]["list"]) - 1)
# 客户信息获取
customer_code = result["data"]["list"][customer_quantity]["customerCode"]
customer_name = result["data"]["list"][customer_quantity]["customerName"]
user_name = result["data"]["list"][customer_quantity]["userName"]

# 第三步套包获取
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url25")
request_body = commonFuc().get_business_data(module, "payload25")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 生成套包随机数
group_quantity = random.randint(1, len(result["data"]["list"]) - 1)
# 获取套包信息
fresenius_group = result["data"]["list"][group_quantity]
product_json = result["data"]["list"][group_quantity]["products"]
group_code = result["data"]["list"][group_quantity]["groupCode"]
group_name = result["data"]["list"][group_quantity]["groupName"]
id = result["data"]["list"][group_quantity]["products"][0]["id"]
product_code = result["data"]["list"][group_quantity]["products"][0]["productCode"]
product_name = result["data"]["list"][group_quantity]["products"][0]["productName"]
manufacturer = result["data"]["list"][group_quantity]["products"][0]["manufacturer"]
manufacturer_code = result["data"]["list"][group_quantity]["products"][0]["manufacturerCode"]
option_str = result["data"]["list"][group_quantity]["products"][0]["optionStr"]
measuring_unit = result["data"]["list"][group_quantity]["products"][0]["measuringUnit"]
material_code = result["data"]["list"][group_quantity]["products"][0]["materialCode"]
quantity = result["data"]["list"][group_quantity]["products"][0]["quantity"]

# 随机生成日期
start_date = date(2000, 1, 1)
end_date = date.today()


def random_date(start, end):
    delta = end - start
    random_days = random.randint(0, delta.days)
    return start + timedelta(days=random_days)


random_date = random_date(start_date, end_date)

# 随机生成唯一字符串用于ref
ref_random = str(uuid.uuid4())
# 生成促销价格
activity_unit_amount = random.randint(10, 10000)
# 生成套包说明
group_str = ""
for i in product_json:
    # 更新套包内每个商品活动价格
    i.update({"activityUnitAmount": activity_unit_amount})
    # 获取套包内包含商品信息
    product_name = i["productName"]
    option_str = i["optionStr"]
    quantity = i["quantity"]
    str1 = "{}({})*{}".format(product_name, option_str, quantity)
    group_str = str1 + "+" + group_str
# 生成套包说明
group_str = "平台套包编号:{} || 套包名称:{}({})".format(group_code, group_name, group_str)
# 更新请求报文中活动价格
fresenius_group.update({"products": product_json})
# 报文组装
fresenius_group.update(commonFuc().get_business_data(module, "payload32", id, product_code, product_name, manufacturer,
                                                     manufacturer_code, option_str, measuring_unit, material_code,
                                                     quantity))

# 第四步套包协议特价新增
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url35")
request_body = {"manufacturerGroupName": "", "manufacturerGroupCode": "", "groupName": "", "limitPurchase": "",
                "isLimit": 0, "freseniusGroup": fresenius_group, "groupStr": group_str, "customerCode": customer_code,
                "userName": user_name, "customerName": customer_name, "documentNo": ref_random,
                "time": [str(start_date), str(random_date)], "effectiveTime": str(start_date), "expirationTime": str(random_date)}

print(request_body)

"""

 场景: 单个新增验证_套包协议特价
 用例名称：单个新增验证_套包协议特价
 输出：{"success":true,"code":"200","message":"OK"}
"""

# 发送请求
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, "checkDict20")
# 断言实际结果中是否包含预期结果的内容
commonFuc().check_result(check_dict, result)
