<?php
namespace app\common\model\sharing;
use think\Cache;
use app\common\model\BaseModel;
class Category extends BaseModel
{
protected $name = 'sharing_category';
public static function getALL()
{
$model = new static;
if (!Cache::get('sharing_category_' . $model::$wxapp_id)) {
$data = $model->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
$all = !empty($data) ? $data->toArray() : [];
$tree = [];
foreach ($all as $first) {
if ($first['parent_id'] != 0) continue;
$twoTree = [];
foreach ($all as $two) {
if ($two['parent_id'] != $first['category_id']) continue;
$threeTree = [];
foreach ($all as $three)
$three['parent_id'] == $two['category_id']
&& $threeTree[$three['category_id']] = $three;
!empty($threeTree) && $two['child'] = $threeTree;
$twoTree[$two['category_id']] = $two;
}
if (!empty($twoTree)) {
array_multisort(array_column($twoTree, 'sort'), SORT_ASC, $twoTree);
$first['child'] = $twoTree;
}
$tree[$first['category_id']] = $first;
}
Cache::tag('cache')->set('sharing_category_' . $model::$wxapp_id, compact('all', 'tree'));
}
return Cache::get('sharing_category_' . $model::$wxapp_id);
}
public static function getCacheAll()
{
return self::getALL()['all'];
}
public static function getCacheTree()
{
return self::getALL()['tree'];
}
public static function getCacheTreeJson()
{
return json_encode(static::getCacheTree());
}
public static function getSubCategoryId($parent_id, $all = [])
{
$arrIds = [$parent_id];
empty($all) && $all = self::getCacheAll();
foreach ($all as $key => $item) {
if ($item['parent_id'] == $parent_id) {
unset($all[$key]);
$subIds = self::getSubCategoryId($item['category_id'], $all);
!empty($subIds) && $arrIds = array_merge($arrIds, $subIds);
}
}
return $arrIds;
}
}