feat: credit_limit 加 grant_orgid 授予机构字段,支持多租户

credit_limit 新增 grant_orgid 字段,区分「客户所属机构(orgid)」
与「授予信用的机构(grant_orgid)」。多租户下,操作员为客户授信时:
- orgid = 客户机构ID
- grant_orgid = 操作员所属机构ID

修改:
- models/credit_limit.json: 加 grant_orgid 字段(VARCHAR(32), default 0) + 索引
- creditlimit.py:
  get_credit_stats/get_all_customer_credits: WHERE orgid → WHERE grant_orgid
  set_credit_limit: 加 grant_orgid 参数
- set_customer_credit.dspy: 写入时 orgid=客户orgid, grant_orgid=操作员orgid;
  三处UPDATE同步加 grant_orgid
- credit_summary.dspy: WHERE orgid → WHERE grant_orgid

get_my_credit_list(客户自看)保持按 cl.orgid 过滤不变。
This commit is contained in:
yumoqing 2026-06-29 12:46:49 +08:00
parent 17554a1038
commit bda9e7fc00
4 changed files with 26 additions and 5 deletions

View File

@ -17,7 +17,7 @@ async def get_credit_stats(sor, orgid):
COUNT(CASE WHEN status = 'active' THEN 1 END) as active_count, COUNT(CASE WHEN status = 'active' THEN 1 END) as active_count,
COUNT(CASE WHEN status = 'expired' THEN 1 END) as expired_count COUNT(CASE WHEN status = 'expired' THEN 1 END) as expired_count
FROM credit_limit FROM credit_limit
WHERE orgid = ${orgid}$ WHERE grant_orgid = ${orgid}$
""" """
recs = await sor.sqlExe(sql, {'orgid': orgid}) recs = await sor.sqlExe(sql, {'orgid': orgid})
if recs and len(recs) > 0: if recs and len(recs) > 0:
@ -71,7 +71,7 @@ async def get_all_customer_credits(sor, orgid, status_filter=None):
Get all customer credit limits for management view. Get all customer credit limits for management view.
For distributor sales to see all their customers' credit status. For distributor sales to see all their customers' credit status.
""" """
where_clause = "WHERE cl.orgid = ${orgid}$" where_clause = "WHERE cl.grant_orgid = ${orgid}$"
params = {'orgid': orgid, 'sort': 'update_at desc'} params = {'orgid': orgid, 'sort': 'update_at desc'}
if status_filter and status_filter != 'all': if status_filter and status_filter != 'all':
where_clause += " AND cl.status = ${status}$" where_clause += " AND cl.status = ${status}$"
@ -145,7 +145,7 @@ async def update_used_credit(sor, accid, new_used_amount):
debug(f'Updated credit for {accid}: used={new_used}, available={new_available}') debug(f'Updated credit for {accid}: used={new_used}, available={new_available}')
async def set_credit_limit(sor, accountid, orgid, credit_limit_amount, async def set_credit_limit(sor, accountid, orgid, credit_limit_amount,
valid_from=None, valid_to=None, created_by=None, remark=None): grant_orgid='0', valid_from=None, valid_to=None, created_by=None, remark=None):
""" """
Set or update credit limit for an account. Set or update credit limit for an account.
If a credit limit already exists, update it; otherwise create new. If a credit limit already exists, update it; otherwise create new.
@ -181,6 +181,7 @@ async def set_credit_limit(sor, accountid, orgid, credit_limit_amount,
'id': new_id, 'id': new_id,
'accountid': accountid, 'accountid': accountid,
'orgid': orgid, 'orgid': orgid,
'grant_orgid': grant_orgid or '0',
'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,

View File

@ -29,6 +29,14 @@
"length": 32, "length": 32,
"nullable": "no" "nullable": "no"
}, },
{
"name": "grant_orgid",
"title": "授予机构ID",
"type": "str",
"length": 32,
"nullable": "no",
"default": "0"
},
{ {
"name": "credit_limit", "name": "credit_limit",
"title": "信用额度", "title": "信用额度",
@ -114,6 +122,11 @@
"idxtype": "index", "idxtype": "index",
"idxfields": ["orgid"] "idxfields": ["orgid"]
}, },
{
"name": "idx_credit_limit_grant_orgid",
"idxtype": "index",
"idxfields": ["grant_orgid"]
},
{ {
"name": "idx_credit_limit_status", "name": "idx_credit_limit_status",
"idxtype": "index", "idxtype": "index",

View File

@ -13,7 +13,7 @@ async with db.sqlorContext(dbname) as sor:
COUNT(CASE WHEN status = 'active' THEN 1 END) as active_count, COUNT(CASE WHEN status = 'active' THEN 1 END) as active_count,
COUNT(CASE WHEN status = 'expired' THEN 1 END) as expired_count COUNT(CASE WHEN status = 'expired' THEN 1 END) as expired_count
FROM credit_limit FROM credit_limit
WHERE orgid = ${orgid}$ WHERE grant_orgid = ${orgid}$
""" """
recs = await sor.sqlExe(sql, {'orgid': orgid}) recs = await sor.sqlExe(sql, {'orgid': orgid})
if recs and len(recs) > 0: if recs and len(recs) > 0:

View File

@ -101,6 +101,8 @@ async with db.sqlorContext(dbname) as sor:
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,
orgid = ${orgid}$,
grant_orgid = ${grant_orgid}$,
valid_from = ${valid_from}$, valid_from = ${valid_from}$,
valid_to = ${valid_to}$, valid_to = ${valid_to}$,
remark = ${remark}$, remark = ${remark}$,
@ -109,6 +111,8 @@ async with db.sqlorContext(dbname) as sor:
""" """
await sor.sqlExe(sql, { await sor.sqlExe(sql, {
'credit_limit': credit_limit_amount, 'credit_limit': credit_limit_amount,
'orgid': customer_orgid,
'grant_orgid': orgid,
'valid_from': valid_from, 'valid_from': valid_from,
'valid_to': valid_to, 'valid_to': valid_to,
'remark': remark, 'remark': remark,
@ -120,6 +124,7 @@ async with db.sqlorContext(dbname) as sor:
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,
grant_orgid = ${grant_orgid}$,
valid_from = ${valid_from}$, valid_from = ${valid_from}$,
valid_to = ${valid_to}$, valid_to = ${valid_to}$,
remark = ${remark}$, remark = ${remark}$,
@ -128,6 +133,7 @@ async with db.sqlorContext(dbname) as sor:
""" """
await sor.sqlExe(sql, { await sor.sqlExe(sql, {
'credit_limit': credit_limit_amount, 'credit_limit': credit_limit_amount,
'grant_orgid': orgid,
'valid_from': valid_from, 'valid_from': valid_from,
'valid_to': valid_to, 'valid_to': valid_to,
'remark': remark, 'remark': remark,
@ -141,7 +147,8 @@ async with db.sqlorContext(dbname) as sor:
data = { data = {
'id': new_id, 'id': new_id,
'accountid': accountid, 'accountid': accountid,
'orgid': orgid, 'orgid': customer_orgid,
'grant_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,