# coding=utf-8 import datetime import typing from library.validator import Validator, UnicodeValidationError from library.validator.fields import StringField from apilib.monetary import RMB, VirtualCoin if typing: from apps.web.user.models import ConsumeRecord class Calculator(object): def __init__(self, order): # type:(ConsumeRecord) -> None self._order = order self._st, self._et = order.dateTimeAdded, order.finishedTime or datetime.datetime.now() self._package = order.package def calculate(self): """ 计费的时候以n小时为周期 不满n小时计算n小时 不超过最大费用 """ price = VirtualCoin(self._package["price"]) maxFee = VirtualCoin(self._package["maxFee"]) cycle = float(self._package["cycle"]) freeHour = float(self._package["freeHour"]) duration = int((self._et - self._st).total_seconds()) # 没有超过免费停车时间的 if int(freeHour * 3600) > duration: return VirtualCoin(0) # 计算应该付款的费用 cycleSecond = cycle * 3600 n = duration // cycleSecond + int(bool(duration % cycleSecond)) fee = price * n # 与单项最大费用做比较S return min(VirtualCoin(fee), VirtualCoin(maxFee))