# -*- encoding=utf8 -*-
from common.fileUtls import FileUtils
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,a_f4_采购单管理_采购单_催单,id2297,id2297-54,sit,on
内网:a_f4_采购单管理_采购单_催单 
"""

from common.common_func import commonFuc
from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
from airtest.core.api import using
import sys


def test1():
    module = "b3_herp3_purchase"
    """
     场景:a_f4_采购单管理_采购单_催单
     用例名称：a_f4_采购单管理_采购单_催单
     输入：无
     输出："rtn_msg": {"code": 0, "msg": null,    "data": true}
    """
    # 内网登录
    username = commonFuc().get_business_data(module, "username")
    # print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)
    #
    import json
    # 从文件提取单据id
    info = FileUtils().r_info8(module, "采购单信息", 'message2')
    print('info', info)
    list_a = info['list_a']
    print('list_a', list_a)
    createTime = 'createTime'
    for i in list_a:
        createTime = i['createTime']
        break
    json_str = json.dumps(list_a, ensure_ascii=False)
    print('json_str', json_str)
    json_str_no_brackets = json_str.replace("[", "").replace("]", "")
    print('json_str_no_brackets', json_str_no_brackets)
    # 追加数据到json中
    # import json

    # 原始JSON字符串
    data = json_str_no_brackets
    # 将JSON字符串转换为字典
    json_obj = json.loads(data)

    # 追加数据到JSON对象
    json_obj.update(commonFuc().get_business_data(module, 'payload19_22_2', createTime))

    print('json_obj', json_obj)
    # 将修改后的JSON对象转换回字符串
    # updated_data = json.dumps(json_obj,ensure_ascii=False)

    # print('updated_data',updated_data)
    # print(type(updated_data))

    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "herpService_purchase_sendMessage_url")
    # print(url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            commonFuc().get_business_data(module, "X-APP-CODE"))
    print('headers', headers)
    # # #获取日期
    # date1=timeUtils().get_time_hms(2)
    # date2=timeUtils().get_time_add(-1)
    # print(date1,date2)
    # # 分页查询
    # str = commonFuc().get_business_data(module, 'data6')
    # list2 = list(str.split(","))
    # print('list2', list2)
    # for i in list2:
    # 请求体
    request_body = json_obj
    print('request_body', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print('result', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    planOrderNo1 = commonFuc().analysis_json('planOrderNo', data)
    if code == 0 and data == 0:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)


test1()
