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

82 lines
2.4 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月29日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Cat;
use app\models\Model;
use yii\data\Pagination;
class CommonCatListForm extends Model
{
public $keywords;
public $page;
public $limit;
public $cx_mch_id;
public $type;
public function rules()
{
return [
[['keywords'], 'trim'],
[['keywords'], 'string'],
[['limit', 'page', 'type', 'cx_mch_id'], 'integer'],
[['page'], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['type', 'cx_mch_id'], 'required']
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$query = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'type' => $this->type,
'is_delete' => 0,
])
->andWhere([
'OR',
['LIKE', 'name', $this->keywords],
['LIKE', 'desc', $this->keywords],
])
->select('id,name,desc,img_url,parent_id,sort,created_at,status,is_hide,big_img_url,advert_url,advert_pic,advert_url_type,advert_open_params');
$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(['sort' => SORT_ASC, '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['status_cn'] = $item['status'] == 1 ? "已开启": "已关闭";
$item['is_hide_cn'] = $item['is_hide'] == 1 ? "已隐藏": "已显示";
$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
];
}
}