From 2e32f4183c2d73a1f7757829e97d827269d1bacd Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 10 Jul 2026 10:44:26 +0800 Subject: [PATCH] fix: replace resellerid/orgid with orgname directly in list SQL using COALESCE --- models/tenant_domain.json | 6 ++ wwwroot/tenant_domain/get_tenant_domain.dspy | 86 ++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 wwwroot/tenant_domain/get_tenant_domain.dspy diff --git a/models/tenant_domain.json b/models/tenant_domain.json index bf40cb5..956e86a 100644 --- a/models/tenant_domain.json +++ b/models/tenant_domain.json @@ -81,6 +81,12 @@ "valuefield": "id", "textfield": "orgname" }, + { + "field": "orgid", + "table": "organization", + "valuefield": "id", + "textfield": "orgname" + }, { "field": "status", "table": "appcodes_kv", diff --git a/wwwroot/tenant_domain/get_tenant_domain.dspy b/wwwroot/tenant_domain/get_tenant_domain.dspy new file mode 100644 index 0000000..5ddf534 --- /dev/null +++ b/wwwroot/tenant_domain/get_tenant_domain.dspy @@ -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": []}