BossBonusLogic.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace addons\Boss\common\logic;
  3. use common\enums\StatusEnum;
  4. use common\models\statistics\UserStatisticsTotal;
  5. use common\models\user\User;
  6. /**
  7. * 股东权重分红条件限制
  8. */
  9. class BossBonusLogic
  10. {
  11. const ORDER_TYPE_PAY = 'order_pay';// 订单支付
  12. const ORDER_TYPE_FINISH = 'order_finish';// 订单完成
  13. /**
  14. * 获取股东分红条件限制
  15. * @param $user
  16. * @param $level
  17. * @return bool
  18. */
  19. public static function getAccordBonus($user, $level): bool
  20. {
  21. // 判断是否开启股东分红限制
  22. if (empty($level['bonus_restrict'])) return true;
  23. // 判断是否选则分红条件限制
  24. $keys = json_decode($level['bonus_restrict_keys'], true);
  25. $values = json_decode($level['bonus_restrict_values'], true);
  26. if (empty($keys) || empty($values)) {// 如果为选择分红条件限制,则默认通过
  27. return true;
  28. }
  29. foreach ($values as $value) {
  30. $system = $value['value'];
  31. if (!in_array($value['key'], $keys)) continue;
  32. switch ($value['key']) {
  33. case 0: // 直推
  34. $boss_result = self::directPushRestrict($user, $system);
  35. if ($boss_result === false) {
  36. return false;
  37. }
  38. break;
  39. case 1: // 消费满
  40. $boss_result = self::consumptionRestrict($user, $system);
  41. if ($boss_result === false) {
  42. return false;
  43. }
  44. break;
  45. default:
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * 直推股东分红限制
  53. * @param $user
  54. * @param array $system
  55. * @return true
  56. */
  57. protected static function directPushRestrict($user, array $system): bool
  58. {
  59. try {
  60. // 获取直推人数
  61. $pushNum = User::find()
  62. ->where(['parent_id' => $user['user_id'], 'mall_id' => $user['mall_id'], 'status' => StatusEnum::ENABLED])
  63. ->Andwhere(['>=', 'level', $system['level']])
  64. ->count();
  65. } catch (\Exception $e) {
  66. return false;
  67. }
  68. if ($pushNum < $system['num']) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * 消费股东分红限制
  75. * @param $user
  76. * @param array $system
  77. * @return true
  78. */
  79. protected static function consumptionRestrict($user, array $system): bool
  80. {
  81. try {
  82. $userIds = User::find()
  83. ->select(['id'])
  84. ->where(['parent_id' => $user['user_id'], 'mall_id' => $user['mall_id'], 'status' => StatusEnum::ENABLED])
  85. ->column();
  86. if (empty($userIds)) return false;
  87. $userMoney = UserStatisticsTotal::find()
  88. ->select('sum(pay_money) as pay_money, sum(refund_order_money) as refund_order_money, sum(finish_order_money) as finish_order_money')
  89. ->where(['user_id' => $userIds, 'mall_id' => $user['mall_id']])
  90. ->asArray()
  91. ->one();
  92. } catch (\Exception $e) {
  93. return false;
  94. }
  95. // 订单支付
  96. // 支付金额等于付款金额减去退款金额
  97. $money = bcsub($userMoney['pay_money'], $userMoney['refund_order_money'], 2);
  98. if ($system['order_type'] === self::ORDER_TYPE_PAY && $money < $system['money']) {
  99. return false;
  100. }
  101. // 订单完成
  102. if ($system['order_type'] === self::ORDER_TYPE_FINISH && $userMoney['finish_order_money'] < $system['money']) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. }