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

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,a_a4_期初录入_低值_录入_获取单据信息,id2301,id2301-4,sit,on
内网:a_a4_期初录入_低值_录入_获取单据信息
"""

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(QCid='QCh034720240409000239'):
    module = "b4_herp3_InitStock"
    """
     场景:a_a4_期初录入_低值_录入_获取单据信息
     用例名称：a_a4_期初录入_低值_录入_获取单据信息
     输入：无
     输出："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_mcmsInitStock_getById_url")
    url=url+QCid
    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)
    # #从文件中获取库房名称，写脚本时用已经存在的库房，方便编写脚本和调试脚本
    # stock_name=commonFuc().get_business_data(module,'stock_name')
    # #============================================
    # #从基础数据模块写入的库房名称中获取（暂时注释）
    # # branch_name = FileUtils().r_info('b2_herp3_bs', '院区新增')["branch_name"]
    # # stock_name=branch_name+'中心库房'
    # #=============================================
    # #通过数据库获取库房id和库区id===============
    # stockId=sql_get_info().get_mcms_stock_info(stock_name)
    # areaCode=sql_get_info().get_mcms_stock_shelf_info(stockId)
    # stockId = stockId.replace("'", "")
    # areaCode=areaCode.replace("'","")
    # # 通过数据库获取库房id和库区id===============
    # # # 请求体
    # hospitalId = commonFuc().get_business_data(module, "hospitalId")
    # branchId = commonFuc().get_business_data(module, "branchId")
    # request_body = commonFuc().get_business_data(module, "payload", stockId,areaCode,hospitalId, branchId)
    # print('request_body',request_body)
    # 发送请求
    result = commonFuc().http_get(url, headers)
    print('result', result)
    # 断言
    code = commonFuc().analysis_json('code', result)
    data = commonFuc().analysis_json('data', result)
    if code == 0 and len(data) >= 1 :
        commonFuc().check_text_exist(0, result)
    else:
        commonFuc().check_text_exist('error', result)
    # 返回的单号写入文件
    print(type(data), data)
    list_a = []
    list_b = []
    list_c = []
    list_a.append(data)
    list_b.append(data)
    list_c.append(data)
    # 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, 'message1')
try:
    test1()
except:
    print('期初单号不存在,请检查脚本或者数据')
    # 从文件读取期初单号_低值
    module = "b4_herp3_InitStock"
    info = FileUtils().r_info8(module, "期初单号低值", 'message')
    list2 = info['list_a']
    QCID = ' '.join(list2)
    print('bbbb', QCID, type(QCID))
    test1(QCID)
# if __name__ == '__main__':
#     test1()