manghe/app/common/model/FFCategories.php
2025-06-25 18:26:26 +08:00

104 lines
2.9 KiB
PHP

<?php
namespace app\common\model;
use app\common\model\Base;
use think\Model;
class FFCategories extends Base{
// 设置当前模型对应的完整数据表名称
protected $table = 'ff_categories';
// 设置主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
/**
* 获取分页列表
* @param array $where 查询条件
* @param string $field 查询字段
* @param string $order 排序规则
* @param string $pageSize 每页数量
* @return array
*/
public static function getList($where = [], $field = '*', $order = 'sort_order asc', $pageSize = "15")
{
$list = self::where($where)
->field($field)
->order($order)
->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['count'] = $list->total();
$data['page'] = $page;
return $data;
}
/**
* 获取所有列表(不分页)
* @param array $where 查询条件
* @param string $field 查询字段
* @param string $order 排序规则
* @param string $limit 限制条数
* @return \think\Collection
*/
public static function getAllList($where = [], $field = '*', $order = 'sort_order asc', $limit = '0')
{
$data = self::where($where)
->field($field)
->order($order)
->limit($limit)
->select();
return $data;
}
/**
* 获取单条数据
* @param array $where 查询条件
* @param string $field 查询字段
* @return array|Model|null
*/
public static function getInfo($where = [], $field = '*')
{
$data = self::where($where)
->field($field)
->find();
return $data;
}
/**
* 获取树形结构分类数据
* @param int $parentId 父级ID
* @param array $where 额外查询条件
* @return array
*/
public static function getTreeList($parentId = 0, $where = [])
{
$where[] = ['status', '=', 1];
$allCategories = self::getAllList($where)->toArray();
return self::buildTree($allCategories, $parentId);
}
/**
* 构建树形结构
* @param array $elements 所有元素
* @param int $parentId 父级ID
* @return array
*/
protected static function buildTree(array $elements, $parentId = 0)
{
$branch = [];
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = self::buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
}