Обновить dags/fin_porfel.py

This commit is contained in:
bn_user 2025-11-13 18:09:56 +00:00
parent c44863c23b
commit 0c5486ce61
1 changed files with 24 additions and 3 deletions

View File

@ -36,7 +36,8 @@ def upsert_list_fin_portfel(**kwargs):
engine = get_db_engine()
query = """
select distinct
osv.schet as schet
osv.uid_dogovor
, osv.schet as schet
, osv.subkonto2 as subkonto2
, null as summa_dogovora
, null as percent_value
@ -50,9 +51,29 @@ def upsert_list_fin_portfel(**kwargs):
df = pd.read_sql(query, engine)
with engine.begin() as conn:
if not df.empty:
conn.execute("CREATE TEMP TABLE temp_fin_porfel (schet text null, subkonto2 text null, summa_dogovora text null, percent_value text null)")
conn.execute("CREATE TEMP TABLE temp_fin_porfel (uid_dogovor text null, schet text null, subkonto2 text null, summa_dogovora text null, percent_value text null)")
df.to_sql('temp_fin_porfel', con=conn, if_exists='append', index=False, method='multi')
conn.execute("INSERT INTO public.fin_porfel (schet, subkonto2, summa_dogovora, percent_value) SELECT distinct schet, subkonto2, summa_dogovora, percent_value FROM temp_fin_porfel")
conn.execute("""
INSERT INTO public.fin_porfel (uid_dogovor, schet, subkonto2, summa_dogovora, percent_value)
SELECT DISTINCT
uid_dogovor
, schet
, subkonto2
, summa_dogovora
, percent_value
FROM temp_fin_porfel
ON CONFLICT (uid_dogovor, schet)
DO UPDATE SET
subkonto2 = EXCLUDED.subkonto2,
summa_dogovora = EXCLUDED.summa_dogovora,
percent_value = EXCLUDED.percent_value
"""
)
conn.execute("""
UPDATE public.fin_porfel fp
SET fp.id = row_nubmer() over (order by uid_dogovor, schet, summa_dogovora)
"""
)
return 'Список обновлен.'
else:
return 'Обновлять нечего.'