discount/wwwroot/api/init_free_customers.dspy

45 lines
1.7 KiB
Plaintext

"""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}}
count = 0
disc_dbname = get_module_dbname('discount')
async with DBPools().sqlorContext('sage') as sor:
if userorgid == '0':
orgs = await sor.sqlExe('SELECT DISTINCT orgid FROM users WHERE orgid IS NOT NULL', {})
else:
orgs = await sor.sqlExe(
'SELECT DISTINCT orgid FROM users WHERE orgid = ${oid}$',
{'oid': userorgid}
)
if not orgs:
return {'widgettype': 'Message', 'options': {'title': '提示', 'message': '该机构下无用户', 'type': 'warning', 'timeout': 3}}
async with DBPools().sqlorContext(disc_dbname) as disc_sor:
for o in orgs:
existing = await disc_sor.sqlExe(
'SELECT id FROM discount_customer_bind WHERE customerid = ${cid}$',
{'cid': o.orgid}
)
if not existing:
await disc_sor.C('discount_customer_bind', {
'id': getID(),
'customerid': o.orgid,
'resellerid': o.orgid,
'sale_id': '',
'promo_code_id': '',
'bind_type': '3',
})
count += 1
return {
'widgettype': 'Message',
'options': {
'title': '初始化完成',
'message': f'共 {len(orgs)} 个机构,新增 {count} 条自由客户记录',
'type': 'success',
'timeout': 0
}
}