正在显示
10 个修改的文件
包含
975 行增加
和
3 行删除
models/bulletin.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + "time" | ||
9 | + | ||
10 | + "github.com/astaxie/beego/orm" | ||
11 | +) | ||
12 | + | ||
13 | +type Bulletin struct { | ||
14 | + Id int `orm:"column(id);auto"` | ||
15 | + Title string `orm:"column(title);size(2000)" description:"标题"` | ||
16 | + Content string `orm:"column(content);null" description:"内容"` | ||
17 | + Cover string `orm:"column(cover);size(255);null" description:"封面地址"` | ||
18 | + W int `orm:"column(w);null" description:"宽"` | ||
19 | + H int `orm:"column(h);null" description:"高"` | ||
20 | + Type int8 `orm:"column(type);null" description:"公告类型(0图+跳转链接、1链接)"` | ||
21 | + Receiver string `orm:"column(receiver);null" description:"接收者"` | ||
22 | + QuestionSwitch int8 `orm:"column(question_switch);null" description:"设置问题开关"` | ||
23 | + CreateTime time.Time `orm:"column(createTime);type(timestamp);null" description:"创建时间"` | ||
24 | + UpdateTime time.Time `orm:"column(updateTime);type(timestamp);null" description:"更新时间"` | ||
25 | + AllowClose int8 `orm:"column(allowClose);null" description:"允许关闭公告(0允许,1禁止)"` | ||
26 | + CompanyId int `orm:"column(company_id);null" description:"公司Id"` | ||
27 | + Status uint8 `orm:"column(status)" description:"状态 0-下架 1- 上架"` | ||
28 | +} | ||
29 | + | ||
30 | +func (t *Bulletin) TableName() string { | ||
31 | + return "bulletin" | ||
32 | +} | ||
33 | + | ||
34 | +func init() { | ||
35 | + orm.RegisterModel(new(Bulletin)) | ||
36 | +} | ||
37 | + | ||
38 | +// AddBulletin insert a new Bulletin into database and returns | ||
39 | +// last inserted Id on success. | ||
40 | +func AddBulletin(m *Bulletin) (id int64, err error) { | ||
41 | + o := orm.NewOrm() | ||
42 | + id, err = o.Insert(m) | ||
43 | + return | ||
44 | +} | ||
45 | + | ||
46 | +// GetBulletinById retrieves Bulletin by Id. Returns error if | ||
47 | +// Id doesn't exist | ||
48 | +func GetBulletinById(id int) (v *Bulletin, err error) { | ||
49 | + o := orm.NewOrm() | ||
50 | + v = &Bulletin{Id: id} | ||
51 | + if err = o.Read(v); err == nil { | ||
52 | + return v, nil | ||
53 | + } | ||
54 | + return nil, err | ||
55 | +} | ||
56 | + | ||
57 | +// GetAllBulletin retrieves all Bulletin matches certain condition. Returns empty list if | ||
58 | +// no records exist | ||
59 | +func GetAllBulletin(query map[string]string, fields []string, sortby []string, order []string, | ||
60 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
61 | + o := orm.NewOrm() | ||
62 | + qs := o.QueryTable(new(Bulletin)) | ||
63 | + // query k=v | ||
64 | + for k, v := range query { | ||
65 | + // rewrite dot-notation to Object__Attribute | ||
66 | + k = strings.Replace(k, ".", "__", -1) | ||
67 | + if strings.Contains(k, "isnull") { | ||
68 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
69 | + } else { | ||
70 | + qs = qs.Filter(k, v) | ||
71 | + } | ||
72 | + } | ||
73 | + // order by: | ||
74 | + var sortFields []string | ||
75 | + if len(sortby) != 0 { | ||
76 | + if len(sortby) == len(order) { | ||
77 | + // 1) for each sort field, there is an associated order | ||
78 | + for i, v := range sortby { | ||
79 | + orderby := "" | ||
80 | + if order[i] == "desc" { | ||
81 | + orderby = "-" + v | ||
82 | + } else if order[i] == "asc" { | ||
83 | + orderby = v | ||
84 | + } else { | ||
85 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
86 | + } | ||
87 | + sortFields = append(sortFields, orderby) | ||
88 | + } | ||
89 | + qs = qs.OrderBy(sortFields...) | ||
90 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
91 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
92 | + for _, v := range sortby { | ||
93 | + orderby := "" | ||
94 | + if order[0] == "desc" { | ||
95 | + orderby = "-" + v | ||
96 | + } else if order[0] == "asc" { | ||
97 | + orderby = v | ||
98 | + } else { | ||
99 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
100 | + } | ||
101 | + sortFields = append(sortFields, orderby) | ||
102 | + } | ||
103 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
104 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
105 | + } | ||
106 | + } else { | ||
107 | + if len(order) != 0 { | ||
108 | + return nil, errors.New("Error: unused 'order' fields") | ||
109 | + } | ||
110 | + } | ||
111 | + | ||
112 | + var l []Bulletin | ||
113 | + qs = qs.OrderBy(sortFields...) | ||
114 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
115 | + if len(fields) == 0 { | ||
116 | + for _, v := range l { | ||
117 | + ml = append(ml, v) | ||
118 | + } | ||
119 | + } else { | ||
120 | + // trim unused fields | ||
121 | + for _, v := range l { | ||
122 | + m := make(map[string]interface{}) | ||
123 | + val := reflect.ValueOf(v) | ||
124 | + for _, fname := range fields { | ||
125 | + m[fname] = val.FieldByName(fname).Interface() | ||
126 | + } | ||
127 | + ml = append(ml, m) | ||
128 | + } | ||
129 | + } | ||
130 | + return ml, nil | ||
131 | + } | ||
132 | + return nil, err | ||
133 | +} | ||
134 | + | ||
135 | +// UpdateBulletin updates Bulletin by Id and returns error if | ||
136 | +// the record to be updated doesn't exist | ||
137 | +func UpdateBulletinById(m *Bulletin) (err error) { | ||
138 | + o := orm.NewOrm() | ||
139 | + v := Bulletin{Id: m.Id} | ||
140 | + // ascertain id exists in the database | ||
141 | + if err = o.Read(&v); err == nil { | ||
142 | + var num int64 | ||
143 | + if num, err = o.Update(m); err == nil { | ||
144 | + fmt.Println("Number of records updated in database:", num) | ||
145 | + } | ||
146 | + } | ||
147 | + return | ||
148 | +} | ||
149 | + | ||
150 | +// DeleteBulletin deletes Bulletin by Id and returns error if | ||
151 | +// the record to be deleted doesn't exist | ||
152 | +func DeleteBulletin(id int) (err error) { | ||
153 | + o := orm.NewOrm() | ||
154 | + v := Bulletin{Id: id} | ||
155 | + // ascertain id exists in the database | ||
156 | + if err = o.Read(&v); err == nil { | ||
157 | + var num int64 | ||
158 | + if num, err = o.Delete(&Bulletin{Id: id}); err == nil { | ||
159 | + fmt.Println("Number of records deleted in database:", num) | ||
160 | + } | ||
161 | + } | ||
162 | + return | ||
163 | +} |
models/bulletin_question.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "time" | ||
6 | + | ||
7 | + "github.com/astaxie/beego/orm" | ||
8 | +) | ||
9 | + | ||
10 | +type BulletinQuestion struct { | ||
11 | + Id int `orm:"column(id);auto"` | ||
12 | + BulletinId int `orm:"column(bulletin_id)" description:"公告id"` | ||
13 | + Type int8 `orm:"column(type)" description:"类型:0-单选,1-多选"` | ||
14 | + Title string `orm:"column(title);size(2000)" description:"标题"` | ||
15 | + Option string `orm:"column(option);size(2000)" description:"内容"` | ||
16 | + CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"` | ||
17 | + UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" description:"更新时间"` | ||
18 | +} | ||
19 | + | ||
20 | +func (t *BulletinQuestion) TableName() string { | ||
21 | + return "bulletin_question" | ||
22 | +} | ||
23 | + | ||
24 | +func init() { | ||
25 | + orm.RegisterModel(new(BulletinQuestion)) | ||
26 | +} | ||
27 | + | ||
28 | +//BulletinQuestionOption 公告问题选项内容的结构 | ||
29 | +type BulletinQuestionOption struct { | ||
30 | + Id int `json:"id"` //选项id | ||
31 | + Content string `json:"content"` //选项描述 | ||
32 | +} | ||
33 | + | ||
34 | +// AddBulletinQuestion insert a new BulletinQuestion into database and returns | ||
35 | +// last inserted Id on success. | ||
36 | +func AddBulletinQuestion(m *BulletinQuestion) (id int64, err error) { | ||
37 | + o := orm.NewOrm() | ||
38 | + id, err = o.Insert(m) | ||
39 | + return | ||
40 | +} | ||
41 | + | ||
42 | +// GetBulletinQuestionById retrieves BulletinQuestion by Id. Returns error if | ||
43 | +// Id doesn't exist | ||
44 | +func GetBulletinQuestionById(id int) (v *BulletinQuestion, err error) { | ||
45 | + o := orm.NewOrm() | ||
46 | + v = &BulletinQuestion{Id: id} | ||
47 | + if err = o.Read(v); err == nil { | ||
48 | + return v, nil | ||
49 | + } | ||
50 | + return nil, err | ||
51 | +} | ||
52 | + | ||
53 | +// UpdateBulletinQuestion updates BulletinQuestion by Id and returns error if | ||
54 | +// the record to be updated doesn't exist | ||
55 | +func UpdateBulletinQuestionById(m *BulletinQuestion) (err error) { | ||
56 | + o := orm.NewOrm() | ||
57 | + v := BulletinQuestion{Id: m.Id} | ||
58 | + // ascertain id exists in the database | ||
59 | + if err = o.Read(&v); err == nil { | ||
60 | + var num int64 | ||
61 | + if num, err = o.Update(m); err == nil { | ||
62 | + fmt.Println("Number of records updated in database:", num) | ||
63 | + } | ||
64 | + } | ||
65 | + return | ||
66 | +} |
models/bulletin_question_answer.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "time" | ||
6 | + | ||
7 | + "github.com/astaxie/beego/orm" | ||
8 | +) | ||
9 | + | ||
10 | +type BulletinQuestionAnswer struct { | ||
11 | + Id int `orm:"column(id);auto"` | ||
12 | + Answer string `orm:"column(answer)" description:"答案"` | ||
13 | + BulletinId int `orm:"column(bulletin_id)" description:"公告id"` | ||
14 | + BulletinQuestionId int `orm:"column(bulletin_question_id)" description:"公告问题id"` | ||
15 | + Uid int64 `orm:"column(uid)" description:"用户id"` | ||
16 | + CreateTime time.Time `orm:"column(createTime);type(timestamp)" description:"创建时间"` | ||
17 | + UpdateTime time.Time `orm:"column(updateTime);type(timestamp)" description:"更新时间"` | ||
18 | +} | ||
19 | + | ||
20 | +func (t *BulletinQuestionAnswer) TableName() string { | ||
21 | + return "bulletin_question_answer" | ||
22 | +} | ||
23 | + | ||
24 | +func init() { | ||
25 | + orm.RegisterModel(new(BulletinQuestionAnswer)) | ||
26 | +} | ||
27 | + | ||
28 | +//公告问题用户反馈结果 | ||
29 | +type BulletinAnswerResult struct { | ||
30 | + VoteResult []int `json:"vote_result"` //问题选项 | ||
31 | + EditContent string `json:"edit_content"` //自定义编辑内容 | ||
32 | +} | ||
33 | + | ||
34 | +// AddBulletinQuestionAnswer insert a new BulletinQuestionAnswer into database and returns | ||
35 | +// last inserted Id on success. | ||
36 | +func AddBulletinQuestionAnswer(m *BulletinQuestionAnswer) (id int64, err error) { | ||
37 | + o := orm.NewOrm() | ||
38 | + id, err = o.Insert(m) | ||
39 | + return | ||
40 | +} | ||
41 | + | ||
42 | +// GetBulletinQuestionAnswerById retrieves BulletinQuestionAnswer by Id. Returns error if | ||
43 | +// Id doesn't exist | ||
44 | +func GetBulletinQuestionAnswerById(id int) (v *BulletinQuestionAnswer, err error) { | ||
45 | + o := orm.NewOrm() | ||
46 | + v = &BulletinQuestionAnswer{Id: id} | ||
47 | + if err = o.Read(v); err == nil { | ||
48 | + return v, nil | ||
49 | + } | ||
50 | + return nil, err | ||
51 | +} | ||
52 | + | ||
53 | +// UpdateBulletinQuestionAnswer updates BulletinQuestionAnswer by Id and returns error if | ||
54 | +// the record to be updated doesn't exist | ||
55 | +func UpdateBulletinQuestionAnswerById(m *BulletinQuestionAnswer) (err error) { | ||
56 | + o := orm.NewOrm() | ||
57 | + v := BulletinQuestionAnswer{Id: m.Id} | ||
58 | + // ascertain id exists in the database | ||
59 | + if err = o.Read(&v); err == nil { | ||
60 | + var num int64 | ||
61 | + if num, err = o.Update(m); err == nil { | ||
62 | + fmt.Println("Number of records updated in database:", num) | ||
63 | + } | ||
64 | + } | ||
65 | + return | ||
66 | +} | ||
67 | + | ||
68 | +// DeleteBulletinQuestionAnswer deletes BulletinQuestionAnswer by Id and returns error if | ||
69 | +// the record to be deleted doesn't exist | ||
70 | +func DeleteBulletinQuestionAnswer(id int) (err error) { | ||
71 | + o := orm.NewOrm() | ||
72 | + v := BulletinQuestionAnswer{Id: id} | ||
73 | + // ascertain id exists in the database | ||
74 | + if err = o.Read(&v); err == nil { | ||
75 | + var num int64 | ||
76 | + if num, err = o.Delete(&BulletinQuestionAnswer{Id: id}); err == nil { | ||
77 | + fmt.Println("Number of records deleted in database:", num) | ||
78 | + } | ||
79 | + } | ||
80 | + return | ||
81 | +} |
models/commend.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
7 | + "reflect" | ||
8 | + "strings" | ||
9 | + "time" | ||
10 | + | ||
11 | + "github.com/astaxie/beego/orm" | ||
12 | +) | ||
13 | + | ||
14 | +type Commend struct { | ||
15 | + Id int64 `orm:"column(id);auto" description:"表彰编号"` | ||
16 | + Content string `orm:"column(content);size(255)" description:"表彰内容"` | ||
17 | + CommendAt time.Time `orm:"column(commend_at);type(timestamp)" description:"表彰时间"` | ||
18 | + UserId int64 `orm:"column(user_id)" description:"表user.id 表彰用户id"` | ||
19 | + CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司id"` | ||
20 | + CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"` | ||
21 | + EnableStatus int8 `orm:"column(enable_status)" description:"状态 1可见 2不可见"` | ||
22 | +} | ||
23 | + | ||
24 | +func (t *Commend) TableName() string { | ||
25 | + return "commend" | ||
26 | +} | ||
27 | + | ||
28 | +func init() { | ||
29 | + orm.RegisterModel(new(Commend)) | ||
30 | +} | ||
31 | + | ||
32 | +// AddCommend insert a new Commend into database and returns | ||
33 | +// last inserted Id on success. | ||
34 | +func AddCommend(m *Commend) (id int64, err error) { | ||
35 | + o := orm.NewOrm() | ||
36 | + id, err = o.Insert(m) | ||
37 | + return | ||
38 | +} | ||
39 | + | ||
40 | +// GetCommendById retrieves Commend by Id. Returns error if | ||
41 | +// Id doesn't exist | ||
42 | +func GetCommendById(id int64) (v *Commend, err error) { | ||
43 | + o := orm.NewOrm() | ||
44 | + v = &Commend{Id: id} | ||
45 | + if err = o.Read(v); err == nil { | ||
46 | + return v, nil | ||
47 | + } | ||
48 | + return nil, err | ||
49 | +} | ||
50 | + | ||
51 | +// GetAllCommend retrieves all Commend matches certain condition. Returns empty list if | ||
52 | +// no records exist | ||
53 | +func GetAllCommend(query map[string]string, fields []string, sortby []string, order []string, | ||
54 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
55 | + o := orm.NewOrm() | ||
56 | + qs := o.QueryTable(new(Commend)) | ||
57 | + // query k=v | ||
58 | + for k, v := range query { | ||
59 | + // rewrite dot-notation to Object__Attribute | ||
60 | + k = strings.Replace(k, ".", "__", -1) | ||
61 | + if strings.Contains(k, "isnull") { | ||
62 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
63 | + } else { | ||
64 | + qs = qs.Filter(k, v) | ||
65 | + } | ||
66 | + } | ||
67 | + // order by: | ||
68 | + var sortFields []string | ||
69 | + if len(sortby) != 0 { | ||
70 | + if len(sortby) == len(order) { | ||
71 | + // 1) for each sort field, there is an associated order | ||
72 | + for i, v := range sortby { | ||
73 | + orderby := "" | ||
74 | + if order[i] == "desc" { | ||
75 | + orderby = "-" + v | ||
76 | + } else if order[i] == "asc" { | ||
77 | + orderby = v | ||
78 | + } else { | ||
79 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
80 | + } | ||
81 | + sortFields = append(sortFields, orderby) | ||
82 | + } | ||
83 | + qs = qs.OrderBy(sortFields...) | ||
84 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
85 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
86 | + for _, v := range sortby { | ||
87 | + orderby := "" | ||
88 | + if order[0] == "desc" { | ||
89 | + orderby = "-" + v | ||
90 | + } else if order[0] == "asc" { | ||
91 | + orderby = v | ||
92 | + } else { | ||
93 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
94 | + } | ||
95 | + sortFields = append(sortFields, orderby) | ||
96 | + } | ||
97 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
98 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
99 | + } | ||
100 | + } else { | ||
101 | + if len(order) != 0 { | ||
102 | + return nil, errors.New("Error: unused 'order' fields") | ||
103 | + } | ||
104 | + } | ||
105 | + | ||
106 | + var l []Commend | ||
107 | + qs = qs.OrderBy(sortFields...) | ||
108 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
109 | + if len(fields) == 0 { | ||
110 | + for _, v := range l { | ||
111 | + ml = append(ml, v) | ||
112 | + } | ||
113 | + } else { | ||
114 | + // trim unused fields | ||
115 | + for _, v := range l { | ||
116 | + m := make(map[string]interface{}) | ||
117 | + val := reflect.ValueOf(v) | ||
118 | + for _, fname := range fields { | ||
119 | + m[fname] = val.FieldByName(fname).Interface() | ||
120 | + } | ||
121 | + ml = append(ml, m) | ||
122 | + } | ||
123 | + } | ||
124 | + return ml, nil | ||
125 | + } | ||
126 | + return nil, err | ||
127 | +} | ||
128 | + | ||
129 | +// UpdateCommend updates Commend by Id and returns error if | ||
130 | +// the record to be updated doesn't exist | ||
131 | +func UpdateCommendById(m *Commend) (err error) { | ||
132 | + o := orm.NewOrm() | ||
133 | + v := Commend{Id: m.Id} | ||
134 | + // ascertain id exists in the database | ||
135 | + if err = o.Read(&v); err == nil { | ||
136 | + var num int64 | ||
137 | + if num, err = o.Update(m); err == nil { | ||
138 | + fmt.Println("Number of records updated in database:", num) | ||
139 | + } | ||
140 | + } | ||
141 | + return | ||
142 | +} | ||
143 | + | ||
144 | +// DeleteCommend deletes Commend by Id and returns error if | ||
145 | +// the record to be deleted doesn't exist | ||
146 | +func DeleteCommend(id int64) (err error) { | ||
147 | + o := orm.NewOrm() | ||
148 | + v := Commend{Id: id} | ||
149 | + // ascertain id exists in the database | ||
150 | + if err = o.Read(&v); err == nil { | ||
151 | + var num int64 | ||
152 | + if num, err = o.Delete(&Commend{Id: id}); err == nil { | ||
153 | + fmt.Println("Number of records deleted in database:", num) | ||
154 | + } | ||
155 | + } | ||
156 | + return | ||
157 | +} | ||
158 | + | ||
159 | +func GetCommends(companyId int,lastId int,pageSize int)(v []*Commend,total int, err error) { | ||
160 | + sql :=mybeego.NewSqlExutor().Table("commend").Order("create_at desc") | ||
161 | + sql.Where(fmt.Sprintf("company_id=%d",companyId)) | ||
162 | + if pageSize>0{ | ||
163 | + sql.Limit(0,pageSize) | ||
164 | + } | ||
165 | + if lastId>0{ | ||
166 | + sql.Where(fmt.Sprintf("id>%d",lastId)) | ||
167 | + } | ||
168 | + if total, err = sql.Querys(&v); err == nil { | ||
169 | + return | ||
170 | + } | ||
171 | + return | ||
172 | +} |
models/company.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + "time" | ||
9 | + | ||
10 | + "github.com/astaxie/beego/orm" | ||
11 | +) | ||
12 | + | ||
13 | +type Company struct { | ||
14 | + Id int64 `orm:"column(id);auto"` | ||
15 | + Name string `orm:"column(name);size(40)"` | ||
16 | + UserId int64 `orm:"column(user_id)"` | ||
17 | + CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now"` | ||
18 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp)"` | ||
19 | + DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"` | ||
20 | + Logo string `orm:"column(logo);size(255)"` | ||
21 | +} | ||
22 | + | ||
23 | +func (t *Company) TableName() string { | ||
24 | + return "company" | ||
25 | +} | ||
26 | + | ||
27 | +func init() { | ||
28 | + orm.RegisterModel(new(Company)) | ||
29 | +} | ||
30 | + | ||
31 | +// AddCompany insert a new Company into database and returns | ||
32 | +// last inserted Id on success. | ||
33 | +func AddCompany(m *Company) (id int64, err error) { | ||
34 | + o := orm.NewOrm() | ||
35 | + id, err = o.Insert(m) | ||
36 | + return | ||
37 | +} | ||
38 | + | ||
39 | +// GetCompanyById retrieves Company by Id. Returns error if | ||
40 | +// Id doesn't exist | ||
41 | +func GetCompanyById(id int64) (v *Company, err error) { | ||
42 | + o := orm.NewOrm() | ||
43 | + v = &Company{Id: id} | ||
44 | + if err = o.Read(v); err == nil { | ||
45 | + return v, nil | ||
46 | + } | ||
47 | + return nil, err | ||
48 | +} | ||
49 | + | ||
50 | +// GetAllCompany retrieves all Company matches certain condition. Returns empty list if | ||
51 | +// no records exist | ||
52 | +func GetAllCompany(query map[string]string, fields []string, sortby []string, order []string, | ||
53 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
54 | + o := orm.NewOrm() | ||
55 | + qs := o.QueryTable(new(Company)) | ||
56 | + // query k=v | ||
57 | + for k, v := range query { | ||
58 | + // rewrite dot-notation to Object__Attribute | ||
59 | + k = strings.Replace(k, ".", "__", -1) | ||
60 | + if strings.Contains(k, "isnull") { | ||
61 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
62 | + } else { | ||
63 | + qs = qs.Filter(k, v) | ||
64 | + } | ||
65 | + } | ||
66 | + // order by: | ||
67 | + var sortFields []string | ||
68 | + if len(sortby) != 0 { | ||
69 | + if len(sortby) == len(order) { | ||
70 | + // 1) for each sort field, there is an associated order | ||
71 | + for i, v := range sortby { | ||
72 | + orderby := "" | ||
73 | + if order[i] == "desc" { | ||
74 | + orderby = "-" + v | ||
75 | + } else if order[i] == "asc" { | ||
76 | + orderby = v | ||
77 | + } else { | ||
78 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
79 | + } | ||
80 | + sortFields = append(sortFields, orderby) | ||
81 | + } | ||
82 | + qs = qs.OrderBy(sortFields...) | ||
83 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
84 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
85 | + for _, v := range sortby { | ||
86 | + orderby := "" | ||
87 | + if order[0] == "desc" { | ||
88 | + orderby = "-" + v | ||
89 | + } else if order[0] == "asc" { | ||
90 | + orderby = v | ||
91 | + } else { | ||
92 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
93 | + } | ||
94 | + sortFields = append(sortFields, orderby) | ||
95 | + } | ||
96 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
97 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
98 | + } | ||
99 | + } else { | ||
100 | + if len(order) != 0 { | ||
101 | + return nil, errors.New("Error: unused 'order' fields") | ||
102 | + } | ||
103 | + } | ||
104 | + | ||
105 | + var l []Company | ||
106 | + qs = qs.OrderBy(sortFields...) | ||
107 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
108 | + if len(fields) == 0 { | ||
109 | + for _, v := range l { | ||
110 | + ml = append(ml, v) | ||
111 | + } | ||
112 | + } else { | ||
113 | + // trim unused fields | ||
114 | + for _, v := range l { | ||
115 | + m := make(map[string]interface{}) | ||
116 | + val := reflect.ValueOf(v) | ||
117 | + for _, fname := range fields { | ||
118 | + m[fname] = val.FieldByName(fname).Interface() | ||
119 | + } | ||
120 | + ml = append(ml, m) | ||
121 | + } | ||
122 | + } | ||
123 | + return ml, nil | ||
124 | + } | ||
125 | + return nil, err | ||
126 | +} | ||
127 | + | ||
128 | +// UpdateCompany updates Company by Id and returns error if | ||
129 | +// the record to be updated doesn't exist | ||
130 | +func UpdateCompanyById(m *Company) (err error) { | ||
131 | + o := orm.NewOrm() | ||
132 | + v := Company{Id: m.Id} | ||
133 | + // ascertain id exists in the database | ||
134 | + if err = o.Read(&v); err == nil { | ||
135 | + var num int64 | ||
136 | + if num, err = o.Update(m); err == nil { | ||
137 | + fmt.Println("Number of records updated in database:", num) | ||
138 | + } | ||
139 | + } | ||
140 | + return | ||
141 | +} | ||
142 | + | ||
143 | +// DeleteCompany deletes Company by Id and returns error if | ||
144 | +// the record to be deleted doesn't exist | ||
145 | +func DeleteCompany(id int64) (err error) { | ||
146 | + o := orm.NewOrm() | ||
147 | + v := Company{Id: id} | ||
148 | + // ascertain id exists in the database | ||
149 | + if err = o.Read(&v); err == nil { | ||
150 | + var num int64 | ||
151 | + if num, err = o.Delete(&Company{Id: id}); err == nil { | ||
152 | + fmt.Println("Number of records deleted in database:", num) | ||
153 | + } | ||
154 | + } | ||
155 | + return | ||
156 | +} |
models/department.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + "time" | ||
9 | + | ||
10 | + "github.com/astaxie/beego/orm" | ||
11 | +) | ||
12 | + | ||
13 | +type Department struct { | ||
14 | + Id int `orm:"column(id);auto"` | ||
15 | + CompanyId int `orm:"column(company_id)" description:"公司id"` | ||
16 | + Name string `orm:"column(name);size(30)" description:"部门名称"` | ||
17 | + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` | ||
18 | + ParentId int `orm:"column(parent_id)" description:"父级id"` | ||
19 | + Relation string `orm:"column(relation);size(400)" description:"父子级关系树"` | ||
20 | + DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"` | ||
21 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` | ||
22 | + Member int `orm:"column(member)" description:"成员数量"` | ||
23 | + Admin int `orm:"column(admin);null" description:"部门负责人id"` | ||
24 | +} | ||
25 | + | ||
26 | +func (t *Department) TableName() string { | ||
27 | + return "department" | ||
28 | +} | ||
29 | + | ||
30 | +func init() { | ||
31 | + orm.RegisterModel(new(Department)) | ||
32 | +} | ||
33 | + | ||
34 | +// AddDepartment insert a new Department into database and returns | ||
35 | +// last inserted Id on success. | ||
36 | +func AddDepartment(m *Department) (id int64, err error) { | ||
37 | + o := orm.NewOrm() | ||
38 | + id, err = o.Insert(m) | ||
39 | + return | ||
40 | +} | ||
41 | + | ||
42 | +// GetDepartmentById retrieves Department by Id. Returns error if | ||
43 | +// Id doesn't exist | ||
44 | +func GetDepartmentById(id int) (v *Department, err error) { | ||
45 | + o := orm.NewOrm() | ||
46 | + v = &Department{Id: id} | ||
47 | + if err = o.Read(v); err == nil { | ||
48 | + return v, nil | ||
49 | + } | ||
50 | + return nil, err | ||
51 | +} | ||
52 | + | ||
53 | +// GetAllDepartment retrieves all Department matches certain condition. Returns empty list if | ||
54 | +// no records exist | ||
55 | +func GetAllDepartment(query map[string]string, fields []string, sortby []string, order []string, | ||
56 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
57 | + o := orm.NewOrm() | ||
58 | + qs := o.QueryTable(new(Department)) | ||
59 | + // query k=v | ||
60 | + for k, v := range query { | ||
61 | + // rewrite dot-notation to Object__Attribute | ||
62 | + k = strings.Replace(k, ".", "__", -1) | ||
63 | + if strings.Contains(k, "isnull") { | ||
64 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
65 | + } else { | ||
66 | + qs = qs.Filter(k, v) | ||
67 | + } | ||
68 | + } | ||
69 | + // order by: | ||
70 | + var sortFields []string | ||
71 | + if len(sortby) != 0 { | ||
72 | + if len(sortby) == len(order) { | ||
73 | + // 1) for each sort field, there is an associated order | ||
74 | + for i, v := range sortby { | ||
75 | + orderby := "" | ||
76 | + if order[i] == "desc" { | ||
77 | + orderby = "-" + v | ||
78 | + } else if order[i] == "asc" { | ||
79 | + orderby = v | ||
80 | + } else { | ||
81 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
82 | + } | ||
83 | + sortFields = append(sortFields, orderby) | ||
84 | + } | ||
85 | + qs = qs.OrderBy(sortFields...) | ||
86 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
87 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
88 | + for _, v := range sortby { | ||
89 | + orderby := "" | ||
90 | + if order[0] == "desc" { | ||
91 | + orderby = "-" + v | ||
92 | + } else if order[0] == "asc" { | ||
93 | + orderby = v | ||
94 | + } else { | ||
95 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
96 | + } | ||
97 | + sortFields = append(sortFields, orderby) | ||
98 | + } | ||
99 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
100 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
101 | + } | ||
102 | + } else { | ||
103 | + if len(order) != 0 { | ||
104 | + return nil, errors.New("Error: unused 'order' fields") | ||
105 | + } | ||
106 | + } | ||
107 | + | ||
108 | + var l []Department | ||
109 | + qs = qs.OrderBy(sortFields...) | ||
110 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
111 | + if len(fields) == 0 { | ||
112 | + for _, v := range l { | ||
113 | + ml = append(ml, v) | ||
114 | + } | ||
115 | + } else { | ||
116 | + // trim unused fields | ||
117 | + for _, v := range l { | ||
118 | + m := make(map[string]interface{}) | ||
119 | + val := reflect.ValueOf(v) | ||
120 | + for _, fname := range fields { | ||
121 | + m[fname] = val.FieldByName(fname).Interface() | ||
122 | + } | ||
123 | + ml = append(ml, m) | ||
124 | + } | ||
125 | + } | ||
126 | + return ml, nil | ||
127 | + } | ||
128 | + return nil, err | ||
129 | +} | ||
130 | + | ||
131 | +// UpdateDepartment updates Department by Id and returns error if | ||
132 | +// the record to be updated doesn't exist | ||
133 | +func UpdateDepartmentById(m *Department) (err error) { | ||
134 | + o := orm.NewOrm() | ||
135 | + v := Department{Id: m.Id} | ||
136 | + // ascertain id exists in the database | ||
137 | + if err = o.Read(&v); err == nil { | ||
138 | + var num int64 | ||
139 | + if num, err = o.Update(m); err == nil { | ||
140 | + fmt.Println("Number of records updated in database:", num) | ||
141 | + } | ||
142 | + } | ||
143 | + return | ||
144 | +} | ||
145 | + | ||
146 | +// DeleteDepartment deletes Department by Id and returns error if | ||
147 | +// the record to be deleted doesn't exist | ||
148 | +func DeleteDepartment(id int) (err error) { | ||
149 | + o := orm.NewOrm() | ||
150 | + v := Department{Id: id} | ||
151 | + // ascertain id exists in the database | ||
152 | + if err = o.Read(&v); err == nil { | ||
153 | + var num int64 | ||
154 | + if num, err = o.Delete(&Department{Id: id}); err == nil { | ||
155 | + fmt.Println("Number of records deleted in database:", num) | ||
156 | + } | ||
157 | + } | ||
158 | + return | ||
159 | +} |
models/position.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + "time" | ||
9 | + | ||
10 | + "github.com/astaxie/beego/orm" | ||
11 | +) | ||
12 | + | ||
13 | +type Position struct { | ||
14 | + Id int `orm:"column(id);auto" description:"职位表id"` | ||
15 | + CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` | ||
16 | + Name string `orm:"column(name);size(100)" description:"职位名称"` | ||
17 | + ParentId int `orm:"column(parent_id)" description:"父级id"` | ||
18 | + Relation string `orm:"column(relation);size(400)" description:"父子级关系树"` | ||
19 | + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` | ||
20 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` | ||
21 | + EnableStatus string `orm:"column(enable_status);size(255)" description:"有效状态 1:有效 0:无效"` | ||
22 | +} | ||
23 | + | ||
24 | +func (t *Position) TableName() string { | ||
25 | + return "position" | ||
26 | +} | ||
27 | + | ||
28 | +func init() { | ||
29 | + orm.RegisterModel(new(Position)) | ||
30 | +} | ||
31 | + | ||
32 | +// AddPosition insert a new Position into database and returns | ||
33 | +// last inserted Id on success. | ||
34 | +func AddPosition(m *Position) (id int64, err error) { | ||
35 | + o := orm.NewOrm() | ||
36 | + id, err = o.Insert(m) | ||
37 | + return | ||
38 | +} | ||
39 | + | ||
40 | +// GetPositionById retrieves Position by Id. Returns error if | ||
41 | +// Id doesn't exist | ||
42 | +func GetPositionById(id int) (v *Position, err error) { | ||
43 | + o := orm.NewOrm() | ||
44 | + v = &Position{Id: id} | ||
45 | + if err = o.Read(v); err == nil { | ||
46 | + return v, nil | ||
47 | + } | ||
48 | + return nil, err | ||
49 | +} | ||
50 | + | ||
51 | +// GetAllPosition retrieves all Position matches certain condition. Returns empty list if | ||
52 | +// no records exist | ||
53 | +func GetAllPosition(query map[string]string, fields []string, sortby []string, order []string, | ||
54 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
55 | + o := orm.NewOrm() | ||
56 | + qs := o.QueryTable(new(Position)) | ||
57 | + // query k=v | ||
58 | + for k, v := range query { | ||
59 | + // rewrite dot-notation to Object__Attribute | ||
60 | + k = strings.Replace(k, ".", "__", -1) | ||
61 | + if strings.Contains(k, "isnull") { | ||
62 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
63 | + } else { | ||
64 | + qs = qs.Filter(k, v) | ||
65 | + } | ||
66 | + } | ||
67 | + // order by: | ||
68 | + var sortFields []string | ||
69 | + if len(sortby) != 0 { | ||
70 | + if len(sortby) == len(order) { | ||
71 | + // 1) for each sort field, there is an associated order | ||
72 | + for i, v := range sortby { | ||
73 | + orderby := "" | ||
74 | + if order[i] == "desc" { | ||
75 | + orderby = "-" + v | ||
76 | + } else if order[i] == "asc" { | ||
77 | + orderby = v | ||
78 | + } else { | ||
79 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
80 | + } | ||
81 | + sortFields = append(sortFields, orderby) | ||
82 | + } | ||
83 | + qs = qs.OrderBy(sortFields...) | ||
84 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
85 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
86 | + for _, v := range sortby { | ||
87 | + orderby := "" | ||
88 | + if order[0] == "desc" { | ||
89 | + orderby = "-" + v | ||
90 | + } else if order[0] == "asc" { | ||
91 | + orderby = v | ||
92 | + } else { | ||
93 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
94 | + } | ||
95 | + sortFields = append(sortFields, orderby) | ||
96 | + } | ||
97 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
98 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
99 | + } | ||
100 | + } else { | ||
101 | + if len(order) != 0 { | ||
102 | + return nil, errors.New("Error: unused 'order' fields") | ||
103 | + } | ||
104 | + } | ||
105 | + | ||
106 | + var l []Position | ||
107 | + qs = qs.OrderBy(sortFields...) | ||
108 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
109 | + if len(fields) == 0 { | ||
110 | + for _, v := range l { | ||
111 | + ml = append(ml, v) | ||
112 | + } | ||
113 | + } else { | ||
114 | + // trim unused fields | ||
115 | + for _, v := range l { | ||
116 | + m := make(map[string]interface{}) | ||
117 | + val := reflect.ValueOf(v) | ||
118 | + for _, fname := range fields { | ||
119 | + m[fname] = val.FieldByName(fname).Interface() | ||
120 | + } | ||
121 | + ml = append(ml, m) | ||
122 | + } | ||
123 | + } | ||
124 | + return ml, nil | ||
125 | + } | ||
126 | + return nil, err | ||
127 | +} | ||
128 | + | ||
129 | +// UpdatePosition updates Position by Id and returns error if | ||
130 | +// the record to be updated doesn't exist | ||
131 | +func UpdatePositionById(m *Position) (err error) { | ||
132 | + o := orm.NewOrm() | ||
133 | + v := Position{Id: m.Id} | ||
134 | + // ascertain id exists in the database | ||
135 | + if err = o.Read(&v); err == nil { | ||
136 | + var num int64 | ||
137 | + if num, err = o.Update(m); err == nil { | ||
138 | + fmt.Println("Number of records updated in database:", num) | ||
139 | + } | ||
140 | + } | ||
141 | + return | ||
142 | +} | ||
143 | + | ||
144 | +// DeletePosition deletes Position by Id and returns error if | ||
145 | +// the record to be deleted doesn't exist | ||
146 | +func DeletePosition(id int) (err error) { | ||
147 | + o := orm.NewOrm() | ||
148 | + v := Position{Id: id} | ||
149 | + // ascertain id exists in the database | ||
150 | + if err = o.Read(&v); err == nil { | ||
151 | + var num int64 | ||
152 | + if num, err = o.Delete(&Position{Id: id}); err == nil { | ||
153 | + fmt.Println("Number of records deleted in database:", num) | ||
154 | + } | ||
155 | + } | ||
156 | + return | ||
157 | +} |
@@ -84,3 +84,24 @@ func (e ErrWithMessage) ParseToMessage() *ResponseMessage { | @@ -84,3 +84,24 @@ func (e ErrWithMessage) ParseToMessage() *ResponseMessage { | ||
84 | Data: nil, | 84 | Data: nil, |
85 | } | 85 | } |
86 | } | 86 | } |
87 | + | ||
88 | +func SearchErr(code int) ErrorCode { | ||
89 | + return errmessge.Search(code) | ||
90 | +} | ||
91 | +func NewReturnResponse(data interface{}, eRR error) *ResponseMessage { | ||
92 | + var msg *ResponseMessage | ||
93 | + if eRR == nil { | ||
94 | + msg = NewMesage(0) | ||
95 | + msg.Data = data | ||
96 | + return msg | ||
97 | + } | ||
98 | + // fmt.Println("日志:" + eRR.Error()) | ||
99 | + if x, ok := eRR.(CustomErrParse); ok { | ||
100 | + return x.ParseToMessage() | ||
101 | + } | ||
102 | + return NewMesage(1) | ||
103 | +} | ||
104 | + | ||
105 | +func BadRequestParam(code int) *ResponseMessage { | ||
106 | + return NewMesage(code) | ||
107 | +} |
@@ -41,9 +41,6 @@ func NewReturnResponse(data interface{}, eRR error) *ResponseMessage { | @@ -41,9 +41,6 @@ func NewReturnResponse(data interface{}, eRR error) *ResponseMessage { | ||
41 | return NewMesage(1) | 41 | return NewMesage(1) |
42 | } | 42 | } |
43 | 43 | ||
44 | -func BadRequestParam(code int) *ResponseMessage { | ||
45 | - return NewMesage(code) | ||
46 | -} | ||
47 | 44 | ||
48 | func InitMessageCode() { | 45 | func InitMessageCode() { |
49 | // messages := []struct { | 46 | // messages := []struct { |
-
请 注册 或 登录 后发表评论