# -*- encoding=utf8 -*-
from common.db.sql.sql_del_branch_info import delData
from common.fileUtls import FileUtils
from common.timeUtils import timeUtils

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,a_m9_手术通知单_列表查询_条件查询_状态,id2297,id2297-129,sit,on
内网:a_m9_手术通知单_列表查询_条件查询_状态 
"""

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_m9_手术通知单_列表查询_条件查询_状态
     用例名称：a_m9_手术通知单_列表查询_条件查询_状态
     输入：无
     输出："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)
    #
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "herpService_operationNotice_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)
    # 从文件中获取院区id
    branchId = commonFuc().get_business_data(module, 'branchId')
    # # #获取日期
    date1 = timeUtils().get_time_hms(5)
    date2 = timeUtils().get_time_add(-180, 2)
    # print(date1,date2)
    # 从文件中获取手术申请单号
    info = FileUtils().r_info8(module, "手术通知单信息", 'message8')
    # print('info', info)
    list_a = info['list_a']
    # print('list_a', list_a)
    orderNo = 'orderNo'
    sourceOrderNo = 'sourceOrderNo'
    provName='provName'
    status='status'
    for i in list_a:
        orderNo = i['orderNo']
        sourceOrderNo = i['sourceOrderNo']
        provName=i['provName']
        status=i['status']
        break
    if status=='已关闭':
        status=70
    ##请求体
    request_body = commonFuc().get_business_data(module, "payload23_4", orderNo, provName,sourceOrderNo, status,date2, date1, branchId)
    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)
    total = commonFuc().analysis_json('total', data)
    if code == 0 and total == 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
    #依次根据不同的状态查询
    str=commonFuc().get_business_data(module,'status3')
    list2=list(str.split(","))
    for i in list2:
        ##请求体
        request_body = commonFuc().get_business_data(module, "payload23_5",  i, branchId)
        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)
        total = commonFuc().analysis_json('total', data)
        if code == 0 and total >=0:
            commonFuc().check_text_exist(0, result)
        else:
            commonFuc().check_text_exist('error', result)
        #将待确认的单据信息写入文件中
        if i=="10":
            data1 = commonFuc().analysis_json('data', data)
            # id1 = commonFuc().analysis_json('id', data1)
            print(type(data1), data1)
            list_a = []
            list_b = []
            list_c = []
            list_a.append(data1[0])
            list_b.append(data1[0])
            list_c.append(data1[0])
            print('list_a', len(list_a), list_a)
            info = (list_a, list_b, list_c)
            titlename = ('list_a', 'list_b', 'list_c')
            # 将手术通知单信息写入文件中
            FileUtils().w_info8(info, module, '手术通知单信息', titlename, 'message8')
test1()
