SharedProsperityUserRepository.php 29 KB

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