| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace app\admin\logic;
- use app\admin\model\dao\MoneyLog;
- use app\admin\model\dao\ScoreLog;
- use app\admin\model\dao\ShopOrder;
- use app\admin\model\dao\User;
- use think\facade\Cache;
- use think\facade\Db;
- class UserLogic
- {
- public function getList($page, $limit, $where, $sort, $userMap)
- {
- $userDao = new User();
- $count = $userDao->getCount($where, $userMap);
- $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap);
- foreach ($list as &$item) {
- //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计
- // 计算团队成员(包括1、2级和自身)ID
- $teamIds = $this->getTeamMoneyAndScore($item['id']);
- // 积分账户合计
- $item['score_total'] = $teamIds['score'];
- // 余额账户合计
- $item['money_total'] = $teamIds['money'];
- }
- $data = [
- 'code' => 0,
- 'msg' => Db::getLastSql(),
- 'count' => $count,
- 'data' => $list,
- ];
- return json($data);
- }
- /**
- * 获取团队
- * @param $uid
- */
- private function getTeamMoneyAndScore($uid)
- {
- $cacheKey = 'userLogicTeamIds_' . $uid;
- if (!Cache::has($cacheKey)) {
- // 自身
- $user = Db::table(User::$table)
- ->where('id', $uid)
- ->field(['id', 'score', 'money'])
- ->find();
- $totalScore = $user['score'];
- $totalMoney = $user['money'];
- // 一级子用户
- Db::table(User::$table)
- ->where('pid', $uid)
- ->field(['id', 'score', 'money'])
- ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
- foreach ($users as $user) {
- $uids[] = $user['id'];
- $totalScore += $user['score'];
- $totalMoney += $user['money'];
- }
- // 二级子用户
- Db::table(User::$table)
- ->whereIn('pid', $uids)
- ->field(['id', 'score', 'money'])
- ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) {
- foreach ($users as $user) {
- $totalScore += $user['score'];
- $totalMoney += $user['money'];
- }
- });
- }, ['score', 'money']);
- Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60);
- }
- return Cache::get($cacheKey);
- }
- /**
- * 修改用户手机号码
- * @param mixed $uid
- * @param mixed $phone
- */
- public function modifyPhone($uid, $phone)
- {
- // 判断手机号码是否使用过
- $user = User::getUserById($uid);
- if ($user['mobile'] == $phone) {
- return "请输入新的手机号码";
- }
- $user = User::getUserByMobile($phone);
- if ($user) {
- return "该手机号码已注册";
- }
- // 修改用户信息
- try {
- $result = User::ModifyMobile($uid, $phone);
- if (!$result) {
- return "更新手机号码失败,请稍后重试";
- }
- } catch (\Exception $e) {
- return '失败' . $e->getMessage();
- }
- return true;
- }
- /**
- * 修改用户所属上级
- * @param mixed $uid
- * @param mixed $pid
- */
- public function modifypid($uid, $pid)
- {
- // 查询pid是否存在用户path中
- $user = User::getUserById($uid);
- if ($uid == $pid) {
- return "不能修改自己为上级";
- }
- if ($user['pid'] == $pid) {
- return "该用户已所属待变更的上级,不能修改";
- }
- // 当前用户关系层级链路
- $newUserPath = "";
- if ($pid == 0) {
- $newPathPrefix = $user['id'];
- } else {
- $pUser = User::getUserById($pid);
- if (empty($pUser)) {
- return "待变更的上级用户不存在";
- }
- $userPathArr = explode(',', $user['path']);
- if (in_array($pid, $userPathArr)) {
- $pidIndex = array_search($pid, $userPathArr);
- $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1);
- } else {
- $pidIndex = array_search($user['pid'], $userPathArr);
- $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex);
- $userPathArrPrefix[] = $pid;
- }
- $newUserPath = implode(',', $userPathArrPrefix);
- // 子级用户Path待替换关系层级前部分链表
- $userPathArrPrefix[] = $user['id'];
- $newPathPrefix = implode(',', $userPathArrPrefix);
- }
- // 子级用户Path被替换关系层级前部分链表
- $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid;
- Db::startTrans();
- try {
- // 子级path变更
- $result = Db::table(User::$table)
- ->where([
- ['path', 'like', $oldPathPrefix . '%'],
- ['id', '<>', $uid]
- ])
- ->field(['id', 'path'])
- ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) {
- foreach ($users as $user) {
- $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']);
- User::modifyUserPath($user['id'], $newPath);
- }
- });
- if (!$result) {
- Db::rollback();
- return "修改用户所属上级失败,请确认用户的层级关系";
- }
- // 更新用户Pid,Path
- User::modifyUserPidAndPath($uid, $pid, $newUserPath);
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollback();
- return "失败:" . $exception->getMessage();
- }
- return true;
- }
- /**
- * 修改余额
- * @param mixed $post
- */
- public function ModifyMoney($post)
- {
- $user = User::getUserById($post['uid']);
- if (empty($user)) {
- return "用户不存在";
- }
- Db::startTrans();
- try {
- if ($post['state'] == 1) {
- $afterMoney = $user['money'] + $post['money'];
- } else {
- $afterMoney = $user['money'] - $post['money'];
- if ($afterMoney < 0) {
- return "账号余额不足";
- }
- }
- $afterMoney = round($afterMoney, 2);
- $moneyLog = [
- 'uid' => $post['uid'],
- 'type' => $post['type'] ?? 0,
- 'money' => $post['money'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'state' => $post['state'] ?? '0',
- 'from_id' => $post['from_id'] ?? '0',
- 'before_money' => $user['money'],
- 'after_money' => $afterMoney,
- 'uid2' => $post['uid2'] ?? 0,
- 'free_type' => $post['free_type'] ?? '',
- 'remark' => $post['remark'],
- ];
- // 子级path变更
- MoneyLog::AddMoneyLog($moneyLog);
- User::UpdateUserMoney($user['id'], $afterMoney);
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollback();
- return "失败:" . $exception->getMessage();
- }
- return true;
- }
- /**
- * 修改积分
- * @param mixed $post
- */
- public function ModifyScore($post)
- {
- $user = User::getUserById($post['uid']);
- if (empty($user)) {
- return "用户不存在";
- }
- Db::startTrans();
- try {
- if ($post['state'] == 1) {
- $afterScore = $user['score'] + $post['score'];
- } else {
- $afterScore = $user['score'] - $post['score'];
- if ($afterScore < 0) {
- return "账号积分不足";
- }
- }
- $moneyLog = [
- 'uid' => $post['uid'],
- 'type' => $post['type'] ?? 0,
- 'score' => $post['score'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'scene' => $post['scene'] ?? 0,
- 'from_id' => $post['from_id'] ?? '0',
- 'state' => $post['state'] ?? '0',
- 'before_score' => $user['score'],
- 'after_score' => $afterScore,
- 'uid2' => $post['uid2'] ?? 0,
- ];
- // 子级path变更
- ScoreLog::AddScoreLog($moneyLog);
- User::UpdateUserScore($user['id'], $afterScore);
- Db::commit();
- } catch (\Exception $exception) {
- Db::rollback();
- return "失败:" . $exception->getMessage();
- }
- return true;
- }
- /**
- * 删除用户
- * @param $id
- */
- public function delUser($id)
- {
- $user = User::getUserById($id);
- if (empty($user)) {
- return "用户不存在";
- }
- if (User::updateState($id, 3) !== 1) {
- return false;
- }
- return true;
- }
- }
|