structures.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. from typing import List, Dict, TYPE_CHECKING
  4. from bson.objectid import ObjectId
  5. from bson.decimal128 import Decimal128
  6. from apilib.monetary import RMB, Ratio, sum_rmb, sum_ratio
  7. from apilib.utils import recursive_repr
  8. from apps.web.constant import PARTITION_ROLE
  9. if TYPE_CHECKING:
  10. from apps.web.device.models import GroupDict
  11. class SubIncomePartition(object):
  12. def __init__(self, object_id, role, share, money):
  13. # type: (ObjectId, str, Ratio, RMB)->None
  14. assert isinstance(object_id, ObjectId)
  15. assert isinstance(share, Ratio)
  16. assert isinstance(money, RMB)
  17. assert role in PARTITION_ROLE.choices()
  18. self.object_id = object_id # type: ObjectId
  19. self.role = role # type: str
  20. self.share = share # type: Ratio
  21. self.money = money # type: RMB
  22. def __repr__(self):
  23. return '<SubIncomePartition role=%s, id=%s, share=%s, money=%s>' % (self.role, self.object_id, self.share, self.money)
  24. class GroupIncomePartition(object):
  25. def __init__(self, money, agent, owner, partner):
  26. # type: (RMB, List[SubIncomePartition], List[SubIncomePartition], List[SubIncomePartition])->None
  27. assert isinstance(money, RMB)
  28. def check_component(component):
  29. # type:(list)->None
  30. assert isinstance(component, list), '{0!r} has to be a list'.format(component)
  31. if not component:
  32. return
  33. else:
  34. assert all( isinstance(c, SubIncomePartition) for c in component ),\
  35. '{0!r} has to be a list of SubIncomePartition'.format(component)
  36. check_component(agent)
  37. check_component(owner)
  38. check_component(partner)
  39. self.money = money # type: List[SubIncomePartition]
  40. self.agent = agent # type: List[SubIncomePartition]
  41. self.owner = owner # type: List[SubIncomePartition]
  42. self.partner = partner
  43. assert sum_ratio(_.share for _ in self.whole) == Ratio(100)
  44. assert sum_rmb(_.money for _ in self.whole) == money
  45. @property
  46. def dealer(self):
  47. return self.owner + self.partner
  48. @property
  49. def whole(self):
  50. return self.agent + self.dealer
  51. @recursive_repr()
  52. def __repr__(self):
  53. return '{name}(sum={money})(owner={owner}, partner={partner}, agent={agent})'.format(
  54. name=self.__class__.__name__,
  55. money=self.money,
  56. owner=self.owner,
  57. partner=self.partner,
  58. agent=self.agent
  59. )
  60. def dealer_money_map(self):
  61. # type: ()->Dict[str, Decimal128]
  62. return { str(dealer.object_id): dealer.money.mongo_amount for dealer in self.dealer }
  63. def dealer_ids(self):
  64. # type: ()->List[ObjectId]
  65. return [ _.object_id for _ in self.dealer ]
  66. @classmethod
  67. def from_ledger(cls, agentId, ownerId, agentProfitShare, group, money):
  68. # type: (str, str, float, GroupDict, RMB)->GroupIncomePartition
  69. agent_partition = []
  70. owner_partition = []
  71. partner_partition = []
  72. # partners = group.partners() # type:List[dict]
  73. # for partner in partners:
  74. # share = Ratio()
  75. # partner_partition.append(
  76. # SubIncomePartition(role='partner', object_id=ObjectId(partner['id']), ))