| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
- namespace App\Web\Models;
- use App\Models\BaseModel;
- use Illuminate\Support\Facades\DB;
- class HelpModel extends BaseModel
- {
- /**
- * 状态字段
- */
- const DELETED_AT = 'status';
- /**
- * @var string
- */
- protected $table = 'help';
- /**
- * 获取文章列表
- * */
- public function getHelpList($params,$fields='a.*'){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $where=[];
- if(isset($params['status'])){
- $where[]=['a.status','=',$params['status']];
- }else{
- $where[]=['a.status','<',2];
- }
- if(!empty($params['lt_total_view'])){
- $where[]=['a.total_view','<',$params['lt_total_view']];
- }
- $query= $this->newInstance()->alias('a')->where($where);
- if (!empty($params['keyword'])) {
- $keyword = $params['keyword'];
- $query->where('a.title', 'like', "%" . $keyword . "%");
- }
- if(!empty($params['sort'])){
- if($params['sort']=='sort_ascending'){
- $query->orderBy('sort','asc');
- }else if($params['sort']=='sort_descending'){
- $query->orderBy('sort','desc');
- }else if($params['sort']=='pub_date_ascending'){
- $query->orderBy('pub_date','asc');
- }else if($params['sort']=='pub_date_descending'){
- $query->orderBy('pub_date','desc');
- }else if($params['sort']=='create_time_ascending'){
- $query->orderBy('create_time','asc');
- }else if($params['sort']=='create_time_descending'){
- $query->orderBy('create_time','desc');
- }else if($params['sort']=='update_time_ascending'){
- $query->orderBy('update_time','asc');
- }else if($params['sort']=='update_time_descending'){
- $query->orderBy('update_time','desc');
- }
- }else{
- $query->orderBy('sort');
- }
- $totalCount = $query->count();
- $list= $query->skip($skip)
- ->limit($pageSize)
- ->selectRaw($fields)
- ->get();
- if(!empty($list)){
- $list=$list->toArray();
- }else{
- $list=[];
- }
- $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
- return $result;
- }
- /**
- * 获取文章分类未关联的文章
- * */
- public function getUnrelatedHelpByTypeIds($params){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $typeId = $params['type_id'] ?? '';
- $keyword = $params['keyword'] ?? '';
- $where=[];
- if ($keyword) {
- $keyword = addslashes($keyword);
- $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
- }
- $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
- ->leftJoin('help_type_relation as b', function ($join)use($typeId) {
- $join->on('a.id', '=', 'b.help_id')
- ->where('b.status', '=', 0)
- ->where('b.type_id', '=', $typeId);
- })->where('a.status','<',2)->whereNull('b.id')->count();
- $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
- $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
- ->leftJoin('help_type_relation as b', function ($join)use($typeId) {
- $join->on('a.id', '=', 'b.help_id')
- ->where('b.status', '=', 0)
- ->where('b.type_id', '=', $typeId);
- })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
- ->limit($pageSize)
- ->selectRaw($fields)
- ->orderBy('sort')
- ->get();
- if(!empty($list)){
- $list=$list->toArray();
- }else{
- $list=[];
- }
- $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
- return $result;
- }
- /**
- * 获取文章标签未关联的文章
- * */
- public function getUnrelatedHelpByTagIds($params){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $tagId = $params['tag_id'] ?? '';
- $keyword = $params['keyword'] ?? '';
- $where=[];
- if ($keyword) {
- $keyword = addslashes($keyword);
- $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
- }
- $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
- ->leftJoin('help_tag_relation as b', function ($join)use($tagId) {
- $join->on('a.id', '=', 'b.help_id')
- ->where('b.status', '=', 0)
- ->where('b.tag_id', '=', $tagId);
- })->where('a.status','<',2)->whereNull('b.id')->count();
- $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
- $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
- ->leftJoin('help_tag_relation as b', function ($join)use($tagId) {
- $join->on('a.id', '=', 'b.help_id')
- ->where('b.status', '=', 0)
- ->where('b.tag_id', '=', $tagId);
- })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
- ->limit($pageSize)
- ->selectRaw($fields)
- ->orderBy('sort')
- ->get();
- if(!empty($list)){
- $list=$list->toArray();
- }else{
- $list=[];
- }
- $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
- return $result;
- }
- /**
- * 文章保存
- * @param array $data
- * */
- public function saveHelpData($data){
- if (!empty($data['id'])) {
- $id = $data['id'];
- $this->newInstance()->where('id', $data['id'])->update($data);
- } else {
- $id = $this->newInstance()->insertGetId($data);
- }
- return $id;
- }
- /**
- * 检查 标签是否唯一
- * */
- public function checkNameUnique($name,$id=0){
- $where=[];
- if(!empty($id)){
- $where[]=['id', '<>', $id];
- }
- $where[]=['status', '<', 2];
- $where[]=['title', '=', trim($name)];
- return $this->checkFieldUnique('title',$where);
- }
- /**
- * 获取文章详情
- * @param array $params
- * */
- public function getHelpInfo($params=[]){
- if(empty($params)){
- return [];
- }
- $where=[];
- $where[]=['status','<',2];
- if(!empty($params['id'])){
- $where['id']=$params['id'];
- }
- $info= $this->newInstance()->where($where)->first();
- if(!empty($info)){
- return $info->toArray();
- }else{
- return [];
- }
- }
- /**
- * 获取文章基本列表
- * */
- public function getPublishHelpList($params,$fields='a.*'){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $where=[];
- $where['a.status']=0;
- $query= $this->newInstance()->alias('a')
- ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
- ->where($where);
- if(!empty($params['sort'])){
- if(!empty($params['sort']['is_hot'])){
- $query=$query->orderBy('a.total_view','desc');
- }
- if(!empty($params['sort']['pub_date'])){
- $query=$query->orderBy('a.pub_date','desc');
- }
- }
- if (!empty($params['keyword'])) {
- $keyword = $params['keyword'];
- $query=$query->where(function ($queryStr) use ($keyword) {
- $queryStr->where('a.title', 'like', "%" . $keyword . "%")
- ->orWhere('a.description', 'like', '%' . $keyword . '%');
- });
- }
- if(!empty($params['only_List'])){
- $list= $query->orderBy('a.id','desc')->selectRaw($fields)->limit($pageSize)->get()->toArray();
- }else{
- $list= $query->orderBy('a.id','desc')->selectRaw($fields)->paginate($pageSize)->toArray();;
- }
- if(empty($list)){
- $list=[];
- }
- return $list;
- }
- /**
- * 根据类型ids获取个类型文章
- * */
- public function getRenderListByTypeIds($typeIds,$params,$fields='a.*'){
- $where=[];
- $where['a.status']=0;
- $where['b.status']=0;
- if(!empty($params['plate_id'])){
- $where['a.plate_id']=$params['plate_id'];
- }
- $pageSize = $this->getPageSize($params);
- $sql = '';
- foreach ($typeIds as $item){
- $sqlStr= $this->newInstance()->alias('a')
- ->leftJoin('help_type_relation as b', 'a.id', '=', 'b.help_id')
- ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
- ->where($where)
- ->where('b.type_id','=',DB::raw($item))
- ->orderBy('a.pub_date','desc')
- ->orderBy('a.id','desc')
- ->limit($pageSize)->selectRaw($fields);
- if (empty($sql)) {
- $sql = $sqlStr;
- } else {
- $sql->unionAll($sqlStr);
- }
- }
- $list=[];
- if(!empty($sql)){
- $list = $sql->get();
- if($list){
- $list=$list->toArray();
- }
- }
- return $list;
- }
- /**
- * 根据类型id获取文章
- * */
- public function getPublishHelpListByTypeId($params,$fields='a.*'){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $where=[];
- $where['a.status']=0;
- if(!empty($params['type_id'])){
- $where['b.type_id']= $params['type_id'];
- $where['b.status']=0;
- }
- $list = $this->newInstance()->alias('a')->selectRaw($fields)
- ->leftJoin('help_type_relation as b', 'a.id', '=', 'b.help_id')
- ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
- ->where($where)
- ->groupBy('a.id')
- ->orderBy('a.pub_date','desc')
- ->orderBy('a.id','desc')
- ->paginate($pageSize)
- ->toArray();
- return $list;
- }
- /**
- * 根据标签id获取文章
- * */
- public function getPublishHelpListByTagId($params,$fields='a.*'){
- list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
- $where=[];
- $where['a.status']=0;
- if(!empty($params['tag_id'])){
- $where['b.tag_id']= $params['tag_id'];
- $where['b.status']=0;
- }
- if (!empty($params['plate_id'])) {
- $where['a.plate_id'] = $params['plate_id'];
- }
- $totalCount= $this->newInstance()->alias('a')
- ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
- ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
- ->where($where)->count();
- $list= $this->newInstance()->alias('a')
- ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
- ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
- ->where($where)
- ->orderBy('a.pub_date','desc')
- ->orderBy('a.id','desc')
- ->limit($pageSize)->selectRaw($fields)->get();
- if($list){
- $list=$list->toArray();
- }else{
- $list=[];
- }
- $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
- return $result;
- }
- /**
- * 根据类型ids获取标签文章
- * */
- public function getRenderListByTagIds($tagIds,$params,$fields='a.*'){
- $where=[];
- $where['a.status']=0;
- $where['b.status']=0;
- if(!empty($params['plate_id'])){
- $where['a.plate_id']=$params['plate_id'];
- }
- $pageSize = $this->getPageSize($params);
- $sql = '';
- foreach ($tagIds as $item){
- $sqlStr= $this->newInstance()->alias('a')
- ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
- ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
- ->where($where)
- ->where('b.tag_id','=',DB::raw($item))
- ->orderBy('a.pub_date','desc')
- ->orderBy('a.id','desc')
- ->limit($pageSize)->selectRaw($fields);
- if (empty($sql)) {
- $sql = $sqlStr;
- } else {
- $sql->unionAll($sqlStr);
- }
- }
- $list=[];
- if(!empty($sql)){
- $list = $sql->get();
- if($list){
- $list=$list->toArray();
- }
- }
- return $list;
- }
- public function updatePv($id){
- DB::update('update help set votes = 1');
- }
- }
|