| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace app\common\dao\shared;
- use app\common\dao\BaseDao;
- use app\common\model\BaseModel;
- use app\common\model\shared\SharedProsperityUser;
- use app\entity\data\shared\SharedProsperityUserEntity;
- class SharedProsperityUserDao extends BaseDao
- {
- /**
- * @return BaseModel
- */
- protected function getModel(): string
- {
- return SharedProsperityUser::class;
- }
- /**
- * @param array $userIdList
- * @return SharedProsperityUserEntity[]
- */
- public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
- {
- $entityList = [];
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $dataRes = $model::getDB()
- ->whereIn('user_id', $userIdList)
- ->select()
- ->toArray();
- if (!empty($dataRes)) {
- $entityList = array_map(function ($data) {
- return SharedProsperityUserEntity::newInstance($data);
- }, $dataRes);
- }
- } catch (\Exception $e) {
- }
- return $entityList;
- }
- /**
- * 获取所有直推用户(不分页)
- * @param int $userId
- * @return array
- * @author 史晨
- * @date 2026/2/9 09:35
- */
- public function getDirectByUserId(int $userId): array
- {
- $list = [];
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $list = $model::getDB()
- ->where('parent_id', $userId)
- ->select()
- ->toArray();
- } catch (\Exception $e) {
- }
- return $list;
- }
- /**
- * 递归获取团队所有下级用户ID(可选择是否包含自己)
- * @param int $userId
- * @param bool $includeSelf 是否包含自己
- * @return int[]
- */
- public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false): array
- {
- $allIds = [];
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $getRecursiveChildren = function ($parentId) use (&$getRecursiveChildren, $model) {
- $children = $model::getDB()
- ->where('parent_id', $parentId)
- ->column('user_id');
- $allChildren = [];
- foreach ($children as $childId) {
- $allChildren[] = $childId;
- $allChildren = array_merge($allChildren, $getRecursiveChildren($childId));
- }
- return $allChildren;
- };
- $allIds = $getRecursiveChildren($userId);
- if ($includeSelf) {
- array_unshift($allIds, $userId);
- }
- } catch (\Exception $e) {
- // 记录日志或忽略
- }
- return $allIds;
- }
- /**
- * 获取团队所有下级用户PV总和(不包括自己)
- * @param int $userId
- * @return float
- */
- public function getTeamPvSum(int $userId): float
- {
- $userIds = $this->getAllSubordinateUserIds($userId, false);
- if (empty($userIds)) {
- return 0.0;
- }
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $sum = $model::getDB()
- ->whereIn('user_id', $userIds)
- ->sum('pv');
- return (float)$sum;
- } catch (\Exception $e) {
- return 0.0;
- }
- }
- /**
- * 获取团队所有下级用户PV总和(包括自己)
- * @param int $userId
- * @return float
- */
- public function getTeamPvSumWithSelf(int $userId): float
- {
- $userIds = $this->getAllSubordinateUserIds($userId, true);
- if (empty($userIds)) {
- return 0.0;
- }
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $sum = $model::getDB()
- ->whereIn('user_id', $userIds)
- ->sum('pv');
- return (float)$sum;
- } catch (\Exception $e) {
- return 0.0;
- }
- }
- /**
- * 获取团队所有下级用户实体列表(不包括自己)
- * @param int $userId
- * @return SharedProsperityUserEntity[]
- */
- public function getTeamUserEntities(int $userId): array
- {
- $userIds = $this->getAllSubordinateUserIds($userId, false);
- if (empty($userIds)) {
- return [];
- }
- return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
- }
- /**
- * 获取团队所有下级用户实体列表(包括自己)
- * @param int $userId
- * @return SharedProsperityUserEntity[]
- */
- public function getTeamUserEntitiesWithSelf(int $userId): array
- {
- $userIds = $this->getAllSubordinateUserIds($userId, true);
- if (empty($userIds)) {
- return [];
- }
- return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
- }
- /**
- * 获取团队所有下级用户(包括分页,不包括自己)
- * @param int $userId
- * @param int $page
- * @param int $pageSize
- * @return array
- */
- public function getTeamUsers(int $userId, int $page = 1, int $pageSize = 10): array
- {
- $userIds = $this->getAllSubordinateUserIds($userId, false);
- if (empty($userIds)) {
- return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
- }
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $list = $model::getDB()
- ->whereIn('user_id', $userIds)
- ->paginate(['page' => $page, 'list_rows' => $pageSize])
- ->toArray();
- return $list;
- } catch (\Exception $e) {
- return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
- }
- }
- /**
- * 获取团队所有下级用户(包括分页,包括自己)
- * @param int $userId
- * @param int $page
- * @param int $pageSize
- * @return array
- */
- public function getTeamUsersWithSelf(int $userId, int $page = 1, int $pageSize = 10): array
- {
- $userIds = $this->getAllSubordinateUserIds($userId, true);
- if (empty($userIds)) {
- return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
- }
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $list = $model::getDB()
- ->whereIn('user_id', $userIds)
- ->paginate(['page' => $page, 'list_rows' => $pageSize])
- ->toArray();
- return $list;
- } catch (\Exception $e) {
- return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
- }
- }
- /**
- * 批量更新
- * @param $dataList
- * @return array
- */
- public function saveAll($dataList): array
- {
- try {
- return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
- } catch (\Exception $e) {
- return [];
- }
- }
- /**
- * 根据用户ID更新记录
- * @param int $userId
- * @param array $data
- * @return int
- */
- public function updateByUserId(int $userId, array $data): int
- {
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- return $model::getDB()->where('user_id', $userId)->update($data);
- } catch (\Exception $e) {
- return 0;
- }
- }
- /**
- * 按合伙人等级分组获取用户
- * @return array [partner_level => [user_id1, user_id2, ...]]
- */
- public function getUserGroupByPartnerLevel(): array
- {
- try {
- /** @var SharedProsperityUser $model */
- $model = $this->getModel();
- $dataRes = $model::getDB()
- ->field('partner_level, user_id')
- ->select()
- ->toArray();
-
- $grouped = [];
- foreach ($dataRes as $row) {
- $level = $row['partner_level'];
- if (!isset($grouped[$level])) {
- $grouped[$level] = [];
- }
- $grouped[$level][] = $row['user_id'];
- }
- return $grouped;
- } catch (\Exception $e) {
- return [];
- }
- }
- }
|