-
Notifications
You must be signed in to change notification settings - Fork 1
/
area.go
180 lines (164 loc) · 4.09 KB
/
area.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package official
import (
"strings"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/webx/application/dbschema"
"github.com/admpub/webx/application/library/top"
)
func NewArea(ctx echo.Context) *Area {
return &Area{
OfficialCommonArea: dbschema.NewOfficialCommonArea(ctx),
}
}
type Area struct {
*dbschema.OfficialCommonArea
}
func (f *Area) Exists(name string) (bool, error) {
return f.OfficialCommonArea.Exists(nil, db.Cond{`name`: name})
}
func (f *Area) ExistsOther(name string, id uint) (bool, error) {
return f.OfficialCommonArea.Exists(nil, db.Cond{`name`: name, `id <>`: id})
}
func (f *Area) check() error {
ctx := f.Context()
f.Name = strings.TrimSpace(f.Name)
if len(f.Name) == 0 {
return ctx.NewError(code.InvalidParameter, `请输入地区名称`).SetZone(`name`)
}
f.Short = strings.TrimSpace(f.Short)
if len(f.Short) == 0 {
return ctx.NewError(code.InvalidParameter, `请输入地区简称`).SetZone(`short`)
}
if len(f.CountryAbbr) != 2 || !com.StrIsAlpha(f.CountryAbbr) {
return ctx.NewError(code.InvalidParameter, `请输入两个字母的国家码`).SetZone(`countryAbbr`)
}
var (
exists bool
err error
)
if f.Id < 1 {
exists, err = f.Exists(f.Name)
} else {
exists, err = f.ExistsOther(f.Name, f.Id)
}
if err != nil {
return err
}
if exists {
return ctx.NewError(code.DataAlreadyExists, `名称“%s”已存在`, f.Name).SetZone(`name`)
}
f.Merged = f.Name
f.Level = 1
if f.Pid > 0 {
if f.Pid == f.Id {
return f.Context().E(`不能选择当前地区数据作为上级地区`)
}
positions, err := f.Positions(f.Pid)
if err != nil {
return err
}
areas := make([]string, len(positions))
for i, v := range positions {
areas[i] = v.Name
}
areas = append(areas, f.Name)
f.Merged = strings.Join(areas, `,`)
f.Level = uint(len(areas))
}
f.Pinyin = strings.TrimSpace(f.Pinyin)
if len(f.Pinyin) == 0 {
f.Pinyin = top.Pinyin(f.Short)
} else {
if !com.StrIsAlpha(f.Pinyin) {
return ctx.NewError(code.InvalidParameter, `请输入拼音字母不正确`).SetZone(`pinyin`)
}
f.Pinyin = strings.ToLower(f.Pinyin)
}
if len(f.Pinyin) > 1 {
f.First = strings.ToUpper(f.Pinyin[0:1])
} else if len(f.Pinyin) == 1 {
f.First = strings.ToUpper(f.Pinyin)
} else {
f.First = ``
}
f.CountryAbbr = strings.ToUpper(f.CountryAbbr)
return nil
}
func (f *Area) Add() (pk interface{}, err error) {
if err = f.check(); err != nil {
return nil, err
}
return f.OfficialCommonArea.Insert()
}
func (f *Area) Edit(mw func(db.Result) db.Result, args ...interface{}) error {
if err := f.check(); err != nil {
return err
}
return f.OfficialCommonArea.Update(mw, args...)
}
func (f *Area) Parent(pid uint) (*dbschema.OfficialCommonArea, error) {
m := dbschema.NewOfficialCommonArea(f.Context())
err := m.Get(nil, `id`, pid)
return m, err
}
func (f *Area) Parents(pid uint) ([]*dbschema.OfficialCommonArea, error) {
parents := make([]*dbschema.OfficialCommonArea, 0)
var (
m *dbschema.OfficialCommonArea
err error
pids = map[uint]struct{}{}
)
loop:
if pid == 0 {
return parents, nil
}
if _, ok := pids[pid]; ok {
return parents, nil
}
m, err = f.Parent(pid)
if err != nil {
if err == db.ErrNoMoreRows {
return parents, nil
}
return parents, err
}
pids[pid] = struct{}{}
pid = m.Pid
parents = append(parents, m)
goto loop
}
func (f *Area) Positions(id uint) ([]*dbschema.OfficialCommonArea, error) {
parents, err := f.Parents(id)
if err != nil {
return parents, err
}
if len(parents) == 0 {
return parents, nil
}
positions := make([]*dbschema.OfficialCommonArea, len(parents))
var index int
for end := len(parents) - 1; end >= 0; end-- {
positions[index] = parents[end]
index++
}
return positions, err
}
func (f *Area) PositionIds(id uint) ([]uint, error) {
parents, err := f.Parents(id)
if err != nil {
return nil, err
}
if len(parents) == 0 {
return nil, nil
}
ids := make([]uint, len(parents))
var index int
for end := len(parents) - 1; end >= 0; end-- {
ids[index] = parents[end].Id
index++
}
return ids, err
}