SharedProsperityUserDao.php 7.6 KB

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