chaoyu/app/model/History.php

53 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace app\model;
class History extends Base
{
public static function onAfterInsert($item)
{
$item->sort = $item->id;
$item->save();
}
public static function getPaginateList($categoryId, $per = 20, $isSample = false)
{
$paginate = [
2020-12-08 08:11:04 +00:00
'list_rows' => $per,
'query' => [
'category_id' => $categoryId
]
];
$items = self::where('category_id', $categoryId)->order('sort', 'desc')->paginate($paginate, $isSample);
if(!$items->isEmpty()) {
$ids = $items->column('id');
$infoList = HistoryInfo::getByHistoryIds($ids);
foreach ($items as $k => $item) {
$items[$k]['info'] = $infoList[$item['id']] ?? [];
}
}
return $items;
}
public static function getByCategoryId($categoryId, $onlyVisible = false, $defaultNum = -1)
{
$items = self::where('category_id', $categoryId)
->when($onlyVisible, function($query){
$query->where('visible', 1);
})
->order('sort', 'desc')
->select();
if(!$items->isEmpty()) {
$ids = $items->column('id');
$infoList = [];
if ($defaultNum != 0) {
$infoList = HistoryInfo::getByHistoryIds($ids, $onlyVisible);
}
foreach ($items as $k => $item) {
$infos = $infoList[$item['id']] ?? [];
$infoData = $defaultNum > 0 ? array_slice($infos, 0, $defaultNum, false) : $infos;
$items[$k]['info'] = $infoData;
}
}
return $items->toArray();
}
}