yl6688

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

Python 通过数据库取往来单位名称和组织机构代码,调用阿里健康接口,获取码上放心入住码并写入数据库

import cx_Oracle
from datetime import datetime
from topsdk.client import TopApiClient,TopException
from topsdk.defaultability.defaultability import Defaultability
from topsdk.defaultability.request.alibaba_alihealth_drug_msc_getentinfolist_request import *

if __name__ == '__main__':
    client = TopApiClient(appkey='**', app_sercet='**', top_gateway_url='http://gw.api.taobao.com/router/rest',verify_ssl=False)
    ability = Defaultability(client=client)
    
    # 建立数据库连接
    connection = cx_Oracle.connect(user="**", password="**", dsn="36.*.*.*:1521/orcl")
    
    # 创建游标
    cursor = connection.cursor()
    
    # 执行查询
    cursor.execute("""
        select a.compid, a.compname, a.vencusno, a.vencusname, b.photo_no 
        from v_vencus a, v_vencus_certificate b 
        where a.compid=b.compid and a.vencusno=b.vencusno 
        and b.certificateid=1 and a.compid=115 and a.status=1
    """)
    
    rows = cursor.fetchall()
    
    for row in rows:
        # 从查询结果中提取数据
        compid = row[0]
        compname = row[1]
        vencusno = row[2]
        vencusname = row[3]
        photo_no = row[4]
        
        # 创建请求参数
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto = AlibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto()
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto.ent_name = vencusname
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto.org_code = photo_no if photo_no else ''
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto.medical_code = '陕CB912000061'
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto.par_ref_ent_id = ''
        alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto.ent_id = ''
        
        # 创建请求
        request = AlibabaAlihealthDrugMscGetentinfolistRequest()
        request.ref_ent_id = '0b7f5c31b76d455f8898ba5318accc23'
        request.query_param = alibabaAlihealthDrugMscGetentinfolistTopEntInfoReqDto
        
        try:
            # 调用API
            response = ability.alibaba_alihealth_drug_msc_getentinfolist(request)
            
            # 获取API返回的数据
            ent_id = response['result']['model'][0]['ent_id']
            ref_ent_id = response['result']['model'][0]['ref_ent_id']
            
            print(f"compid: {compid}, vencusno: {vencusno}, ent_id: {ent_id}, ref_ent_id: {ref_ent_id}")
            # 插入数据
            insert_sql = """
                INSERT INTO taobao_rzm_wms(compid, vencusno, vencusname, photo_no, ent_id, ref_ent_id) 
                VALUES (:1, :2, :3, :4, :5, :6)
            """
            cursor.execute(insert_sql, (compid, vencusno, vencusname, photo_no, ent_id, ref_ent_id))
            connection.commit()
            
        except TopException as e:
            print(f"API调用异常: {e}")
        except cx_Oracle.DatabaseError as e:
            print(f"数据库操作异常: {e}")
            connection.rollback()  # 回滚事务
        except Exception as e:
            print(f"其他异常: {e}")
            connection.rollback()  # 回滚事务
    
    # 关闭游标和连接
    cursor.close()
    connection.close()

 

posted on 2026-04-02 16:36  追梦寒星  阅读(28)  评论(0)    收藏  举报