fix: 重写get_credit_limit移除default_filterjson; set_customer_credit加try/except防Duplicate 500

This commit is contained in:
yumoqing 2026-06-29 10:55:12 +08:00
parent 4ca0243506
commit cd26eb1852
2 changed files with 151 additions and 194 deletions

View File

@ -89,82 +89,108 @@ async with db.sqlorContext(dbname) as sor:
} }
} }
# Check if credit limit already exists for this account (UNIQUE on accountid) try:
exist_sql = "select id, used_credit from credit_limit where accountid = ${accountid}$" # Check if credit limit already exists for this account (UNIQUE on accountid)
exist_recs = await sor.sqlExe(exist_sql, {'accountid': 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: if exist_recs and len(exist_recs) > 0:
# Account already has a credit limit record — update it # Account already has a credit limit record — update it
existing = exist_recs[0] existing = exist_recs[0]
sql = """ sql = """
UPDATE credit_limit UPDATE credit_limit
SET credit_limit = ${credit_limit}$, SET credit_limit = ${credit_limit}$,
available_credit = ${credit_limit}$ - used_credit, available_credit = ${credit_limit}$ - used_credit,
valid_from = ${valid_from}$, valid_from = ${valid_from}$,
valid_to = ${valid_to}$, valid_to = ${valid_to}$,
remark = ${remark}$, remark = ${remark}$,
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE accountid = ${accountid}$ WHERE accountid = ${accountid}$
""" """
await sor.sqlExe(sql, { await sor.sqlExe(sql, {
'credit_limit': credit_limit_amount, 'credit_limit': credit_limit_amount,
'valid_from': valid_from, 'valid_from': valid_from,
'valid_to': valid_to, 'valid_to': valid_to,
'remark': remark, 'remark': remark,
'accountid': accountid 'accountid': accountid
}) })
debug(f'Updated credit limit for {customer.orgname}(accountid={accountid}): {credit_limit_amount}') debug(f'Updated credit limit for {customer.orgname}(accountid={accountid}): {credit_limit_amount}')
elif record_id: elif record_id:
sql = """ sql = """
UPDATE credit_limit UPDATE credit_limit
SET credit_limit = ${credit_limit}$, SET credit_limit = ${credit_limit}$,
available_credit = ${credit_limit}$ - used_credit, available_credit = ${credit_limit}$ - used_credit,
valid_from = ${valid_from}$, valid_from = ${valid_from}$,
valid_to = ${valid_to}$, valid_to = ${valid_to}$,
remark = ${remark}$, remark = ${remark}$,
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE id = ${id}$ AND orgid = ${orgid}$ WHERE id = ${id}$ AND orgid = ${orgid}$
""" """
await sor.sqlExe(sql, { await sor.sqlExe(sql, {
'credit_limit': credit_limit_amount, 'credit_limit': credit_limit_amount,
'valid_from': valid_from, 'valid_from': valid_from,
'valid_to': valid_to, 'valid_to': valid_to,
'remark': remark, 'remark': remark,
'id': record_id, 'id': record_id,
'orgid': orgid 'orgid': orgid
}) })
debug(f'Updated credit limit {record_id} to {credit_limit_amount}') debug(f'Updated credit limit {record_id} to {credit_limit_amount}')
else: else:
new_id = getID() new_id = getID()
now = datetime.datetime.now() now = datetime.datetime.now()
data = { data = {
'id': new_id, 'id': new_id,
'accountid': accountid, 'accountid': accountid,
'orgid': orgid, 'orgid': orgid,
'credit_limit': credit_limit_amount, 'credit_limit': credit_limit_amount,
'used_credit': 0, 'used_credit': 0,
'available_credit': credit_limit_amount, 'available_credit': credit_limit_amount,
'valid_from': valid_from, 'valid_from': valid_from,
'valid_to': valid_to, 'valid_to': valid_to,
'status': 'active', 'status': 'active',
'created_at': now, 'created_at': now,
'updated_at': now, 'updated_at': now,
'created_by': user_id, 'created_by': user_id,
'remark': remark 'remark': remark
} }
await sor.C('credit_limit', data) await sor.C('credit_limit', data)
debug(f'Created credit limit for {customer.orgname}(orgid={customer_orgid}, accountid={accountid}): {credit_limit_amount}') debug(f'Created credit limit for {customer.orgname}(orgid={customer_orgid}, accountid={accountid}): {credit_limit_amount}')
return { return {
"widgettype": "Message", "widgettype": "Message",
"options": { "options": {
"cwidth": 16, "cwidth": 16,
"cheight": 9, "cheight": 9,
"title": "授信额度设置成功", "title": "授信额度设置成功",
"timeout": 3, "timeout": 3,
"message": "ok" "message": "ok"
}
}
except Exception as e:
err_msg = str(e)
debug(f'set_customer_credit error: {format_exc()}')
if 'Duplicate' in err_msg or '1062' in err_msg:
return {
"widgettype": "Error",
"options": {
"title": "重复数据",
"cwidth": 16,
"cheight": 9,
"timeout": 5,
"message": f"客户 {customer.orgname} 已有授信记录,请使用调整功能修改额度"
}
}
return {
"widgettype": "Error",
"options": {
"title": "设置失败",
"cwidth": 16,
"cheight": 9,
"timeout": 5,
"message": err_msg
}
} }
}
return { return {
"widgettype": "Error", "widgettype": "Error",

View File

@ -1,128 +1,59 @@
ns = params_kw.copy() result = {'total': 0, 'rows': []}
try:
page = int(params_kw.get('page', 1))
rows_per_page = int(params_kw.get('rows', params_kw.get('pagerows', 50)))
offset = (page - 1) * rows_per_page
sort_field = params_kw.get('sort', 'created_at desc')
debug(f'get_credit_limit.dspy:{ns=}') # Build filter conditions from data_filter
if not ns.get('page'): conditions = ['1=1']
ns['page'] = 1 ns = {}
if not ns.get('sort'):
ns['sort'] = 'created_at desc'
data_filter = params_kw.get('data_filter')
filterjson = None
if data_filter:
try:
filterjson = json.loads(data_filter) if isinstance(data_filter, str) else data_filter
except:
pass
sql = '''select a.*, b.accountid_text, c.orgid_text, d.status_text if filterjson:
from (select * from credit_limit where 1=1 [[filterstr]]) a filter_fields = ['orgid', 'status']
left join (select id as accountid, id as accountid_text from account where 1 = 1) b on a.accountid = b.accountid for f in filter_fields:
left join (select id as orgid, orgname as orgid_text from organization where 1 = 1) c on a.orgid = c.orgid val = params_kw.get(f)
left join (select k as status, v as status_text from appcodes_kv where parentid='credit_status') d on a.status = d.status''' if val:
conditions.append(f'a.{f}=${f}$')
ns[f] = val
filterjson = params_kw.get('data_filter') where_clause = 'WHERE ' + ' AND '.join(conditions)
fields_str=r'''[
{
"name": "id",
"title": "id",
"type": "str",
"length": 32
},
{
"name": "accountid",
"title": "账户ID",
"type": "str",
"length": 32
},
{
"name": "orgid",
"title": "机构ID",
"type": "str",
"length": 32
},
{
"name": "credit_limit",
"title": "信用额度",
"type": "float",
"length": 18,
"dec": 2
},
{
"name": "used_credit",
"title": "已用额度",
"type": "float",
"length": 18,
"dec": 2
},
{
"name": "available_credit",
"title": "可用额度",
"type": "float",
"length": 18,
"dec": 2
},
{
"name": "valid_from",
"title": "生效日期",
"type": "date"
},
{
"name": "valid_to",
"title": "失效日期",
"type": "date"
},
{
"name": "status",
"title": "状态",
"type": "str",
"length": 10
},
{
"name": "created_at",
"title": "创建时间",
"type": "timestamp"
},
{
"name": "updated_at",
"title": "更新时间",
"type": "timestamp"
},
{
"name": "created_by",
"title": "创建人",
"type": "str",
"length": 32
},
{
"name": "remark",
"title": "备注",
"type": "str",
"length": 500
}
]'''
ori_fields = json.loads(fields_str)
if not filterjson:
fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns)
filterdic = ns.copy()
filterdic['filterstr'] = ''
filterdic['userorgid'] = '${userorgid}$'
filterdic['userid'] = '${userid}$'
if filterjson:
dbf = DBFilter(filterjson)
conds = dbf.gen(ns)
if conds:
ns.update(dbf.consts)
conds = f' and {conds}'
filterdic['filterstr'] = conds
ac = ArgsConvert('[[', ']]')
vars = ac.findAllVariables(sql)
NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' }
filterdic.update(NameSpace)
sql = ac.convert(sql, filterdic)
debug(f'{sql=}') count_sql = f'select count(*) as cnt from credit_limit {where_clause}'
db = DBPools() data_sql = f'''select a.*,
dbname = get_module_dbname('accounting') acc.id as accountid_text,
async with db.sqlorContext(dbname) as sor: org.orgname as orgid_text,
r = await sor.sqlPaging(sql, ns) akv.v as status_text
return r from credit_limit a
return { left join account acc on a.accountid = acc.id
"total":0, left join organization org on a.orgid = org.id
"rows":[] left join appcodes_kv akv on a.status = akv.k and akv.parentid = 'credit_status'
} {where_clause}
order by {sort_field}
limit {rows_per_page} offset {offset}'''
db = DBPools()
dbname = get_module_dbname('accounting')
async with db.sqlorContext(dbname) as sor:
count_recs = await sor.sqlExe(count_sql, ns)
total = count_recs[0].cnt if count_recs else 0
rows = await sor.sqlExe(data_sql, ns)
result['rows'] = rows if rows else []
result['total'] = total
except Exception as e:
debug(f'get_credit_limit error: {format_exc()}')
return result