// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\SupervisorsConsultRecordsModel; use App\Models\SupervisorsModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; /** * 导师管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class SupervisorsService extends BaseService { protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new SupervisorsModel(); } /** * 静态入口 * @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 = ['supervisors.mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $type = isset($params['type']) ? $params['type'] : 0; $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0; if ($status > 0) { $where['supervisors.status'] = $status; } if ($isRecommend > 0) { $where['supervisors.is_recommend'] = $isRecommend; } if ($type > 0) { $where['supervisors.type'] = $type; } $list = $this->model->with(['member']) ->from('supervisors') ->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('supervisors.name', 'like', "%{$keyword}%") ->orWhere('supervisors.mobile','like',"%{$keyword}%") ->orWhere('supervisors.occupation','like',"%{$keyword}%"); } }) ->select(['supervisors.*']) ->withCount(['consults']) ->orderBy('supervisors.sort', 'desc') ->orderBy('supervisors.id', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['consults_count']=$item['guidance_count']?$item['guidance_count']:$item['consults_count']; $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'] : [] ]; } /** * 详情 * @param $id * @param $userId * @return array|mixed */ public function getInfo($id,$userId=0) { $cacheKey = "caches:supervisors:info_{$id}_{$userId}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $where = ['id'=> $id,'mark'=>1]; $info = $this->model->with(['member'])->where($where)->withCount(['consults'])->first(); $info = $info? $info->toArray() : []; if($info){ RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } /** * 获取推荐数据 * @return array|mixed */ public function getRecommendList($limit=0) { $limit = $limit?$limit:ConfigService::make()->getConfigByCode('supervisors_recommend_num',6); $cacheKey = "caches:supervisors:recommendList"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->with(['member']) ->where(['is_recommend'=> 1,'status'=>1,'mark'=>1]) ->withCount(['consults']) ->limit($limit) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 咨询信息提交 * @param $userId * @param $params * @return array|false */ public function consultSubmit($userId, $params) { $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0; $realname = isset($params['realname']) ? trim($params['realname']) : ''; $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; $industry = isset($params['industry']) ? trim($params['industry']) : ''; $position = isset($params['position']) ? trim($params['position']) : ''; if (empty($realname)) { $this->error = '请填写姓名'; return false; } if (empty($mobile)) { $this->error = '请填写联系方式'; return false; } if (empty($industry)) { $this->error = '请填写所在行业'; return false; } if (empty($position)) { $this->error = '请填写当前角色/职务'; return false; } $cacheKey = "caches:supervisors:consult:{$userId}_{$sourceId}"; if (RedisService::get($cacheKey)) { $this->error = '您近期已经提交过,请30秒后重试'; return false; } $sourceInfo = $this->model->where(['id'=>$sourceId,'mark'=>1])->first(); if(empty($sourceInfo)){ RedisService::clear($cacheKey); $this->error = '提交失败,咨询导师不存在'; return false; } $data = [ 'user_id' => $userId, 'source_id' => $sourceId, 'realname' => $realname, 'mobile' => $mobile, 'industry' => $industry, 'position' => $position, 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '', 'interest' => isset($params['interest'])? trim($params['interest']) : '', 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '', 'create_time' => time(), 'update_time' => time(), 'status' => 1, 'mark' => 1, ]; if (!$id = SupervisorsConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) { if(!$id = SupervisorsConsultRecordsModel::insertGetId($data)){ RedisService::clear($cacheKey); $this->error = '提交失败'; return false; } }else{ SupervisorsConsultRecordsModel::where(['id'=>$id])->update($data); } $this->error = '提交成功'; RedisService::set($cacheKey, $data, 30); return ['id' => $id]; } }