SharedProsperityUserDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\common\dao\shared;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\shared\SharedProsperityUser;
  5. use app\entity\data\shared\SharedProsperityUserEntity;
  6. class SharedProsperityUserDao extends BaseDao
  7. {
  8. protected function getModel(): string
  9. {
  10. return SharedProsperityUser::class;
  11. }
  12. /**
  13. * @param array $userIdList
  14. * @return SharedProsperityUserEntity[]
  15. */
  16. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  17. {
  18. $entityList = [];
  19. try {
  20. /** @var SharedProsperityUser $model */
  21. $model = $this->getModel();
  22. $dataRes = $model::getDB()
  23. ->whereIn('user_id', $userIdList)
  24. ->select()
  25. ->toArray();
  26. if (!empty($dataRes)) {
  27. $entityList = array_map(function ($data) {
  28. return SharedProsperityUserEntity::newInstance($data);
  29. }, $dataRes);
  30. }
  31. } catch (\Exception $e) {
  32. }
  33. return $entityList;
  34. }
  35. /**
  36. * 获取所有直推用户(不分页)
  37. * @param int $userId
  38. * @return array
  39. * @author 史晨
  40. * @date 2026/2/9 09:35
  41. */
  42. public function getDirectByUserId(int $userId): array
  43. {
  44. $list = [];
  45. try {
  46. /** @var SharedProsperityUser $model */
  47. $model = $this->getModel();
  48. $list = $model::getDB()
  49. ->where('parent_id', $userId)
  50. ->select()
  51. ->toArray();
  52. } catch (\Exception $e) {
  53. }
  54. return $list;
  55. }
  56. /**
  57. * 递归获取团队所有下级用户ID(可选择是否包含自己)
  58. * @param int $userId
  59. * @param bool $includeSelf 是否包含自己
  60. * @return int[]
  61. */
  62. public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false): array
  63. {
  64. $allIds = [];
  65. try {
  66. /** @var SharedProsperityUser $model */
  67. $model = $this->getModel();
  68. $getRecursiveChildren = function ($parentId) use (&$getRecursiveChildren, $model) {
  69. $children = $model::getDB()
  70. ->where('parent_id', $parentId)
  71. ->column('user_id');
  72. $allChildren = [];
  73. foreach ($children as $childId) {
  74. $allChildren[] = $childId;
  75. $allChildren = array_merge($allChildren, $getRecursiveChildren($childId));
  76. }
  77. return $allChildren;
  78. };
  79. $allIds = $getRecursiveChildren($userId);
  80. if ($includeSelf) {
  81. array_unshift($allIds, $userId);
  82. }
  83. } catch (\Exception $e) {
  84. // 记录日志或忽略
  85. }
  86. return $allIds;
  87. }
  88. /**
  89. * 获取团队所有下级用户PV总和(不包括自己)
  90. * @param int $userId
  91. * @return float
  92. */
  93. public function getTeamPvSum(int $userId): float
  94. {
  95. $userIds = $this->getAllSubordinateUserIds($userId, false);
  96. if (empty($userIds)) {
  97. return 0.0;
  98. }
  99. try {
  100. /** @var SharedProsperityUser $model */
  101. $model = $this->getModel();
  102. $sum = $model::getDB()
  103. ->whereIn('user_id', $userIds)
  104. ->sum('pv');
  105. return (float)$sum;
  106. } catch (\Exception $e) {
  107. return 0.0;
  108. }
  109. }
  110. /**
  111. * 获取团队所有下级用户PV总和(包括自己)
  112. * @param int $userId
  113. * @return float
  114. */
  115. public function getTeamPvSumWithSelf(int $userId): float
  116. {
  117. $userIds = $this->getAllSubordinateUserIds($userId, true);
  118. if (empty($userIds)) {
  119. return 0.0;
  120. }
  121. try {
  122. /** @var SharedProsperityUser $model */
  123. $model = $this->getModel();
  124. $sum = $model::getDB()
  125. ->whereIn('user_id', $userIds)
  126. ->sum('pv');
  127. return (float)$sum;
  128. } catch (\Exception $e) {
  129. return 0.0;
  130. }
  131. }
  132. /**
  133. * 获取团队所有下级用户实体列表(不包括自己)
  134. * @param int $userId
  135. * @return SharedProsperityUserEntity[]
  136. */
  137. public function getTeamUserEntities(int $userId): array
  138. {
  139. $userIds = $this->getAllSubordinateUserIds($userId, false);
  140. if (empty($userIds)) {
  141. return [];
  142. }
  143. return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
  144. }
  145. /**
  146. * 获取团队所有下级用户实体列表(包括自己)
  147. * @param int $userId
  148. * @return SharedProsperityUserEntity[]
  149. */
  150. public function getTeamUserEntitiesWithSelf(int $userId): array
  151. {
  152. $userIds = $this->getAllSubordinateUserIds($userId, true);
  153. if (empty($userIds)) {
  154. return [];
  155. }
  156. return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
  157. }
  158. /**
  159. * 获取团队所有下级用户(包括分页,不包括自己)
  160. * @param int $userId
  161. * @param int $page
  162. * @param int $pageSize
  163. * @return array
  164. */
  165. public function getTeamUsers(int $userId, int $page = 1, int $pageSize = 10): array
  166. {
  167. $userIds = $this->getAllSubordinateUserIds($userId, false);
  168. if (empty($userIds)) {
  169. return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
  170. }
  171. try {
  172. /** @var SharedProsperityUser $model */
  173. $model = $this->getModel();
  174. $list = $model::getDB()
  175. ->whereIn('user_id', $userIds)
  176. ->paginate(['page' => $page, 'list_rows' => $pageSize])
  177. ->toArray();
  178. return $list;
  179. } catch (\Exception $e) {
  180. return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
  181. }
  182. }
  183. /**
  184. * 获取团队所有下级用户(包括分页,包括自己)
  185. * @param int $userId
  186. * @param int $page
  187. * @param int $pageSize
  188. * @return array
  189. */
  190. public function getTeamUsersWithSelf(int $userId, int $page = 1, int $pageSize = 10): array
  191. {
  192. $userIds = $this->getAllSubordinateUserIds($userId, true);
  193. if (empty($userIds)) {
  194. return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
  195. }
  196. try {
  197. /** @var SharedProsperityUser $model */
  198. $model = $this->getModel();
  199. $list = $model::getDB()
  200. ->whereIn('user_id', $userIds)
  201. ->paginate(['page' => $page, 'list_rows' => $pageSize])
  202. ->toArray();
  203. return $list;
  204. } catch (\Exception $e) {
  205. return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
  206. }
  207. }
  208. }