InviteNewUserRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/15 11:52
  12. */
  13. namespace App\Repositories;
  14. use App\Models\InviteNewUser;
  15. use App\Models\InviteNewUsersConfig;
  16. use App\Models\Order;
  17. use App\Models\RentOrder;
  18. class InviteNewUserRepository extends BaseRepository
  19. {
  20. public function __construct(InviteNewUser $inviteNewUser)
  21. {
  22. $this->model = $inviteNewUser;
  23. }
  24. public function create($invite_id, $register_id, $source = 1)
  25. {
  26. if($invite_id != 0){
  27. $data = [
  28. 'invite_id' => $invite_id,
  29. 'register_id' => $register_id,
  30. 'source' => $source,
  31. ];
  32. return $this->model->create($data);
  33. }
  34. }
  35. /**
  36. * 更新实名 updateCardCertified
  37. *
  38. * @param $register_id
  39. * @return mixed
  40. * @author Fx
  41. *
  42. */
  43. public function updateCardCertified($register_id)
  44. {
  45. return $this->model
  46. ->where('register_id', $register_id)
  47. ->update(['is_card_certified' => $this->model::CARD_CERTIFIED_OK]);
  48. }
  49. /**
  50. * 更新首单 updateFirstOrder
  51. *
  52. * @param $register_id
  53. * @return mixed
  54. * @author Fx
  55. *
  56. */
  57. public function updateFirstOrder($register_id)
  58. {
  59. return $this->model
  60. ->where('register_id', $register_id)
  61. ->update(['is_first_order' => $this->model::FIRST_ORDER_OK]);
  62. }
  63. /**
  64. * 是否达成条件 isCondition
  65. *
  66. * @param $invite_id integer 邀请人id
  67. * @param $condition int 条件
  68. * @param $num int 需要数量
  69. * @return bool|array
  70. * @author Fx
  71. *
  72. */
  73. public function isCondition($invite_id, $condition, $num)
  74. {
  75. if ($condition == InviteNewUsersConfig::CONDITION_IDCARD) {
  76. // 实名认证
  77. $res = $this->model->query()
  78. ->where('invite_id', $invite_id)
  79. ->where('status', $this->model::STATUS_OK)
  80. ->where('is_card_certified', $this->model::CARD_CERTIFIED_OK)
  81. ->limit($num)
  82. ->pluck('register_id')
  83. ->toArray();
  84. if (count($res) == $num) {
  85. return $res;
  86. } else {
  87. return false;
  88. }
  89. } elseif ($condition == InviteNewUsersConfig::CONDITION_NEW_ORDER) {
  90. // 验证首单完成并更新
  91. $this->verifyFirstOrder($invite_id);
  92. // 完成首单人数
  93. $res = $this->model->query()
  94. ->where('invite_id', $invite_id)
  95. ->where('status', $this->model::STATUS_OK)
  96. ->where('is_card_certified', $this->model::CARD_CERTIFIED_OK)
  97. ->where('is_first_order', $this->model::FIRST_ORDER_OK)
  98. ->limit($num)
  99. ->pluck('register_id')
  100. ->toArray();
  101. if (count($res) == $num) {
  102. return $res;
  103. } else {
  104. return false;
  105. }
  106. } else {
  107. return false;
  108. }
  109. }
  110. /**
  111. * 验证首单完成并更新 verifyFirstOrder
  112. *
  113. * @param $invite_id
  114. * @return void
  115. * @author Fx
  116. *
  117. */
  118. public function verifyFirstOrder($invite_id)
  119. {
  120. // 实名认证人数
  121. $user_ids = $this->model->query()
  122. ->where('invite_id', $invite_id)
  123. ->where('status', $this->model::STATUS_OK)
  124. ->where('is_card_certified', $this->model::CARD_CERTIFIED_OK)
  125. ->pluck('register_id');
  126. if (!empty($user_ids)) {
  127. foreach ($user_ids as $v) {
  128. $order = Order::query()->where('user_id', $v)->where('status', Order::STATUS_COMPLETE_ORDER)->first();
  129. $orderRent = RentOrder::query()->where('user_id', $v)->where('status', RentOrder::STATUS_COMPLETE_ORDER)->first();
  130. if (!empty($order) || !empty($orderRent)) {
  131. $this->updateFirstOrder($v);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * 更新用户名额失效 updateStatus
  138. *
  139. * @param $register_ids array 注册用户ids
  140. * @return void
  141. * @author Fx
  142. *
  143. */
  144. public function updateStatus($register_ids){
  145. return $this->model->whereIn('register_id',$register_ids)->update(['status'=>$this->model::STATUS_NO]);
  146. }
  147. public function getInviteUsers($invite_id){
  148. $this->verifyFirstOrder($invite_id);
  149. return $this->model->where('invite_id',$invite_id)->get();
  150. }
  151. }