From 4ca0243506848954901aecbc88091fee778cea05 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Mon, 29 Jun 2026 10:51:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E5=A2=9E=E6=8E=88=E4=BF=A1?= =?UTF-8?q?=E6=94=B9=E4=B8=BAUPSERT=EF=BC=8C=E9=81=BF=E5=85=8Daccountid?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E7=BA=A6=E6=9D=9F=E5=86=B2=E7=AA=81=EF=BC=9B?= =?UTF-8?q?uuid=E6=94=B9=E4=B8=BAgetID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../credit_limit/api/set_customer_credit.dspy | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/wwwroot/credit_limit/api/set_customer_credit.dspy b/wwwroot/credit_limit/api/set_customer_credit.dspy index 23e7c0d..f34bcc4 100644 --- a/wwwroot/credit_limit/api/set_customer_credit.dspy +++ b/wwwroot/credit_limit/api/set_customer_credit.dspy @@ -89,7 +89,32 @@ async with db.sqlorContext(dbname) as sor: } } - if record_id: + # Check if credit limit already exists for this account (UNIQUE on accountid) + exist_sql = "select id, used_credit from credit_limit where accountid = ${accountid}$" + exist_recs = await sor.sqlExe(exist_sql, {'accountid': accountid}) + + if exist_recs and len(exist_recs) > 0: + # Account already has a credit limit record — update it + existing = exist_recs[0] + sql = """ + UPDATE credit_limit + SET credit_limit = ${credit_limit}$, + available_credit = ${credit_limit}$ - used_credit, + valid_from = ${valid_from}$, + valid_to = ${valid_to}$, + remark = ${remark}$, + updated_at = CURRENT_TIMESTAMP + WHERE accountid = ${accountid}$ + """ + await sor.sqlExe(sql, { + 'credit_limit': credit_limit_amount, + 'valid_from': valid_from, + 'valid_to': valid_to, + 'remark': remark, + 'accountid': accountid + }) + debug(f'Updated credit limit for {customer.orgname}(accountid={accountid}): {credit_limit_amount}') + elif record_id: sql = """ UPDATE credit_limit SET credit_limit = ${credit_limit}$, @@ -110,7 +135,7 @@ async with db.sqlorContext(dbname) as sor: }) debug(f'Updated credit limit {record_id} to {credit_limit_amount}') else: - new_id = uuid() + new_id = getID() now = datetime.datetime.now() data = { 'id': new_id,