admin/addon/article/model/ArticleCategory.php

122 lines
3.9 KiB
PHP

<?php
/** ZJMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2022-2032 四川正今科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.zjphp.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道官方颁发授权证书,无纸质授权凭证书视为侵权行为。
* =========================================================
*/
namespace addon\article\model;
use app\model\NewBaseModel;
class ArticleCategory extends NewBaseModel{
protected $pk = 'category_id';
protected $autoWriteTimestamp = 'int'; // 开启自动时间戳
protected $createTime = 'create_time'; // 默认添加时间字段
protected $updateTime = 'update_time'; // 默认编辑时间字段
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2022/10/18 14:45
* @param $siteId
* @param false $isPage
* @return array
* @throws \think\db\exception\DbException
*/
public function getList($siteId,$isPage = true){
// 参数获取
$page = input('page',1);
$pageSize = input('page_size',PAGE_LIST_ROWS);
$categoryName = (string)input('category_name');
$sort = (string)input('sort');
$order = ['create_time'=>'DESC'];
if($sort) $order = ['sort'=>$sort];
// 列表获取
$model = $this
->field('category_id,category_name,sort,create_time,update_time')
->where('site_id',$siteId)
->where('category_name','LIKE',"%{$categoryName}%")
->withCount(['relevanceArticle'=>'article_count'])
->order($order);
if($isPage){
$result = $model->paginate(['list_rows' => $pageSize,'page' => $page]);
if($result) $result = $result->toArray();
$list = [
'count' => $result['total'],
'list' => $result['data'],
'page_count' => $result['last_page'],
];
}else{
$list = $model->select();
if($list) $list = $list->toArray();
}
return $this->success($list);
}
/**
* Common: 关联文章表
* Author: wu-hui
* Time: 2022/10/17 11:10
*/
public function relevanceArticle(){
return $this->hasMany(Article::class, 'category_id','category_id');
}
/**
* Common: 根据id获取分类列表
* Author: wu-hui
* Time: 2022/11/03 16:50
* @param $ids
* @return array 返回由分类id为键 分类名称为值的内容
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getListCateList($ids){
$list = (new ArticleCategory())
->field('category_id,category_name')
->whereIn('category_id',$ids)
->select();
if($list){
$list = $list->toArray();
return array_column($list,'category_name','category_id');
}
return [];
}
/**
* Common: 客户端文章列表获取全部分类
* Author: wu-hui
* Time: 2022/11/07 15:28
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function apiGetList(){
// 列表获取
$list = $this
->field('category_id,category_name')
->where('site_id',$this->site_id)
->order(['sort'=>'desc','create_time'=>'DESC'])
->select();
if($list) $list = $list->toArray();
return $this->success($list);
}
}