|
|
@@ -0,0 +1,227 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Common;
|
|
|
+
|
|
|
+use App\Models\InstitutionModel;
|
|
|
+use App\Models\ActionLogModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * InstitutionService 服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2024/01/08
|
|
|
+ * Class InstitutionService
|
|
|
+ * @package App\Services\Common
|
|
|
+ */
|
|
|
+class InstitutionService extends BaseService
|
|
|
+{
|
|
|
+ public static $instance = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @since 2024/01/08
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new InstitutionModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ * @return static|null
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = (new static());
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取院校列表
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 15)
|
|
|
+ {
|
|
|
+ $query = $this->getQuery($params);
|
|
|
+
|
|
|
+ $model = clone $query;
|
|
|
+ $counts = [
|
|
|
+ 'count' => $model->count('id'),
|
|
|
+ ];
|
|
|
+
|
|
|
+ $list = $query->select(['*'])
|
|
|
+ ->orderBy('sort', 'desc')
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ $list = $list ? $list->toArray() : [];
|
|
|
+
|
|
|
+ if ($list) {
|
|
|
+ foreach ($list['data'] as &$item) {
|
|
|
+ // 格式化时间
|
|
|
+ $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
|
|
|
+ // 格式化状态
|
|
|
+ $item['status_text'] = $item['status'] == 1 ? '正常' : '不显示';
|
|
|
+ // 格式化链接类型
|
|
|
+ $item['link_type_text'] = $item['link_type'] == 1 ? 'Web链接' : '小程序链接';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
+ 'counts' => $counts,
|
|
|
+ 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询院校
|
|
|
+ * @param $params
|
|
|
+ * @return \Illuminate\Database\Eloquent\Builder
|
|
|
+ */
|
|
|
+ public function getQuery($params)
|
|
|
+ {
|
|
|
+ $where = ['mark' => 1];
|
|
|
+ $status = isset($params['status']) ? $params['status'] : 0;
|
|
|
+ $linkType = isset($params['link_type']) ? $params['link_type'] : 0;
|
|
|
+
|
|
|
+ if ($status > 0) {
|
|
|
+ $where['status'] = $status;
|
|
|
+ }
|
|
|
+ if ($linkType > 0) {
|
|
|
+ $where['link_type'] = $linkType;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->model->where($where)
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $keyword = isset($params['keyword']) ? $params['keyword'] : '';
|
|
|
+ if ($keyword) {
|
|
|
+ $query->where(function ($q) use ($keyword) {
|
|
|
+ $q->where('name', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('code', 'like', "%{$keyword}%");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理日期区间
|
|
|
+ $date = isset($params['date']) ? $params['date'] : [];
|
|
|
+ $start = isset($date[0]) ? $date[0] : '';
|
|
|
+ $end = isset($date[1]) ? $date[1] : '';
|
|
|
+ if ($start) {
|
|
|
+ $query->where('create_time', '>=', strtotime($start));
|
|
|
+ }
|
|
|
+ if ($end) {
|
|
|
+ $query->where('create_time', '<=', strtotime($end));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加或编辑院校
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function edit()
|
|
|
+ {
|
|
|
+ $data = request()->all();
|
|
|
+
|
|
|
+ // 验证必填字段
|
|
|
+ if (empty($data['name'])) {
|
|
|
+ return message('院校名称不能为空', false);
|
|
|
+ }
|
|
|
+ if (empty($data['code'])) {
|
|
|
+ return message('院校代码不能为空', false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查院校代码是否重复
|
|
|
+ $existId = $this->checkExists('code', $data['code'], 'id', 0);
|
|
|
+ if ($existId && $existId != ($data['id'] ?? 0)) {
|
|
|
+ return message('院校代码已存在', false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保所有必填字段都存在
|
|
|
+ $data = [
|
|
|
+ 'name' => $data['name'] ?? '',
|
|
|
+ 'code' => $data['code'] ?? '',
|
|
|
+ 'thumb' => $data['thumb'] ?? '',
|
|
|
+ 'link_url' => $data['link_url'] ?? '',
|
|
|
+ 'link_type' => $data['link_type'] ?? 1,
|
|
|
+ 'description' => $data['description'] ?? '',
|
|
|
+ 'sort' => $data['sort'] ?? 0,
|
|
|
+ 'status' => $data['status'] ?? 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ return parent::edit($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除院校
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function delete()
|
|
|
+ {
|
|
|
+ // 设置日志标题
|
|
|
+ ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除院校信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
|
|
|
+ ActionLogModel::record();
|
|
|
+
|
|
|
+ // 删除七天之前标记软删除的数据
|
|
|
+ $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
|
|
|
+ return parent::delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 院校选项
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function options()
|
|
|
+ {
|
|
|
+ // 获取参数
|
|
|
+ $param = request()->all();
|
|
|
+ // 关键词
|
|
|
+ $keyword = getter($param, "keyword");
|
|
|
+ $datas = $this->model
|
|
|
+ ->where(function ($query) {
|
|
|
+ $query->where(['status' => 1, 'mark' => 1]);
|
|
|
+ })
|
|
|
+ ->where(function ($query) use ($keyword) {
|
|
|
+ if ($keyword) {
|
|
|
+ $query->where('name', 'like', "%{$keyword}%");
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['id', 'name', 'code'])
|
|
|
+ ->orderBy('sort', 'desc')
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ return $datas ? $datas->toArray() : [];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置排序
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function sort()
|
|
|
+ {
|
|
|
+ $data = request()->all();
|
|
|
+ if (!$data['id']) {
|
|
|
+ return message('记录ID不能为空', false);
|
|
|
+ }
|
|
|
+ if (!isset($data['sort'])) {
|
|
|
+ return message('排序值不能为空', false);
|
|
|
+ }
|
|
|
+
|
|
|
+ $error = '';
|
|
|
+ $item = [
|
|
|
+ 'id' => $data['id'],
|
|
|
+ 'sort' => $data['sort']
|
|
|
+ ];
|
|
|
+ $rowId = $this->model->edit($item, $error);
|
|
|
+ if (!$rowId) {
|
|
|
+ return message($error, false);
|
|
|
+ }
|
|
|
+ return message();
|
|
|
+ }
|
|
|
+}
|