oneCardGate.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # coding=utf-8
  2. import datetime
  3. import typing
  4. from library.validator import Validator, UnicodeValidationError
  5. from library.validator.fields import StringField
  6. from apilib.monetary import RMB, VirtualCoin
  7. if typing:
  8. from apps.web.user.models import ConsumeRecord
  9. class Calculator(object):
  10. def __init__(self, order): # type:(ConsumeRecord) -> None
  11. self._order = order
  12. self._st, self._et = order.dateTimeAdded, order.finishedTime or datetime.datetime.now()
  13. self._package = order.package
  14. def calculate(self):
  15. """
  16. 计费的时候以n小时为周期 不满n小时计算n小时 不超过最大费用
  17. """
  18. price = VirtualCoin(self._package["price"])
  19. maxFee = VirtualCoin(self._package["maxFee"])
  20. cycle = float(self._package["cycle"])
  21. freeHour = float(self._package["freeHour"])
  22. duration = int((self._et - self._st).total_seconds())
  23. # 没有超过免费停车时间的
  24. if int(freeHour * 3600) > duration:
  25. return VirtualCoin(0)
  26. # 计算应该付款的费用
  27. cycleSecond = cycle * 3600
  28. n = duration // cycleSecond + int(bool(duration % cycleSecond))
  29. fee = price * n
  30. # 与单项最大费用做比较S
  31. return min(VirtualCoin(fee), VirtualCoin(maxFee))