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

107 lines
3.1 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月30日
* @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 CommonFaqTreeListForm extends Model
{
public $cx_mch_id;
public function rules()
{
return [
[['cx_mch_id'], 'integer'],
[['cx_mch_id'], 'required'],
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$cat_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_FAQ,
'status' => 1,
'is_hide' => 0
])
->select('id,name,img_url')
->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC])
->asArray()->all();
$faq_list = FaqMeta::find()->alias('fm')
->leftJoin(['f' => Faq::tableName()], 'f.id=fm.faq_id')
->where([
'f.is_delete' => 0,
'f.cx_mch_id' => $this->cx_mch_id,
'f.status' => Faq::STATUS_PUBLISHED,
'fm.is_delete' => 0,
'fm.type' => FaqMeta::TYPE_CAT
])
->select('fm.value as cat_id,f.id,f.title,f.content,f.created_at,f.published_at,f.status,f.sort,f.is_hot')
->orderBy(['f.sort' => SORT_ASC, 'f.created_at' => SORT_DESC])->asArray()->all();
foreach ($faq_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']);
$faq_list[$index] = $item;
}
foreach ($cat_list as $index => $item){
$item['list'] = $this->getFaqList($item['id'], $faq_list);
$item['img_url'] = SiteHelper::getFullUrl($item['img_url']);
$cat_list[$index] = $item;
}
$data = $cat_list;
return $this->apiReturnSuccess("ok", $data);
}
private function getFaqList($cat_id, $faq_list)
{
$list = [];
foreach ($faq_list as $index => $faq){
if(in_array($cat_id, array_column($faq['cat_list'], 'cat_id')))
$list[] = $faq;
}
return $list;
}
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;
}
}