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

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,a_a3_手工采购计划_添加产品页面_列表查询_条件查询_模糊查询,id2297,id2297-3,sit,on
内网:a_a3_手工采购计划_添加产品页面_列表查询_条件查询_模糊查询 
"""

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_a7_手工采购计划_添加产品页面_列表查询_条件查询_模糊查询
     用例名称：a_a7_手工采购计划_添加产品页面_列表查询_条件查询_模糊查询
     输入：无
     输出："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, "mcmsBranchGoodsInfo_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)
    info = FileUtils().r_info8(module, "产品信息", 'message1')
    # print('info', info)
    list_a = info['list_a']
    # print('list_a', list_a)
    goodsMfrsName = "a"
    goodsName = "a"
    provName = "a"
    for i in list_a:
        goodsMfrsName = i['goodsMfrsName']
        goodsName = i['goodsName']
        provName = i['provName']
        break

    hospitalId = commonFuc().get_business_data(module, "hospitalId")
    branchId = commonFuc().get_business_data(module, "branchId")
    # # #获取日期
    # date1=timeUtils().get_time_hms(2)
    # date2=timeUtils().get_time_add(-1)
    # print(date1,date2)
    # # 请求体
    goodsMfrsName=goodsMfrsName[0:5]
    goodsName=goodsName[0:2]
    provName=goodsName[0:5]
    print('ffffffffffffffffffffffffffffffffffffffffffffff',goodsMfrsName)
    request_body = commonFuc().get_business_data(module, "payload_5_1",hospitalId,branchId,goodsMfrsName,goodsName,provName)
    # 发送请求
    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 len(data) >= 1 and total == 1:
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)

    # 按器械注册人模糊查询
    goodsMfrsName1 = goodsMfrsName[0:5]
    goodsMfrsName2 = goodsMfrsName[2:6]
    goodsMfrsName3 = goodsMfrsName[6:len(goodsMfrsName)]

    # str = commonFuc().get_business_data(module, 'data4')
    list2 = [goodsMfrsName1,goodsMfrsName2,goodsMfrsName3]
    print('list2', list2)
    for i in list2:
        # # 请求体
        request_body = commonFuc().get_business_data(module, "payload_6_1", hospitalId,branchId,i)
        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 len(data) >= 1 and total >= 1:
            commonFuc().check_text_exist(0, result)
        else:
            commonFuc().check_text_exist('error', result)
    # 按产品名称/编号/简称模糊查询
    str = commonFuc().get_business_data(module, 'data5')
    list2 = list(str.split(","))
    print('list2', list2)
    for i in list2:
        # # 请求体
        request_body = commonFuc().get_business_data(module, "payload_8_1", hospitalId,branchId,i)
        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 len(data) >= 1 and total >= 1:
            commonFuc().check_text_exist(0, result)
        else:
            commonFuc().check_text_exist('error', result)
    # 按进院供应商模糊查询
    str = commonFuc().get_business_data(module, 'data4')
    list2 = list(str.split(","))
    print('list2', list2)
    for i in list2:
        # # 请求体
        request_body = commonFuc().get_business_data(module, "payload_9_1", hospitalId,branchId,i)
        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 len(data) >= 1 and total >= 1:
            commonFuc().check_text_exist(0, result)
        else:
            commonFuc().check_text_exist('error', result)
test1()
