From a7c8d9c482d605260ab757608b9e8abf4ee14f55 Mon Sep 17 00:00:00 2001 From: bn_user Date: Tue, 2 Dec 2025 13:57:59 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20dags/OSV=5Fwith=5Fdocs.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/OSV_with_docs.py | 64 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/dags/OSV_with_docs.py b/dags/OSV_with_docs.py index 3aa2287..dc46881 100644 --- a/dags/OSV_with_docs.py +++ b/dags/OSV_with_docs.py @@ -291,6 +291,62 @@ def upsert_list_fin_portfel(**kwargs): else: return 'Обновлять нечего.' +def pogasheniya(**kwargs): + engine = get_db_engine() + query = text(""" + SELECT DISTINCT + osv.schet + , osv.uid_subkonto2 as uid_dogovor + , osv.subkonto2 as name + , osv.nomer + , osv.date_begin + , osv.date_end + FROM oborotno_salbdovaya_vedomostb osv + LEFT JOIN pogasheniya p on p.uid_dogovor = osv.uid_subkonto2 and p.schet = osv.schet + WHERE p.uid_dogovor is null and (osv.schet::text like '%03%' OR osv.schet::text like '%01%') + UNION ALL + SELECT + '76.07.1' as schet + , '00000000-0000-0000-0000-000000000000' as uid_dogovor + , 'Лизинг' as name + , osv.agreement_num as nomer + , osv.agreement_date as date_begin + , osv.redemption_date as date_end + FROM lizingi_garantii osv + LEFT JOIN pogasheniya p on p.nomer = osv.agreement_num + WHERE p.uid_dogovor is null + """) + df = pd.read_sql(query, engine) + with engine.begin() as conn: + if not df.empty: + conn.execute("CREATE TEMP TABLE temp_pogasheniya (schet text null, uid_dogovor text null, name text null, nomer text null, date_begin text null, date_end text null)") + df.to_sql('temp_pogasheniya', con=conn, if_exists='append', index=False, method='multi') + conn.execute(""" + INSERT INTO public.pogasheniya (schet, uid_dogovor, name, nomer, date_begin, date_end) + SELECT * FROM temp_pogasheniya + ON CONFLICT (schet, uid_dogovor, nomer) + DO UPDATE SET + name = EXCLUDED.name, + nomer = EXCLUDED.nomer, + date_begin = EXCLUDED.date_begin, + date_end = EXCLUDED.date_end + """) + # conn.execute(""" + # UPDATE public.pogasheniya fp + # SET id = subquery.new_id + # FROM ( + # SELECT + # id, + # ROW_NUMBER() OVER (ORDER BY uid_dogovor, schet, nomer) as new_id + # FROM public.pogasheniya + # ) AS subquery + # WHERE fp.id = subquery.id; + # """ + # ) + return 'Список обновлен.' + else: + return 'Обновлять нечего.' + with DAG( dag_id='data_download_from_1C_source_with_docs', default_args=default_args, @@ -312,4 +368,10 @@ with DAG( provide_context=True ) -read_data_1C_task >> upsert_list_fin_portfel_task \ No newline at end of file + pogasheniya_task = PythonOperator( + task_id='pogasheniya', + python_callable=pogasheniya, + provide_context=True + ) + +read_data_1C_task >> [upsert_list_fin_portfel_task, pogasheniya_task] \ No newline at end of file