SharedProsperityUserDao.php 8.5 KB

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