| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace App\Services\Common;
- use App\Models\ActionLogModel;
- use App\Models\JobsModel;
- use App\Services\BaseService;
- /**
- * 招聘信息-服务类
- */
- class JobsService extends BaseService
- {
- public function __construct()
- {
- $this->model = new JobsModel();
- }
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获取状态文本
- * @param int $status
- * @return string
- */
- private function getStatusText($status)
- {
- $statusMap = [
- 1 => '已发布',
- 2 => '待发布',
- 3 => '待审核',
- 4 => '审核失败'
- ];
- return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
- }
- /**
- * 获取数据列表
- * @param array $params 请求参数
- * @param int $pageSize 分页大小
- * @param int $storeId 商家ID,用于数据隔离(0表示超级管理员,>0表示商户用户)
- */
- public function getDataList($params, $pageSize = 15, $storeId = 0)
- {
- $query = $this->model->where('mark', 1);
- // 数据隔离:商户用户只能查看自己的招聘信息
- if ($storeId > 0) {
- $query->where('store_id', $storeId);
- }
- // 状态筛选(使用 status 字段)
- if (isset($params['status']) && $params['status'] > 0) {
- $query->where('status', $params['status']);
- }
- // 分类筛选
- if (isset($params['category_id']) && $params['category_id']) {
- $query->where('category_id', $params['category_id']);
- }
- // 关键词搜索
- if (isset($params['keyword']) && $params['keyword']) {
- $keyword = $params['keyword'];
- $query->where(function ($q) use ($keyword) {
- $q->where('job_name', 'like', '%' . $keyword . '%')
- ->orWhere('job_title', 'like', '%' . $keyword . '%')
- ->orWhere('company', 'like', '%' . $keyword . '%');
- });
- }
- $list = $query->with(['category'])
- ->orderBy('sort', 'desc')
- ->orderBy('id', 'desc')
- ->paginate($pageSize);
- $list = $list ? $list->toArray() : [];
- if ($list && isset($list['data'])) {
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? date('Y-m-d H:i:s', strtotime($item['create_time'])) : '';
- $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s', strtotime($item['update_time'])) : '';
- $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
- $item['category_name'] = $item['category']['name'] ?? '';
- $item['audit_status_text'] = $this->getStatusText($item['status'] ?? 3);
- }
- }
- return [
- 'msg' => '操作成功',
- 'code' => 0,
- 'data' => $list['data'] ?? [],
- 'count' => $list['total'] ?? 0,
- ];
- }
- /**
- * 添加
- * @param int $storeId 商家ID,用于数据隔离(0表示超级管理员,>0表示商户用户)
- */
- public function add($storeId = 0)
- {
- $data = request()->all();
- // 图片处理
- if (isset($data['logo'])) {
- $data['logo'] = get_image_path($data['logo']);
- }
- // 验证职位名称
- if (empty($data['job_name'])) {
- return message('请填写职位名称', false);
- }
- // 验证分类
- if (!isset($data['category_id']) || $data['category_id'] <= 0) {
- return message('请选择职位分类', false);
- }
- // 数据隔离:商户用户添加招聘时自动设置 store_id
- if ($storeId > 0) {
- $data['store_id'] = $storeId;
- } else {
- if (!isset($data['store_id'])) {
- $data['store_id'] = 0;
- }
- }
- // 确保不包含 id 字段(新增不应该有 id)
- unset($data['id']);
- // 设置默认值
- if (!isset($data['status'])) {
- $data['status'] = 3; // 默认待审核
- }
- if (!isset($data['sort'])) {
- $data['sort'] = 0;
- }
- // 确保所有字段都存在
- $fields = ['tags', 'company_desc', 'content', 'recruiter_position', 'remark'];
- foreach ($fields as $field) {
- if (!isset($data[$field])) {
- $data[$field] = '';
- }
- }
- return parent::edit($data);
- }
- /**
- * 添加或编辑
- * @param array $data 数据
- * @param int $storeId 商家ID,用于数据隔离(0表示超级管理员,>0表示商户用户)
- */
- public function edit($data = [], $storeId = 0)
- {
- if (empty($data)) {
- $data = request()->all();
- }
- // 数据隔离:商户用户只能编辑自己的招聘信息
- if ($storeId > 0) {
- // 如果是编辑,检查权限
- if (!empty($data['id'])) {
- $job = $this->model->where('id', $data['id'])->where('mark', 1)->first();
- if (!$job || $job->store_id != $storeId) {
- return ['code' => 1, 'msg' => '无权限操作'];
- }
- }
- // 自动设置 store_id
- $data['store_id'] = $storeId;
- } else {
- // 超级管理员:如果没有指定 store_id,默认为 0(平台)
- if (!isset($data['store_id'])) {
- $data['store_id'] = 0;
- }
- }
- // 确保所有字段都存在,即使为空字符串
- $fields = ['tags', 'company_desc', 'content', 'recruiter_position', 'remark', 'logo'];
- foreach ($fields as $field) {
- if (!isset($data[$field])) {
- $data[$field] = '';
- }
- }
- $data['update_time'] = time();
- if (empty($data['id'])) {
- $data['create_time'] = time();
- }
- ActionLogModel::setTitle("编辑招聘信息");
- ActionLogModel::record();
- return parent::edit($data);
- }
- /**
- * 删除
- * @param int $storeId 商家ID,用于数据隔离(0表示超级管理员,>0表示商户用户)
- */
- public function delete($storeId = 0)
- {
- $id = request()->post('id');
- if (!$id) {
- return ['code' => 1, 'msg' => '参数错误'];
- }
- // 数据隔离:商户用户只能删除自己的招聘信息
- $query = $this->model;
- if ($storeId > 0) {
- $query = $query->where('store_id', $storeId);
- }
- // 支持批量删除
- if (is_array($id)) {
- $result = $query->whereIn('id', $id)->update(['mark' => 0]);
- } else {
- $result = $query->where('id', $id)->update(['mark' => 0]);
- }
- if ($result) {
- ActionLogModel::setTitle("删除招聘信息");
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '删除成功'];
- }
- return ['code' => 1, 'msg' => '删除失败'];
- }
- /**
- * 修改状态(发布/待发布互转)
- */
- public function status()
- {
- $params = request()->all();
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $status = isset($params['status']) ? intval($params['status']) : 0;
- if (!$id) {
- return ['code' => 1, 'msg' => '招聘ID不能为空'];
- }
- if (!$status || ($status != 1 && $status != 2)) {
- return ['code' => 1, 'msg' => '状态参数错误'];
- }
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '招聘信息不存在'];
- }
- // 只有已发布(1)和待发布(2)状态才能互转
- if ($info->status != 1 && $info->status != 2) {
- return ['code' => 1, 'msg' => '只有已发布和待发布状态才能切换'];
- }
- $this->model->where('id', $id)->update([
- 'status' => $status,
- 'update_time' => time()
- ]);
- ActionLogModel::setTitle("修改招聘状态");
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '状态修改成功'];
- }
- /**
- * 审核
- */
- public function confirm()
- {
- $params = request()->all();
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $status = isset($params['audit_status']) ? intval($params['audit_status']) : 0;
- $remark = isset($params['remark']) ? trim($params['remark']) : '';
- if (!$id) {
- return ['code' => 1, 'msg' => '招聘ID不能为空'];
- }
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '招聘信息不存在'];
- }
- // 审核通过
- if ($status == 1) {
- $updateData = [
- 'status' => 1, // 1-已发布
- 'remark' => $remark ?: '审核通过',
- 'update_time' => time()
- ];
- $this->model->where('id', $id)->update($updateData);
- ActionLogModel::setTitle("审核通过招聘信息");
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '审核通过成功'];
- }
- // 审核驳回
- elseif ($status == 4) {
- if (empty($remark)) {
- return ['code' => 1, 'msg' => '驳回理由不能为空'];
- }
- $this->model->where('id', $id)->update([
- 'status' => 4, // 4-审核失败
- 'remark' => $remark,
- 'update_time' => time()
- ]);
- ActionLogModel::setTitle("驳回招聘信息");
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '审核驳回成功'];
- } else {
- return ['code' => 1, 'msg' => '审核状态参数错误'];
- }
- }
- /**
- * 获取详情
- * @param int $id 招聘信息ID
- * @param int $storeId 商家ID,用于数据隔离(0表示超级管理员,>0表示商户用户)
- */
- public function getInfo($id, $storeId = 0)
- {
- $query = $this->model->where('id', $id)->where('mark', 1);
- // 数据隔离:商户用户只能查看自己的招聘信息
- if ($storeId > 0) {
- $query->where('store_id', $storeId);
- }
- $info = $query->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '数据不存在'];
- }
- $info = $info->toArray();
- $info['logo'] = $info['logo'] ? get_image_url($info['logo']) : '';
- return ['code' => 0, 'data' => $info];
- }
- }
|