InstitutionService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\InstitutionModel;
  4. use App\Models\ActionLogModel;
  5. use App\Services\BaseService;
  6. /**
  7. * InstitutionService 服务类
  8. * @author laravel开发员
  9. * @since 2024/01/08
  10. * Class InstitutionService
  11. * @package App\Services\Common
  12. */
  13. class InstitutionService extends BaseService
  14. {
  15. public static $instance = null;
  16. /**
  17. * 构造函数
  18. * @since 2024/01/08
  19. */
  20. public function __construct()
  21. {
  22. $this->model = new InstitutionModel();
  23. }
  24. /**
  25. * 静态入口
  26. * @return static|null
  27. */
  28. public static function make()
  29. {
  30. if (!self::$instance) {
  31. self::$instance = (new static());
  32. }
  33. return self::$instance;
  34. }
  35. /**
  36. * 获取院校列表
  37. * @param $params
  38. * @param int $pageSize
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 15)
  42. {
  43. $query = $this->getQuery($params);
  44. $model = clone $query;
  45. $counts = [
  46. 'count' => $model->count('id'),
  47. ];
  48. $list = $query->select(['*'])
  49. ->orderBy('sort', 'desc')
  50. ->orderBy('id', 'desc')
  51. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  52. $list = $list ? $list->toArray() : [];
  53. if ($list) {
  54. foreach ($list['data'] as &$item) {
  55. // 格式化时间
  56. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  57. $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  58. // 处理封面图片URL
  59. if (isset($item['thumb']) && $item['thumb']) {
  60. $item['thumb'] = get_image_url($item['thumb']);
  61. }
  62. // 格式化状态
  63. $item['status_text'] = $item['status'] == 1 ? '正常' : '不显示';
  64. // 格式化链接类型
  65. $item['link_type_text'] = $item['link_type'] == 1 ? 'Web链接' : '小程序链接';
  66. }
  67. }
  68. return [
  69. 'pageSize' => $pageSize,
  70. 'total' => isset($list['total']) ? $list['total'] : 0,
  71. 'counts' => $counts,
  72. 'list' => isset($list['data']) ? $list['data'] : []
  73. ];
  74. }
  75. /**
  76. * 查询院校
  77. * @param $params
  78. * @return \Illuminate\Database\Eloquent\Builder
  79. */
  80. public function getQuery($params)
  81. {
  82. $where = ['mark' => 1];
  83. $status = isset($params['status']) ? $params['status'] : 0;
  84. $linkType = isset($params['link_type']) ? $params['link_type'] : 0;
  85. if ($status > 0) {
  86. $where['status'] = $status;
  87. }
  88. if ($linkType > 0) {
  89. $where['link_type'] = $linkType;
  90. }
  91. return $this->model->where($where)
  92. ->where(function ($query) use ($params) {
  93. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  94. if ($keyword) {
  95. $query->where(function ($q) use ($keyword) {
  96. $q->where('name', 'like', "%{$keyword}%")
  97. ->orWhere('code', 'like', "%{$keyword}%");
  98. });
  99. }
  100. // 处理日期区间
  101. $date = isset($params['date']) ? $params['date'] : [];
  102. $start = isset($date[0]) ? $date[0] : '';
  103. $end = isset($date[1]) ? $date[1] : '';
  104. if ($start) {
  105. $query->where('create_time', '>=', strtotime($start));
  106. }
  107. if ($end) {
  108. $query->where('create_time', '<=', strtotime($end));
  109. }
  110. });
  111. }
  112. /**
  113. * 添加或编辑院校
  114. * @return array
  115. */
  116. public function edit()
  117. {
  118. $data = request()->all();
  119. // 验证必填字段
  120. if (empty($data['name'])) {
  121. return message('院校名称不能为空', false);
  122. }
  123. if (empty($data['code'])) {
  124. return message('院校代码不能为空', false);
  125. }
  126. // 检查院校代码是否重复
  127. $existId = $this->checkExists('code', $data['code'], 'id', 0);
  128. if ($existId && $existId != ($data['id'] ?? 0)) {
  129. return message('院校代码已存在', false);
  130. }
  131. // 处理封面图片路径(前端传递的是完整 URL,需要转换为相对路径)
  132. if (!empty($data['thumb'])) {
  133. $data['thumb'] = get_image_path($data['thumb']);
  134. }
  135. // 直接传递给 parent::edit,确保包含 id 字段用于判断新增/编辑
  136. return parent::edit($data);
  137. }
  138. /**
  139. * 获取记录详情
  140. * @return array
  141. */
  142. public function info()
  143. {
  144. // 记录ID
  145. $id = request()->input("id", 0);
  146. $info = [];
  147. if ($id) {
  148. // 查询记录
  149. $record = $this->model->where('id', $id)->where('mark', 1)->first();
  150. if ($record) {
  151. $info = $record->toArray();
  152. // 处理封面图片URL(确保返回完整URL)
  153. if (isset($info['thumb']) && $info['thumb']) {
  154. $info['thumb'] = get_image_url($info['thumb']);
  155. }
  156. }
  157. }
  158. return message(MESSAGE_OK, true, $info);
  159. }
  160. /**
  161. * 删除院校
  162. * @return array
  163. */
  164. public function delete()
  165. {
  166. // 设置日志标题
  167. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除院校信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  168. ActionLogModel::record();
  169. // 删除七天之前标记软删除的数据
  170. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  171. return parent::delete();
  172. }
  173. /**
  174. * 设置状态
  175. * @return array
  176. */
  177. public function status()
  178. {
  179. return parent::status();
  180. }
  181. /**
  182. * 院校选项
  183. * @return array
  184. */
  185. public function options()
  186. {
  187. // 获取参数
  188. $param = request()->all();
  189. // 关键词
  190. $keyword = getter($param, "keyword");
  191. $datas = $this->model
  192. ->where(function ($query) {
  193. $query->where(['status' => 1, 'mark' => 1]);
  194. })
  195. ->where(function ($query) use ($keyword) {
  196. if ($keyword) {
  197. $query->where('name', 'like', "%{$keyword}%");
  198. }
  199. })
  200. ->select(['id', 'name', 'code'])
  201. ->orderBy('sort', 'desc')
  202. ->orderBy('id', 'desc')
  203. ->get();
  204. return $datas ? $datas->toArray() : [];
  205. }
  206. /**
  207. * 设置排序
  208. * @return array
  209. */
  210. public function sort()
  211. {
  212. $data = request()->all();
  213. if (!$data['id']) {
  214. return message('记录ID不能为空', false);
  215. }
  216. if (!isset($data['sort'])) {
  217. return message('排序值不能为空', false);
  218. }
  219. $error = '';
  220. $item = [
  221. 'id' => $data['id'],
  222. 'sort' => $data['sort']
  223. ];
  224. $rowId = $this->model->edit($item, $error);
  225. if (!$rowId) {
  226. return message($error, false);
  227. }
  228. return message();
  229. }
  230. }