discount/discount/init.py
2026-02-25 11:56:23 +08:00

141 lines
4.3 KiB
Python

from appPublic.qr import gen_qr
from appPublic.uniqueID import getID
from sqlor.dbpools import get_sor_context
from ahserver.serverenv import ServerEnv
from ahserver.filestorage import FileStorage
async def discount_qrcode(request, params_kw):
"""
discount less then 1 and greate then 0
valid_term is digit + one of ["D", "M", "Y"]
expired_date is a date after it the promote qrcode invalidable
"""
discount = params_kw.discount
valid_term = params_kw.valid_term
expired_date = params_kw.expired_date
env = request._run_ns
resellerid = await env.get_userorgid()
id = getID()
url = env.entire_url('./promote') + f'?id={id}'
fs = FileStorage()
p = fs._name2path(f'{getID()}.png')
gen_qr(url, p)
webp = fs.webpath(p)
async with get_sor_context(env, 'discount') as sor:
biz_date = await env.get_business_date(sor)
if biz_date >= expired_date:
raise Exception('Promote QRCODE is out of time')
ret = {
'id': id,
'resellerid': resellerid,
'discount': discount,
'valid_term': valid_term,
'expired_date': expired_date,
'qr_webpath': webp
}
await sor.C('discount_qr', Cret.copy())
return ret
return None
async def set_promote_discount(request, params_kw):
env = request._run_ns
id = params_kw.id
customerid = await env.get_userorgid()
async with get_sor_context(env, 'discount') as sor:
recs = await sor.R('discount_qr', {'id': id})
if not recs:
raise Exception(f'promote id({id}) not exists')
biz_date = await env.get_business_date(sor)
if recs[0].expired_date <= biz_date:
raise Exception('Promote QRCODE is out of time')
cnt = int(recs[0].valid_term[:-1])
unit = recs[0].valid_term[-1]
enabled_date = biz_date
expired_date = ''
if unit == 'D':
expired_date = env.strdate_add(enabled_date, days=cnt)
elif unit == 'M':
expired_date = env.strdate_add(enabled_date, months=cnt)
elif unit == 'Y':
expired_date = env.strdate_add(enabled_date, years=cnt)
else:
raise Exception(f'Invalid valid_term({recs[0].valid_term})')
need_new_discount = await disable_old_discount(sor, recs[0].resellerid,
customerid, biz_date, recs[0].discount)
if not need_new_discount:
return
ret = {
'id': getID(),
'resellerid': recs[0].resellerid,
'customerid': customerid,
'discount': recs[0].discount,
'enabled_date': enabled_date,
'expired_date': expired_date
}
await sor.C('discount', ret.copy())
return ret
return None
async def disable_old_discount(sor, resellerid, customerid, biz_date, new_discount):
sql = """select * from discount
where resellerid = ${resellerid}$
and customerid=${customerid}$
and enabled_date <= ${biz_date}$
and expired_date > ${biz_date}$ for update"""
recs = await sor.sqlExe(sql, {'resellerid': resellerid, 'customerid': customerid, 'biz_date': biz_date})
if not recs:
return True
if new_discount > news[0].discount:
return False
await sor.U('discount', {'id': recs[0].id, 'expired_date': biz_date})
return True
async def sor_get_star_discount(sor, resellerid, biz_date):
env = ServerEnv()
sql = """select * from discount
where resellerid = ${resellerid}$
and enabled_date <= ${biz_date}$
and expired_date > ${biz_date}$"""
ns = {
"resellerid": resellerid,
"biz_date": biz_date
}
recs = await sqlExe(sql, ns)
if not recs:
return 1
return recs[0].discount
async def sor_get_customer_discount(sor, resellerid, customerid):
env = ServerEnv()
biz_date = await env.get_business_date(sor)
sql = """select * from discount
where resellerid = ${resellerid}$
and customerid = ${customerid}$
and enabled_date <= ${biz_date}$
and expired_date > ${biz_date}$"""
ns = {
"resellerid": resellerid,
"customerid": customerid,
"biz_date": biz_date
}
recs = await sqlExe(sql, ns)
if not recs:
return await sor_get_star_discount(sor, resellerid, biz_date)
return recs[0].discount
async def get_customer_discount(resellerid, customerid):
env = ServerEnv()
async with get_sor_context(env, 'discount') as sor:
return sor_get_customer_discount(sor, resellerid, customerid)
return 1
def load_discount():
env = ServerEnv()
env.get_customer_discount = get_customer_discount
env.sor_get_customer_discount = sor_get_customer_discount
env.discount_qrcode = discount_qrcode
env.set_promote_discount = set_promote_discount