# payfee from appPublic.log import debug, exception from ahserver.serverenv import ServerEnv from sqlor.dbpools import DBPools async def get_pay_fee(providerid, amount): db = DBPools() env = ServerEnv() dbname = env.get_module_dbname('unipay') async with db.sqlorContext(dbname) as sor: return await sor_get_pay_fee(sor, providerid, amount) return None async def get_paychannels(): env = ServerEnv() db = DBPools() dbname = env.get_module_dbname('unipay') async with db.sqlorContext(dbname) as sor: return await sor_get_paychannels(sor) return None async def sor_get_paychannels(sor): env = ServerEnv() dbname = env.get_module_dbname('unipay') biz_date = await env.get_business_date(sor) sql = """select a.id, a.name, b.fee_rate from paychannel a left join payfee b on a.id = b.channelid where a.enabled_date <= ${biz_date}$ and a.expired_date >= ${biz_date}$ and b.enabled_date <= ${biz_date}$ and b.expired_date >= ${biz_date}$ """ recs = await sor.sqlExe(sql, {'channelid': channelid, 'biz_date': biz_date}) return recs async def get_pay_feerate(sor, channelid): env = ServerEnv() db = DBPools() dbname = env.get_module_dbname('unipay') async with db.sqlorContext(dbname) as sor: return await sor_get_pay_feerate(sor, channelid) return None async def sor_get_pay_feerate(sor, channelid): env = ServerEnv() dbname = env.get_module_dbname('unipay') biz_date = await env.get_business_date(sor) sql = """select a.id, a.name, b.fee_rate from paychannel a left join payfee b on a.id = b.channelid where a.enabled_date <= ${biz_date}$ and a.expired_date >= ${biz_date}$ and b.enabled_date <= ${biz_date}$ and b.expired_date >= ${biz_date}$ and a.id = ${channelid}$ """ ns = { 'channelid': channelid, 'biz_date': biz_date } recs = await sor.sqlExe(sql, ns.copy()) debug(f'{recs=},{ns=}, {sql=}') if len(recs) > 0: return recs[0] return None async def sor_get_pay_fee(sor, providerid, amount): rec = await sor_get_pay_feerate(sor, providerid) if rec is None: e = Exception(f'{providerid} pay channel not found') exception(f'Exception:{e}') raise e if rec.fee_rate is None: e = Exception(f'{providerid} channel has not defined fee rate') exception(f'Exception:{e}') rec.fee_rate = 0.00 debug(f'{rec=}, {rec.fee_rate=}, {amount=}') return rec.fee_rate * amount