fix: replace resellerid/orgid with orgname directly in list SQL using COALESCE
This commit is contained in:
parent
2022d0ee3e
commit
2e32f4183c
@ -81,6 +81,12 @@
|
||||
"valuefield": "id",
|
||||
"textfield": "orgname"
|
||||
},
|
||||
{
|
||||
"field": "orgid",
|
||||
"table": "organization",
|
||||
"valuefield": "id",
|
||||
"textfield": "orgname"
|
||||
},
|
||||
{
|
||||
"field": "status",
|
||||
"table": "appcodes_kv",
|
||||
|
||||
86
wwwroot/tenant_domain/get_tenant_domain.dspy
Normal file
86
wwwroot/tenant_domain/get_tenant_domain.dspy
Normal file
@ -0,0 +1,86 @@
|
||||
ns = params_kw.copy()
|
||||
|
||||
userorgid = await get_userorgid()
|
||||
if not userorgid:
|
||||
return {
|
||||
"widgettype": "Error",
|
||||
"options": {
|
||||
"title": "Authorization Error",
|
||||
"timeout": 3,
|
||||
"cwidth": 16,
|
||||
"cheight": 9,
|
||||
"message": "Please login"
|
||||
}
|
||||
}
|
||||
ns['resellerid'] = userorgid
|
||||
ns['userorgid'] = userorgid
|
||||
|
||||
debug(f'get_tenant_domain.dspy:{ns=}')
|
||||
|
||||
if not ns.get('page'):
|
||||
ns['page'] = 1
|
||||
if not ns.get('sort'):
|
||||
ns['sort'] = ["created_at desc"]
|
||||
|
||||
sql = '''select a.id, a.domain,
|
||||
COALESCE(b.orgname, a.resellerid) as resellerid,
|
||||
COALESCE(c.orgname, a.orgid) as orgid,
|
||||
CASE a.status WHEN 'active' THEN '启用' WHEN 'inactive' THEN '停用' ELSE a.status END as status,
|
||||
a.created_at, a.updated_at
|
||||
from (select * from tenant_domain where 1=1 [[filterstr]]) a
|
||||
left join (select id, orgname from organization) b on a.resellerid = b.id
|
||||
left join (select id, orgname from organization) c on a.orgid = c.id'''
|
||||
|
||||
filterjson = params_kw.get('data_filter')
|
||||
if filterjson and isinstance(filterjson, str):
|
||||
try:
|
||||
filterjson = json.loads(filterjson)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
filterjson = None
|
||||
|
||||
if not filterjson:
|
||||
fields_str = '''[
|
||||
{"name": "id", "title": "主键ID", "type": "str", "length": 32, "nullable": "no"},
|
||||
{"name": "domain", "title": "域名", "type": "str", "length": 200, "nullable": "no"},
|
||||
{"name": "resellerid", "title": "分销商", "type": "str", "length": 32, "nullable": "no"},
|
||||
{"name": "orgid", "title": "所属平台", "type": "str", "length": 32, "nullable": "no"},
|
||||
{"name": "status", "title": "状态", "type": "str", "length": 10, "nullable": "no", "default": "active"},
|
||||
{"name": "created_at", "title": "创建时间", "type": "timestamp", "nullable": "no"},
|
||||
{"name": "updated_at", "title": "更新时间", "type": "timestamp", "nullable": "no"}
|
||||
]'''
|
||||
ori_fields = json.loads(fields_str)
|
||||
fields = [f['name'] for f in ori_fields]
|
||||
filterjson = default_filterjson(fields, ns)
|
||||
|
||||
if filterjson:
|
||||
if not isinstance(filterjson, dict) or 'AND' not in filterjson:
|
||||
filterjson = {'AND': [filterjson] if filterjson else []}
|
||||
filterjson['AND'].append({'field': 'orgid', 'op': '=', 'var': '__logined_orgid__'})
|
||||
ns['__logined_orgid__'] = userorgid
|
||||
|
||||
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=}')
|
||||
db = DBPools()
|
||||
dbname = get_module_dbname('tenant')
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
r = await sor.sqlPaging(sql, ns)
|
||||
return r
|
||||
|
||||
return {"total": 0, "rows": []}
|
||||
Loading…
x
Reference in New Issue
Block a user