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

__author__ = "xiaohesheng"

"""
case_tag:api,spd_core,a_a3_流程三_产品导入和审批,id2302,id2302-3,sit,on
主数据平台:a_a3_流程三_产品导入和审批
python runner_test.py tag id2302-3 debug mdm3
"""

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

module = "b5_spd3_core_business_flow"


def h_login():
    module = "a4_mdm3_cm"
    username = commonFuc().get_business_data(module, "username")
    password = commonFuc().get_business_data(module, "password1")
    print('医院名称', username)
    print(username, password, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    import time
    while True:
        try:
            token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
        except:
            time.sleep(6)
            continue
        else:
            return token, projectCode, uxid, corpId, username


def e_login(type=1):
    module = "a4_mdm3_cm"
    username = 'username'
    if type == 1:  # 注册一个新企业，并审批
        pro_path = commonFuc().get_pro_path()
        sys.path.append(pro_path + r'\\air_case\\a2_mdm3_ea\\企业审核.air')
        using(pro_path + r'\\air_case\\a2_mdm3_ea\\企业审核.air')
        from 企业审核 import test1

        info = FileUtils().r_info("a2_mdm3_ea", "企业信息")
        print(info)
        username = info['cname']
        print('aaaaaaaaaaaaaaaaaaaaaaaaaaa', username)
    else:  # 读取文件中存入的企业信息
        info = FileUtils().r_info("a2_mdm3_ea", "企业信息")
        print(info)
        username = info['cname']
        print('aaaaaaaaaaaaaaaaaaaaaaaaaaa', username)
    # a2供货关系申请_客户类型_医院_登录_企业登录
    # print('医院名称',username)
    password = commonFuc().get_business_data(module, "password")
    print(username, password)
    # 获取token和projectCode
    import time
    while True:
        try:
            token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
        except:
            time.sleep(6)
            continue
        else:
            return token, projectCode, uxid, corpId, username
            # break


# 高值
def add_goods():
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a5_mdm3_pdm"
    # b18供货目录申请_客户类型_医院_提交供货申请单_添加产品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "addGoodsHandleInfos_url")
    print('url', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    goodsname = commonFuc().randomString(5)
    request_body = commonFuc().get_business_data(module, "payload_1", h_corpId, h_name, corpId, e_name, goodsname,
                                                 e_name)
    # request_body = commonFuc().get_business_data(module, "payload10_1")

    # 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)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist(-1, result)


def apply():
    module = "a5_mdm3_pdm"

    token, projectCode, uxid, corpId, username = e_login(2)
    # 获取医院corpId

    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    # 登录
    username = h_name
    password = commonFuc().get_business_data(module, "password1")
    print(username, password)
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 审批产品驳回
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "singleAudit_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    info = FileUtils().r_info6(module, "产品审核信息", 'message2')
    print(info)
    MSPid = info['MSPid']
    hosId = info['hosId']
    hosGoodsId = info['hosGoodsId']
    provGoodsid = info['provGoodsid']
    goodsAuditBillId = info['goodsAuditBillId']
    targetId = info['targetId']

    request_body = commonFuc().get_business_data(module, "payload3_1", MSPid, hosId, hosGoodsId, provGoodsid,
                                                 goodsAuditBillId, targetId)

    print('产品信息审核查询请求体', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print("产品信息审核查询返回结果", result)
    checkDict = commonFuc().get_business_data(module, 'checkDict')
    commonFuc().check_result(checkDict, result)


def select1(messageNO='message2'):
    module = "a5_mdm3_pdm"

    token, projectCode, uxid, corpId, username = e_login(2)
    # 获取医院corpId

    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    # 登录
    username = h_name
    password = commonFuc().get_business_data(module, "password1")
    print(username, password)
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # 产品信息审核查询
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "getAuditGoodsListByPage_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)

    request_body = commonFuc().get_business_data(module, "payload2", h_corpId)

    # print('产品信息审核查询请求体', request_body)
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    print("产品信息审核查询返回结果", result)
    data = commonFuc().analysis_json('data', result)
    total = commonFuc().analysis_json('total', data)
    data1 = commonFuc().analysis_json('data', data)
    if total >= 1:
        id1 = commonFuc().analysis_json('id', data1[0])
        auditOrgId = commonFuc().analysis_json('hosId', data1[0])
        hosGoodsId = commonFuc().analysis_json('hosGoodsid', data1[0])
        provGoodsid = commonFuc().analysis_json('provGoodsid', data1[0])
        goodsAuditBillId = commonFuc().analysis_json('billId', data1[0])
        targetId = commonFuc().analysis_json('id', data1[0])
        print(id1, auditOrgId, hosGoodsId, provGoodsid, goodsAuditBillId, targetId)
        info = (id1, auditOrgId, hosGoodsId, provGoodsid, goodsAuditBillId, targetId)
        titlename = ("MSPid", 'hosId', 'hosGoodsId', 'provGoodsid', 'goodsAuditBillId', 'targetId')
        # 将产品审核信息写入到b5_spd3_core_business_flow模块messsage文件中
        FileUtils().w_info6(info, module, '产品审核信息', titlename, 'message2')
        # 将产品审核信息写入到文件中
        FileUtils().w_info8(info, 'b5_spd3_core_business_flow', '产品审核信息', titlename, messageNO)
    else:
        commonFuc().check_text_exist(0, result)


def add_goods1():  # 低值
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a5_mdm3_pdm"
    # b18供货目录申请_客户类型_医院_提交供货申请单_添加产品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "addGoodsHandleInfos_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    goodsname = commonFuc().randomString(5)
    request_body = commonFuc().get_business_data(module, "payload", h_corpId, h_name, corpId, e_name, goodsname, e_name)
    # request_body = commonFuc().get_business_data(module, "payload10_1")

    # 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)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist(-1, result)


def add_goods2():  # 试剂
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a5_mdm3_pdm"
    # b18供货目录申请_客户类型_医院_提交供货申请单_添加产品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "addGoodsHandleInfos_url")
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    goodsname = commonFuc().randomString(5)
    request_body = commonFuc().get_business_data(module, "payload_2", h_corpId, h_name, corpId, e_name, goodsname,
                                                 e_name)
    # request_body = commonFuc().get_business_data(module, "payload10_1")

    # 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)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist(-1, result)


def add_goods3():
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a5_mdm3_pdm"
    # b18供货目录申请_客户类型_医院_提交供货申请单_添加产品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "addGoodsHandleInfos_url")
    print('url', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    goodsname = commonFuc().randomString(5)
    request_body = commonFuc().get_business_data(module, "payload_1_1", h_corpId, h_name, corpId, e_name, goodsname,
                                                 '浙江高仰医疗科技有限公司', 'p10d95')
    # request_body = commonFuc().get_business_data(module, "payload10_1")

    # 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)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist(-1, result)


def add_goods4():
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 获取医院corpId
    h_token, h_projectCode, h_uxid, h_corpId, h_name = h_login()

    module = "a5_mdm3_pdm"
    # b18供货目录申请_客户类型_医院_提交供货申请单_添加产品
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "addGoodsHandleInfos_url")
    print('url', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)
    goodsname = commonFuc().randomString(5)
    request_body = commonFuc().get_business_data(module, "payload_1_2", h_corpId, h_name, corpId, e_name, goodsname,
                                                 '浙江高仰医疗科技有限公司', 'p10d95')
    # request_body = commonFuc().get_business_data(module, "payload10_1")

    # 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)
    if code == 0:
        commonFuc().check_text_exist(code, result)
    else:
        commonFuc().check_text_exist(-1, result)


def supplyProvHosGoods_add_DI():
    module = "a5_mdm3_pdm"
    token, projectCode, uxid, corpId, e_name = e_login(2)

    # 维护产品DI信息列表查询
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "hdiFusion_getHosGoodsForDiList_url")
    print('维护产品DI信息列表查询url', url)
    # 获取请求头信息
    headers = commonFuc().get_business_data(module, "json_headers2",
                                            commonFuc().get_business_data(module, "json_contentType"), token,
                                            projectCode)

    request_body = commonFuc().get_business_data(module, "payload6", get_hosid())
    # 发送请求
    result = commonFuc().http_post(url, request_body, headers)
    # import pdb
    # pdb.set_trace()
    print('维护产品DI信息列表查询result', result)
    data = commonFuc().analysis_json('data', commonFuc().analysis_json('data', result))
    print('维护产品DI信息列表查询data', data)
    for i in data:
        hosGoodsId = commonFuc().analysis_json('id', i)
        url1 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "hdiFusion_getGoodsinfoAndDiLst_url")
        request_body1 = commonFuc().get_business_data(module, "payload6_1", hosGoodsId)
        print('request_body1', request_body1)
        result1 = commonFuc().http_post(url1, request_body1, headers)
        packUnit = commonFuc().analysis_json('unit', commonFuc().analysis_json('goodsInfo',
                                                                               commonFuc().analysis_json('data',
                                                                                                         result1)))
        basGoodsId = commonFuc().analysis_json('spdGoodsCode', commonFuc().analysis_json('goodsInfo',
                                                                                         commonFuc().analysis_json(
                                                                                             'data', result1)))
        url2 = commonFuc().get_api_url() + commonFuc().get_business_data(module, "hdiFusion_supplyProvHosGoods_add_url")
        request_body2 = commonFuc().get_business_data(module, "payload6_2", packUnit, basGoodsId)
        print('request_body2', request_body2)
        result2 = commonFuc().http_post(url2, request_body2, headers)
        print('result2', result2)


# 删除上次新增的产品，避免产生过多的重复数据====这里不能每天删除，万一导入产品功能出现问题会影响后续脚本
# 每隔一段时间来删除重复的产品数据即可，内外网同步删除
# try:
#     list2=[]
#     for i in range(1, 6):
#         info = FileUtils().r_info8('b5_spd3_core_business_flow', "产品审核信息", 'message%s' % i)
#         hosGoodsId = info['hosGoodsId']
#         list2.append(hosGoodsId)
#     print(list2)
#     for i in list2:
#         print(i,'aaaaaaaaaaaaaaaa')
#         delData().Del_goods_info(i)
# except:
#     print('产品数据删除出现异常')
# 删除上次新增的产品，避免产生过多的重复数据====这里不能每天删除，万一导入产品功能出现问题会影响后续脚本
# 每隔一段时间来删除重复的产品数据即可，内外网同步删除
def main():
    try:
        # ==============分割线 暂时注释===============
        add_goods()#直销高值
        select1('message1')
        apply()
        add_goods1()#直销低值
        select1('message2')
        apply()
        add_goods2()#直销试剂
        select1('message3')
        apply()
        add_goods3()#集配
        select1('message4')
        apply()
        add_goods4()#定向 "isSubprov": "1"
        select1('message5')
        apply()
        #20240618外网在配送的时候增加了DI验证，加入维护di的脚本
        supplyProvHosGoods_add_DI()
        # ==============分割线 暂时注释===============
        commonFuc().check_text_exist_result_text('succees', 'succees')
    except:
        print('新增产品信息出现异常,后续的脚本必然无法走通,需要检查程序或者脚本或者环境是否出现问题')
        commonFuc().check_text_exist_result_text('产品新增失败', '产品新增成功')
    else:
        # 删除今天之前的产品信息(包含内外网)
        delData().Del_goods_info_all()
        commonFuc().check_text_exist_result_text('succees', 'succees')
        print('=================流程三结束=================')
# main()