1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace app\common\model\plus\business;
 
use think\facade\Cache;
use app\common\model\BaseModel;
 
/**
 * 行业模型
 */
class Industry extends BaseModel
{
    protected $pk = 'industry_id';
    protected $name = 'industry';
 
    /**
     * 行业详情
     */
    public static function detail($industry_id)
    {
        return (new static())->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();
    }
}