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,6 +89,7 @@ async with db.sqlorContext(dbname) as sor:
} }
} }
try:
# Check if credit limit already exists for this account (UNIQUE on accountid) # 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_sql = "select id, used_credit from credit_limit where accountid = ${accountid}$"
exist_recs = await sor.sqlExe(exist_sql, {'accountid': accountid}) exist_recs = await sor.sqlExe(exist_sql, {'accountid': accountid})
@ -166,6 +167,31 @@ async with db.sqlorContext(dbname) as sor:
} }
} }
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",
"options": { "options": {

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