正在显示
11 个修改的文件
包含
278 行增加
和
41 行删除
1 | +package command | ||
2 | + | ||
3 | +//AddCompanyCommand 数据来源 企业平台 推送的消息 | ||
4 | +//新建公司 | ||
5 | +type AddCompanyCommand struct { | ||
6 | + //新添加的公司 | ||
7 | + Comapany struct { | ||
8 | + Id int64 `json:"id" ` | ||
9 | + Logo string `json:"logo"` // 公司logo | ||
10 | + Name string `json:"name"` // 公司名称 | ||
11 | + Status int `json:"status"` // 公司状态,1正常 2禁用 | ||
12 | + } `json:"company"` | ||
13 | + //新添加公司时, 新建的公司主管理员 | ||
14 | + User struct { | ||
15 | + Id int64 ` json:"id"` // 用户Id | ||
16 | + Phone string `json:"phone"` // 用户账号 | ||
17 | + Avatar string `json:"avatar"` // 用户头像URL | ||
18 | + CompanyId int64 `json:"company_id"` // 公司编号 | ||
19 | + AdminType int `json:"admin_type"` // 1普通员工 2 主管理员 | ||
20 | + Name string `json:"name"` // 用户姓名 | ||
21 | + Status int `json:"status"` // 用户状态(1正常 2禁用) | ||
22 | + } `json:"user"` | ||
23 | +} |
1 | +package command | ||
2 | + | ||
3 | +//UpdateCompanyCommand 数据来源 企业平台 推送的消息 | ||
4 | +type UpdateCompanyCommand struct { | ||
5 | + Id int64 `json:"id,string"` // 公司编号 | ||
6 | + Logo string `json:"logo"` // 公司logo | ||
7 | + Name string `json:"name"` // 公司名称 | ||
8 | + Status int `json:"status"` // 公司状态,1正常 2禁用 | ||
9 | + // 公司主管理员 | ||
10 | + User struct { | ||
11 | + Id int64 ` json:"id"` // 用户Id | ||
12 | + Phone string `json:"phone"` // 用户账号 | ||
13 | + Avatar string `json:"avatar"` // 用户头像URL | ||
14 | + CompanyId int64 `json:"company_id"` // 公司编号 | ||
15 | + AdminType int `json:"admin_type"` // 1普通员工 2 主管理员 | ||
16 | + Name string `json:"name"` // 用户姓名 | ||
17 | + Status int `json:"status"` // 用户状态(1正常 2禁用) | ||
18 | + } `json:"user"` | ||
19 | +} |
1 | package company | 1 | package company |
2 | 2 | ||
3 | -type CompanyAction struct { | 3 | +type CompanyServices struct { |
4 | } | 4 | } |
5 | 5 | ||
6 | //从BusinessAdmins 接收消息,变更公司数据 | 6 | //从BusinessAdmins 接收消息,变更公司数据 |
7 | -func (c CompanyAction) BusinessAdminCompany() error { | 7 | +func (c CompanyServices) BusinessAdminCompany() error { |
8 | return nil | 8 | return nil |
9 | } | 9 | } |
10 | 10 | ||
11 | -func (c CompanyAction) AddCompany() error { | 11 | +func (c CompanyServices) AddCompany() error { |
12 | return nil | 12 | return nil |
13 | } | 13 | } |
14 | 14 | ||
15 | -func (c CompanyAction) EditCompany() error { | 15 | +func (c CompanyServices) EditCompany() error { |
16 | return nil | 16 | return nil |
17 | } | 17 | } |
18 | 18 | ||
19 | -func (c CompanyAction) ChangeAdmin() error { | 19 | +func (c CompanyServices) ChangeAdmin() error { |
20 | return nil | 20 | return nil |
21 | } | 21 | } |
22 | 22 | ||
23 | -func (c CompanyAction) SetCompanyCharge() error { | 23 | +func (c CompanyServices) SetCompanyCharge() error { |
24 | return nil | 24 | return nil |
25 | } | 25 | } |
@@ -9,11 +9,12 @@ type Company struct { | @@ -9,11 +9,12 @@ type Company struct { | ||
9 | Status int //公司状态,1正常 2禁用 | 9 | Status int //公司状态,1正常 2禁用 |
10 | UpdateAt time.Time //更新时间 | 10 | UpdateAt time.Time //更新时间 |
11 | CreatedAt time.Time //创建时间 | 11 | CreatedAt time.Time //创建时间 |
12 | + DeleteAt *time.Time | ||
12 | } | 13 | } |
13 | 14 | ||
14 | type CompanyRepository interface { | 15 | type CompanyRepository interface { |
15 | Save(company *Company) (*Company, error) | 16 | Save(company *Company) (*Company, error) |
16 | Remove(company *Company) (*Company, error) | 17 | Remove(company *Company) (*Company, error) |
17 | FindOne(queryOptions map[string]interface{}) (*Company, error) | 18 | FindOne(queryOptions map[string]interface{}) (*Company, error) |
18 | - Find(queryOptions map[string]interface{}) (int64, []*Company, error) | 19 | + Find(queryOptions map[string]interface{}) (int, []*Company, error) |
19 | } | 20 | } |
@@ -3,21 +3,23 @@ package domain | @@ -3,21 +3,23 @@ package domain | ||
3 | import "time" | 3 | import "time" |
4 | 4 | ||
5 | type User struct { | 5 | type User struct { |
6 | - UserId int64 // 用户Id | ||
7 | - UserAccount string // 用户账号 | ||
8 | - UserAvatarUrl string // 用户头像URL | ||
9 | - CompanyId int64 // 公司编号 | ||
10 | - IsPrincipal bool // 是否公司负责人 | ||
11 | - AdminType int // 1普通员工 2 主管理员 | ||
12 | - UserName string // 用户姓名 | ||
13 | - UserRoleId int64 // 用户角色id | ||
14 | - UserStatus int // 用户状态(1正常 2禁用) | ||
15 | - UpdateAt time.Time // 更新时间 | 6 | + Id int64 // 用户Id |
7 | + Account string // 用户账号 | ||
8 | + AvatarUrl string // 用户头像URL | ||
9 | + CompanyId int64 // 公司编号 | ||
10 | + IsPrincipal bool // 是否公司负责人 | ||
11 | + AdminType int // 1普通员工 2 主管理员 | ||
12 | + Name string // 用户姓名 | ||
13 | + RoleId int64 // 用户角色id | ||
14 | + Status int // 用户状态(1正常 2禁用) | ||
15 | + UpdateAt time.Time // 更新时间 | ||
16 | + DeleteAt *time.Time | ||
17 | + CreateAt time.Time | ||
16 | } | 18 | } |
17 | 19 | ||
18 | type UserRepository interface { | 20 | type UserRepository interface { |
19 | Save(user *User) (*User, error) | 21 | Save(user *User) (*User, error) |
20 | Remove(user *User) (*User, error) | 22 | Remove(user *User) (*User, error) |
21 | FindOne(queryOptions map[string]interface{}) (*User, error) | 23 | FindOne(queryOptions map[string]interface{}) (*User, error) |
22 | - Find(queryOptions map[string]interface{}) (int64, []*User, error) | 24 | + Find(queryOptions map[string]interface{}) (int, []*User, error) |
23 | } | 25 | } |
pkg/infrastructure/pg/models/comapny.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import "time" | ||
4 | + | ||
5 | +type Company struct { | ||
6 | + tableName struct{} `pg:"company"` | ||
7 | + Id int64 `pg:"pk:id"` //公司id | ||
8 | + Logo string //公司logo | ||
9 | + Name string //公司名称 | ||
10 | + Status int //公司状态,1正常 2禁用 | ||
11 | + UpdateAt time.Time //更新时间 | ||
12 | + CreateAt time.Time //创建时间 | ||
13 | + DeleteAt *time.Time //删除时间 | ||
14 | +} |
pkg/infrastructure/pg/models/user.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import "time" | ||
4 | + | ||
5 | +type User struct { | ||
6 | + tableName struct{} `pg:"user"` | ||
7 | + Id int64 `pg:"pk:id"` // 用户Id | ||
8 | + Account string // 用户账号 | ||
9 | + AvatarUrl string // 用户头像URL | ||
10 | + CompanyId int64 // 公司编号 | ||
11 | + IsPrincipal bool // 是否公司负责人 | ||
12 | + AdminType int // 1普通员工 2 主管理员 | ||
13 | + Name string // 用户姓名 | ||
14 | + RoleId int64 // 用户角色id | ||
15 | + Status int // 用户状态(1正常 2禁用) | ||
16 | + UpdateAt time.Time // 更新时间 | ||
17 | + CreateAt time.Time // 创建时间 | ||
18 | + DeleteAt *time.Time // 删除时间 | ||
19 | +} |
@@ -2,9 +2,12 @@ package repository | @@ -2,9 +2,12 @@ package repository | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "errors" | 4 | "errors" |
5 | + "time" | ||
5 | 6 | ||
7 | + "github.com/go-pg/pg/v10" | ||
6 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | 9 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" |
10 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models" | ||
8 | ) | 11 | ) |
9 | 12 | ||
10 | type CompanyRepository struct { | 13 | type CompanyRepository struct { |
@@ -13,24 +16,88 @@ type CompanyRepository struct { | @@ -13,24 +16,88 @@ type CompanyRepository struct { | ||
13 | 16 | ||
14 | var _ domain.CompanyRepository = (*CompanyRepository)(nil) | 17 | var _ domain.CompanyRepository = (*CompanyRepository)(nil) |
15 | 18 | ||
16 | -func (repo *CompanyRepository) Save(user *domain.Company) (*domain.Company, error) { | ||
17 | - return user, nil | 19 | +func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error) { |
20 | + companyModel := models.Company{ | ||
21 | + Id: u.Id, | ||
22 | + Logo: u.Logo, | ||
23 | + Name: u.Name, | ||
24 | + Status: u.Status, | ||
25 | + UpdateAt: u.UpdateAt, | ||
26 | + CreateAt: u.CreatedAt, | ||
27 | + DeleteAt: u.DeleteAt, | ||
28 | + } | ||
29 | + tx := repo.transactionContext.PgTx | ||
30 | + if companyModel.Id == 0 { | ||
31 | + _, err := tx.Model(&companyModel).Insert() | ||
32 | + if err != nil { | ||
33 | + return nil, err | ||
34 | + } | ||
35 | + u.Id = companyModel.Id | ||
36 | + } else { | ||
37 | + _, err := tx.Model(&companyModel).WherePK().Update() | ||
38 | + if err != nil { | ||
39 | + return nil, err | ||
40 | + } | ||
41 | + } | ||
42 | + return u, nil | ||
18 | } | 43 | } |
19 | 44 | ||
20 | -func (repo *CompanyRepository) Remove(user *domain.Company) (*domain.Company, error) { | ||
21 | - // tx := repository.transactionContext.PgTx | ||
22 | - // userModel := new(models.Company) | ||
23 | - // userModel.UserId = user.Identify().(int64) | ||
24 | - // if _, err := tx.Model(userModel).WherePK().Delete(); err != nil { | ||
25 | - // return user, err | ||
26 | - // } | ||
27 | - return user, errors.New("no support") | 45 | +func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) { |
46 | + nowTime := time.Now() | ||
47 | + u.DeleteAt = &nowTime | ||
48 | + _, err := repo.Save(u) | ||
49 | + return u, err | ||
28 | } | 50 | } |
29 | 51 | ||
30 | func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) { | 52 | func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) { |
31 | - return nil, nil | 53 | + tx := repo.transactionContext.PgTx |
54 | + companyModel := models.Company{} | ||
55 | + query := tx.Model(&companyModel) | ||
56 | + if v, ok := queryOptions["id"]; ok { | ||
57 | + query.Where("id=?", v) | ||
58 | + } | ||
59 | + err := query.First() | ||
60 | + if err == pg.ErrNoRows { | ||
61 | + return nil, errors.New("company 不存在") | ||
62 | + } | ||
63 | + if err != nil { | ||
64 | + return nil, err | ||
65 | + } | ||
66 | + result := repo.TransformToCompanyDomain(&companyModel) | ||
67 | + return result, nil | ||
32 | } | 68 | } |
33 | 69 | ||
34 | -func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Company, error) { | ||
35 | - return 0, nil, nil | 70 | +func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Company, error) { |
71 | + tx := repo.transactionContext.PgTx | ||
72 | + companyModel := []models.Company{} | ||
73 | + query := tx.Model(&companyModel). | ||
74 | + Limit(20) | ||
75 | + if v, ok := queryOptions["limit"]; ok { | ||
76 | + query.Limit(v.(int)) | ||
77 | + } | ||
78 | + if v, ok := queryOptions["offset"]; ok { | ||
79 | + query.Offset(v.(int)) | ||
80 | + } | ||
81 | + cnt, err := query.SelectAndCount() | ||
82 | + if err != nil { | ||
83 | + return 0, nil, err | ||
84 | + } | ||
85 | + var resultList []*domain.Company | ||
86 | + for i := range companyModel { | ||
87 | + result := repo.TransformToCompanyDomain(&companyModel[i]) | ||
88 | + resultList = append(resultList, result) | ||
89 | + } | ||
90 | + return cnt, resultList, nil | ||
91 | +} | ||
92 | + | ||
93 | +func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company { | ||
94 | + return &domain.Company{ | ||
95 | + Id: m.Id, | ||
96 | + Logo: m.Logo, | ||
97 | + Name: m.Name, | ||
98 | + Status: m.Status, | ||
99 | + UpdateAt: m.UpdateAt, | ||
100 | + CreatedAt: m.CreateAt, | ||
101 | + DeleteAt: m.DeleteAt, | ||
102 | + } | ||
36 | } | 103 | } |
@@ -2,9 +2,12 @@ package repository | @@ -2,9 +2,12 @@ package repository | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "errors" | 4 | "errors" |
5 | + "time" | ||
5 | 6 | ||
7 | + "github.com/go-pg/pg/v10" | ||
6 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 8 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | 9 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" |
10 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models" | ||
8 | ) | 11 | ) |
9 | 12 | ||
10 | type UserRepository struct { | 13 | type UserRepository struct { |
@@ -14,23 +17,97 @@ type UserRepository struct { | @@ -14,23 +17,97 @@ type UserRepository struct { | ||
14 | var _ domain.UserRepository = (*UserRepository)(nil) | 17 | var _ domain.UserRepository = (*UserRepository)(nil) |
15 | 18 | ||
16 | func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) { | 19 | func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) { |
20 | + userModel := models.User{ | ||
21 | + Id: user.Id, | ||
22 | + Account: user.Account, | ||
23 | + AvatarUrl: user.AvatarUrl, | ||
24 | + CompanyId: user.CompanyId, | ||
25 | + IsPrincipal: user.IsPrincipal, | ||
26 | + AdminType: user.AdminType, | ||
27 | + Name: user.Name, | ||
28 | + RoleId: user.RoleId, | ||
29 | + Status: user.Status, | ||
30 | + UpdateAt: user.UpdateAt, | ||
31 | + CreateAt: user.CreateAt, | ||
32 | + DeleteAt: user.DeleteAt, | ||
33 | + } | ||
34 | + tx := repo.transactionContext.PgTx | ||
35 | + if userModel.Id == 0 { | ||
36 | + _, err := tx.Model(&userModel).Insert() | ||
37 | + if err != nil { | ||
38 | + return nil, err | ||
39 | + } | ||
40 | + user.Id = userModel.Id | ||
41 | + } else { | ||
42 | + _, err := tx.Model(&userModel).WherePK().Update() | ||
43 | + if err != nil { | ||
44 | + return nil, err | ||
45 | + } | ||
46 | + } | ||
17 | return user, nil | 47 | return user, nil |
18 | } | 48 | } |
19 | 49 | ||
20 | func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) { | 50 | func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) { |
21 | - // tx := repository.transactionContext.PgTx | ||
22 | - // userModel := new(models.User) | ||
23 | - // userModel.UserId = user.Identify().(int64) | ||
24 | - // if _, err := tx.Model(userModel).WherePK().Delete(); err != nil { | ||
25 | - // return user, err | ||
26 | - // } | ||
27 | - return user, errors.New("no support") | 51 | + nowTime := time.Now() |
52 | + user.DeleteAt = &nowTime | ||
53 | + _, err := repo.Save(user) | ||
54 | + return user, err | ||
28 | } | 55 | } |
29 | 56 | ||
30 | func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) { | 57 | func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) { |
31 | - return nil, nil | 58 | + tx := repo.transactionContext.PgTx |
59 | + userModel := models.User{} | ||
60 | + query := tx.Model(&userModel) | ||
61 | + if v, ok := queryOptions["id"]; ok { | ||
62 | + query.Where("id=?", v) | ||
63 | + } | ||
64 | + err := query.First() | ||
65 | + if err == pg.ErrNoRows { | ||
66 | + return nil, errors.New("user 不存在") | ||
67 | + } | ||
68 | + if err != nil { | ||
69 | + return nil, err | ||
70 | + } | ||
71 | + result := repo.TransformToUserDomain(&userModel) | ||
72 | + return result, nil | ||
32 | } | 73 | } |
33 | 74 | ||
34 | -func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.User, error) { | ||
35 | - return 0, nil, nil | 75 | +func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*domain.User, error) { |
76 | + tx := repo.transactionContext.PgTx | ||
77 | + userModel := []models.User{} | ||
78 | + query := tx.Model(&userModel). | ||
79 | + Limit(20) | ||
80 | + if v, ok := queryOptions["limit"]; ok { | ||
81 | + query.Limit(v.(int)) | ||
82 | + } | ||
83 | + if v, ok := queryOptions["offset"]; ok { | ||
84 | + query.Offset(v.(int)) | ||
85 | + } | ||
86 | + cnt, err := query.SelectAndCount() | ||
87 | + if err != nil { | ||
88 | + return 0, nil, err | ||
89 | + } | ||
90 | + var resultList []*domain.User | ||
91 | + for i := range userModel { | ||
92 | + result := repo.TransformToUserDomain(&userModel[i]) | ||
93 | + resultList = append(resultList, result) | ||
94 | + } | ||
95 | + return cnt, resultList, nil | ||
96 | +} | ||
97 | + | ||
98 | +func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User { | ||
99 | + return &domain.User{ | ||
100 | + Id: user.Id, | ||
101 | + Account: user.Account, | ||
102 | + AvatarUrl: user.AvatarUrl, | ||
103 | + CompanyId: user.CompanyId, | ||
104 | + IsPrincipal: user.IsPrincipal, | ||
105 | + AdminType: user.AdminType, | ||
106 | + Name: user.Name, | ||
107 | + RoleId: user.RoleId, | ||
108 | + Status: user.Status, | ||
109 | + UpdateAt: user.UpdateAt, | ||
110 | + CreateAt: user.CreateAt, | ||
111 | + DeleteAt: user.DeleteAt, | ||
112 | + } | ||
36 | } | 113 | } |
-
请 注册 或 登录 后发表评论