catchAdmin/extend/catcher/CatchModelCollection.php

131 lines
3.1 KiB
PHP
Raw Normal View History

2020-10-21 08:12:07 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2020-10-21 08:12:07 +08:00
namespace catcher;
2020-10-21 13:54:55 +08:00
use catcher\library\excel\Excel;
use catcher\library\excel\ExcelContract;
2020-10-21 17:23:22 +08:00
use think\facade\Cache;
2021-03-01 21:00:09 +08:00
use think\helper\Str;
2020-10-21 08:12:07 +08:00
use think\model\Collection;
class CatchModelCollection extends Collection
{
2020-10-21 13:54:55 +08:00
/**
* tree 结构
*
* @time 2020年10月21日
* @param int $pid
* @param string $pidField
* @param string $children
* @return array
*/
2021-02-27 18:30:40 +08:00
public function toTree($pid = 0, $pidField = 'parent_id', $children = 'children'): array
2020-10-21 08:12:07 +08:00
{
$pk = 'id';
if ($this->count()) {
$pk = $this->first()->getPk();
}
return Tree::setPk($pk)->done($this->toArray(), $pid, $pidField, $children);
2020-10-21 08:12:07 +08:00
}
2020-10-21 13:54:55 +08:00
/**
* 导出数据
*
* @time 2020年10月21日
* @param $header
* @param string $path
* @param string $disk
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @return mixed|string[]
*/
2021-02-27 18:30:40 +08:00
public function export($header, $path = '', $disk = 'local'): array
2020-10-21 13:54:55 +08:00
{
$excel = new Class($header, $this->items) implements ExcelContract
{
protected $headers;
protected $sheets;
public function __construct($headers, $sheets)
{
$this->headers = $headers;
$this->sheets = $sheets;
}
public function headers(): array
{
// TODO: Implement headers() method.
return $this->headers;
}
public function sheets()
{
// TODO: Implement sheets() method.
return $this->sheets;
}
};
if (!$path) {
$path = Utils::publicPath('exports');
}
return (new Excel)->save($excel, $path, $disk);
}
2020-10-21 17:23:22 +08:00
/**
* 缓存 collection
*
* @time 2020年10月21日
* @param $key
* @param int $ttl
* @param string $store
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
2021-02-27 18:30:40 +08:00
public function cache($key, int $ttl = 0, string $store = 'redis'): bool
2020-10-21 17:23:22 +08:00
{
return Cache::store($store)->set($key, $this->items, $ttl);
}
/**
* 获取当前级别下的所有子级
*
* @time 2020年11月04日
* @param array $ids
* @param string $parentFields
* @param string $column
* @return array
*/
2021-02-27 18:30:40 +08:00
public function getAllChildrenIds(array $ids, $parentFields = 'parent_id', $column = 'id'): array
{
array_walk($ids, function (&$item){
$item = intval($item);
});
2021-02-27 18:30:40 +08:00
$childIds = $this->whereIn($parentFields, $ids)->column($column);
2021-02-27 18:30:40 +08:00
if (!empty($childIds)) {
$childIds = array_merge($childIds, $this->getAllChildrenIds($childIds));
}
2021-02-27 18:30:40 +08:00
return $childIds;
}
/**
* implode
*
* @time 2021年02月24日
* @param string $separator
* @param string $column
* @return string
*/
public function implode(string $column = '', string $separator = ','): string
{
return implode($separator, $column ? array_column($this->items, $column) : $this->items);
}
2020-10-21 08:12:07 +08:00
}