From 9d3c4e0adf50e6e9384b377f451ab60704f6dad5 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 14 Jul 2026 10:38:02 +0800 Subject: [PATCH] feat: init_free_customers.dspy + button in free_customer_assign --- wwwroot/api/init_free_customers.dspy | 42 +++++++++++++++++++++++++++ wwwroot/free_customer_assign/index.ui | 16 ++++++++++ 2 files changed, 58 insertions(+) create mode 100644 wwwroot/api/init_free_customers.dspy diff --git a/wwwroot/api/init_free_customers.dspy b/wwwroot/api/init_free_customers.dspy new file mode 100644 index 0000000..96959d2 --- /dev/null +++ b/wwwroot/api/init_free_customers.dspy @@ -0,0 +1,42 @@ +"""Initialize discount_customer_bind for all users in current org. +Users without a bind record get bind_type='3' (domain registration), sale_id=NULL.""" +userorgid = await get_userorgid() +if not userorgid or userorgid == '0': + return {'widgettype': 'Error', 'options': {'title': 'Error', 'message': '仅机构用户可执行', 'timeout': 3}} + +count = 0 +async with DBPools().sqlorContext('sage') as sor: + # Get all users in this org + users = await sor.sqlExe( + 'SELECT id, orgid FROM users WHERE orgid = ${oid}$', + {'oid': userorgid} + ) + if not users: + return {'widgettype': 'Message', 'options': {'title': '提示', 'message': '该机构下无用户', 'type': 'warning', 'timeout': 3}} + + async with DBPools().sqlorContext('discount') as disc_sor: + for u in users: + existing = await disc_sor.sqlExe( + 'SELECT id FROM discount_customer_bind WHERE customerid = ${cid}$', + {'cid': u.id} + ) + if not existing: + await disc_sor.C('discount_customer_bind', { + 'id': getID(), + 'customerid': u.id, + 'resellerid': userorgid, + 'sale_id': None, + 'promo_code_id': None, + 'bind_type': '3', + }) + count += 1 + +return { + 'widgettype': 'Message', + 'options': { + 'title': '初始化完成', + 'message': f'共 {len(users)} 个用户,新增 {count} 条自由客户记录', + 'type': 'success', + 'timeout': 0 + } +} diff --git a/wwwroot/free_customer_assign/index.ui b/wwwroot/free_customer_assign/index.ui index 52f41d2..3726bd1 100644 --- a/wwwroot/free_customer_assign/index.ui +++ b/wwwroot/free_customer_assign/index.ui @@ -11,6 +11,22 @@ "fontSize": "16px", "fontWeight": "600", "color": "#F1F5F9", "padding": "8px 12px" } }, + { + "widgettype": "HBox", + "options": {"padding": "4px 12px", "alignItems": "center"}, + "subwidgets": [ + { + "widgettype": "Button", + "options": {"label": "初始化自由客户", "css": "primary", "fontSize": "12px", "padding": "4px 12px", "borderRadius": "4px"}, + "binds": [{ + "wid": "self", "event": "click", + "actiontype": "urlwidget", "target": "app.sage_main_content", + "mode": "replace", + "options": {"url": "{{entire_url('/discount/api/init_free_customers.dspy')}}", "method": "POST"} + }] + } + ] + }, { "widgettype": "HBox", "options": {"bgcolor": "#0F172A", "padding": "8px 0", "borderRadius": "6px 6px 0 0", "alignItems": "center", "width": "100%"},