models2.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # coding=utf-8
  2. from typing import TYPE_CHECKING, Optional
  3. from mongoengine import Document, StringField, BooleanField, EmbeddedDocumentListField, EmbeddedDocument, DoesNotExist, ListField
  4. from apilib.monetary import Percent
  5. from apps.web.core.db import PercentField
  6. from apps.web.device.models import Group
  7. from apps.web.dealer.utils import Dealer
  8. if TYPE_CHECKING:
  9. from apps.web.device.models import GroupDict
  10. class GroupShare(EmbeddedDocument):
  11. groupId = StringField(verbose_name=u"地址")
  12. isActive = BooleanField(verbose_name=u"是否参与分账", default=True)
  13. payElecFee = BooleanField(verbose_name=u"电费支付方", default=False)
  14. ratio = PercentField(verbose_name=u"分成百分比", max_value=Percent(100), min_value=Percent(0))
  15. contracts = ListField(verbose_name=u"电子合同")
  16. def to_dict(self):
  17. return {
  18. "groupId": self.groupId,
  19. "ratio": self.ratio,
  20. "isActive": self.isActive,
  21. "payElecFee": self.payElecFee,
  22. "contracts": self.contracts or list()
  23. }
  24. @property
  25. def group(self): # type:() -> GroupDict
  26. return Group.get_group(self.groupId)
  27. def __eq__(self, groupId):
  28. """配合for in 使用"""
  29. return self.groupId == groupId
  30. class Partner(Document):
  31. """
  32. 合伙人的关系表 查询表
  33. """
  34. dealerId = StringField(verbose_name=u"经销商ID", required=True)
  35. partnerId = StringField(verbose_name=u"合伙人ID[角色为经销商]", required=True)
  36. shareGroups = EmbeddedDocumentListField(verbose_name=u"地址分成的数据", document_type=GroupShare, default=list)
  37. isDelete = BooleanField(verbose_name=u"绑定关系", default=False)
  38. @property
  39. def dealer(self):
  40. return Dealer.get_dealer(self.dealerId)
  41. @property
  42. def partner(self):
  43. return Dealer.get_dealer(self.partnerId)
  44. @classmethod
  45. def get_partner(cls, _id):
  46. return cls.objects.filter(id=_id, isDelete=False).first()
  47. @classmethod
  48. def get_partner_by_dealer(cls, dealerId, partnerId):
  49. return cls.objects.filter(dealerId=dealerId, partnerId=partnerId, isDelete=False).first()
  50. @classmethod
  51. def get_partners(cls, dealer, pageIndex=1, pageSize=10): # type:(Dealer, int, int) -> list
  52. dCls, result = dealer.__class__, list()
  53. for _item in cls.objects.filter(dealerId=str(dealer.id), isDelete=False).skip((pageIndex-1)*pageSize).limit(pageSize): # type: Partner
  54. _partner = dCls.get_dealer(_item.partnerId)
  55. _temp = {
  56. "id": str(_item.id),
  57. "partnerId": str(_partner.id),
  58. "name": _partner.nickname,
  59. "phone": _partner.username,
  60. "groupCount": len(_item.shareGroups)
  61. }
  62. result.append(_temp)
  63. return result
  64. @classmethod
  65. def add_relation(cls, dealer, partner): # type:(Dealer, Dealer) -> Partner
  66. try:
  67. relation = cls.objects.get(dealerId=str(dealer.id), partnerId=str(partner.id))
  68. except DoesNotExist:
  69. return cls(dealerId=str(dealer.id), partnerId=str(partner.id)).save()
  70. if relation.isDelete:
  71. relation.update(isDelete=False, shareGroups=[])
  72. return relation.reload()
  73. raise ValueError(u"请勿重复添加合伙人")
  74. @classmethod
  75. def delete_relation(cls, dealer, partner): # type:(Dealer, Dealer) -> None
  76. try:
  77. relation = cls.objects.get(dealerId=str(dealer.id), partnerId=str(partner.id)) # type: Partner
  78. except DoesNotExist:
  79. raise ValueError(u"未找到合伙人,删除失败")
  80. if relation.isDelete:
  81. raise ValueError(u"未找到合伙人,删除失败")
  82. for _share in relation.shareGroups: # type: GroupShare
  83. _share.group.remove_partner(str(partner.id))
  84. relation.update(shareGroups=[], isDelete=True)
  85. return
  86. def get_partner_groups(self): # type:() -> list
  87. result = list()
  88. for _share in self.shareGroups: # type: GroupShare
  89. _data = _share.to_dict()
  90. _group = _share.group
  91. _data.update({
  92. "address": _group.address,
  93. "groupName": _group.groupName
  94. })
  95. result.append(_data)
  96. return result
  97. def get_share_group(self, groupId): # type:(str) -> Optional[GroupShare, None]
  98. for _share in self.shareGroups:
  99. if _share.groupId != str(groupId):
  100. continue
  101. return _share
  102. else:
  103. return None
  104. def add_group_share(self, group, ratio, payElecFee, isActive, contracts): # type:(GroupDict, Percent, bool, bool, list) -> Partner
  105. """
  106. 添加组
  107. """
  108. if str(group.id) in self.shareGroups:
  109. raise ValueError(u"重复添加")
  110. group.update_partner(self.partnerId, ratio, payElecFee, isActive)
  111. self.shareGroups.append(GroupShare(
  112. groupId=str(group.id),
  113. ratio=Percent(ratio),
  114. payElecFee=payElecFee,
  115. isActive=isActive,
  116. contracts=contracts
  117. ))
  118. return self.save()
  119. def update_group_share(self, group, ratio, payElecFee, isActive, contracts):
  120. """
  121. 更新组内分成情况
  122. """
  123. share = self.get_share_group(str(group.id)) # type: GroupShare
  124. if not share:
  125. raise ValueError("当前场地合伙人未参与分成")
  126. group.update_partner(self.partnerId, ratio, payElecFee, isActive)
  127. share.ratio = ratio
  128. share.payElecFee = payElecFee
  129. share.isActive = isActive
  130. share.contracts = contracts
  131. return share._instance.save()
  132. def remove_group_share(self, group):
  133. """
  134. 删除组
  135. """
  136. if str(group.id) not in self.shareGroups:
  137. raise ValueError(u"移除失败,未找到组内合伙人")
  138. group.remove_partner(self.partnerId)
  139. _share = self.get_share_group(group.groupId)
  140. self.shareGroups.remove(_share)
  141. return self.save()