SharedProsperityUserRepository.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. }