UserLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\model\dao\MoneyLog;
  4. use app\admin\model\dao\ScoreLog;
  5. use app\admin\model\dao\User;
  6. use app\common\model\UserModel;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. class UserLogic
  10. {
  11. public function getList($page, $limit, $where, $sort, $userMap)
  12. {
  13. $userDao = new User();
  14. $count = $userDao->getCount($where, $userMap);
  15. $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap);
  16. foreach ($list as &$item) {
  17. //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计
  18. // 计算团队成员(包括1、2级和自身)ID
  19. $teamIds = $this->getTeamMoneyAndScore($item['id']);
  20. // 积分账户合计
  21. $item['score_total'] = $teamIds['score'];
  22. // 余额账户合计
  23. $item['money_total'] = $teamIds['money'];
  24. }
  25. $data = [
  26. 'code' => 0,
  27. 'msg' => Db::getLastSql(),
  28. 'count' => $count,
  29. 'data' => $list,
  30. ];
  31. return json($data);
  32. }
  33. /**
  34. * 获取团队
  35. * @param $uid
  36. */
  37. private function getTeamMoneyAndScore($uid)
  38. {
  39. $cacheKey = 'userLogicTeamIds_' . $uid;
  40. if (!Cache::has($cacheKey)) {
  41. // 自身
  42. $user = Db::table(User::$table)
  43. ->where('id', $uid)
  44. ->field(['id', 'score', 'money'])
  45. ->find();
  46. $totalScore = $user['score'];
  47. $totalMoney = $user['money'];
  48. // 一级子用户
  49. Db::table(User::$table)
  50. ->where('pid', $uid)
  51. ->field(['id', 'score', 'money'])
  52. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  53. foreach ($users as $user) {
  54. $uids[] = $user['id'];
  55. $totalScore += $user['score'];
  56. $totalMoney += $user['money'];
  57. }
  58. // 二级子用户
  59. Db::table(User::$table)
  60. ->whereIn('pid', $uids)
  61. ->field(['id', 'score', 'money'])
  62. ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
  63. foreach ($users as $user) {
  64. $totalScore += $user['score'];
  65. $totalMoney += $user['money'];
  66. }
  67. });
  68. }, ['score', 'money']);
  69. Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60);
  70. }
  71. return Cache::get($cacheKey);
  72. }
  73. /**
  74. * 修改用户手机号码
  75. * @param mixed $uid
  76. * @param mixed $phone
  77. */
  78. public function modifyPhone($uid, $phone)
  79. {
  80. // 判断手机号码是否使用过
  81. $user = User::getUserById($uid);
  82. if ($user['mobile'] == $phone) {
  83. return "请输入新的手机号码";
  84. }
  85. $user = User::getUserByMobile($phone);
  86. if ($user) {
  87. return "该手机号码已注册";
  88. }
  89. // 修改用户信息
  90. try {
  91. $result = User::ModifyMobile($uid, $phone);
  92. if (!$result) {
  93. return "更新手机号码失败,请稍后重试";
  94. }
  95. } catch (\Exception $e) {
  96. return '失败' . $e->getMessage();
  97. }
  98. return true;
  99. }
  100. /**
  101. * 修改用户所属上级
  102. * @param mixed $uid
  103. * @param mixed $pid
  104. */
  105. public function modifypid($uid, $pid)
  106. {
  107. // 查询pid是否存在用户path中
  108. $user = User::getUserById($uid);
  109. if ($uid == $pid) {
  110. return "不能修改自己为上级";
  111. }
  112. if ($user['pid'] == $pid) {
  113. return "该用户已所属待变更的上级,不能修改";
  114. }
  115. // 当前用户关系层级链路
  116. $newUserPath = "";
  117. if ($pid == 0) {
  118. $newPathPrefix = $user['id'];
  119. } else {
  120. $pUser = User::getUserById($pid);
  121. if (empty($pUser)) {
  122. return "待变更的上级用户不存在";
  123. }
  124. $userPathArr = explode(',', $user['path']);
  125. if (in_array($pid, $userPathArr)) {
  126. $pidIndex = array_search($pid, $userPathArr);
  127. $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1);
  128. } else {
  129. $pidIndex = array_search($user['pid'], $userPathArr);
  130. $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex);
  131. $userPathArrPrefix[] = $pid;
  132. }
  133. $newUserPath = implode(',', $userPathArrPrefix);
  134. // 子级用户Path待替换关系层级前部分链表
  135. $userPathArrPrefix[] = $user['id'];
  136. $newPathPrefix = implode(',', $userPathArrPrefix);
  137. }
  138. // 子级用户Path被替换关系层级前部分链表
  139. $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid;
  140. Db::startTrans();
  141. try {
  142. // 子级path变更
  143. $result = Db::table(User::$table)
  144. ->where([
  145. ['path', 'like', $oldPathPrefix . '%'],
  146. ['id', '<>', $uid]
  147. ])
  148. ->field(['id', 'path'])
  149. ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) {
  150. foreach ($users as $user) {
  151. $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']);
  152. User::modifyUserPath($user['id'], $newPath);
  153. }
  154. });
  155. if (!$result) {
  156. Db::rollback();
  157. return "修改用户所属上级失败,请确认用户的层级关系";
  158. }
  159. // 更新用户Pid,Path
  160. User::modifyUserPidAndPath($uid, $pid, $newUserPath);
  161. Db::commit();
  162. } catch (\Exception $exception) {
  163. Db::rollback();
  164. return "失败:" . $exception->getMessage();
  165. }
  166. return true;
  167. }
  168. /**
  169. * 修改余额
  170. * @param mixed $post
  171. */
  172. public function ModifyMoney($post)
  173. {
  174. $user = User::getUserById($post['uid']);
  175. if (empty($user)) {
  176. return "用户不存在";
  177. }
  178. Db::startTrans();
  179. try {
  180. if ($post['state'] == 1) {
  181. $afterMoney = $user['money'] + $post['money'];
  182. } else {
  183. $afterMoney = $user['money'] - $post['money'];
  184. if ($afterMoney < 0) {
  185. return "账号余额不足";
  186. }
  187. }
  188. $afterMoney = round($afterMoney, 2);
  189. $moneyLog = [
  190. 'uid' => $post['uid'],
  191. 'type' => $post['type'] ?? 0,
  192. 'money' => $post['money'],
  193. 'create_at' => date('Y-m-d H:i:s'),
  194. 'state' => $post['state'] ?? '0',
  195. 'from_id' => $post['from_id'] ?? '0',
  196. 'before_money' => $user['money'],
  197. 'after_money' => $afterMoney,
  198. 'uid2' => $post['uid2'] ?? 0,
  199. 'free_type' => $post['free_type'] ?? '',
  200. 'remark' => $post['remark'],
  201. ];
  202. // 子级path变更
  203. MoneyLog::AddMoneyLog($moneyLog);
  204. User::UpdateUserMoney($user['id'], $afterMoney);
  205. Db::commit();
  206. } catch (\Exception $exception) {
  207. Db::rollback();
  208. return "失败:" . $exception->getMessage();
  209. }
  210. return true;
  211. }
  212. /**
  213. * 修改积分
  214. * @param mixed $post
  215. */
  216. public function ModifyScore($post)
  217. {
  218. $user = User::getUserById($post['uid']);
  219. if (empty($user)) {
  220. return "用户不存在";
  221. }
  222. Db::startTrans();
  223. try {
  224. if ($post['state'] == 1) {
  225. $afterScore = $user['score'] + $post['score'];
  226. } else {
  227. $afterScore = $user['score'] - $post['score'];
  228. if ($afterScore < 0) {
  229. return "账号积分不足";
  230. }
  231. }
  232. $moneyLog = [
  233. 'uid' => $post['uid'],
  234. 'type' => $post['type'] ?? 0,
  235. 'score' => $post['score'],
  236. 'create_at' => date('Y-m-d H:i:s'),
  237. 'scene' => $post['scene'] ?? 0,
  238. 'from_id' => $post['from_id'] ?? '0',
  239. 'state' => $post['state'] ?? '0',
  240. 'before_score' => $user['score'],
  241. 'after_score' => $afterScore,
  242. 'uid2' => $post['uid2'] ?? 0,
  243. ];
  244. // 子级path变更
  245. ScoreLog::AddScoreLog($moneyLog);
  246. User::UpdateUserScore($user['id'], $afterScore);
  247. Db::commit();
  248. } catch (\Exception $exception) {
  249. Db::rollback();
  250. return "失败:" . $exception->getMessage();
  251. }
  252. return true;
  253. }
  254. /**
  255. * 删除用户
  256. * @param $id
  257. */
  258. public function delUser($id)
  259. {
  260. $user = User::getUserById($id);
  261. if (empty($user)) {
  262. return "用户不存在";
  263. }
  264. if (User::updateState($id, 3) !== 1) {
  265. return false;
  266. }
  267. return true;
  268. }
  269. /* 注册用户数量统计
  270. * @param string $time 时间节点如:2023-03-01
  271. * @return mixed
  272. * @throws \think\db\exception\DbException
  273. */
  274. public static function getCountByTime($time='0')
  275. {
  276. $cacheKey = "caches:user:counts_{$time}";
  277. if(!Cache::has($cacheKey)){
  278. $data = UserModel::where(['status'=>1])
  279. ->where(function($query) use($time){
  280. if($time){
  281. $query->where('reg_time','>=', $time);
  282. }
  283. })->count('id');
  284. if($data){
  285. Cache::set($cacheKey, $data, rand(3,5));
  286. }
  287. return $data;
  288. }
  289. return Cache::get($cacheKey);
  290. }
  291. }