UserLogic.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\model\dao\ShopOrder;
  4. use app\admin\model\dao\User;
  5. use app\common\model\User as UserModel;
  6. use think\facade\Cache;
  7. use think\facade\Db;
  8. class UserLogic
  9. {
  10. public function getList($page, $limit, $where, $sort, $userMap)
  11. {
  12. $userDao = new User();
  13. $count = $userDao->getCount($where, $userMap);
  14. $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap);
  15. foreach ($list as &$item) {
  16. //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计
  17. // 计算团队成员(包括1、2级和自身)ID
  18. $teamIds = $this->getTeamMoneyAndScore($item['id']);
  19. // 积分账户合计
  20. $item['score_total'] = $teamIds['score'];
  21. // 余额账户合计
  22. $item['money_total'] = $teamIds['money'];
  23. }
  24. $data = [
  25. 'code' => 0,
  26. 'msg' => Db::getLastSql(),
  27. 'count' => $count,
  28. 'data' => $list,
  29. ];
  30. return json($data);
  31. }
  32. /**
  33. * 获取团队
  34. * @param $uid
  35. */
  36. private function getTeamMoneyAndScore($uid)
  37. {
  38. $cacheKey = 'userLogicTeamIds_' . $uid;
  39. if (!Cache::has($cacheKey)) {
  40. // 自身
  41. $user = Db::table(User::$table)
  42. ->where('id', $uid)
  43. ->field(['id', 'score', 'money'])
  44. ->find();
  45. $totalScore = $user['score'];
  46. $totalMoney = $user['money'];
  47. // 一级子用户
  48. Db::table(User::$table)
  49. ->where('pid', $uid)
  50. ->field(['id', 'score', 'money'])
  51. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  52. foreach ($users as $user) {
  53. $uids[] = $user['id'];
  54. $totalScore += $user['score'];
  55. $totalMoney += $user['money'];
  56. }
  57. // 二级子用户
  58. Db::table(User::$table)
  59. ->whereIn('pid', $uids)
  60. ->field(['id', 'score', 'money'])
  61. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  62. foreach ($users as $user) {
  63. $totalScore += $user['score'];
  64. $totalMoney += $user['money'];
  65. }
  66. });
  67. }, ['score', 'money']);
  68. Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60);
  69. }
  70. return Cache::get($cacheKey);
  71. }
  72. /**
  73. * 修改用户手机号码
  74. * @param mixed $uid
  75. * @param mixed $phone
  76. */
  77. public function modifyPhone($uid, $phone)
  78. {
  79. // 判断手机号码是否使用过
  80. $user = User::getUserById($uid);
  81. if ($user['mobile'] == $phone) {
  82. return "请输入新的手机号码";
  83. }
  84. $user = User::getUserByMobile($phone);
  85. if ($user) {
  86. return "该手机号码已注册";
  87. }
  88. // 修改用户信息
  89. try {
  90. $result = User::ModifyMobile($uid, $phone);
  91. if (!$result) {
  92. return "更新手机号码失败,请稍后重试";
  93. }
  94. } catch (\Exception $e) {
  95. return '失败' . $e->getMessage();
  96. }
  97. return true;
  98. }
  99. /**
  100. * 修改用户所属上级
  101. * @param mixed $uid
  102. * @param mixed $pid
  103. */
  104. public function modifypid($uid, $pid)
  105. {
  106. // 查询pid是否存在用户path中
  107. $user = User::getUserById($uid);
  108. if ($uid == $pid) {
  109. return "不能修改自己为上级";
  110. }
  111. if ($user['pid'] == $pid) {
  112. return "该用户已所属待变更的上级,不能修改";
  113. }
  114. // 当前用户关系层级链路
  115. $newUserPath = "";
  116. if ($pid == 0) {
  117. $newPathPrefix = $user['id'];
  118. } else {
  119. $pUser = User::getUserById($pid);
  120. if (empty($pUser)) {
  121. return "待变更的上级用户不存在";
  122. }
  123. $userPathArr = explode(',', $user['path']);
  124. if (in_array($pid, $userPathArr)) {
  125. $pidIndex = array_search($pid, $userPathArr);
  126. $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1);
  127. } else {
  128. $pidIndex = array_search($user['pid'], $userPathArr);
  129. $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex);
  130. $userPathArrPrefix[] = $pid;
  131. }
  132. $newUserPath = implode(',', $userPathArrPrefix);
  133. // 子级用户Path待替换关系层级前部分链表
  134. $userPathArrPrefix[] = $user['id'];
  135. $newPathPrefix = implode(',', $userPathArrPrefix);
  136. }
  137. // 子级用户Path被替换关系层级前部分链表
  138. $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid;
  139. Db::startTrans();
  140. try {
  141. // 子级path变更
  142. $result = Db::table(User::$table)
  143. ->where([
  144. ['path', 'like', $oldPathPrefix . '%'],
  145. ['id', '<>', $uid]
  146. ])
  147. ->field(['id', 'path'])
  148. ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) {
  149. foreach ($users as $user) {
  150. $userPathArr = explode(',', $user['path']);
  151. $uidIndex = array_search($uid, $userPathArr);
  152. $userPathArrSub = array_slice($userPathArr, $uidIndex);
  153. if (in_array($uid, $userPathArrSub)) {
  154. return false;
  155. }
  156. $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']);
  157. User::modifyUserPath($user['id'], $newPath);
  158. }
  159. });
  160. if (!$result) {
  161. Db::rollback();
  162. return "修改用户所属上级失败,请确认用户的层级关系";
  163. }
  164. // 更新用户Pid,Path
  165. User::modifyUserPidAndPath($uid, $pid, $newUserPath);
  166. Db::commit();
  167. } catch (\Exception $exception) {
  168. Db::rollback();
  169. return "失败:" . $exception->getMessage();
  170. }
  171. return true;
  172. }
  173. }