UserLogic.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\model\dao\User;
  4. use app\common\model\User as UserModel;
  5. use think\facade\Cache;
  6. use think\facade\Db;
  7. class UserLogic
  8. {
  9. public function getList($page, $limit, $where, $sort, $userMap)
  10. {
  11. $userDao = new User();
  12. $count = $userDao->getCount($where, $userMap);
  13. $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap);
  14. foreach ($list as &$item) {
  15. //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计
  16. // 计算团队成员(包括1、2级和自身)ID
  17. $teamIds = $this->getTeamMoneyAndScore($item['id']);
  18. // 积分账户合计
  19. $item['score_total'] = $teamIds['score'];
  20. // 余额账户合计
  21. $item['money_total'] = $teamIds['money'];
  22. }
  23. $data = [
  24. 'code' => 0,
  25. 'msg' => Db::getLastSql(),
  26. 'count' => $count,
  27. 'data' => $list,
  28. ];
  29. return json($data);
  30. }
  31. /**
  32. * 获取团队
  33. * @param $uid
  34. */
  35. private function getTeamMoneyAndScore($uid)
  36. {
  37. $cacheKey = 'userLogicTeamIds_' . $uid;
  38. if (!Cache::has($cacheKey)) {
  39. // 自身
  40. $user = Db::table(User::$table)
  41. ->where('id', $uid)
  42. ->field(['id', 'score', 'money'])
  43. ->find();
  44. $totalScore = $user['score'];
  45. $totalMoney = $user['money'];
  46. // 一级子用户
  47. Db::table(User::$table)
  48. ->where('pid', $uid)
  49. ->field(['id', 'score', 'money'])
  50. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  51. foreach ($users as $user) {
  52. $uids[] = $user['id'];
  53. $totalScore += $user['score'];
  54. $totalMoney += $user['money'];
  55. }
  56. // 二级子用户
  57. Db::table(User::$table)
  58. ->whereIn('pid', $uids)
  59. ->field(['id', 'score', 'money'])
  60. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  61. foreach ($users as $user) {
  62. $totalScore += $user['score'];
  63. $totalMoney += $user['money'];
  64. }
  65. });
  66. }, ['score', 'money']);
  67. Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60);
  68. }
  69. return Cache::get($cacheKey);
  70. }
  71. }