-
Notifications
You must be signed in to change notification settings - Fork 1
/
area_group.go
95 lines (81 loc) · 2.37 KB
/
area_group.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
package official
import (
"strings"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/webx/application/dbschema"
)
func NewAreaGroup(ctx echo.Context) *AreaGroup {
return &AreaGroup{
OfficialCommonAreaGroup: dbschema.NewOfficialCommonAreaGroup(ctx),
}
}
type AreaGroup struct {
*dbschema.OfficialCommonAreaGroup
}
func (f *AreaGroup) Exists(countryAbbr string, abbr string) (bool, error) {
return f.OfficialCommonAreaGroup.Exists(nil, db.And(
db.Cond{`country_abbr`: countryAbbr},
db.Cond{`abbr`: abbr},
))
}
func (f *AreaGroup) ExistsOther(countryAbbr string, abbr string, id uint) (bool, error) {
return f.OfficialCommonAreaGroup.Exists(nil, db.And(
db.Cond{`country_abbr`: countryAbbr},
db.Cond{`abbr`: abbr},
db.Cond{`id`: db.NotEq(id)},
))
}
func (f *AreaGroup) check() error {
var (
exists bool
err error
)
if f.Id < 1 {
exists, err = f.Exists(f.CountryAbbr, f.Abbr)
} else {
exists, err = f.ExistsOther(f.CountryAbbr, f.Abbr, f.Id)
}
if err != nil {
return err
}
if exists {
return f.Context().E(`缩写“%s-%s”已存在`, f.CountryAbbr, f.Abbr)
}
f.CountryAbbr = strings.ToUpper(f.CountryAbbr)
return nil
}
func (f *AreaGroup) Add() (pk interface{}, err error) {
if err = f.check(); err != nil {
return nil, err
}
return f.OfficialCommonAreaGroup.Insert()
}
func (f *AreaGroup) Edit(mw func(db.Result) db.Result, args ...interface{}) error {
if err := f.check(); err != nil {
return err
}
return f.OfficialCommonAreaGroup.Update(mw, args...)
}
func (f *AreaGroup) GetWithExt(cond *db.Compounds) (*AreaGroupExt, error) {
recv := &AreaGroupExt{}
err := f.NewParam().SetArgs(cond.And()).SetRecv(recv).One()
return recv, err
}
func (f *AreaGroup) ListPageWithExt(cond *db.Compounds, sorts ...interface{}) ([]*AreaGroupExt, error) {
list := []*AreaGroupExt{}
_, err := handler.NewLister(f.OfficialCommonAreaGroup, &list, func(r db.Result) db.Result {
return r.OrderBy(sorts...)
}, cond.And()).Paging(f.Context())
return list, err
}
func (f *AreaGroup) ListPage(cond *db.Compounds, sorts ...interface{}) ([]*dbschema.OfficialCommonAreaGroup, error) {
_, err := handler.NewLister(f.OfficialCommonAreaGroup, nil, func(r db.Result) db.Result {
return r.OrderBy(sorts...)
}, cond.And()).Paging(f.Context())
if err != nil {
return nil, err
}
return f.Objects(), err
}