SupervisorsService.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\MemberModel;
  14. use App\Models\SupervisorsModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. /**
  18. * 导师管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class SupervisorsService extends BaseService
  24. {
  25. public static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new SupervisorsModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 获取列表
  47. */
  48. public function getList()
  49. {
  50. $params = request()->all();
  51. $pageSize = $params['limit'] ?? 20;
  52. $where = ['supervisors.mark' => 1];
  53. $query = $this->model->with(['member'])
  54. ->from('supervisors')
  55. ->where($where)
  56. ->where(function ($query) use ($params) {
  57. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  58. if ($keyword) {
  59. $query->where('supervisors.name', 'like', "%{$keyword}%")->orWhere('supervisors.mobile', 'like', "%{$keyword}%");
  60. }
  61. })
  62. ->where(function ($query) use ($params) {
  63. $status = isset($params['status']) ? $params['status'] : 0;
  64. if ($status > 0) {
  65. $query->where('supervisors.status',$status);
  66. }
  67. // 认证
  68. $isAuth = isset($params['is_auth']) ? intval($params['is_auth']) : 0;
  69. if ($isAuth) {
  70. $query->where('supervisors.is_auth', $isAuth);
  71. }
  72. // 推荐
  73. $isRecommend = isset($params['is_recommend']) ? intval($params['is_recommend']) : 0;
  74. if ($isRecommend) {
  75. $query->where('supervisors.is_recommend', $isRecommend);
  76. }
  77. // 类型
  78. $type = isset($params['type']) ? intval($params['type']) : 0;
  79. if ($type) {
  80. $query->where('supervisors.type', $type);
  81. }
  82. })
  83. ->select(['supervisors.*'])
  84. ->withCount(['consults'])
  85. ->orderBy('supervisors.sort', 'desc')
  86. ->orderBy('supervisors.id', 'desc');
  87. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999);
  88. $list = $list ? $list->toArray() : [];
  89. if ($list) {
  90. foreach ($list['data'] as &$item) {
  91. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  92. $item['create_time_text'] = datetime($item['create_time'], 'Y-m-d H:i');
  93. $item['consults_count'] = $item['consults_count']?$item['consults_count']:0;
  94. $item['intro'] = $item['intro']? get_format_content($item['intro']):'';
  95. }
  96. }
  97. return [
  98. 'code' => 0,
  99. 'msg' => '获取成功',
  100. 'data' => isset($list['data'])?$list['data']:[],
  101. 'count' => isset($list['total'])?$list['total']:0
  102. ];
  103. }
  104. /**
  105. * 搜索用户(用于下拉选择)
  106. * @param string $keyword 搜索关键词
  107. * @param int $accountType 账户类型
  108. * @param int $limit 返回数量
  109. * @return array
  110. */
  111. public function searchUsers($keyword = '',$limit = 20)
  112. {
  113. try {
  114. $query = MemberModel::where('mark', 1);
  115. // 关键词搜索:昵称、手机号、ID
  116. if (!empty($keyword)) {
  117. $query->where(function ($q) use ($keyword) {
  118. $q->where('id', $keyword)
  119. ->orWhere('name', 'like', "%{$keyword}%")
  120. ->orWhere('mobile', 'like', "%{$keyword}%");
  121. });
  122. }
  123. $list = $query->select(['id', 'name', 'mobile', 'type'])
  124. ->limit($limit)
  125. ->get()
  126. ->toArray();
  127. // 转换为数组(如果是对象)
  128. $list = json_decode(json_encode($list), true);
  129. return [
  130. 'code' => 0,
  131. 'msg' => '获取成功',
  132. 'data' => $list
  133. ];
  134. } catch (\Exception $e) {
  135. return [
  136. 'code' => 1,
  137. 'msg' => '搜索失败:' . $e->getMessage(),
  138. 'data' => []
  139. ];
  140. }
  141. }
  142. /**
  143. * 用户选项
  144. * @return array
  145. */
  146. public function options()
  147. {
  148. // 获取参数
  149. $param = request()->all();
  150. // 用户ID
  151. $keyword = getter($param, "keyword");
  152. $id = getter($param, "id");
  153. $datas = $this->model->where(['status' => 1, 'mark' => 1])
  154. ->where(function ($query) use ($id) {
  155. if ($id) {
  156. $query->whereNotIn('id', [$id]);
  157. }
  158. })
  159. ->where(function ($query) use ($keyword) {
  160. if ($keyword) {
  161. $query->where('name', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
  162. }
  163. })
  164. ->select(['id', 'name', 'mobile', 'type', 'status'])
  165. ->get();
  166. return $datas ? $datas->toArray() : [];
  167. }
  168. /**
  169. * 编辑
  170. * @return array
  171. * @since 2020/11/11
  172. * @author laravel开发员
  173. */
  174. public function edit()
  175. {
  176. // 请求参数
  177. $data = request()->all();
  178. $id = isset($data['id']) ? $data['id'] : 0;
  179. // 头像处理
  180. if (isset($data['avatar']) && $data['avatar']) {
  181. $data['avatar'] = get_image_path(trim($data['avatar']));
  182. }
  183. // 手机号唯一性验证
  184. $name = isset($data['name']) ? trim($data['name']) : '';
  185. if ($name) {
  186. $checkId = $this->model->where(['name' => $name, 'mark' => 1])->value('id');
  187. if ($checkId && ($id != $checkId)) {
  188. return message('导师已存在', false);
  189. }
  190. }
  191. $data['user_id'] = isset($data['user_id']) ? intval($data['user_id']) : 0;
  192. if ($data['user_id']) {
  193. $checkId = $this->model->where(['user_id' => $data['user_id'], 'mark' => 1])->value('id');
  194. if ($checkId && ($id != $checkId)) {
  195. return message('该会员账号已绑定其他导师', false);
  196. }
  197. }
  198. // 处理数值类型字段
  199. if (isset($data['status'])) {
  200. $data['status'] = (int)$data['status'];
  201. }
  202. // 设置日志
  203. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '编辑导师信息', 'content' => json_encode($data, 256), 'module' => 'admin']);
  204. ActionLogModel::record();
  205. return parent::edit($data);
  206. }
  207. /**
  208. * 设置状态
  209. */
  210. public function status()
  211. {
  212. $params = request()->all();
  213. $id = $params['id'] ?? 0;
  214. $status = $params['status'] ?? 1;
  215. $member = $this->model->where('id', $id)
  216. ->where('mark', 1)
  217. ->first();
  218. if (!$member) {
  219. return ['code' => 1, 'msg' => '记录不存在'];
  220. }
  221. $member->status = $status;
  222. $member->update_time = time();
  223. $member->save();
  224. return ['code' => 0, 'msg' => '设置成功'];
  225. }
  226. /**
  227. * 设置推荐
  228. */
  229. public function recommend()
  230. {
  231. $params = request()->all();
  232. $id = $params['id'] ?? 0;
  233. $status = $params['is_recommend'] ?? 1;
  234. $member = $this->model->where('id', $id)
  235. ->where('mark', 1)
  236. ->first();
  237. if (!$member) {
  238. return ['code' => 1, 'msg' => '记录不存在'];
  239. }
  240. $member->is_recommend = $status;
  241. $member->update_time = time();
  242. $member->save();
  243. return ['code' => 0, 'msg' => '设置成功'];
  244. }
  245. /**
  246. * 设置认证
  247. */
  248. public function auth()
  249. {
  250. $params = request()->all();
  251. $id = $params['id'] ?? 0;
  252. $status = $params['is_auth'] ?? 1;
  253. $member = $this->model->where('id', $id)
  254. ->where('mark', 1)
  255. ->first();
  256. if (!$member) {
  257. return ['code' => 1, 'msg' => '记录不存在'];
  258. }
  259. $member->is_auth = $status;
  260. $member->update_time = time();
  261. $member->save();
  262. return ['code' => 0, 'msg' => '设置成功'];
  263. }
  264. /**
  265. * 删除
  266. * @return array
  267. */
  268. public function delete()
  269. {
  270. $id = request()->input('id');
  271. // 删除数据
  272. $this->model->where('mark', 1)->where('update_time','<=', 600)->delete();
  273. if (is_array($id)) {
  274. // 批量删除
  275. $count = $this->model->whereIn('id', $id)
  276. ->where('mark', 1)
  277. ->update(['mark' => 0, 'update_time' => time()]);
  278. return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
  279. } else {
  280. // 单个删除
  281. $info = $this->model->where('id', $id)
  282. ->where('mark', 1)
  283. ->first();
  284. if (!$info) {
  285. return ['code' => 1, 'msg' => '记录不存在'];
  286. }
  287. $info->mark = 0;
  288. $info->update_time = time();
  289. $info->save();
  290. return ['code' => 0, 'msg' => '删除成功'];
  291. }
  292. }
  293. }