UserSpreadServices.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserSpreadDao;
  15. /**
  16. * Class UserSpreadServices
  17. * @package app\services\user
  18. */
  19. class UserSpreadServices extends BaseServices
  20. {
  21. /**
  22. * UserSpreadServices constructor.
  23. * @param UserSpreadDao $dao
  24. */
  25. public function __construct(UserSpreadDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * 记录推广关系
  31. * @param int $uid
  32. * @param int $spread_uid
  33. * @return false|mixed
  34. */
  35. public function setSpread(int $uid, int $spread_uid, int $spread_time = 0)
  36. {
  37. if (!$uid || !$spread_uid) return false;
  38. /** @var UserServices $userServices */
  39. $userServices = app()->make(UserServices::class);
  40. if (!$userServices->getUserInfo($uid, 'uid')) {
  41. return false;
  42. }
  43. if (!$userServices->getUserInfo($spread_uid, 'uid')) {
  44. return false;
  45. }
  46. if ($this->dao->save(['uid' => $uid, 'spread_uid' => $spread_uid, 'spread_time' => $spread_time ?: time()])) {
  47. $userServices->incField($spread_uid, 'spread_count', 1);
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. /**
  54. * 查询推广用户uids
  55. * @param int $uid
  56. * @param int $type 1:一级2:二级 0:所有
  57. * @param array $where
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function getSpreadUids(int $uid, int $type = 0, array $where = [])
  64. {
  65. if (!$uid) return [];
  66. /** @var UserServices $userServices */
  67. $userServices = app()->make(UserServices::class);
  68. if (!$userServices->getUserInfo($uid, 'uid')) {
  69. return [];
  70. }
  71. if ($where && isset($where['time'])) {
  72. $where['timeKey'] = 'spread_time';
  73. }
  74. $where['spread_uid'] = $uid;
  75. $spread_one = $this->dao->getSpreadUids($where);
  76. if ($type == 1) {
  77. return $spread_one;
  78. }
  79. $where['spread_uid'] = $spread_one;
  80. $spread_two = $this->dao->getSpreadUids($where);
  81. if ($type == 2) {
  82. return $spread_two;
  83. }
  84. return array_unique(array_merge($spread_one, $spread_two));
  85. }
  86. }