diff --git a/pricing/pricing.py b/pricing/pricing.py index 9d658e8..15da3f8 100644 --- a/pricing/pricing.py +++ b/pricing/pricing.py @@ -1,3 +1,4 @@ +import json from ahserver.serverenv import ServerEnv from sqlor.dbpools import DBPools from appPublic.log import debug, exception @@ -51,21 +52,21 @@ where enabled_date >= ${biz_date}$ return None async def get_specs(sor, psid): - sql = """select a.* from pricing_spec a where a.id=${psid}$""" - recs = await sor.sqlExe(sql, {'psid': psid}) - if len(recs)>0: - return recs[0] - return None + sql = """select a.* from pricing_spec a where a.id=${psid}$""" + recs = await sor.sqlExe(sql, {'psid': psid}) + if len(recs)>0: + return recs[0] + return None async def sor_get_program_items(sor, ppid, biz_date): - sql = """select b.* from pricing_program_timing a, pricing_item b + sql = """select b.* from pricing_program_timing a, pricing_item b where b.pptid = a.id - and a.ppid = ${ppid}$ - and a.enabled_date <= ${biz_date}$ - and a.expired_date > ${biz_date}$ + and a.ppid = ${ppid}$ + and a.enabled_date <= ${biz_date}$ + and a.expired_date > ${biz_date}$ """ - recs = await sor.sqlExe(sql, {'ppid': ppid, 'biz_date': biz_date}) - return recs + recs = await sor.sqlExe(sql, {'ppid': ppid, 'biz_date': biz_date}) + return recs async def get_pricing_specs_by_pptid(pptid): env = ServerEnv() @@ -94,35 +95,35 @@ async def get_remote_pricing(sor, charge, data): async def pricing_program_charging(sor, pricing_program_id, data): env = ServerEnv() - if not data.get('biz_date'): - biz_date = await env.get_business_date(self.sor) - data['biz_date'] = biz_date - debug(f'{pricing_program_id=}, {data=}') - pp_items = await sor_get_program_items(sor, pricing_program_id, data['biz_date']) - charges = [] - debug(f'{pp_items=}, {data["biz_date"]=}') - for item in pp_items: - charge = item.copy() - spec = await get_specs(sor, charge.psid) - if spec.pricing_spec_mode == 'spec_name': - d = data.get(spec.spec_name) - if d == item.spec_value: - change[spec.count_name] = cnt - charge.amount = item.pricing_amount * cnt - charges.append(charge) - elif spec.pricing_spec_mode == 'spec_amount': - cnt = data.get(spec.count_name, 1) - if charge.pricing_unit is None or charge.pricing_unit < 1: - charge.pricing_unit = 1 - charge.amount = cnt * charge.pricing_amount / charge.pricing_unit - charges.append(charge) - elif spec.pricing_spec_mode == 'remote_pricing': - charge.amount = await get_remote_pricing(sor, charge, params=d) - charges.append(charge) - elif spec.pricing_spec_mode == 'spec_subtype': - sub_charges = await pricing_program_chargeing(self.sor, - charge.subppid, d) - charges += sub_charges + if not data.get('biz_date'): + biz_date = await env.get_business_date(self.sor) + data['biz_date'] = biz_date + debug(f'{pricing_program_id=}, {data=}') + pp_items = await sor_get_program_items(sor, pricing_program_id, data['biz_date']) + charges = [] + debug(f'{pp_items=}, {data["biz_date"]=}') + for item in pp_items: + charge = item.copy() + spec = await get_specs(sor, charge.psid) + if spec.pricing_spec_mode == 'spec_amount': + if item.spec_value: + d = json.loads(item.spec_value) + for k,v in d.items(): + if v != item['k']: + continue - return charges + cnt = data.get(spec.count_name, 1) + if charge.pricing_unit is None or charge.pricing_unit < 1: + charge.pricing_unit = 1 + charge.amount = cnt * charge.pricing_amount / charge.pricing_unit + charges.append(charge) + elif spec.pricing_spec_mode == 'remote_pricing': + charge.amount = await get_remote_pricing(sor, charge, params=d) + charges.append(charge) + elif spec.pricing_spec_mode == 'spec_subtype': + sub_charges = await pricing_program_chargeing(self.sor, + charge.subppid, d) + charges += sub_charges + + return charges