// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\JobsCategoryModel; use App\Models\JobsModel; use App\Services\BaseService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 招聘管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class JobsService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new JobsModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $categoryId = isset($params['category_id']) ? $params['category_id'] : 0; if ($status > 0) { $where['a.status'] = $status; } if ($categoryId > 0) { $where['a.category_id'] = $categoryId; } $list = $this->model->with(['store','category'])->from('jobs as a') ->leftJoin('jobs_categorys as b', 'b.id', '=', 'a.category_id') ->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('a.job_name', 'like', "%{$keyword}%") ->orWhere('a.job_title','like',"%{$keyword}%") ->orWhere('a.tags','like',"%{$keyword}%") ->orWhere('a.company','like',"%{$keyword}%"); } }) ->select(['a.*']) ->orderBy('a.create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 分类 * @return array|mixed */ public function getCategoryList() { $cacheKey = "caches:jobs:categoryList"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = JobsCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1]) ->select(['id','name','pid','remark','sort']) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 详情 * @param $id * @param $userId * @return array|mixed */ public function getInfo($id,$userId=0) { $cacheKey = "caches:jobs:info_{$id}_{$userId}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['id'=> $id,'mark'=>1]; $info = $this->model->with(['store','category'])->where($where)->first(); $info = $info? $info->toArray() : []; if($info){ $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : ''; RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } }