SharedProsperityUserRepository.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. namespace app\common\repositories\shared;
  3. use app\common\dao\shared\SharedProsperityGroupUserDao;
  4. use app\common\dao\shared\SharedProsperityRewardRecordDao;
  5. use app\common\dao\shared\SharedProsperityUserDao;
  6. use app\common\dao\user\UserDao;
  7. use app\common\enum\CommonEnum;
  8. use app\common\enum\shared\SharedProsperityRewardRecordEnum;
  9. use app\common\enum\shared\SharedProsperityUserEnum;
  10. use app\common\model\user\User;
  11. use app\common\repositories\BaseRepository;
  12. use app\entity\data\shared\SharedProsperityRecommendConfigEntity;
  13. use app\entity\data\shared\SharedProsperityUserEntity;
  14. use think\db\exception\DbException;
  15. use think\facade\Db;
  16. class SharedProsperityUserRepository extends BaseRepository
  17. {
  18. protected $dao;
  19. /**
  20. * SharedProsperityUserRepository constructor.
  21. * @param SharedProsperityUserDao $dao
  22. */
  23. public function __construct(SharedProsperityUserDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * @param int $id
  29. * @return SharedProsperityUserEntity
  30. */
  31. public function getSharedProsperityUserEntityById(int $id): SharedProsperityUserEntity
  32. {
  33. $entityList = $this->getSharedProsperityUserEntityListByIdList([$id]);
  34. $entity = array_shift($entityList);
  35. if ($entity instanceof SharedProsperityUserEntity) {
  36. return $entity;
  37. } else {
  38. /** @var SharedProsperityUserEntity */
  39. return SharedProsperityUserEntity::newInstance();
  40. }
  41. }
  42. /**
  43. * @param array $idList
  44. * @return SharedProsperityUserEntity[]
  45. */
  46. public function getSharedProsperityUserEntityListByIdList(array $idList): array
  47. {
  48. return $this->dao->getSharedProsperityUserEntityListByIdList($idList);
  49. }
  50. /**
  51. * @param int $userId
  52. * @return SharedProsperityUserEntity
  53. */
  54. public function getSharedProsperityUserEntityByUserId(int $userId): SharedProsperityUserEntity
  55. {
  56. $entityList = $this->getSharedProsperityUserEntityListByUserIdList([$userId]);
  57. $entity = array_shift($entityList);
  58. if ($entity instanceof SharedProsperityUserEntity) {
  59. return $entity;
  60. } else {
  61. /** @var SharedProsperityUserEntity */
  62. return SharedProsperityUserEntity::newInstance();
  63. }
  64. }
  65. /**
  66. * @param array $userIdList
  67. * @return SharedProsperityUserEntity[]
  68. */
  69. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  70. {
  71. return $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  72. }
  73. /**
  74. * 获取直接下级用户ID列表
  75. * @param int $parentId 用户ID
  76. * @return SharedProsperityUserEntity[] 直接下级用户ID列表
  77. */
  78. public function getSharedProsperityUserEntityListByParentId(int $parentId): array
  79. {
  80. return $this->dao->getSharedProsperityUserEntityListByParentId($parentId);
  81. }
  82. /**
  83. * @param array $userIdList
  84. * @return SharedProsperityUserEntity[]
  85. */
  86. public function getSharedProsperityUserEntityMapByUserIdList(array $userIdList): array
  87. {
  88. $sharedProsperityUserEntityList = $this->dao->getSharedProsperityUserEntityListByUserIdList($userIdList);
  89. /** @var SharedProsperityUserEntity[] $sharedProsperityUserEntityMapByUserId */
  90. $sharedProsperityUserEntityMapByUserId = array_column($sharedProsperityUserEntityList, null, 'userId');
  91. return $sharedProsperityUserEntityMapByUserId;
  92. }
  93. public function create($data)
  94. {
  95. return $this->dao->create($data);
  96. }
  97. /**
  98. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  99. * @return SharedProsperityUserEntity
  100. */
  101. public function createByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  102. {
  103. $result = $this->create($sharedProsperityUserEntity->toUnderlineArray())->toArray();
  104. /** @var SharedProsperityUserEntity $entity */
  105. $entity = SharedProsperityUserEntity::newInstance($result);
  106. return $entity;
  107. }
  108. /**
  109. * @param $id
  110. * @param $data
  111. * @return int
  112. */
  113. public function update($id, $data): int
  114. {
  115. try {
  116. return $this->dao->update($id, $data);
  117. } catch (DbException $e) {
  118. return 0;
  119. }
  120. }
  121. public function updateByUserId($userId, $data): int
  122. {
  123. try {
  124. return $this->dao->updateByUserId($userId, $data);
  125. } catch (DbException $e) {
  126. return 0;
  127. }
  128. }
  129. /**
  130. * @param SharedProsperityUserEntity $sharedProsperityUserEntity
  131. * @return SharedProsperityUserEntity
  132. */
  133. public function updateByEntity(SharedProsperityUserEntity $sharedProsperityUserEntity): SharedProsperityUserEntity
  134. {
  135. if (empty($sharedProsperityUserEntity->getId())) {
  136. return SharedProsperityUserEntity::newInstance();
  137. }
  138. $result = $this->update($sharedProsperityUserEntity->getId(), $sharedProsperityUserEntity->toUnderlineArray());
  139. if ($result > 0) {
  140. return $this->getSharedProsperityUserEntityById($sharedProsperityUserEntity->getId());
  141. }
  142. return SharedProsperityUserEntity::newInstance();
  143. }
  144. /**
  145. * 绑定 上级
  146. * @param int $userId
  147. * @param int $parentId
  148. * @return bool
  149. */
  150. public function bindParentId(int $userId, int $parentId): bool
  151. {
  152. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
  153. if (empty($sharedProsperityUserEntity->getUserId())) {
  154. return false;
  155. }
  156. $sharedProsperityUserEntityByParentId = $this->getSharedProsperityUserEntityByUserId($parentId);
  157. if (empty($sharedProsperityUserEntityByParentId->getUserId())) {
  158. return false;
  159. }
  160. $sharedProsperityUserEntity = $this->updateByEntity($sharedProsperityUserEntity->setParentId($sharedProsperityUserEntityByParentId->getUserId()));
  161. if (empty($sharedProsperityUserEntity->getId())) {
  162. return false;
  163. }
  164. return true;
  165. }
  166. /**
  167. * 获取直推用户列表,并计算每个直推用户的团队总人数和团队总业绩(包含自己)
  168. * 返回三个列表:全部直推、大部门(团队业绩最大的直推用户)、小部门(其余直推用户)
  169. * 同时返回统计数据:我的部门总人数、总业绩、小部门业绩总和、大部门业绩总和
  170. * @param int $userId
  171. * @return array
  172. */
  173. public function getTeam(int $userId): array
  174. {
  175. // 获取直推用户
  176. $list = $this->dao->getDirectByUserId($userId);
  177. if (empty($list)) {
  178. // 没有直推用户,仍然返回统计数据(我的部门总人数和总业绩)
  179. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
  180. $myTeamTotalCount = count($myTeamUserIds);
  181. $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
  182. return [
  183. 'all_direct' => [],
  184. 'big_department' => [],
  185. 'small_department' => [],
  186. 'stats' => [
  187. 'my_team_total_count' => $myTeamTotalCount,
  188. 'my_team_pv_sum' => $myTeamPvSum,
  189. 'small_department_pv_sum' => 0,
  190. 'big_department_pv_sum' => 0,
  191. ]
  192. ];
  193. }
  194. // 收集所有直推用户ID
  195. $userIds = array_column($list, 'user_id');
  196. // 获取用户详细信息(昵称、头像、注册时间)
  197. /** @var UserDao $userDao */
  198. $userDao = app()->make(UserDao::class);
  199. $userEntities = $userDao->getUserEntityListByUidList($userIds);
  200. $userMap = [];
  201. foreach ($userEntities as $userEntity) {
  202. $userMap[$userEntity->getUid()] = [
  203. 'nickname' => $userEntity->getNickname(),
  204. 'avatar' => $userEntity->getAvatar(),
  205. 'create_time' => $userEntity->getCreateTime(),
  206. ];
  207. }
  208. // 计算每个直推用户的团队总人数和团队总业绩
  209. $maxPv = 0;
  210. $maxPvIndex = -1;
  211. foreach ($list as $index => &$item) {
  212. $teamUserIds = $this->dao->getAllSubordinateUserIds($item['user_id'], true);
  213. $teamTotalCount = count($teamUserIds);
  214. $teamPvSum = $this->dao->getTeamPvSumWithSelf($item['user_id']);
  215. $item['team_total_count'] = $teamTotalCount;
  216. $item['team_pv_sum'] = $teamPvSum;
  217. // 补充用户信息
  218. if (isset($userMap[$item['user_id']])) {
  219. $item['nickname'] = $userMap[$item['user_id']]['nickname'];
  220. $item['avatar'] = $userMap[$item['user_id']]['avatar'];
  221. $item['create_time'] = $userMap[$item['user_id']]['create_time'];
  222. } else {
  223. $item['nickname'] = '';
  224. $item['avatar'] = '';
  225. $item['create_time'] = '';
  226. }
  227. // 找出团队业绩最大的直推用户
  228. if ($teamPvSum > $maxPv) {
  229. $maxPv = $teamPvSum;
  230. $maxPvIndex = $index;
  231. }
  232. }
  233. unset($item);
  234. // 分割大部门和小部门
  235. $bigDepartment = [];
  236. $smallDepartment = [];
  237. if ($maxPvIndex >= 0) {
  238. $bigDepartment[] = $list[$maxPvIndex];
  239. foreach ($list as $index => $item) {
  240. if ($index !== $maxPvIndex) {
  241. $smallDepartment[] = $item;
  242. }
  243. }
  244. } else {
  245. // 没有直推用户(理论上不会发生)
  246. $smallDepartment = $list;
  247. }
  248. // 计算统计数据
  249. $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
  250. $myTeamTotalCount = count($myTeamUserIds);
  251. $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
  252. $smallDepartmentPvSum = 0;
  253. foreach ($smallDepartment as $item) {
  254. $smallDepartmentPvSum += $item['team_pv_sum'];
  255. }
  256. $bigDepartmentPvSum = 0;
  257. foreach ($bigDepartment as $item) {
  258. $bigDepartmentPvSum += $item['team_pv_sum'];
  259. }
  260. return [
  261. 'all_direct' => $list,
  262. 'big_department' => $bigDepartment,
  263. 'small_department' => $smallDepartment,
  264. 'stats' => [
  265. 'my_team_total_count' => $myTeamTotalCount,
  266. 'my_team_pv_sum' => $myTeamPvSum,
  267. 'small_department_pv_sum' => $smallDepartmentPvSum,
  268. 'big_department_pv_sum' => $bigDepartmentPvSum,
  269. ]
  270. ];
  271. }
  272. /**
  273. * 批量更新
  274. * @param $dataList
  275. * @return array
  276. */
  277. public function batchUpdateSharedProsperityUserDataByDataList($dataList): array
  278. {
  279. return $this->dao->saveAll($dataList);
  280. }
  281. /**
  282. * 用户身份升级
  283. * @param array $userIdList
  284. * @return void
  285. */
  286. public function identityUpgrade(array $userIdList)
  287. {
  288. if (empty($userIdList)) {
  289. return;
  290. }
  291. /** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
  292. $sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
  293. $sharedProsperityRecommendConfigEntityList = $sharedProsperityRecommendConfigRepository->getList();
  294. usort($sharedProsperityRecommendConfigEntityList, function (SharedProsperityRecommendConfigEntity $a, SharedProsperityRecommendConfigEntity $b) {
  295. return $a->getPv() <=> $b->getPv(); // 从小到大排序
  296. });
  297. $sharedProsperityUserEntityList = $this->getSharedProsperityUserEntityListByUserIdList($userIdList);
  298. // 批量更新用户等级
  299. $this->batchUpgradeUsers($sharedProsperityUserEntityList, $sharedProsperityRecommendConfigEntityList);
  300. // foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
  301. // $partnerLevel = 0;
  302. // foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
  303. // if ($sharedProsperityUserEntity->getPv() >= $sharedProsperityRecommendConfigEntity->getPv()) {
  304. // $partnerLevel = $sharedProsperityRecommendConfigEntity->getLevel();
  305. // } else {
  306. // // 如果本次验证的等级结果大于 之前的等级,说明用户升级了 需要更新等级
  307. // if ($partnerLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  308. // $this->updateByEntity(
  309. // SharedProsperityUserEntity::newInstance()
  310. // ->setId($sharedProsperityUserEntity->getId())
  311. // ->setPartnerLevel($partnerLevel)
  312. // );
  313. // }
  314. // break;
  315. // }
  316. // }
  317. // }
  318. }
  319. /**
  320. * 批量更新用户等级
  321. * @param SharedProsperityUserEntity[] $sharedProsperityUserEntityList 用户实体列表
  322. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  323. * @return void
  324. */
  325. private function batchUpgradeUsers(array $sharedProsperityUserEntityList, array $sharedProsperityRecommendConfigEntityList)
  326. {
  327. foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
  328. // // 计算用户的团队业绩(向下5级)
  329. // $teamPv = $this->calculateTeamPv($sharedProsperityUserEntity->getId());
  330. // 根据团队业绩确定等级
  331. // $newLevel = $this->calculateLevelByTeamPv($teamPv, $sharedProsperityRecommendConfigEntityList);
  332. //
  333. // // 如果新等级大于当前等级,则更新
  334. // if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  335. // $this->updateByEntity(
  336. // SharedProsperityUserEntity::newInstance()
  337. // ->setId($sharedProsperityUserEntity->getId())
  338. // ->setPartnerLevel($newLevel)
  339. // );
  340. // }
  341. $this->upgradeUpperLevels($sharedProsperityUserEntity->getUserId(), $sharedProsperityRecommendConfigEntityList);
  342. }
  343. }
  344. /**
  345. * 向上追溯更新上级身份等级(最多向上5层)
  346. * @param int $userId 当前用户ID
  347. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  348. * @return void
  349. */
  350. private function upgradeUpperLevels(int $userId, array $sharedProsperityRecommendConfigEntityList)
  351. {
  352. $maxLevels = SharedProsperityUserEnum::TEAM_DEPTH;
  353. $currentUserId = $userId;
  354. for ($i = 0; $i < $maxLevels; $i++) {
  355. // 获取上级用户ID
  356. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($currentUserId);
  357. $parentId = $sharedProsperityUserEntity->getParentId();
  358. if (empty($parentId)) {
  359. break; // 没有上级,停止追溯
  360. }
  361. // 重新计算上级的团队业绩和等级
  362. $this->upgradeSingleUser($parentId, $sharedProsperityRecommendConfigEntityList);
  363. // 继续向上追溯
  364. $currentUserId = $parentId;
  365. }
  366. }
  367. /**
  368. * 升级单个用户身份
  369. * @param int $userId 用户ID
  370. * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
  371. * @return void
  372. */
  373. private function upgradeSingleUser(int $userId, array $sharedProsperityRecommendConfigEntityList)
  374. {
  375. // 获取用户实体
  376. $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
  377. if (empty($sharedProsperityUserEntity->getId())) {
  378. return;
  379. }
  380. // 计算团队业绩
  381. $teamPvMap = $this->calculateTeamPv($userId);
  382. // 当前只计算团队业绩的 小团队业绩
  383. $maxKey = array_keys($teamPvMap, max($teamPvMap))[0];
  384. unset($teamPvMap[$maxKey]);
  385. $teamPvBySmallSum = array_sum($teamPvMap);
  386. // 计算新等级
  387. $newLevel = $this->calculateLevelByTeamPv($teamPvBySmallSum, $sharedProsperityRecommendConfigEntityList);
  388. // 如果新等级大于当前等级,则更新
  389. if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
  390. $this->updateByEntity(
  391. SharedProsperityUserEntity::newInstance()
  392. ->setId($sharedProsperityUserEntity->getId())
  393. ->setPartnerLevel($newLevel)
  394. );
  395. }
  396. }
  397. /**
  398. * 计算用户的团队业绩(向下5级)
  399. * @param int $userId 用户ID
  400. * @return array 团队业绩
  401. */
  402. private function calculateTeamPv(int $userId): array
  403. {
  404. $teamPvMap = [];
  405. // 获取所有下级用户ID(向下5级)
  406. // 第一层级在这里查询 因为要统计大小区
  407. $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($userId);
  408. // 循环 直属下级
  409. foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntityByUnderling) {
  410. $departmentTeamUserIdList = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntityByUnderling->getUserId(), SharedProsperityUserEnum::TEAM_DEPTH - 1);
  411. $departmentTeamUserIdList[] = $sharedProsperityUserEntityByUnderling->getUserId();
  412. // 直属团队的 业绩总和
  413. $teamPv = '0.00';
  414. if (!empty($departmentTeamUserIdList)) {
  415. // 批量获取下级用户的个人业绩并求和
  416. // 假设有方法批量获取用户的PV
  417. $sharedProsperityUserEntityListByTeam = $this->getSharedProsperityUserEntityListByUserIdList($departmentTeamUserIdList);
  418. foreach ($sharedProsperityUserEntityListByTeam as $sharedProsperityUserEntityByTeam) {
  419. $teamPv = bcadd($teamPv, (string)$sharedProsperityUserEntityByTeam->getPv(), 2);
  420. }
  421. }
  422. $teamPvMap[$sharedProsperityUserEntityByUnderling->getUserId()] = $teamPv;
  423. }
  424. return $teamPvMap;
  425. }
  426. /**
  427. * 根据团队业绩计算等级
  428. * @param float $teamPv 团队业绩
  429. * @param array $configEntityList 等级配置列表
  430. * @return int 等级
  431. */
  432. private function calculateLevelByTeamPv(float $teamPv, array $configEntityList): int
  433. {
  434. $level = 0;
  435. foreach ($configEntityList as $configEntity) {
  436. if ($teamPv >= $configEntity->getPv()) {
  437. $level = $configEntity->getLevel();
  438. } else {
  439. break;
  440. }
  441. }
  442. return $level;
  443. }
  444. /**
  445. * 获取下级用户ID列表(向下指定层级)
  446. * @param int $parentId 用户ID
  447. * @param int $maxLevel 最大层级数
  448. * @param int $currentLevel 当前层级(内部递归使用)
  449. * @return array 下级用户ID列表
  450. */
  451. private function getUserIdListByParentIdForDepth(int $parentId, int $maxLevel, int $currentLevel = 1): array
  452. {
  453. if ($currentLevel > $maxLevel) {
  454. return [];
  455. }
  456. $subordinateIds = [];
  457. // 获取直接下级用户ID列表(假设有方法获取直接下级)
  458. $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($parentId);
  459. if (empty($sharedProsperityUserEntityListByParentId)) {
  460. return [];
  461. }
  462. // 添加直接下级
  463. $subordinateIds = array_merge($subordinateIds, array_column($sharedProsperityUserEntityListByParentId, 'userId'));
  464. // 递归获取下级的下级
  465. foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntity) {
  466. $nestedSubordinateIds = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntity->getUserId(), $maxLevel, $currentLevel + 1);
  467. if (!empty($nestedSubordinateIds)) {
  468. $subordinateIds = array_merge($subordinateIds, $nestedSubordinateIds);
  469. }
  470. }
  471. return array_unique($subordinateIds);
  472. }
  473. /**
  474. * 获取用户树
  475. * @param int $userId 用户ID
  476. * @return array 用户树,格式:[{name: '张三', parent: null, children: [...]}]
  477. */
  478. public function getUserTree($userId)
  479. {
  480. /** @var SharedProsperityUserDao $userDao */
  481. $userDao = app()->make(SharedProsperityUserDao::class);
  482. // 获取所有下级用户ID(包括自己)
  483. $subordinateIds = $userDao->getAllSubordinateUserIds($userId, true);
  484. if (empty($subordinateIds)) {
  485. return [];
  486. }
  487. // 获取这些共富用户的基本信息(包含parent_id)
  488. $sharedUsers = $userDao->getSharedProsperityUserEntityListByUserIdList($subordinateIds);
  489. if (empty($sharedUsers)) {
  490. return [];
  491. }
  492. // 获取用户昵称映射
  493. $userIds = array_map(function ($entity) {
  494. return $entity->getUserId();
  495. }, $sharedUsers);
  496. /** @var UserDao $userDao */
  497. $userDao = app()->make(UserDao::class);
  498. $userInfos = $userDao->getUserEntityListByUidList($userIds);
  499. $idToName = [];
  500. foreach ($userInfos as $userInfo) {
  501. $idToName[$userInfo->getUid()] = $userInfo->getNickname() . '(' . $userInfo->account . ')' ?? ('用户' . $userInfo->getUid());
  502. }
  503. // 构建节点数组
  504. $nodes = [];
  505. foreach ($sharedUsers as $sharedUser) {
  506. $nodes[] = [
  507. 'user_id' => $sharedUser->getUserId(),
  508. 'parent_id' => $sharedUser->getParentId(),
  509. 'name' => $idToName[$sharedUser->getUserId()] ?? ('用户' . $sharedUser->getUserId()),
  510. ];
  511. }
  512. // 构建树,以传入的用户为根节点
  513. $rootNode = $this->findNode($nodes, $userId);
  514. if (!$rootNode) {
  515. return [];
  516. }
  517. $tree = [
  518. 'name' => $rootNode['name'],
  519. 'parent' => null,
  520. 'children' => $this->buildSharedTree($nodes, $userId)
  521. ];
  522. return [$tree];
  523. }
  524. /**
  525. * 递归构建共富用户树
  526. * @param array $nodes 节点数组,每个节点包含 user_id, parent_id, name
  527. * @param int $parentId 父ID
  528. * @return array
  529. */
  530. private function buildSharedTree($nodes, $parentId)
  531. {
  532. $branch = [];
  533. foreach ($nodes as $node) {
  534. if ($node['parent_id'] == $parentId) {
  535. $children = $this->buildSharedTree($nodes, $node['user_id']);
  536. $branch[] = [
  537. 'name' => $node['name'],
  538. 'parent' => $this->findNodeName($nodes, $parentId) ?? null,
  539. 'children' => $children
  540. ];
  541. }
  542. }
  543. return $branch;
  544. }
  545. /**
  546. * 根据user_id查找节点名称
  547. * @param array $nodes
  548. * @param int $userId
  549. * @return string|null
  550. */
  551. private function findNodeName($nodes, $userId)
  552. {
  553. foreach ($nodes as $node) {
  554. if ($node['user_id'] == $userId) {
  555. return $node['name'];
  556. }
  557. }
  558. return null;
  559. }
  560. /**
  561. * 根据user_id查找节点
  562. * @param array $nodes
  563. * @param int $userId
  564. * @return array|null
  565. */
  566. private function findNode($nodes, $userId)
  567. {
  568. foreach ($nodes as $node) {
  569. if ($node['user_id'] == $userId) {
  570. return $node;
  571. }
  572. }
  573. return null;
  574. }
  575. public function adminList($params)
  576. {
  577. $page = $params['page'] ?? 1;
  578. $where = [];
  579. /** @var UserDao $userDao */
  580. $userDao = app()->make(UserDao::class);
  581. // 手机号搜索
  582. if (isset($params['account']) && $params['account']) {
  583. $userData = $userDao->accountByUser($params['account']);
  584. $uid = 0;
  585. if ($userData) {
  586. $uid = $userData->uid;
  587. }
  588. $where[] = ['user_id', '=', $uid];
  589. }
  590. // 昵称搜索
  591. if (isset($params['nickname']) && $params['nickname']) {
  592. $userData = $userDao->nicknameByUser($params['nickname']);
  593. $uid = 0;
  594. if ($userData) {
  595. $uid = $userData->uid;
  596. }
  597. $where[] = ['user_id', '=', $uid];
  598. }
  599. // 合伙人等级搜索
  600. if (isset($params['partner_level']) && $params['partner_level']) {
  601. $where[] = ['partner_level', '=', $params['partner_level']];
  602. }
  603. // 共富团等级搜索
  604. if (isset($params['user_group']) && $params['user_group']) {
  605. /** @var SharedProsperityGroupUserDao $groupUserDao */
  606. $groupUserDao = app()->make(SharedProsperityGroupUserDao::class);
  607. $groupUserData = $groupUserDao->getUserByLevel($params['user_group']);
  608. $uids = array_column($groupUserData['data'], 'user_id');
  609. $where[] = ['user_id', 'in', $uids];
  610. $page = 1;
  611. }
  612. $list = $this->dao->adminList($where, $page);
  613. return $list;
  614. }
  615. /**
  616. * @param $userId
  617. * @param $level
  618. * @return int
  619. * @author 史晨
  620. * @date 2026/2/27 14:23
  621. * 更新用户等级
  622. */
  623. public function updateLevel($userId, $level)
  624. {
  625. return $this->dao->updateByUserId($userId, ['partner_level' => $level]);
  626. }
  627. /**
  628. * @param $userId
  629. * @param $type
  630. * @param $number
  631. * @return bool
  632. * @author 史晨
  633. * @date 2026/2/27 15:16
  634. * 更新用户奖励
  635. */
  636. public function updateReward($userId, $type, $number)
  637. {
  638. $map = [
  639. 1001 => 'consumer_stocks',
  640. 1002 => 'withdrawal_limit',
  641. 1003 => 'pv'
  642. ];
  643. try {
  644. Db::startTrans();
  645. $pm = 1;
  646. $remark = '后台操作增加' . $number;
  647. if ($number < 0) {
  648. $pm = 0;
  649. $remark = '后台操作减少' . abs($number);
  650. }
  651. if (isset($map[$type])) {
  652. // 更新数据
  653. $this->dao->updateRewardByUserId($userId, $map[$type], $number);
  654. // 记录日志
  655. $insertData = [
  656. 'user_id' => $userId,
  657. 'type' => $type,
  658. 'pm' => $pm,
  659. 'number' => abs($number),
  660. 'balance' => 0,
  661. 'status' => SharedProsperityRewardRecordEnum::STATUS['VALID']['code'],
  662. 'type_shop' => SharedProsperityRewardRecordEnum::TYPE_SHOP['Platform']['code'],
  663. 'order_sn' => 0,
  664. 'take_time' => date('Y-m-d H:i:s', time()),
  665. 'remark' => $remark,
  666. 'create_time' => date('Y-m-d H:i:s', time()),
  667. 'update_time' => date('Y-m-d H:i:s', time()),
  668. ];
  669. /** @var SharedProsperityRewardRecordDao $rewardRecordDao */
  670. $rewardRecordDao = app()->make(SharedProsperityRewardRecordDao::class);
  671. $rewardRecordDao->insertAll([$insertData]);
  672. } else {
  673. // 更新数据
  674. /** @var UserDao $userDao */
  675. $userDao = app()->make(UserDao::class);
  676. $userDao->updateBrokeragePriceByUid($userId, $number);;
  677. // 记录日志
  678. $bill = [
  679. 'uid' => $userId,
  680. 'link_id' => 0,
  681. 'pm' => $pm,
  682. 'title' => $remark,
  683. 'category' => 'now_money',
  684. 'type' => 'commission',
  685. 'number' => abs($number),
  686. 'balance' => 0,
  687. 'mark' => $remark,
  688. 'create_time' => date('Y-m-d H:i:s'),
  689. 'status' => 1,
  690. 'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],
  691. 'order_sn' => 0,
  692. 'tripartite' => 0,
  693. 'type_shop' => 0,
  694. 'mer_id' => 0,
  695. 'source' => 0,
  696. 'order_type' => 1,
  697. 'is_red_brokerage' => 0,
  698. 'gzc' => 3,
  699. 'take_time' => time(),
  700. 'district_id' => '',
  701. 'street_id' => '',
  702. 'month' => '',
  703. ];
  704. Db::name('user_bill')->insert($bill);
  705. }
  706. Db::commit();
  707. return true;
  708. } catch (\Exception $e) {
  709. Db::rollback();
  710. return false;
  711. }
  712. }
  713. }