Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/opp into dev
正在显示
20 个修改的文件
包含
894 行增加
和
6 行删除
1 | [dev] | 1 | [dev] |
2 | #数据库相关 | 2 | #数据库相关 |
3 | mysql_user = "${MYSQL_USER||root}" | 3 | mysql_user = "${MYSQL_USER||root}" |
4 | -mysql_password = "${MYSQL_PASSWORD||123456}" | ||
5 | -mysql_host = "${MYSQL_HOST||192.168.100.102}" | 4 | +mysql_password = "${MYSQL_PASSWORD||sutianxia2015}" |
5 | +mysql_host = "${MYSQL_HOST||115.29.205.99}" | ||
6 | mysql_port = "${MYSQL_PORT||3306}" | 6 | mysql_port = "${MYSQL_PORT||3306}" |
7 | mysql_db_name = "${MYSQL_DB_NAME||opportunity}" | 7 | mysql_db_name = "${MYSQL_DB_NAME||opportunity}" |
8 | 8 |
controllers/v1/commend.go
0 → 100644
1 | +package v1 | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
6 | + "opp/controllers" | ||
7 | + "opp/protocol" | ||
8 | + "opp/services/commend" | ||
9 | +) | ||
10 | + | ||
11 | +type CommendController struct { | ||
12 | + controllers.BaseController | ||
13 | +} | ||
14 | + | ||
15 | +//Commend | ||
16 | +// @router /company [post] | ||
17 | +func (this *CommendController) Company() { | ||
18 | + var msg *protocol.ResponseMessage | ||
19 | + defer func() { | ||
20 | + this.Resp(msg) | ||
21 | + }() | ||
22 | + var request *protocol.CommendRequest | ||
23 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
24 | + log.Error(err) | ||
25 | + msg = protocol.BadRequestParam(1) | ||
26 | + return | ||
27 | + } | ||
28 | + if b, m := this.Valid(request); !b { | ||
29 | + msg = m | ||
30 | + return | ||
31 | + } | ||
32 | + header := controllers.GetRequestHeader(this.Ctx) | ||
33 | + msg = protocol.NewReturnResponse(commend.Commend(header, request)) | ||
34 | +} |
go.mod
100755 → 100644
@@ -7,6 +7,7 @@ require ( | @@ -7,6 +7,7 @@ require ( | ||
7 | github.com/go-sql-driver/mysql v1.4.1 | 7 | github.com/go-sql-driver/mysql v1.4.1 |
8 | github.com/gorilla/websocket v1.4.1 | 8 | github.com/gorilla/websocket v1.4.1 |
9 | github.com/prometheus/client_golang v1.1.0 | 9 | github.com/prometheus/client_golang v1.1.0 |
10 | + github.com/prometheus/common v0.6.0 | ||
10 | github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect | 11 | github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect |
11 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 | 12 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 |
12 | google.golang.org/appengine v1.6.2 // indirect | 13 | google.golang.org/appengine v1.6.2 // indirect |
internal/repository/commend.go
0 → 100644
1 | +package repository | ||
2 | + | ||
3 | +import "opp/models" | ||
4 | + | ||
5 | +type ICommendRepository interface { | ||
6 | + GetCommends(companyId int, llastId int, pageSize int) (v []*models.Commend, total int, err error) | ||
7 | +} | ||
8 | + | ||
9 | +var _ ICommendRepository = (*CommendRepository)(nil) | ||
10 | + | ||
11 | +type CommendRepository struct{} | ||
12 | + | ||
13 | +func (r *CommendRepository) GetCommends(companyId int, lastId int, pageSize int) (v []*models.Commend, total int, err error) { | ||
14 | + return models.GetCommends(companyId, lastId, pageSize) | ||
15 | +} |
internal/repository/company.go
0 → 100644
1 | +package repository | ||
2 | + | ||
3 | +import "opp/models" | ||
4 | + | ||
5 | +type ICompanyRepository interface { | ||
6 | + GetCompanyById(companyId int64) (v *models.Company, err error) | ||
7 | +} | ||
8 | + | ||
9 | +var _ ICompanyRepository = (*CompanyRepository)(nil) | ||
10 | + | ||
11 | +type CompanyRepository struct{} | ||
12 | + | ||
13 | +func (r *CompanyRepository) GetCompanyById(companyId int64) (v *models.Company, err error) { | ||
14 | + return models.GetCompanyById(companyId) | ||
15 | +} |
internal/repository/department.go
0 → 100644
1 | +package repository | ||
2 | + | ||
3 | +import "opp/models" | ||
4 | + | ||
5 | +type IDepartmentRepository interface { | ||
6 | + GetDepartmentById(companyId int) (v *models.Department, err error) | ||
7 | +} | ||
8 | + | ||
9 | +var _ IDepartmentRepository = (*DepartmentRepository)(nil) | ||
10 | + | ||
11 | +type DepartmentRepository struct{} | ||
12 | + | ||
13 | +func (r *DepartmentRepository) GetDepartmentById(id int) (v *models.Department, err error) { | ||
14 | + return models.GetDepartmentById(id) | ||
15 | +} |
internal/repository/position.go
0 → 100644
1 | +package repository | ||
2 | + | ||
3 | +import "opp/models" | ||
4 | + | ||
5 | +type IPositionRepository interface { | ||
6 | + GetPositionById(companyId int) (v *models.Position, err error) | ||
7 | +} | ||
8 | + | ||
9 | +var _ IPositionRepository = (*PositionRepository)(nil) | ||
10 | + | ||
11 | +type PositionRepository struct{} | ||
12 | + | ||
13 | +func (r *PositionRepository) GetPositionById(id int) (v *models.Position, err error) { | ||
14 | + return models.GetPositionById(id) | ||
15 | +} |
@@ -4,9 +4,13 @@ import "sync" | @@ -4,9 +4,13 @@ import "sync" | ||
4 | 4 | ||
5 | //全局 | 5 | //全局 |
6 | var ( | 6 | var ( |
7 | - once sync.Once | ||
8 | - User IUserRepository | ||
9 | - UserAuth IUserAuthRepository | 7 | + once sync.Once |
8 | + User IUserRepository | ||
9 | + UserAuth IUserAuthRepository | ||
10 | + Commend ICommendRepository | ||
11 | + Company ICompanyRepository | ||
12 | + Department IDepartmentRepository | ||
13 | + Position IPositionRepository | ||
10 | ) | 14 | ) |
11 | 15 | ||
12 | func init() { | 16 | func init() { |
@@ -16,6 +20,10 @@ func init() { | @@ -16,6 +20,10 @@ func init() { | ||
16 | func InitRepository() { | 20 | func InitRepository() { |
17 | User = &UserRepository{} | 21 | User = &UserRepository{} |
18 | UserAuth = &UserAuthRepository{} | 22 | UserAuth = &UserAuthRepository{} |
23 | + Commend = &CommendRepository{} | ||
24 | + Company = &CompanyRepository{} | ||
25 | + Department = &DepartmentRepository{} | ||
26 | + Position = &PositionRepository{} | ||
19 | } | 27 | } |
20 | 28 | ||
21 | func InitRepositoryMock() { | 29 | func InitRepositoryMock() { |
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 | +} |
protocol/aggregation.go
0 → 100644
protocol/commend.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +/*公司表彰*/ | ||
4 | +type CommendRequest struct { | ||
5 | + LastId int `json:"lastId"` | ||
6 | + PageSize int `json:"pageSize" valid:"Required"` | ||
7 | +} | ||
8 | + | ||
9 | +type CommendResponse struct { | ||
10 | + Lists []CommendInfo `json:"lists"` | ||
11 | + Total int `json:"total"` | ||
12 | +} | ||
13 | + | ||
14 | +type CommendInfo struct { | ||
15 | + Id int64 `json:"cid"` //表彰编号 | ||
16 | + Content string `json:"content"` //表彰内容 | ||
17 | + Company string `json:"company"` //公司名 | ||
18 | + CommendAt int64 `json:"getTime"` //表彰时间 | ||
19 | + Honored HonoredUserInfo `json:"honored"` //证书获得者 信息 | ||
20 | +} | ||
21 | + | ||
22 | +type HonoredUserInfo struct { | ||
23 | + UserId int64 `json:"uid"` //用户id | ||
24 | + NickName string `json:"uname"` //用户名 | ||
25 | + Department string `json:"department"` //部门 | ||
26 | + Position string `json:"position"` //岗位 | ||
27 | +} |
@@ -20,6 +20,7 @@ var errmessge ErrorMap = map[int]string{ | @@ -20,6 +20,7 @@ var errmessge ErrorMap = map[int]string{ | ||
20 | 4142: "Uuid已存在,请求失败", | 20 | 4142: "Uuid已存在,请求失败", |
21 | } | 21 | } |
22 | 22 | ||
23 | + | ||
23 | func InitMessageCode() { | 24 | func InitMessageCode() { |
24 | // messages := []struct { | 25 | // messages := []struct { |
25 | // Code int | 26 | // Code int |
@@ -47,6 +47,14 @@ func init() { | @@ -47,6 +47,14 @@ func init() { | ||
47 | MethodParams: param.Make(), | 47 | MethodParams: param.Make(), |
48 | Params: nil}) | 48 | Params: nil}) |
49 | 49 | ||
50 | + beego.GlobalControllerRouter["opp/controllers/v1:CommendController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:CommendController"], | ||
51 | + beego.ControllerComments{ | ||
52 | + Method: "Company", | ||
53 | + Router: `/company`, | ||
54 | + AllowHTTPMethods: []string{"post"}, | ||
55 | + MethodParams: param.Make(), | ||
56 | + Params: nil}) | ||
57 | + | ||
50 | beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"], | 58 | beego.GlobalControllerRouter["opp/controllers/v1:UploadController"] = append(beego.GlobalControllerRouter["opp/controllers/v1:UploadController"], |
51 | beego.ControllerComments{ | 59 | beego.ControllerComments{ |
52 | Method: "Image", | 60 | Method: "Image", |
@@ -15,6 +15,7 @@ func init() { | @@ -15,6 +15,7 @@ func init() { | ||
15 | beego.NSNamespace("auth", beego.NSInclude(&v1.AuthController{})), | 15 | beego.NSNamespace("auth", beego.NSInclude(&v1.AuthController{})), |
16 | beego.NSNamespace("upload", beego.NSInclude(&v1.UploadController{})), | 16 | beego.NSNamespace("upload", beego.NSInclude(&v1.UploadController{})), |
17 | beego.NSNamespace("version", beego.NSInclude(&v1.VersionController{})), | 17 | beego.NSNamespace("version", beego.NSInclude(&v1.VersionController{})), |
18 | + beego.NSNamespace("commend", beego.NSInclude(&v1.CommendController{})), | ||
18 | ) | 19 | ) |
19 | beego.AddNamespace(nsV1) | 20 | beego.AddNamespace(nsV1) |
20 | beego.SetStaticPath("/file/ab", beego.AppConfig.String("source_path")) | 21 | beego.SetStaticPath("/file/ab", beego.AppConfig.String("source_path")) |
services/agg/aggregation.go
0 → 100644
1 | +package agg | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
5 | + "opp/internal/repository" | ||
6 | + "opp/protocol" | ||
7 | + "sync" | ||
8 | +) | ||
9 | + | ||
10 | +func GetUserBaseInfo(uid int64) (v *protocol.UserBaseInfoAggregation, err error) { | ||
11 | + v = &protocol.UserBaseInfoAggregation{} | ||
12 | + var wg sync.WaitGroup | ||
13 | + if v.User, err = repository.User.GetUsersById(uid); err != nil { | ||
14 | + log.Error(err) | ||
15 | + return | ||
16 | + } | ||
17 | + wg.Add(3) | ||
18 | + | ||
19 | + go func() { | ||
20 | + defer wg.Done() | ||
21 | + if v.Company, err = repository.Company.GetCompanyById(int64(v.User.CompanyId)); err != nil { | ||
22 | + log.Error(err) | ||
23 | + return | ||
24 | + } | ||
25 | + }() | ||
26 | + | ||
27 | + go func() { | ||
28 | + defer wg.Done() | ||
29 | + if v.Department, err = repository.Department.GetDepartmentById(v.User.DepartmentId); err != nil { | ||
30 | + log.Error(err) | ||
31 | + return | ||
32 | + } | ||
33 | + }() | ||
34 | + | ||
35 | + go func() { | ||
36 | + defer wg.Done() | ||
37 | + if v.Position, err = repository.Position.GetPositionById(v.User.PositionId); err != nil { | ||
38 | + log.Error(err) | ||
39 | + return | ||
40 | + } | ||
41 | + }() | ||
42 | + wg.Wait() | ||
43 | + return | ||
44 | +} |
services/commend/commend.go
0 → 100644
1 | +package commend | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" | ||
5 | + "opp/internal/repository" | ||
6 | + "opp/models" | ||
7 | + "opp/protocol" | ||
8 | + "opp/services/agg" | ||
9 | +) | ||
10 | + | ||
11 | +func Commend(header *protocol.RequestHeader, request *protocol.CommendRequest) (rsp *protocol.CommendResponse, err error) { | ||
12 | + var ( | ||
13 | + userBaseInfo *protocol.UserBaseInfoAggregation | ||
14 | + user *models.User | ||
15 | + commends []*models.Commend | ||
16 | + ) | ||
17 | + rsp = &protocol.CommendResponse{} | ||
18 | + user, err = repository.User.GetUsersById(header.Uid) | ||
19 | + if err != nil { | ||
20 | + return | ||
21 | + } | ||
22 | + if commends, rsp.Total, err = repository.Commend.GetCommends(user.CompanyId, request.LastId, request.PageSize); err != nil { | ||
23 | + return | ||
24 | + } | ||
25 | + for i := 0; i < len(commends); i++ { | ||
26 | + c := commends[i] | ||
27 | + userBaseInfo, err = agg.GetUserBaseInfo(c.UserId) | ||
28 | + if err != nil { | ||
29 | + continue | ||
30 | + } | ||
31 | + user = userBaseInfo.User | ||
32 | + cInfo := protocol.CommendInfo{ | ||
33 | + Id: c.Id, | ||
34 | + Content: c.Content, | ||
35 | + Company: userBaseInfo.Company.Name, | ||
36 | + CommendAt: time.GetUnixTimeByNDayUnix(c.CommendAt.Unix(), 0), | ||
37 | + Honored: protocol.HonoredUserInfo{ | ||
38 | + UserId: c.UserId, | ||
39 | + NickName: user.NickName, | ||
40 | + Department: userBaseInfo.Department.Name, | ||
41 | + Position: userBaseInfo.Position.Name, | ||
42 | + }, | ||
43 | + } | ||
44 | + rsp.Lists = append(rsp.Lists, cInfo) | ||
45 | + } | ||
46 | + return | ||
47 | +} |
-
请 注册 或 登录 后发表评论