luck-draw/app/model/ArchivesCategory.php

146 lines
3.8 KiB
PHP

<?php
namespace app\model;
use think\Collection;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;
class ArchivesCategory extends Base
{
const index_id = 1;//首页
/**
* 检测数据下 是否有子栏目
*
* @param array $ids
* @return bool
*/
public static function hasChildrenByIds(array $ids): bool
{
return self::whereIn('pid', $ids)->count() > 0;
}
/**
* 检测数据下 是否有子内容
*
* @param array $ids
* @return bool
*/
public static function hasContentByIds(array $ids): bool
{
return Archives::whereIn('category_id', $ids)->count() > 0;
}
/**
* 获取列表
*
* @return ArchivesCategory[]|array|Collection
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function getList()
{
return self::field('id,pid,title,name,sort,path,true as open')
->order('sort', 'desc')
->order('id', 'asc')
->select();
}
/**
* 模型
*
* @return HasOne
*/
public function model(): HasOne
{
return $this->hasOne(ArchivesModel::class, 'id', 'model_id')->bind(['model' => 'name']);
}
//--王兴龙 auth start --------------------------------------------------------------------------------------------------
//获取前台菜单
public static function getListForFrontMenu()
{
$items = self::withJoin(["model"])
->order(['sort' =>'desc',"id"=>"asc"])
->select()
->toArray();
return self::getCates($items);
}
//获取递归栏目
public static function getCates($cates, $parentId = 0)
{
$menus = [];
foreach ($cates as $cate) {
if ($cate['pid'] == $parentId) {
$children = self::getCates($cates, $cate['id']);
if (!empty($children)) {
$cate['children'] = $children;
}
$menus[] = $cate;
}
}
return $menus;
}
//当前分类的最高级分类Id
public static function firstGradeById($id)
{
$res = 0;
$item = self::getById($id);
if ($item) {
$res = $id;
if ($item['pid'] > 0) {
$items = self::select()->toArray();
$first = self::getFirstGrade($items, $item['pid']);
if (!empty($first)) {
$res = $first['id'];
}
}
}
return $res;
}
public static function getFirstGrade($items, $parentId)
{
$data = [];
foreach ($items as $key => $item) {
if ($item['id'] == $parentId) {
if ($item['pid'] > 0) {
unset($items[$key]);
$parent = self::getFirstGrade($items, $item['pid']);
if (!empty($parent)) {
$data = $parent;
} else {
$data = $item;
}
} else {
$data = $item;
}
}
}
return $data;
}
/**
* 获取每个栏目文章封面大小 数组
* */
public static function getArchiveCoverSizeArr()
{
return json_encode(self::column(["id","archive_cover_size"],"id"));
}
/**
* 获取每个栏目 设置路由
* */
public static function getRoute()
{
return self::column(["id","name","route"],"id");
}
//--王兴龙 auth end --------------------------------------------------------------------------------------------------
}