SharedProsperityUserDao.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. use think\db\exception\DbException;
  9. class SharedProsperityUserDao extends BaseDao
  10. {
  11. /**
  12. * @return BaseModel
  13. */
  14. protected function getModel(): string
  15. {
  16. return SharedProsperityUser::class;
  17. }
  18. /**
  19. * @param array $idList
  20. * @return SharedProsperityUserEntity[]
  21. */
  22. public function getSharedProsperityUserEntityListByIdList(array $idList): array
  23. {
  24. $entityList = [];
  25. try {
  26. /** @var SharedProsperityUser $model */
  27. $model = $this->getModel();
  28. $dataRes = $model::getDB()
  29. ->whereIn('id', $idList)
  30. ->select()
  31. ->toArray();
  32. if (!empty($dataRes)) {
  33. $entityList = array_map(function ($data) {
  34. return SharedProsperityUserEntity::newInstance($data);
  35. }, $dataRes);
  36. }
  37. } catch (\Exception $e) {
  38. }
  39. return $entityList;
  40. }
  41. /**
  42. * @param array $userIdList
  43. * @return SharedProsperityUserEntity[]
  44. */
  45. public function getSharedProsperityUserEntityListByUserIdList(array $userIdList): array
  46. {
  47. $entityList = [];
  48. try {
  49. /** @var SharedProsperityUser $model */
  50. $model = $this->getModel();
  51. $dataRes = $model::getDB()
  52. ->whereIn('user_id', $userIdList)
  53. ->select()
  54. ->toArray();
  55. if (!empty($dataRes)) {
  56. $entityList = array_map(function ($data) {
  57. return SharedProsperityUserEntity::newInstance($data);
  58. }, $dataRes);
  59. }
  60. } catch (\Exception $e) {
  61. }
  62. return $entityList;
  63. }
  64. /**
  65. * @param int $parentId
  66. * @return SharedProsperityUserEntity[]
  67. */
  68. public function getSharedProsperityUserEntityListByParentId(int $parentId): array
  69. {
  70. $entityList = [];
  71. try {
  72. /** @var SharedProsperityUser $model */
  73. $model = $this->getModel();
  74. $dataRes = $model::getDB()
  75. ->where('parent_id', '=', $parentId)
  76. ->select()
  77. ->toArray();
  78. if (!empty($dataRes)) {
  79. $entityList = array_map(function ($data) {
  80. return SharedProsperityUserEntity::newInstance($data);
  81. }, $dataRes);
  82. }
  83. } catch (\Exception $e) {
  84. }
  85. return $entityList;
  86. }
  87. /**
  88. * 获取所有直推用户(不分页)
  89. * @param int $userId
  90. * @return array
  91. * @author 史晨
  92. * @date 2026/2/9 09:35
  93. */
  94. public function getDirectByUserId(int $userId): array
  95. {
  96. $list = [];
  97. try {
  98. /** @var SharedProsperityUser $model */
  99. $model = $this->getModel();
  100. $list = $model::getDB()
  101. ->where('parent_id', $userId)
  102. ->select()
  103. ->toArray();
  104. } catch (\Exception $e) {
  105. }
  106. return $list;
  107. }
  108. /**
  109. * 递归获取团队所有下级用户ID(可选择是否包含自己)
  110. * @param int $userId
  111. * @param bool $includeSelf 是否包含自己
  112. * @param int $hierarchy 层级限制,0表示不限制,>0表示限制层级
  113. * @return int[]
  114. */
  115. public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false, int $hierarchy = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Hierarchy']['code']): array
  116. {
  117. $allIds = [];
  118. try {
  119. /** @var SharedProsperityUser $model */
  120. $model = $this->getModel();
  121. $getRecursiveChildren = function ($parentId, $currentLevel = 1) use (&$getRecursiveChildren, $model, $hierarchy) {
  122. // 如果设置了层级限制且当前层级超过限制,则停止递归
  123. if ($hierarchy > 0 && $currentLevel > $hierarchy) {
  124. return [];
  125. }
  126. $children = $model::getDB()
  127. ->where('parent_id', $parentId)
  128. ->column('user_id');
  129. $allChildren = [];
  130. foreach ($children as $childId) {
  131. $allChildren[] = $childId;
  132. $allChildren = array_merge($allChildren, $getRecursiveChildren($childId, $currentLevel + 1));
  133. }
  134. return $allChildren;
  135. };
  136. $allIds = $getRecursiveChildren($userId);
  137. if ($includeSelf) {
  138. array_unshift($allIds, $userId);
  139. }
  140. } catch (\Exception $e) {
  141. // 记录日志或忽略
  142. }
  143. return $allIds;
  144. }
  145. /**
  146. * 获取团队所有下级用户PV总和(不包括自己)
  147. * @param int $userId
  148. * @return float
  149. */
  150. public function getTeamPvSum(int $userId): float
  151. {
  152. $userIds = $this->getAllSubordinateUserIds($userId, false);
  153. if (empty($userIds)) {
  154. return 0.0;
  155. }
  156. try {
  157. /** @var SharedProsperityUser $model */
  158. $model = $this->getModel();
  159. $sum = $model::getDB()
  160. ->whereIn('user_id', $userIds)
  161. ->sum('pv');
  162. return (float)$sum;
  163. } catch (\Exception $e) {
  164. return 0.0;
  165. }
  166. }
  167. /**
  168. * 获取团队所有下级用户PV总和(包括自己)
  169. * @param int $userId
  170. * @return float
  171. */
  172. public function getTeamPvSumWithSelf(int $userId): float
  173. {
  174. $userIds = $this->getAllSubordinateUserIds($userId, true);
  175. if (empty($userIds)) {
  176. return 0.0;
  177. }
  178. try {
  179. /** @var SharedProsperityUser $model */
  180. $model = $this->getModel();
  181. $sum = $model::getDB()
  182. ->whereIn('user_id', $userIds)
  183. ->sum('pv');
  184. return (float)$sum;
  185. } catch (\Exception $e) {
  186. return 0.0;
  187. }
  188. }
  189. /**
  190. * 批量更新
  191. * @param $dataList
  192. * @return array
  193. */
  194. public function saveAll($dataList): array
  195. {
  196. try {
  197. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  198. } catch (\Exception $e) {
  199. return [];
  200. }
  201. }
  202. /**
  203. * 根据用户ID更新记录
  204. * @param int $userId
  205. * @param array $data
  206. * @return int
  207. */
  208. public function updateByUserId(int $userId, array $data): int
  209. {
  210. try {
  211. /** @var SharedProsperityUser $model */
  212. $model = $this->getModel();
  213. return $model::getDB()->where('user_id', $userId)->update($data);
  214. } catch (\Exception $e) {
  215. return 0;
  216. }
  217. }
  218. public function updateRewardByUserId($userId, $field, $value)
  219. {
  220. try {
  221. /** @var SharedProsperityUser $model */
  222. $model = $this->getModel();
  223. return $model::getDB()->where('user_id', $userId)->inc($field,$value)->update();
  224. } catch (\Exception $e) {
  225. return 0;
  226. }
  227. }
  228. /**
  229. * 按合伙人等级分组获取用户
  230. * @return array [partner_level => [user_id1, user_id2, ...]]
  231. */
  232. public function getUserGroupByPartnerLevel(): array
  233. {
  234. try {
  235. /** @var SharedProsperityUser $model */
  236. $model = $this->getModel();
  237. $dataRes = $model::getDB()
  238. ->field('partner_level, user_id')
  239. ->select()
  240. ->toArray();
  241. $grouped = [];
  242. foreach ($dataRes as $row) {
  243. $level = $row['partner_level'];
  244. if (!isset($grouped[$level])) {
  245. $grouped[$level] = [];
  246. }
  247. $grouped[$level][] = $row['user_id'];
  248. }
  249. return $grouped;
  250. } catch (\Exception $e) {
  251. return [];
  252. }
  253. }
  254. /**
  255. * @return array|void
  256. * @author 史晨
  257. * @date 2026/2/27 09:32
  258. * 后台用户列表
  259. */
  260. public function adminList($where,$page)
  261. {
  262. /** @var SharedProsperityUser $model */
  263. $model = $this->getModel();
  264. try {
  265. $list = $model::getDB()
  266. ->where($where)
  267. ->with([
  268. 'user' => function ($query) {
  269. $query->field('uid,avatar,nickname,account');
  270. },
  271. 'parent' => function ($query) {
  272. $query->field('uid,avatar,nickname,account');
  273. },
  274. 'group' => function ($query) {
  275. $query->field('group_id,user_id');
  276. }
  277. ])
  278. ->paginate(['page' => $page, 'list_rows' => 20])
  279. ->toArray();
  280. return $list;
  281. } catch (DbException $e) {
  282. return [];
  283. }
  284. }
  285. }