diff --git a/discount/init.py b/discount/init.py index a9b75cc..084192a 100644 --- a/discount/init.py +++ b/discount/init.py @@ -1041,19 +1041,19 @@ async def get_free_customers(request): if is_owner: rows = await sor.sqlExe( """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 - 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 = '' - ORDER BY customer_name""", {}) + ORDER BY o.orgname""", {}) else: rows = await sor.sqlExe( """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 - 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 = '') - ORDER BY customer_name""", + ORDER BY o.orgname""", {'rid': userorgid}) return rows or [] @@ -1072,23 +1072,23 @@ async def get_bound_customers(request): if is_owner: rows = await sor.sqlExe( """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 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 WHERE dcb.sale_id IS NOT NULL AND dcb.sale_id != '' - ORDER BY customer_name""", {}) + ORDER BY o.orgname""", {}) else: rows = await sor.sqlExe( """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 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 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}) return rows or [] diff --git a/wwwroot/api/init_free_customers.dspy b/wwwroot/api/init_free_customers.dspy index 07c10c8..b355c34 100644 --- a/wwwroot/api/init_free_customers.dspy +++ b/wwwroot/api/init_free_customers.dspy @@ -1,5 +1,5 @@ -"""Initialize discount_customer_bind for all users who don't have a bind record. -Each user is bound to their own org (users.orgid) as resellerid, bind_type='3'.""" +"""Initialize discount_customer_bind for all orgs that don't have a bind record. +Each org is bound as customerid=orgid, resellerid=orgid, bind_type='3'.""" userorgid = await get_userorgid() if not userorgid: return {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '请先登录', 'timeout': 3}} @@ -7,28 +7,27 @@ if not userorgid: count = 0 disc_dbname = get_module_dbname('discount') async with DBPools().sqlorContext('sage') as sor: - # Get all users (filtered by org if not owner) 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: - users = await sor.sqlExe( - 'SELECT id, orgid FROM users WHERE orgid = ${oid}$', + orgs = await sor.sqlExe( + 'SELECT DISTINCT orgid FROM users WHERE orgid = ${oid}$', {'oid': userorgid} ) - if not users: + if not orgs: return {'widgettype': 'Message', 'options': {'title': '提示', 'message': '该机构下无用户', 'type': 'warning', 'timeout': 3}} async with DBPools().sqlorContext(disc_dbname) as disc_sor: - for u in users: + for o in orgs: existing = await disc_sor.sqlExe( 'SELECT id FROM discount_customer_bind WHERE customerid = ${cid}$', - {'cid': u.id} + {'cid': o.orgid} ) if not existing: await disc_sor.C('discount_customer_bind', { 'id': getID(), - 'customerid': u.id, - 'resellerid': u.orgid or '0', + 'customerid': o.orgid, + 'resellerid': o.orgid, 'sale_id': '', 'promo_code_id': '', 'bind_type': '3', @@ -39,8 +38,8 @@ return { 'widgettype': 'Message', 'options': { 'title': '初始化完成', - 'message': f'共 {len(users)} 个用户,新增 {count} 条自由客户记录', + 'message': f'共 {len(orgs)} 个机构,新增 {count} 条自由客户记录', 'type': 'success', 'timeout': 0 } -} +} \ No newline at end of file