SharedProsperityUserDao.php 6.9 KB

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