cxfoot/models/common/CommonFaqListForm.php
2023-10-24 14:54:18 +08:00

125 lines
3.8 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月29日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Faq;
use app\models\FaqMeta;
use app\models\Cat;
use app\models\User;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonFaqListForm extends Model
{
public $keywords;
public $limit;
public $page;
public $cat_id;
public $cx_mch_id;
public $status;
public $is_hot;
public $faq_id;
public function rules()
{
return [
[['keywords'], 'trim'],
[['keywords'], 'string'],
[['limit', 'page', 'cat_id', 'cx_mch_id', 'status', 'is_hot' ,'faq_id'], 'integer'],
[['page'], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['cx_mch_id'], 'required'],
[['faq_id'], 'required', 'on' => 'detail'],
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$cat_query = FaqMeta::find()
->where([
'type' => FaqMeta::TYPE_CAT,
'is_delete' => 0,
'cx_mch_id' => $this->cx_mch_id,
])
->select('faq_id,value as cat_id');
$query = Faq::find()->alias('f')
->leftJoin(['fm' => $cat_query], 'fm.faq_id=f.id')
->where([
'f.is_delete' => 0,
'f.cx_mch_id' => $this->cx_mch_id,
])
->andWhere([
'OR',
['LIKE', 'f.title', $this->keywords],
['LIKE', 'f.content', $this->keywords],
])
->andFilterWhere([
'f.is_hot' => $this->is_hot,
'f.status' => $this->status,
'f.id' => $this->faq_id,
'fm.cat_id' => $this->cat_id
])
->select('f.id,f.title,f.content,f.created_at,f.published_at,f.status,f.sort,f.is_hot');
$count_query = clone $query;
$count = $count_query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['f.sort' => SORT_ASC, 'f.created_at' => SORT_DESC])->asArray()->all();
foreach ($list as $index => $item){
$item['created_at_cn'] = date('Y-m-d H:i:s',$item['created_at']);
$item['published_at_cn'] = date('Y-m-d H:i:s',$item['published_at']);
$item['status_cn'] = Faq::getStatus($item['status']);
$item['cat_list'] = $this->getCatList($item['id']);
if($this->scenario == 'detail')
$item['content'] = SiteHelper::repairContent($item['content']);
$item['viewed_num'] = Faq::getViewedNum($item['id']);
$list[$index] = $item;
}
//是否已经全部加载
$end_flag = $this->page > $pagination->pageCount ? true : false;
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
private function getCatList($faq_id)
{
$list = FaqMeta::find()->alias('fm')
->leftJoin(['c' => Cat::tableName()],'c.id=fm.value')
->where([
'fm.is_delete' => 0,
'fm.type' => FaqMeta::TYPE_CAT,
'fm.faq_id' => $faq_id,
'c.is_delete' => 0,
'c.type' => Cat::TYPE_FAQ,
])
->select('c.name,c.id as cat_id')->asArray()->all();
return $list;
}
}