JobsCategoryService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\ActionLogModel;
  4. use App\Models\JobsCategoryModel;
  5. use App\Services\BaseService;
  6. /**
  7. * 招聘分类-服务类
  8. */
  9. class JobsCategoryService extends BaseService
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new JobsCategoryModel();
  14. }
  15. public static function make()
  16. {
  17. if (!self::$instance) {
  18. self::$instance = (new static());
  19. }
  20. return self::$instance;
  21. }
  22. /**
  23. * 获取数据列表
  24. */
  25. public function getDataList($params, $pageSize = 15)
  26. {
  27. $query = $this->model->where('mark', 1);
  28. // 状态筛选
  29. if (isset($params['status']) && $params['status'] > 0) {
  30. $query->where('status', $params['status']);
  31. }
  32. // 关键词搜索
  33. if (isset($params['keyword']) && $params['keyword']) {
  34. $query->where('name', 'like', '%' . $params['keyword'] . '%');
  35. }
  36. // 上级分类筛选
  37. if (isset($params['pid'])) {
  38. $query->where('pid', $params['pid']);
  39. }
  40. $list = $query->orderBy('sort', 'desc')
  41. ->orderBy('id', 'desc')
  42. ->paginate($pageSize);
  43. $list = $list ? $list->toArray() : [];
  44. if ($list && isset($list['data'])) {
  45. foreach ($list['data'] as &$item) {
  46. $item['create_time'] = $item['create_time'] ? date('Y-m-d H:i:s', strtotime($item['create_time'])) : '';
  47. $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s', strtotime($item['update_time'])) : '';
  48. }
  49. }
  50. return [
  51. 'msg' => '操作成功',
  52. 'code' => 0,
  53. 'data' => $list['data'] ?? [],
  54. 'count' => $list['total'] ?? 0,
  55. ];
  56. }
  57. /**
  58. * 添加或编辑
  59. */
  60. public function edit($data = [])
  61. {
  62. if (empty($data)) {
  63. $data = request()->all();
  64. }
  65. $data['update_time'] = time();
  66. if (empty($data['id'])) {
  67. $data['create_time'] = time();
  68. }
  69. ActionLogModel::setTitle("编辑招聘分类");
  70. ActionLogModel::record();
  71. return parent::edit($data);
  72. }
  73. /**
  74. * 删除
  75. */
  76. public function delete()
  77. {
  78. $id = request()->post('id');
  79. if (!$id) {
  80. return ['code' => 1, 'msg' => '参数错误'];
  81. }
  82. // 支持批量删除
  83. if (is_array($id)) {
  84. $result = $this->model->whereIn('id', $id)->update(['mark' => 0]);
  85. } else {
  86. $result = $this->model->where('id', $id)->update(['mark' => 0]);
  87. }
  88. if ($result) {
  89. ActionLogModel::setTitle("删除招聘分类");
  90. ActionLogModel::record();
  91. return ['code' => 0, 'msg' => '删除成功'];
  92. }
  93. return ['code' => 1, 'msg' => '删除失败'];
  94. }
  95. /**
  96. * 获取分类选项
  97. */
  98. public function options()
  99. {
  100. $list = $this->model->where('mark', 1)
  101. ->where('status', 1)
  102. ->orderBy('sort', 'desc')
  103. ->orderBy('id', 'asc')
  104. ->select(['id', 'name', 'pid'])
  105. ->get()
  106. ->toArray();
  107. return $list;
  108. }
  109. }