find($industry_id); } /** * 所有行业 */ public static function getALL() { $model = new static; if (!Cache::get('industry_' . $model::$app_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['industry_id']) continue; $threeTree = []; foreach ($all as $three) $three['parent_id'] == $two['industry_id'] && $threeTree[$three['industry_id']] = $three; !empty($threeTree) && $two['child'] = $threeTree; array_push($twoTree, $two); } if (!empty($twoTree)) { $temp_two_tree = array_column($twoTree, 'sort'); array_multisort($temp_two_tree, SORT_ASC, $twoTree); $first['child'] = $twoTree; } array_push($tree, $first); } Cache::tag('cache')->set('industry_' . $model::$app_id, compact('all', 'tree')); } return Cache::get('industry_' . $model::$app_id); } /** * 获取所有行业 */ public static function getCacheAll() { return self::getALL()['all']; } /** * 获取所有行业(树状结构) */ public static function getCacheTree() { return self::getALL()['tree']; } /** * 获取所有行业(树状结构) * @return string */ public static function getCacheTreeJson() { return json_encode(static::getCacheTree()); } /** * 获取指定行业下的所有子行业id */ public static function getSubIndustryId($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::getSubIndustryId($item['industry_id'], $all); !empty($subIds) && $arrIds = array_merge($arrIds, $subIds); } } return $arrIds; } /** * 指定的行业下是否存在子行业 */ protected static function hasSubIndustry($parentId) { $all = self::getCacheAll(); foreach ($all as $item) { if ($item['parent_id'] == $parentId) { return true; } } return false; } /** * 获取所有一级行业 */ public static function getFirstIndustry() { return (new static())->where('parent_id', '=', 0) ->order(['sort' => 'asc', 'create_time' => 'asc']) ->select(); } public function getListByIds($ids) { return $this->field(['industry_id', 'name', 'parent_id'])->where('industry_id', 'in', $ids)->select(); } }