fix: customerid改存orgid; init_free按机构去重; 查询回JOIN organization

This commit is contained in:
yumoqing 2026-07-14 11:18:13 +08:00
parent 5626e1b516
commit 6c0f5a6572
2 changed files with 24 additions and 25 deletions

View File

@ -1041,19 +1041,19 @@ async def get_free_customers(request):
if is_owner: if is_owner:
rows = await sor.sqlExe( rows = await sor.sqlExe(
"""SELECT dcb.id as bind_id, dcb.customerid, """SELECT dcb.id as bind_id, dcb.customerid,
COALESCE(u.username, u.nick_name, u.id) as customer_name, dcb.sale_id o.orgname as customer_name, dcb.sale_id
FROM discount_customer_bind dcb FROM discount_customer_bind dcb
LEFT JOIN users u ON u.id = dcb.customerid JOIN organization o ON o.id = dcb.customerid
WHERE dcb.sale_id IS NULL OR dcb.sale_id = '' WHERE dcb.sale_id IS NULL OR dcb.sale_id = ''
ORDER BY customer_name""", {}) ORDER BY o.orgname""", {})
else: else:
rows = await sor.sqlExe( rows = await sor.sqlExe(
"""SELECT dcb.id as bind_id, dcb.customerid, """SELECT dcb.id as bind_id, dcb.customerid,
COALESCE(u.username, u.nick_name, u.id) as customer_name, dcb.sale_id o.orgname as customer_name, dcb.sale_id
FROM discount_customer_bind dcb FROM discount_customer_bind dcb
LEFT JOIN users u ON u.id = dcb.customerid JOIN organization o ON o.id = dcb.customerid
WHERE dcb.resellerid = ${rid}$ AND (dcb.sale_id IS NULL OR dcb.sale_id = '') WHERE dcb.resellerid = ${rid}$ AND (dcb.sale_id IS NULL OR dcb.sale_id = '')
ORDER BY customer_name""", ORDER BY o.orgname""",
{'rid': userorgid}) {'rid': userorgid})
return rows or [] return rows or []
@ -1072,23 +1072,23 @@ async def get_bound_customers(request):
if is_owner: if is_owner:
rows = await sor.sqlExe( rows = await sor.sqlExe(
"""SELECT dcb.id as bind_id, dcb.customerid, """SELECT dcb.id as bind_id, dcb.customerid,
COALESCE(u.username, u.nick_name, u.id) as customer_name, dcb.sale_id, o.orgname as customer_name, dcb.sale_id,
COALESCE(u.username, u.nick_name, u.id) as sale_name COALESCE(u.username, u.nick_name, u.id) as sale_name
FROM discount_customer_bind dcb FROM discount_customer_bind dcb
LEFT JOIN users u ON u.id = dcb.customerid JOIN organization o ON o.id = dcb.customerid
LEFT JOIN users u ON u.id = dcb.sale_id LEFT JOIN users u ON u.id = dcb.sale_id
WHERE dcb.sale_id IS NOT NULL AND dcb.sale_id != '' WHERE dcb.sale_id IS NOT NULL AND dcb.sale_id != ''
ORDER BY customer_name""", {}) ORDER BY o.orgname""", {})
else: else:
rows = await sor.sqlExe( rows = await sor.sqlExe(
"""SELECT dcb.id as bind_id, dcb.customerid, """SELECT dcb.id as bind_id, dcb.customerid,
COALESCE(u.username, u.nick_name, u.id) as customer_name, dcb.sale_id, o.orgname as customer_name, dcb.sale_id,
COALESCE(u.username, u.nick_name, u.id) as sale_name COALESCE(u.username, u.nick_name, u.id) as sale_name
FROM discount_customer_bind dcb FROM discount_customer_bind dcb
LEFT JOIN users u ON u.id = dcb.customerid JOIN organization o ON o.id = dcb.customerid
LEFT JOIN users u ON u.id = dcb.sale_id LEFT JOIN users u ON u.id = dcb.sale_id
WHERE dcb.resellerid = ${rid}$ AND dcb.sale_id IS NOT NULL AND dcb.sale_id != '' WHERE dcb.resellerid = ${rid}$ AND dcb.sale_id IS NOT NULL AND dcb.sale_id != ''
ORDER BY customer_name""", ORDER BY o.orgname""",
{'rid': userorgid}) {'rid': userorgid})
return rows or [] return rows or []

View File

@ -1,5 +1,5 @@
"""Initialize discount_customer_bind for all users who don't have a bind record. """Initialize discount_customer_bind for all orgs that don't have a bind record.
Each user is bound to their own org (users.orgid) as resellerid, bind_type='3'.""" Each org is bound as customerid=orgid, resellerid=orgid, bind_type='3'."""
userorgid = await get_userorgid() userorgid = await get_userorgid()
if not userorgid: if not userorgid:
return {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '请先登录', 'timeout': 3}} return {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '请先登录', 'timeout': 3}}
@ -7,28 +7,27 @@ if not userorgid:
count = 0 count = 0
disc_dbname = get_module_dbname('discount') disc_dbname = get_module_dbname('discount')
async with DBPools().sqlorContext('sage') as sor: async with DBPools().sqlorContext('sage') as sor:
# Get all users (filtered by org if not owner)
if userorgid == '0': if userorgid == '0':
users = await sor.sqlExe('SELECT id, orgid FROM users', {}) orgs = await sor.sqlExe('SELECT DISTINCT orgid FROM users WHERE orgid IS NOT NULL', {})
else: else:
users = await sor.sqlExe( orgs = await sor.sqlExe(
'SELECT id, orgid FROM users WHERE orgid = ${oid}$', 'SELECT DISTINCT orgid FROM users WHERE orgid = ${oid}$',
{'oid': userorgid} {'oid': userorgid}
) )
if not users: if not orgs:
return {'widgettype': 'Message', 'options': {'title': '提示', 'message': '该机构下无用户', 'type': 'warning', 'timeout': 3}} return {'widgettype': 'Message', 'options': {'title': '提示', 'message': '该机构下无用户', 'type': 'warning', 'timeout': 3}}
async with DBPools().sqlorContext(disc_dbname) as disc_sor: async with DBPools().sqlorContext(disc_dbname) as disc_sor:
for u in users: for o in orgs:
existing = await disc_sor.sqlExe( existing = await disc_sor.sqlExe(
'SELECT id FROM discount_customer_bind WHERE customerid = ${cid}$', 'SELECT id FROM discount_customer_bind WHERE customerid = ${cid}$',
{'cid': u.id} {'cid': o.orgid}
) )
if not existing: if not existing:
await disc_sor.C('discount_customer_bind', { await disc_sor.C('discount_customer_bind', {
'id': getID(), 'id': getID(),
'customerid': u.id, 'customerid': o.orgid,
'resellerid': u.orgid or '0', 'resellerid': o.orgid,
'sale_id': '', 'sale_id': '',
'promo_code_id': '', 'promo_code_id': '',
'bind_type': '3', 'bind_type': '3',
@ -39,8 +38,8 @@ return {
'widgettype': 'Message', 'widgettype': 'Message',
'options': { 'options': {
'title': '初始化完成', 'title': '初始化完成',
'message': f'共 {len(users)} 个用户,新增 {count} 条自由客户记录', 'message': f'共 {len(orgs)} 个机构,新增 {count} 条自由客户记录',
'type': 'success', 'type': 'success',
'timeout': 0 'timeout': 0
} }
} }