-
Notifications
You must be signed in to change notification settings - Fork 1
/
industry.go
111 lines (89 loc) · 2.12 KB
/
industry.go
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
package random
// Industry 行业类别
type Industry struct {
Level1 IndustryElement // 门类
Level2 IndustryElement // 大类
Level3 IndustryElement // 中类
Level4 IndustryElement // 小类
}
// IndustryElement 行业类别基础分类
type IndustryElement struct {
Code string
Name string
}
type industryClassNode struct {
Code string
Name string
children []*industryClassNode
}
type industryRoot struct { // 固定4层
Category []*industryClassNode // 门类 大 中 小
}
var industryTree *industryRoot
func init() {
var inds []*industryClassNode
registers[DataIndustryChina] = func() {
CheckAndDecompress(dconf.dataUrls["industry"], &inds)
industryTree = createFrom(inds)
}
}
func (inds *industryClassNode) String() string {
return inds.Code + " " + inds.Name
}
func createFrom(data []*industryClassNode) *industryRoot { // 固定4层
industryTree = &industryRoot{}
var c1, c2, c3 *industryClassNode
for _, c := range data {
// log.Println(c.Code, c.Name)
if len(c.Code) == 1 {
industryTree.Category = append(industryTree.Category, c)
c1 = c
continue
}
if len(c.Code) == 2 {
c1.children = append(c1.children, c)
c2 = c
continue
}
if len(c.Code) == 3 {
c2.children = append(c2.children, c)
c3 = c
continue
}
if len(c.Code) == 4 {
c3.children = append(c3.children, c)
continue
}
}
return industryTree
}
func (inds *industryRoot) get(selects ...int) *industryClassNode {
var sel *industryClassNode
cur := inds.Category
for _, s := range selects {
sel = cur[s]
cur = cur[s].children
}
return sel
}
// Industry 随机行业数据
func (ext *Extend) Industry() *Industry {
var ind *Industry = &Industry{}
cur := industryTree.Category
sel := cur[ext.Intn(len(cur))]
ind.Level1.Code = sel.Code
ind.Level1.Name = sel.Name
cur = sel.children
sel = cur[ext.Intn(len(cur))]
ind.Level2.Code = sel.Code
ind.Level2.Name = sel.Name
cur = sel.children
sel = cur[ext.Intn(len(cur))]
ind.Level3.Code = sel.Code
ind.Level3.Name = sel.Name
cur = sel.children
sel = cur[ext.Intn(len(cur))]
ind.Level4.Code = sel.Code
ind.Level4.Name = sel.Name
return ind
}