97 lines
3.2 KiB
PHP
97 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace app\api\model;
|
|
|
|
/**
|
|
* 文章
|
|
* @mixin \think\Model
|
|
*/
|
|
class Article extends BaseModel
|
|
{
|
|
protected $schema = [
|
|
'id' => 'int',
|
|
'platform_id' => 'int',
|
|
'language_id' => 'int',
|
|
'site_id' => 'int',
|
|
'article_type_id' => 'int',
|
|
'title' => 'string',
|
|
'brief' => 'string',
|
|
'desc' => 'string',
|
|
'thumb' => 'string',
|
|
'url' => 'string',
|
|
'meta_title' => 'string',
|
|
'keywords' => 'string',
|
|
'detail' => 'string',
|
|
'sort' => 'int',
|
|
'status' => 'int',
|
|
'is_outer_chain' => 'int',
|
|
'click_num' => 'int',
|
|
'create_time' => 'int',
|
|
'update_time' => 'int'
|
|
];
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function articleType():\think\model\relation\HasOne
|
|
{
|
|
return $this->hasOne(ArticleType::class,'id','article_type_id')->bind(['article_type_name'=>'name']);
|
|
}
|
|
|
|
/**
|
|
*/
|
|
public function languageName():\think\model\relation\HasOne
|
|
{
|
|
return $this->hasOne(Language::class,'id','site_id')->bind(['language_name'=>'name']);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取列表
|
|
* @param array $data 查询条件
|
|
* @param mixed $simple 模糊搜索条件
|
|
* @param mixed $focus 精准搜索条件
|
|
* @param null $sort
|
|
* @param null $field
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getAllList(array $data,$simple = null,$focus = null,$sort = null,$field=null):array
|
|
{
|
|
$fields = array_keys($this->schema);
|
|
$collection = $this->field($field)->where(function ($query) use ($data,$fields,$simple,$focus) {
|
|
if (isset($data['platform_id'])) {
|
|
if (in_array('platform_id',$fields)) {
|
|
$query->where('platform_id',$data['platform_id']);
|
|
}
|
|
}
|
|
//模糊查询条件
|
|
if (!empty($simple)) {
|
|
foreach ($simple as $key => $item) {
|
|
if (in_array($key,$fields)) {
|
|
$query->where($key,'like',"%".$item."%");
|
|
}
|
|
}
|
|
}
|
|
//精准查询
|
|
if (!empty($focus)) {
|
|
foreach ($focus as $key => $item) {
|
|
if (in_array($key,$fields)) {
|
|
$query->where($key,$item);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (isset($data['page']) && isset($data['size'])) {
|
|
$count = $collection->count() ?: 0;
|
|
$page = $collection->limit((int) $data['size'])->page((int)$data['page'])->order($sort)->with(['articleType','languageName'])->select()->toArray();
|
|
return ['page'=>$page,'count'=>$count];
|
|
} else {
|
|
return $collection->order($sort)->with(['articleType','languageName'])->select()->toArray();
|
|
}
|
|
}
|
|
}
|