SharedProsperityUserDao.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\common\dao\shared;
  3. use app\common\dao\BaseDao;
  4. use app\common\enum\CommonEnum;
  5. use app\common\model\BaseModel;
  6. use app\common\model\shared\SharedProsperityUser;
  7. use app\entity\data\shared\SharedProsperityUserEntity;
  8. class SharedProsperityUserDao extends BaseDao
  9. {
  10. /**
  11. * @return BaseModel
  12. */
  13. protected function getModel(): string
  14. {
  15. return SharedProsperityUser::class;
  16. }
  17. /**
  18. * @param array $userIdList
  19. * @return SharedProsperityUserEntity[]
  20. */
  21. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  22. {
  23. $entityList = [];
  24. try {
  25. /** @var SharedProsperityUser $model */
  26. $model = $this->getModel();
  27. $dataRes = $model::getDB()
  28. ->whereIn('user_id', $userIdList)
  29. ->select()
  30. ->toArray();
  31. if (!empty($dataRes)) {
  32. $entityList = array_map(function ($data) {
  33. return SharedProsperityUserEntity::newInstance($data);
  34. }, $dataRes);
  35. }
  36. } catch (\Exception $e) {
  37. }
  38. return $entityList;
  39. }
  40. /**
  41. * 获取所有直推用户(不分页)
  42. * @param int $userId
  43. * @return array
  44. * @author 史晨
  45. * @date 2026/2/9 09:35
  46. */
  47. public function getDirectByUserId(int $userId): array
  48. {
  49. $list = [];
  50. try {
  51. /** @var SharedProsperityUser $model */
  52. $model = $this->getModel();
  53. $list = $model::getDB()
  54. ->where('parent_id', $userId)
  55. ->select()
  56. ->toArray();
  57. } catch (\Exception $e) {
  58. }
  59. return $list;
  60. }
  61. /**
  62. * 递归获取团队所有下级用户ID(可选择是否包含自己)
  63. * @param int $userId
  64. * @param bool $includeSelf 是否包含自己
  65. * @param int $hierarchy 层级限制,0表示不限制,>0表示限制层级
  66. * @return int[]
  67. */
  68. public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false, int $hierarchy = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Hierarchy']['code']): array
  69. {
  70. $allIds = [];
  71. try {
  72. /** @var SharedProsperityUser $model */
  73. $model = $this->getModel();
  74. $getRecursiveChildren = function ($parentId, $currentLevel = 1) use (&$getRecursiveChildren, $model, $hierarchy) {
  75. // 如果设置了层级限制且当前层级超过限制,则停止递归
  76. if ($hierarchy > 0 && $currentLevel > $hierarchy) {
  77. return [];
  78. }
  79. $children = $model::getDB()
  80. ->where('parent_id', $parentId)
  81. ->column('user_id');
  82. $allChildren = [];
  83. foreach ($children as $childId) {
  84. $allChildren[] = $childId;
  85. $allChildren = array_merge($allChildren, $getRecursiveChildren($childId, $currentLevel + 1));
  86. }
  87. return $allChildren;
  88. };
  89. $allIds = $getRecursiveChildren($userId);
  90. if ($includeSelf) {
  91. array_unshift($allIds, $userId);
  92. }
  93. } catch (\Exception $e) {
  94. // 记录日志或忽略
  95. }
  96. return $allIds;
  97. }
  98. /**
  99. * 获取团队所有下级用户PV总和(不包括自己)
  100. * @param int $userId
  101. * @return float
  102. */
  103. public function getTeamPvSum(int $userId): float
  104. {
  105. $userIds = $this->getAllSubordinateUserIds($userId, false);
  106. if (empty($userIds)) {
  107. return 0.0;
  108. }
  109. try {
  110. /** @var SharedProsperityUser $model */
  111. $model = $this->getModel();
  112. $sum = $model::getDB()
  113. ->whereIn('user_id', $userIds)
  114. ->sum('pv');
  115. return (float)$sum;
  116. } catch (\Exception $e) {
  117. return 0.0;
  118. }
  119. }
  120. /**
  121. * 获取团队所有下级用户PV总和(包括自己)
  122. * @param int $userId
  123. * @return float
  124. */
  125. public function getTeamPvSumWithSelf(int $userId): float
  126. {
  127. $userIds = $this->getAllSubordinateUserIds($userId, true);
  128. if (empty($userIds)) {
  129. return 0.0;
  130. }
  131. try {
  132. /** @var SharedProsperityUser $model */
  133. $model = $this->getModel();
  134. $sum = $model::getDB()
  135. ->whereIn('user_id', $userIds)
  136. ->sum('pv');
  137. return (float)$sum;
  138. } catch (\Exception $e) {
  139. return 0.0;
  140. }
  141. }
  142. /**
  143. * 批量更新
  144. * @param $dataList
  145. * @return array
  146. */
  147. public function saveAll($dataList): array
  148. {
  149. try {
  150. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  151. } catch (\Exception $e) {
  152. return [];
  153. }
  154. }
  155. /**
  156. * 根据用户ID更新记录
  157. * @param int $userId
  158. * @param array $data
  159. * @return int
  160. */
  161. public function updateByUserId(int $userId, array $data): int
  162. {
  163. try {
  164. /** @var SharedProsperityUser $model */
  165. $model = $this->getModel();
  166. return $model::getDB()->where('user_id', $userId)->update($data);
  167. } catch (\Exception $e) {
  168. return 0;
  169. }
  170. }
  171. /**
  172. * 按合伙人等级分组获取用户
  173. * @return array [partner_level => [user_id1, user_id2, ...]]
  174. */
  175. public function getUserGroupByPartnerLevel(): array
  176. {
  177. try {
  178. /** @var SharedProsperityUser $model */
  179. $model = $this->getModel();
  180. $dataRes = $model::getDB()
  181. ->field('partner_level, user_id')
  182. ->select()
  183. ->toArray();
  184. $grouped = [];
  185. foreach ($dataRes as $row) {
  186. $level = $row['partner_level'];
  187. if (!isset($grouped[$level])) {
  188. $grouped[$level] = [];
  189. }
  190. $grouped[$level][] = $row['user_id'];
  191. }
  192. return $grouped;
  193. } catch (\Exception $e) {
  194. return [];
  195. }
  196. }
  197. }