| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\ActionLogModel;
- use App\Models\MemberModel;
- use App\Models\SupervisorsModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 导师管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class SupervisorsService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new SupervisorsModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取列表
- */
- public function getList()
- {
- $params = request()->all();
- $pageSize = $params['limit'] ?? 20;
- $where = ['supervisors.mark' => 1];
- $query = $this->model->with(['member'])
- ->from('supervisors')
- ->where($where)
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- if ($keyword) {
- $query->where('supervisors.name', 'like', "%{$keyword}%")->orWhere('supervisors.mobile', 'like', "%{$keyword}%");
- }
- })
- ->where(function ($query) use ($params) {
- $status = isset($params['status']) ? $params['status'] : 0;
- if ($status > 0) {
- $query->where('supervisors.status',$status);
- }
- // 认证
- $isAuth = isset($params['is_auth']) ? intval($params['is_auth']) : 0;
- if ($isAuth) {
- $query->where('supervisors.is_auth', $isAuth);
- }
- // 推荐
- $isRecommend = isset($params['is_recommend']) ? intval($params['is_recommend']) : 0;
- if ($isRecommend) {
- $query->where('supervisors.is_recommend', $isRecommend);
- }
- // 类型
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type) {
- $query->where('supervisors.type', $type);
- }
- })
- ->select(['supervisors.*'])
- ->withCount(['consults'])
- ->orderBy('supervisors.sort', 'desc')
- ->orderBy('supervisors.id', 'desc');
- $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
- $item['create_time_text'] = datetime($item['create_time'], 'Y-m-d H:i');
- $item['consults_count'] = $item['consults_count']?$item['consults_count']:0;
- $item['intro'] = $item['intro']? get_format_content($item['intro']):'';
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => isset($list['data'])?$list['data']:[],
- 'count' => isset($list['total'])?$list['total']:0
- ];
- }
- /**
- * 搜索用户(用于下拉选择)
- * @param string $keyword 搜索关键词
- * @param int $accountType 账户类型
- * @param int $limit 返回数量
- * @return array
- */
- public function searchUsers($keyword = '',$limit = 20)
- {
- try {
- $query = MemberModel::where('mark', 1);
- // 关键词搜索:昵称、手机号、ID
- if (!empty($keyword)) {
- $query->where(function ($q) use ($keyword) {
- $q->where('id', $keyword)
- ->orWhere('name', 'like', "%{$keyword}%")
- ->orWhere('mobile', 'like', "%{$keyword}%");
- });
- }
- $list = $query->select(['id', 'name', 'mobile', 'type'])
- ->limit($limit)
- ->get()
- ->toArray();
- // 转换为数组(如果是对象)
- $list = json_decode(json_encode($list), true);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '搜索失败:' . $e->getMessage(),
- 'data' => []
- ];
- }
- }
- /**
- * 用户选项
- * @return array
- */
- public function options()
- {
- // 获取参数
- $param = request()->all();
- // 用户ID
- $keyword = getter($param, "keyword");
- $id = getter($param, "id");
- $datas = $this->model->where(['status' => 1, 'mark' => 1])
- ->where(function ($query) use ($id) {
- if ($id) {
- $query->whereNotIn('id', [$id]);
- }
- })
- ->where(function ($query) use ($keyword) {
- if ($keyword) {
- $query->where('name', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
- }
- })
- ->select(['id', 'name', 'mobile', 'type', 'status'])
- ->get();
- return $datas ? $datas->toArray() : [];
- }
- /**
- * 编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 请求参数
- $data = request()->all();
- $id = isset($data['id']) ? $data['id'] : 0;
- // 头像处理
- if (isset($data['avatar']) && $data['avatar']) {
- $data['avatar'] = get_image_path(trim($data['avatar']));
- }
- // 手机号唯一性验证
- $name = isset($data['name']) ? trim($data['name']) : '';
- if ($name) {
- $checkId = $this->model->where(['name' => $name, 'mark' => 1])->value('id');
- if ($checkId && ($id != $checkId)) {
- return message('导师已存在', false);
- }
- }
- $data['user_id'] = isset($data['user_id']) ? intval($data['user_id']) : 0;
- if ($data['user_id']) {
- $checkId = $this->model->where(['user_id' => $data['user_id'], 'mark' => 1])->value('id');
- if ($checkId && ($id != $checkId)) {
- return message('该会员账号已绑定其他导师', false);
- }
- }
- // 处理数值类型字段
- if (isset($data['status'])) {
- $data['status'] = (int)$data['status'];
- }
- // 设置日志
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '编辑导师信息', 'content' => json_encode($data, 256), 'module' => 'admin']);
- ActionLogModel::record();
- return parent::edit($data);
- }
- /**
- * 设置状态
- */
- public function status()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['status'] ?? 1;
- $member = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$member) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $member->status = $status;
- $member->update_time = time();
- $member->save();
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 设置推荐
- */
- public function recommend()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['is_recommend'] ?? 1;
- $member = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$member) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $member->is_recommend = $status;
- $member->update_time = time();
- $member->save();
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 设置认证
- */
- public function auth()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['is_auth'] ?? 1;
- $member = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$member) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $member->is_auth = $status;
- $member->update_time = time();
- $member->save();
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- $id = request()->input('id');
- // 删除数据
- $this->model->where('mark', 1)->where('update_time','<=', 600)->delete();
- if (is_array($id)) {
- // 批量删除
- $count = $this->model->whereIn('id', $id)
- ->where('mark', 1)
- ->update(['mark' => 0, 'update_time' => time()]);
- return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
- } else {
- // 单个删除
- $info = $this->model->where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $info->mark = 0;
- $info->update_time = time();
- $info->save();
- return ['code' => 0, 'msg' => '删除成功'];
- }
- }
- }
|