// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\NoticeModel; use App\Services\BaseService; /** * 通知公告-服务类 * @author laravel开发员 * @since 2020/11/11 * Class NoticeService * @package App\Services\Common */ class NoticeService extends BaseService { protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * NoticeService constructor. */ public function __construct() { $this->model = new NoticeModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取挂单广告列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 15, $field=[]) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; if($status>0){ $where['a.status'] = $status; } $list = $this->model->from('notice as a') ->where($where) ->where(function($query) use($params){ $title = isset($params['title'])? trim($params['title']) : ''; if($title){ $query->where('a.title','like',"%{$title}%"); } }) ->select($field? $field : ['a.*']) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):''; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 获取最新公告列表 * @param int $limit * @return mixed */ public function getNewList($limit=6) { return $this->model->from('notice as a')->where(['status'=>1,'mark'=>1])->take($limit)->pluck('title'); } /** * 设置置顶 * @return array * @since 2020/11/21 * @author laravel开发员 */ public function setIsTop() { $data = request()->all(); if (!$data['id']) { return message('记录ID不能为空', false); } if (!$data['is_top']) { return message('设置置顶不能为空', false); } $error = ''; $item = [ 'id' => $data['id'], 'is_top' => $data['is_top'] ]; $rowId = $this->model->edit($item, $error); if (!$rowId) { return message($error, false); } return message(); } }