InstitutionService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // 格式化状态
  59. $item['status_text'] = $item['status'] == 1 ? '正常' : '不显示';
  60. // 格式化链接类型
  61. $item['link_type_text'] = $item['link_type'] == 1 ? 'Web链接' : '小程序链接';
  62. }
  63. }
  64. return [
  65. 'pageSize' => $pageSize,
  66. 'total' => isset($list['total']) ? $list['total'] : 0,
  67. 'counts' => $counts,
  68. 'list' => isset($list['data']) ? $list['data'] : []
  69. ];
  70. }
  71. /**
  72. * 查询院校
  73. * @param $params
  74. * @return \Illuminate\Database\Eloquent\Builder
  75. */
  76. public function getQuery($params)
  77. {
  78. $where = ['mark' => 1];
  79. $status = isset($params['status']) ? $params['status'] : 0;
  80. $linkType = isset($params['link_type']) ? $params['link_type'] : 0;
  81. if ($status > 0) {
  82. $where['status'] = $status;
  83. }
  84. if ($linkType > 0) {
  85. $where['link_type'] = $linkType;
  86. }
  87. return $this->model->where($where)
  88. ->where(function ($query) use ($params) {
  89. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  90. if ($keyword) {
  91. $query->where(function ($q) use ($keyword) {
  92. $q->where('name', 'like', "%{$keyword}%")
  93. ->orWhere('code', 'like', "%{$keyword}%");
  94. });
  95. }
  96. // 处理日期区间
  97. $date = isset($params['date']) ? $params['date'] : [];
  98. $start = isset($date[0]) ? $date[0] : '';
  99. $end = isset($date[1]) ? $date[1] : '';
  100. if ($start) {
  101. $query->where('create_time', '>=', strtotime($start));
  102. }
  103. if ($end) {
  104. $query->where('create_time', '<=', strtotime($end));
  105. }
  106. });
  107. }
  108. /**
  109. * 添加或编辑院校
  110. * @return array
  111. */
  112. public function edit()
  113. {
  114. $data = request()->all();
  115. // 验证必填字段
  116. if (empty($data['name'])) {
  117. return message('院校名称不能为空', false);
  118. }
  119. if (empty($data['code'])) {
  120. return message('院校代码不能为空', false);
  121. }
  122. // 检查院校代码是否重复
  123. $existId = $this->checkExists('code', $data['code'], 'id', 0);
  124. if ($existId && $existId != ($data['id'] ?? 0)) {
  125. return message('院校代码已存在', false);
  126. }
  127. // 确保所有必填字段都存在
  128. $data = [
  129. 'name' => $data['name'] ?? '',
  130. 'code' => $data['code'] ?? '',
  131. 'thumb' => $data['thumb'] ?? '',
  132. 'link_url' => $data['link_url'] ?? '',
  133. 'link_type' => $data['link_type'] ?? 1,
  134. 'description' => $data['description'] ?? '',
  135. 'sort' => $data['sort'] ?? 0,
  136. 'status' => $data['status'] ?? 1,
  137. ];
  138. return parent::edit($data);
  139. }
  140. /**
  141. * 删除院校
  142. * @return array
  143. */
  144. public function delete()
  145. {
  146. // 设置日志标题
  147. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除院校信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  148. ActionLogModel::record();
  149. // 删除七天之前标记软删除的数据
  150. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  151. return parent::delete();
  152. }
  153. /**
  154. * 院校选项
  155. * @return array
  156. */
  157. public function options()
  158. {
  159. // 获取参数
  160. $param = request()->all();
  161. // 关键词
  162. $keyword = getter($param, "keyword");
  163. $datas = $this->model
  164. ->where(function ($query) {
  165. $query->where(['status' => 1, 'mark' => 1]);
  166. })
  167. ->where(function ($query) use ($keyword) {
  168. if ($keyword) {
  169. $query->where('name', 'like', "%{$keyword}%");
  170. }
  171. })
  172. ->select(['id', 'name', 'code'])
  173. ->orderBy('sort', 'desc')
  174. ->orderBy('id', 'desc')
  175. ->get();
  176. return $datas ? $datas->toArray() : [];
  177. }
  178. /**
  179. * 设置排序
  180. * @return array
  181. */
  182. public function sort()
  183. {
  184. $data = request()->all();
  185. if (!$data['id']) {
  186. return message('记录ID不能为空', false);
  187. }
  188. if (!isset($data['sort'])) {
  189. return message('排序值不能为空', false);
  190. }
  191. $error = '';
  192. $item = [
  193. 'id' => $data['id'],
  194. 'sort' => $data['sort']
  195. ];
  196. $rowId = $this->model->edit($item, $error);
  197. if (!$rowId) {
  198. return message($error, false);
  199. }
  200. return message();
  201. }
  202. }