InstitutionService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. $type = isset($params['type']) ? $params['type'] : 0;
  86. if ($status > 0) {
  87. $where['status'] = $status;
  88. }
  89. if ($linkType > 0) {
  90. $where['link_type'] = $linkType;
  91. }
  92. if ($type > 0) {
  93. $where['type'] = $type==3? 3 : 1;
  94. }
  95. return $this->model->where($where)
  96. ->where(function ($query) use ($params) {
  97. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  98. if ($keyword) {
  99. $query->where(function ($q) use ($keyword) {
  100. $q->where('name', 'like', "%{$keyword}%")
  101. ->orWhere('code', 'like', "%{$keyword}%");
  102. });
  103. }
  104. // 处理日期区间
  105. $date = isset($params['date']) ? $params['date'] : [];
  106. $start = isset($date[0]) ? $date[0] : '';
  107. $end = isset($date[1]) ? $date[1] : '';
  108. if ($start) {
  109. $query->where('create_time', '>=', strtotime($start));
  110. }
  111. if ($end) {
  112. $query->where('create_time', '<=', strtotime($end));
  113. }
  114. });
  115. }
  116. /**
  117. * 添加或编辑院校
  118. * @return array
  119. */
  120. public function edit()
  121. {
  122. $data = request()->all();
  123. // 验证必填字段
  124. if (empty($data['name'])) {
  125. return message('院校名称不能为空', false);
  126. }
  127. if (empty($data['code'])) {
  128. return message('院校代码不能为空', false);
  129. }
  130. $data['link_url'] = isset($data['link_url'])? $data['link_url'] : '';
  131. $data['mp_appid'] = isset($data['mp_appid'])? $data['mp_appid'] : '';
  132. // 检查院校代码是否重复
  133. // $existId = $this->checkExists('code', $data['code'], 'id', 0);
  134. // if ($existId && $existId != ($data['id'] ?? 0)) {
  135. // return message('院校代码已存在', false);
  136. // }
  137. // 处理封面图片路径(前端传递的是完整 URL,需要转换为相对路径)
  138. if (!empty($data['thumb'])) {
  139. $data['thumb'] = get_image_path($data['thumb']);
  140. }
  141. // 直接传递给 parent::edit,确保包含 id 字段用于判断新增/编辑
  142. return parent::edit($data);
  143. }
  144. /**
  145. * 获取记录详情
  146. * @return array
  147. */
  148. public function info()
  149. {
  150. // 记录ID
  151. $id = request()->input("id", 0);
  152. $info = [];
  153. if ($id) {
  154. // 查询记录
  155. $record = $this->model->where('id', $id)->where('mark', 1)->first();
  156. if ($record) {
  157. $info = $record->toArray();
  158. // 处理封面图片URL(确保返回完整URL)
  159. if (isset($info['thumb']) && $info['thumb']) {
  160. $info['thumb'] = get_image_url($info['thumb']);
  161. }
  162. }
  163. }
  164. return message(MESSAGE_OK, true, $info);
  165. }
  166. /**
  167. * 删除院校
  168. * @return array
  169. */
  170. public function delete()
  171. {
  172. // 设置日志标题
  173. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除院校信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  174. ActionLogModel::record();
  175. // 删除七天之前标记软删除的数据
  176. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  177. return parent::delete();
  178. }
  179. /**
  180. * 设置状态
  181. * @return array
  182. */
  183. public function status()
  184. {
  185. return parent::status();
  186. }
  187. /**
  188. * 院校选项
  189. * @return array
  190. */
  191. public function options()
  192. {
  193. // 获取参数
  194. $param = request()->all();
  195. // 关键词
  196. $keyword = getter($param, "keyword");
  197. $datas = $this->model
  198. ->where(function ($query) {
  199. $query->where(['status' => 1, 'mark' => 1]);
  200. })
  201. ->where(function ($query) use ($keyword) {
  202. if ($keyword) {
  203. $query->where('name', 'like', "%{$keyword}%");
  204. }
  205. })
  206. ->select(['id', 'name', 'code'])
  207. ->orderBy('sort', 'desc')
  208. ->orderBy('id', 'desc')
  209. ->get();
  210. return $datas ? $datas->toArray() : [];
  211. }
  212. /**
  213. * 设置排序
  214. * @return array
  215. */
  216. public function sort()
  217. {
  218. $data = request()->all();
  219. if (!$data['id']) {
  220. return message('记录ID不能为空', false);
  221. }
  222. if (!isset($data['sort'])) {
  223. return message('排序值不能为空', false);
  224. }
  225. $error = '';
  226. $item = [
  227. 'id' => $data['id'],
  228. 'sort' => $data['sort']
  229. ];
  230. $rowId = $this->model->edit($item, $error);
  231. if (!$rowId) {
  232. return message($error, false);
  233. }
  234. return message();
  235. }
  236. }