This commit is contained in:
yumoqing 2025-10-21 18:01:33 +08:00
parent 4971eb41d2
commit 1572df3d16
2 changed files with 65 additions and 0 deletions

59
charge/calculate.py Normal file
View File

@ -0,0 +1,59 @@
from appPublic.log import debug, exception
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import DBPools
class PayeeCharge:
def __init__(self, resourceid, ctid):
self.resourceid = resourceid
self.ctid = ctid
async get_ct_specs(self, sor):
sql = """select a.charge_type_spec_mode as spec_type, b.*
from charge_type a, charge_type_spec b
where a.id = ${ctid}
and a.id = b.ctid"""
ctspecs = await sor.sqlExe(sql, {'ctid': self.ctid})
return ctspecs
async def get_payee_charge_rules(self, sor, payee):
pcrs = await sor.R('payee_charge_rule', {
'resourceid': self.resourceid,
'ctid': self.ctid,
'payeeid': payee
})
return pcrs
def get_spec_by_ctsid(self, ctsid, ctspecs):
for spec in ctspecs:
if ctsid == spec.id:
return spec
return None
async def calculate_charge(self, sor, payeeids, data):
ctspecs = await self.get_ct_specs(sor)
charges = {}
for payeeid in payeeids:
charge_arr = []
pcrs = await self.get_payee_charge_rule(sor, payeeid)
for pcr in pcrs:
spec = self.get_spec_by_ctsid(pcr.ctsid, ctspecs)
if spec is None:
continue
charge = pcr.copy()
d = data.get(spec.spec_name)
if d is None:
continue
cnt = data.get(spec.count_name, 1)
if spec_mode == 'spec_name':
if d == pci.spec_value:
pci.amount = pcr.charge_amount * cnt
elif spec_mode == 'spec_amount':
if pcr.charge_unit is None or pcr.charge_unit == 0:
pci.charge_unit = 1
charge.amount = d * pcr.charge_amount / pcr.charge_unit
else:
continue
charge_arr.append(charge)
charges[payeeid] = charge_arr
return charges

6
charge/init.py Normal file
View File

@ -0,0 +1,6 @@
from ahserver.serverenv import ServerEnv
from charge.calculate import PayeeCharge
def load_charge():
env = ServerEnv()
env.PayeeCharge = PayeeCharge