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

"""
case_tag:cmdc_api,cmdc特价单品新增功能验证,2264,2264-60,sit,bs
主数据平台:后台运营系统客户列表查询、商品列表查询、特价单品新增等接口
"""

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

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)
# 随机生成唯一字符串用于ref
ref_random = str(uuid.uuid4())
# 客户信息获取
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, "product_list")
request_body = commonFuc().get_business_data(module, "product_payload")
# 发送请求
result = requests.post(url, json=request_body, headers=headers)
result = json.loads(result.content)

# 获取商品信息
product_detail = result["data"]["list"][random.randint(1, len(result["data"]["list"]))-1]
product_detail["activityUnitAmount"] = customer_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)
# 第四步特价单品新增
url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "url17")
request_body = {"manufacturerGroupName": "", "manufacturerGroupCode": "", "groupName": "", "limitPurchase": "",
                "isLimit": 0, "products": [product_detail], "customerCode": customer_code, "userName": user_name,
                "customerName": customer_name, "times": [str(start_date), str(random_date)],
                "documentNo": ref_random,
                "supplementNo": customer_quantity, "salesProvince": customer_quantity, "salesArea": customer_quantity,
                "applicationType": customer_quantity, "effectiveTime": str(start_date),
                "expirationTime": str(random_date),
                "freseniusProduct": product_detail}

"""
 场景: 特价单品新增功能验证
 用例名称：特价单品新增功能验证
 输出：{"total": 1}
"""

# 发送请求
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

# print(result)
if result["success"]:
    # 数据库操作
    mysql_handle = mySql()
    # 获取conf.ini文件中配置的数据库信息
    host, port, user, pwd = mysql_handle.getConf(db="cmdc_db")

    # 数据库查询新增特价单品
    sql = "SELECT t.groupId FROM `cmdc-order`.tc_fresenius_agreement_price t " \
          "WHERE customerCode = {} and documentNo = '{}';".format(customer_code, ref_random)
    # print(sql)
    total = len(mysql_handle.selectSql(host, port, user, pwd, "cmdc-order", sql))
    result = {"total": total}

    result["api_time"] = api_time

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