SharedProsperityUserDao.php 9.3 KB

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