2020-11-25 01:07:06 +00:00
|
|
|
<?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
|
|
|
|
]
|
2020-11-25 01:07:06 +00:00
|
|
|
];
|
2020-12-03 05:47:57 +00:00
|
|
|
$items = self::where('category_id', $categoryId)->order('sort', 'desc')->paginate($paginate, $isSample);
|
2020-11-25 01:07:06 +00:00
|
|
|
if(!$items->isEmpty()) {
|
|
|
|
$ids = $items->column('id');
|
|
|
|
$infoList = HistoryInfo::getByHistoryIds($ids);
|
|
|
|
foreach ($items as $k => $item) {
|
|
|
|
$items[$k]['info'] = $infoList[$item['id']] ?? [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:57 +00:00
|
|
|
public static function getByCategoryId($categoryId, $onlyVisible = false, $defaultNum = -1)
|
2020-11-25 01:07:06 +00:00
|
|
|
{
|
|
|
|
$items = self::where('category_id', $categoryId)
|
|
|
|
->when($onlyVisible, function($query){
|
|
|
|
$query->where('visible', 1);
|
|
|
|
})
|
2020-12-03 05:47:57 +00:00
|
|
|
->order('sort', 'desc')
|
2020-11-25 01:07:06 +00:00
|
|
|
->select();
|
|
|
|
if(!$items->isEmpty()) {
|
|
|
|
$ids = $items->column('id');
|
2020-12-03 05:47:57 +00:00
|
|
|
$infoList = [];
|
|
|
|
if ($defaultNum != 0) {
|
|
|
|
$infoList = HistoryInfo::getByHistoryIds($ids, $onlyVisible);
|
|
|
|
}
|
2020-11-25 01:07:06 +00:00
|
|
|
foreach ($items as $k => $item) {
|
2020-12-03 05:47:57 +00:00
|
|
|
$infos = $infoList[$item['id']] ?? [];
|
|
|
|
$infoData = $defaultNum > 0 ? array_slice($infos, 0, $defaultNum, false) : $infos;
|
|
|
|
$items[$k]['info'] = $infoData;
|
2020-11-25 01:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $items->toArray();
|
|
|
|
}
|
|
|
|
}
|