正在显示
12 个修改的文件
包含
0 行增加
和
873 行删除
1 | -package command | ||
2 | - | ||
3 | -import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" | ||
4 | - | ||
5 | -type LoginBySecretKeyCommand struct { | ||
6 | - Secret string `json:"secret"` | ||
7 | -} | ||
8 | - | ||
9 | -func (login LoginBySecretKeyCommand) ValidateCommand() error { | ||
10 | - if len(login.Secret) == 0 { | ||
11 | - return lib.ThrowError(lib.ARG_ERROR, "登录参数错误") | ||
12 | - } | ||
13 | - return nil | ||
14 | -} |
1 | -package command | ||
2 | - | ||
3 | -import ( | ||
4 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" | ||
5 | -) | ||
6 | - | ||
7 | -type SaveAdminUserCommand struct { | ||
8 | - Id int64 `json:"id"` | ||
9 | - // 员工姓名 | ||
10 | - Name string `json:"name"` | ||
11 | - // 员工账号 | ||
12 | - Account string `json:"account" ` | ||
13 | - //密码 | ||
14 | - Password string `json:"password"` | ||
15 | - // 员工角色 | ||
16 | - PermissionId []int64 `json:"PermissionId"` | ||
17 | - | ||
18 | - IsUsable bool `json:"isUsable"` | ||
19 | -} | ||
20 | - | ||
21 | -func (command SaveAdminUserCommand) ValidateCommand() error { | ||
22 | - if len(command.Name) == 0 { | ||
23 | - return lib.ThrowError(lib.ARG_ERROR, "用户名称必填") | ||
24 | - } | ||
25 | - if len(command.Account) == 0 { | ||
26 | - return lib.ThrowError(lib.ARG_ERROR, "账号必填") | ||
27 | - } | ||
28 | - return nil | ||
29 | -} |
1 | -package command | ||
2 | - | ||
3 | -import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" | ||
4 | - | ||
5 | -//UpdateAdminUserPwdCommand 修改密码 | ||
6 | -type UpdateAdminUserPwdCommand struct { | ||
7 | - Id int64 `json:"id"` | ||
8 | - //密码 | ||
9 | - Password string `json:"password"` | ||
10 | -} | ||
11 | - | ||
12 | -func (command UpdateAdminUserPwdCommand) ValidateCommand() error { | ||
13 | - if len(command.Password) == 0 { | ||
14 | - return lib.ThrowError(lib.ARG_ERROR, "密码必填") | ||
15 | - } | ||
16 | - return nil | ||
17 | -} |
1 | -package service | ||
2 | - | ||
3 | -import ( | ||
4 | - "crypto/sha1" | ||
5 | - "fmt" | ||
6 | - | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command" | ||
8 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query" | ||
9 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory" | ||
10 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
11 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao" | ||
12 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" | ||
13 | -) | ||
14 | - | ||
15 | -//AdminUserService 管理员相关服务 | ||
16 | -type AdminUserService struct { | ||
17 | -} | ||
18 | - | ||
19 | -func NewAdminUserService(option map[string]interface{}) *AdminUserService { | ||
20 | - newAdminUserService := new(AdminUserService) | ||
21 | - return newAdminUserService | ||
22 | -} | ||
23 | - | ||
24 | -func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAdminUserQuery) (*domain.AdminUser, error) { | ||
25 | - //实际业务 | ||
26 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
27 | - if err != nil { | ||
28 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
29 | - } | ||
30 | - var ( | ||
31 | - adminuserRepository domain.AdminUserRepository | ||
32 | - adminuser *domain.AdminUser | ||
33 | - ) | ||
34 | - if value, err := factory.CreateAdminUserRepository(map[string]interface{}{ | ||
35 | - "transactionContext": transactionContext, | ||
36 | - }); err != nil { | ||
37 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
38 | - } else { | ||
39 | - adminuserRepository = value | ||
40 | - } | ||
41 | - adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{ | ||
42 | - AccountEqual: getAdminUserQuery.AdminAccount, | ||
43 | - AdminUserId: getAdminUserQuery.Id, | ||
44 | - }) | ||
45 | - if err != nil { | ||
46 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
47 | - } | ||
48 | - return adminuser, nil | ||
49 | -} | ||
50 | - | ||
51 | -func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdminUserCommand) (*domain.AdminUser, error) { | ||
52 | - if err := saveUserCmd.ValidateCommand(); err != nil { | ||
53 | - return nil, lib.ThrowError(lib.ARG_ERROR, err.Error()) | ||
54 | - } | ||
55 | - //实际业务 | ||
56 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
57 | - if err != nil { | ||
58 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
59 | - } | ||
60 | - if err := transactionContext.StartTransaction(); err != nil { | ||
61 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
62 | - } | ||
63 | - defer func() { | ||
64 | - transactionContext.RollbackTransaction() | ||
65 | - }() | ||
66 | - var ( | ||
67 | - adminuserRepository domain.AdminUserRepository | ||
68 | - adminuser *domain.AdminUser | ||
69 | - ) | ||
70 | - if value, err := factory.CreateAdminUserRepository(map[string]interface{}{ | ||
71 | - "transactionContext": transactionContext, | ||
72 | - }); err != nil { | ||
73 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
74 | - } else { | ||
75 | - adminuserRepository = value | ||
76 | - } | ||
77 | - | ||
78 | - //获取权限 | ||
79 | - var ( | ||
80 | - permissionRepository domain.AdminPermissionRepository | ||
81 | - permissions []domain.AdminPermission | ||
82 | - ) | ||
83 | - if value, err := factory.CreateAdminPermissionRepository(map[string]interface{}{ | ||
84 | - "transactionContext": transactionContext, | ||
85 | - }); err != nil { | ||
86 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
87 | - } else { | ||
88 | - permissionRepository = value | ||
89 | - } | ||
90 | - permissions, err = permissionRepository.Find(domain.PermissionFindOption{ | ||
91 | - Ids: saveUserCmd.PermissionId, | ||
92 | - }) | ||
93 | - if err != nil { | ||
94 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
95 | - } | ||
96 | - for i := range permissions { | ||
97 | - if permissions[i].Code == domain.PERMINSSION_ADMIN_USER { | ||
98 | - return nil, lib.ThrowError(lib.BUSINESS_ERROR, "操作异常") | ||
99 | - } | ||
100 | - } | ||
101 | - permissionBases := []domain.AdminPermissionBase{} | ||
102 | - | ||
103 | - for i := range permissions { | ||
104 | - p := domain.AdminPermissionBase{ | ||
105 | - Id: permissions[i].Id, Code: permissions[i].Code, | ||
106 | - } | ||
107 | - permissionBases = append(permissionBases, p) | ||
108 | - } | ||
109 | - //账号是否有变更 | ||
110 | - var accountChange bool | ||
111 | - if saveUserCmd.Id > 0 { | ||
112 | - //更新数据 | ||
113 | - adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{ | ||
114 | - AdminUserId: saveUserCmd.Id, | ||
115 | - }) | ||
116 | - if err != nil { | ||
117 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
118 | - } | ||
119 | - if adminuser.Account != saveUserCmd.Account { | ||
120 | - accountChange = true | ||
121 | - } | ||
122 | - adminuser.Account = saveUserCmd.Account | ||
123 | - adminuser.AdminName = saveUserCmd.Name | ||
124 | - adminuser.IsUsable = saveUserCmd.IsUsable | ||
125 | - if !adminuser.IsDefault { | ||
126 | - adminuser.Permission = permissionBases | ||
127 | - } | ||
128 | - | ||
129 | - } else { | ||
130 | - //添加新数据 | ||
131 | - accountChange = true | ||
132 | - defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456"))) | ||
133 | - adminuser = &domain.AdminUser{ | ||
134 | - Id: saveUserCmd.Id, | ||
135 | - Account: saveUserCmd.Account, | ||
136 | - Password: defaultPwd, | ||
137 | - AdminName: saveUserCmd.Name, | ||
138 | - IsUsable: saveUserCmd.IsUsable, | ||
139 | - Permission: permissionBases, | ||
140 | - } | ||
141 | - } | ||
142 | - if accountChange { | ||
143 | - //检查账号是否已存在 | ||
144 | - var ( | ||
145 | - adminuserDao *dao.AdminUserDao | ||
146 | - ) | ||
147 | - if v, err := factory.CreateAdminUserkDao(map[string]interface{}{ | ||
148 | - "transactionContext": transactionContext, | ||
149 | - }); err != nil { | ||
150 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
151 | - } else { | ||
152 | - adminuserDao = v | ||
153 | - } | ||
154 | - ok, err := adminuserDao.AdminUserAccountExist(saveUserCmd.Account) | ||
155 | - if err != nil { | ||
156 | - return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
157 | - } | ||
158 | - if ok { | ||
159 | - return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在") | ||
160 | - } | ||
161 | - } | ||
162 | - | ||
163 | - adminuser, err = adminuserRepository.Save(*adminuser) | ||
164 | - if err != nil { | ||
165 | - return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
166 | - } | ||
167 | - transactionContext.CommitTransaction() | ||
168 | - return adminuser, nil | ||
169 | -} | ||
170 | - | ||
171 | -func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query.ListAdminUserQuery) ([]domain.AdminUser, int, error) { | ||
172 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
173 | - if err != nil { | ||
174 | - return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
175 | - } | ||
176 | - var ( | ||
177 | - adminuserRepository domain.AdminUserRepository | ||
178 | - adminusers []domain.AdminUser | ||
179 | - cnt int | ||
180 | - ) | ||
181 | - if value, err := factory.CreateAdminUserRepository(map[string]interface{}{ | ||
182 | - "transactionContext": transactionContext, | ||
183 | - }); err != nil { | ||
184 | - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
185 | - } else { | ||
186 | - adminuserRepository = value | ||
187 | - } | ||
188 | - adminusers, err = adminuserRepository.Find(domain.AdminUserFindQuery{ | ||
189 | - AccountLike: listAdminUserQuery.AdminAccountMatch, | ||
190 | - Offset: listAdminUserQuery.Offset, | ||
191 | - Limit: listAdminUserQuery.Limit, | ||
192 | - }) | ||
193 | - if err != nil { | ||
194 | - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
195 | - } | ||
196 | - cnt, err = adminuserRepository.CountAll(domain.AdminUserFindQuery{ | ||
197 | - AccountLike: listAdminUserQuery.AdminAccountMatch, | ||
198 | - }) | ||
199 | - if err != nil { | ||
200 | - return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
201 | - } | ||
202 | - return adminusers, cnt, nil | ||
203 | -} | ||
204 | - | ||
205 | -func (adminUserSrv AdminUserService) UpdateAdminPassword(updatecmd command.UpdateAdminUserPwdCommand) error { | ||
206 | - if err := updatecmd.ValidateCommand(); err != nil { | ||
207 | - return lib.ThrowError(lib.ARG_ERROR, err.Error()) | ||
208 | - } | ||
209 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
210 | - if err != nil { | ||
211 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
212 | - } | ||
213 | - if err := transactionContext.StartTransaction(); err != nil { | ||
214 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
215 | - } | ||
216 | - defer func() { | ||
217 | - transactionContext.RollbackTransaction() | ||
218 | - }() | ||
219 | - | ||
220 | - var ( | ||
221 | - adminuserDao *dao.AdminUserDao | ||
222 | - ) | ||
223 | - if v, err := factory.CreateAdminUserkDao(map[string]interface{}{ | ||
224 | - "transactionContext": transactionContext, | ||
225 | - }); err != nil { | ||
226 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
227 | - } else { | ||
228 | - adminuserDao = v | ||
229 | - } | ||
230 | - err = adminuserDao.UpdatePassword(updatecmd.Id, updatecmd.Password) | ||
231 | - if err != nil { | ||
232 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
233 | - } | ||
234 | - transactionContext.CommitTransaction() | ||
235 | - return nil | ||
236 | -} | ||
237 | - | ||
238 | -func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable bool) error { | ||
239 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
240 | - if err != nil { | ||
241 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
242 | - } | ||
243 | - if err := transactionContext.StartTransaction(); err != nil { | ||
244 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
245 | - } | ||
246 | - defer func() { | ||
247 | - transactionContext.RollbackTransaction() | ||
248 | - }() | ||
249 | - var ( | ||
250 | - adminuserDao *dao.AdminUserDao | ||
251 | - ) | ||
252 | - if v, err := factory.CreateAdminUserkDao(map[string]interface{}{ | ||
253 | - "transactionContext": transactionContext, | ||
254 | - }); err != nil { | ||
255 | - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) | ||
256 | - } else { | ||
257 | - adminuserDao = v | ||
258 | - } | ||
259 | - if ok, err := adminuserDao.AdminUserIsDefault(uid); err != nil { | ||
260 | - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
261 | - } else if ok { | ||
262 | - return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员") | ||
263 | - } | ||
264 | - err = adminuserDao.UpdateIsUsable(uid, isUsable) | ||
265 | - if err != nil { | ||
266 | - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) | ||
267 | - } | ||
268 | - transactionContext.CommitTransaction() | ||
269 | - return nil | ||
270 | -} |
@@ -5,14 +5,6 @@ import ( | @@ -5,14 +5,6 @@ import ( | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | 5 | "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | -func CreateAdminUserkDao(options map[string]interface{}) (*dao.AdminUserDao, error) { | ||
9 | - var transactionContext *transaction.TransactionContext | ||
10 | - if value, ok := options["transactionContext"]; ok { | ||
11 | - transactionContext = value.(*transaction.TransactionContext) | ||
12 | - } | ||
13 | - return dao.NewAdminUserDao(transactionContext) | ||
14 | -} | ||
15 | - | ||
16 | func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, error) { | 8 | func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, error) { |
17 | var transactionContext *transaction.TransactionContext | 9 | var transactionContext *transaction.TransactionContext |
18 | if value, ok := options["transactionContext"]; ok { | 10 | if value, ok := options["transactionContext"]; ok { |
@@ -15,15 +15,6 @@ func CreatePartnerInfoRepository(options map[string]interface{}) (domain.Partner | @@ -15,15 +15,6 @@ func CreatePartnerInfoRepository(options map[string]interface{}) (domain.Partner | ||
15 | return repository.NewPartnerInfoRepository(transactionContext) | 15 | return repository.NewPartnerInfoRepository(transactionContext) |
16 | } | 16 | } |
17 | 17 | ||
18 | -//CreateAdminUserRepository 管理员信息 | ||
19 | -func CreateAdminUserRepository(options map[string]interface{}) (domain.AdminUserRepository, error) { | ||
20 | - var transactionContext *transaction.TransactionContext | ||
21 | - if value, ok := options["transactionContext"]; ok { | ||
22 | - transactionContext = value.(*transaction.TransactionContext) | ||
23 | - } | ||
24 | - return repository.NewAdminUserRepository(transactionContext) | ||
25 | -} | ||
26 | - | ||
27 | //CreateAdminUserRepository 管理员权限信息 | 18 | //CreateAdminUserRepository 管理员权限信息 |
28 | func CreateAdminPermissionRepository(options map[string]interface{}) (domain.AdminPermissionRepository, error) { | 19 | func CreateAdminPermissionRepository(options map[string]interface{}) (domain.AdminPermissionRepository, error) { |
29 | var transactionContext *transaction.TransactionContext | 20 | var transactionContext *transaction.TransactionContext |
1 | -package dao | ||
2 | - | ||
3 | -import ( | ||
4 | - "fmt" | ||
5 | - | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
8 | -) | ||
9 | - | ||
10 | -type AdminUserDao struct { | ||
11 | - transactionContext *transaction.TransactionContext | ||
12 | -} | ||
13 | - | ||
14 | -func NewAdminUserDao(transactionContext *transaction.TransactionContext) (*AdminUserDao, error) { | ||
15 | - if transactionContext == nil { | ||
16 | - return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
17 | - } else { | ||
18 | - return &AdminUserDao{ | ||
19 | - transactionContext: transactionContext, | ||
20 | - }, nil | ||
21 | - } | ||
22 | -} | ||
23 | - | ||
24 | -//UpdatePassword .... | ||
25 | -func (dao *AdminUserDao) UpdatePassword(id int64, pwd string) error { | ||
26 | - tx := dao.transactionContext.PgDd | ||
27 | - m := &models.AdminUser{} | ||
28 | - err := tx.Model(m).Where("id=?", id).First() | ||
29 | - if err != nil { | ||
30 | - return err | ||
31 | - } | ||
32 | - _, err = tx.Model(m).Where("id=?", id). | ||
33 | - Set("password=?", pwd). | ||
34 | - Update() | ||
35 | - return err | ||
36 | -} | ||
37 | - | ||
38 | -//UpdateIsUsable .... | ||
39 | -func (dao *AdminUserDao) UpdateIsUsable(id int64, isUsable bool) error { | ||
40 | - tx := dao.transactionContext.PgDd | ||
41 | - m := &models.AdminUser{} | ||
42 | - err := tx.Model(m).Where("id=?", id).First() | ||
43 | - if err != nil { | ||
44 | - return err | ||
45 | - } | ||
46 | - _, err = tx.Model(m).Where("id=?", id). | ||
47 | - Set("is_usable=?", isUsable). | ||
48 | - Update() | ||
49 | - return err | ||
50 | -} | ||
51 | - | ||
52 | -//AdminUserAccountExist ... | ||
53 | -func (dao *AdminUserDao) AdminUserAccountExist(account string) (bool, error) { | ||
54 | - tx := dao.transactionContext.PgDd | ||
55 | - m := &models.AdminUser{} | ||
56 | - ok, err := tx.Model(m). | ||
57 | - Where("account=?", account). | ||
58 | - Exists() | ||
59 | - | ||
60 | - return ok, err | ||
61 | -} | ||
62 | - | ||
63 | -func (dao *AdminUserDao) AdminUserIsDefault(id int64) (bool, error) { | ||
64 | - tx := dao.transactionContext.PgDd | ||
65 | - m := &models.AdminUser{} | ||
66 | - err := tx.Model(m). | ||
67 | - Where("id=?", id). | ||
68 | - Column("is_default"). | ||
69 | - First() | ||
70 | - | ||
71 | - return m.IsDefault, err | ||
72 | -} |
1 | -package models | ||
2 | - | ||
3 | -import ( | ||
4 | - "context" | ||
5 | - "time" | ||
6 | - | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
8 | - | ||
9 | - "github.com/go-pg/pg/v10" | ||
10 | -) | ||
11 | - | ||
12 | -type AdminUser struct { | ||
13 | - tableName struct{} `pg:"admin_user,alias:admin_user"` | ||
14 | - //id | ||
15 | - Id int64 `pg:",pk"` | ||
16 | - //用户账号 | ||
17 | - Account string `pg:",unique"` | ||
18 | - //用户名称 | ||
19 | - AdminName string | ||
20 | - //账号密码 | ||
21 | - Password string | ||
22 | - //是否是默认账号 | ||
23 | - IsDefault bool `pg:",use_zero"` | ||
24 | - //账号是否可用 | ||
25 | - IsUsable bool `pg:",use_zero"` | ||
26 | - //用户的权限 | ||
27 | - Permission []domain.AdminPermissionBase | ||
28 | - | ||
29 | - CreateAt time.Time | ||
30 | - UpdateAt time.Time | ||
31 | -} | ||
32 | - | ||
33 | -var _ pg.BeforeUpdateHook = (*AdminUser)(nil) | ||
34 | - | ||
35 | -func (user *AdminUser) BeforeUpdate(ctx context.Context) (context.Context, error) { | ||
36 | - user.UpdateAt = time.Now() | ||
37 | - return ctx, nil | ||
38 | -} | ||
39 | - | ||
40 | -var _ pg.BeforeInsertHook = (*AdminUser)(nil) | ||
41 | - | ||
42 | -func (user *AdminUser) BeforeInsert(ctx context.Context) (context.Context, error) { | ||
43 | - user.CreateAt = time.Now() | ||
44 | - user.UpdateAt = time.Now() | ||
45 | - return ctx, nil | ||
46 | -} |
1 | -package repository | ||
2 | - | ||
3 | -import ( | ||
4 | - "fmt" | ||
5 | - | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
8 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
9 | -) | ||
10 | - | ||
11 | -type AdminUserRepository struct { | ||
12 | - transactionContext *transaction.TransactionContext | ||
13 | -} | ||
14 | - | ||
15 | -var ( | ||
16 | - _ domain.AdminUserRepository = (*AdminUserRepository)(nil) | ||
17 | -) | ||
18 | - | ||
19 | -func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) { | ||
20 | - if transactionContext == nil { | ||
21 | - return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
22 | - } | ||
23 | - return &AdminUserRepository{transactionContext: transactionContext}, nil | ||
24 | -} | ||
25 | -func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) { | ||
26 | - result := domain.AdminUser{ | ||
27 | - Id: adminuserModel.Id, | ||
28 | - Account: adminuserModel.Account, | ||
29 | - AdminName: adminuserModel.AdminName, | ||
30 | - IsDefault: adminuserModel.IsDefault, | ||
31 | - CreateAt: adminuserModel.CreateAt, | ||
32 | - IsUsable: adminuserModel.IsUsable, | ||
33 | - Password: adminuserModel.Password, | ||
34 | - Permission: adminuserModel.Permission, | ||
35 | - } | ||
36 | - return result, nil | ||
37 | -} | ||
38 | - | ||
39 | -func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindOneQuery) (*domain.AdminUser, error) { | ||
40 | - db := reponsitory.transactionContext.PgDd | ||
41 | - adminuserModel := new(models.AdminUser) | ||
42 | - query := db.Model(adminuserModel) | ||
43 | - | ||
44 | - if queryOption.AdminUserId > 0 { | ||
45 | - query = query.Where("id=?", queryOption.AdminUserId) | ||
46 | - } | ||
47 | - if len(queryOption.AccountEqual) > 0 { | ||
48 | - query = query.Where("account=?", queryOption.AccountEqual) | ||
49 | - } | ||
50 | - err := query.First() | ||
51 | - if err != nil { | ||
52 | - return nil, err | ||
53 | - } | ||
54 | - adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel) | ||
55 | - return &adminUser, err | ||
56 | -} | ||
57 | - | ||
58 | -func (reponsitory AdminUserRepository) updateAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) { | ||
59 | - tx := reponsitory.transactionContext.PgTx | ||
60 | - adminUserModel := &models.AdminUser{ | ||
61 | - Id: adminuser.Id, | ||
62 | - Account: adminuser.Account, | ||
63 | - AdminName: adminuser.AdminName, | ||
64 | - Password: adminuser.Password, | ||
65 | - IsUsable: adminuser.IsUsable, | ||
66 | - Permission: adminuser.Permission, | ||
67 | - } | ||
68 | - _, err := tx.Model(adminUserModel). | ||
69 | - Where("id=?", adminUserModel.Id). | ||
70 | - Column("admin_name", "account", "password", "is_usable", "permission"). | ||
71 | - Update() | ||
72 | - if err != nil { | ||
73 | - return nil, fmt.Errorf("更新用户数据失败:%s", err) | ||
74 | - } | ||
75 | - return &adminuser, nil | ||
76 | -} | ||
77 | - | ||
78 | -func (reponsitory AdminUserRepository) addAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) { | ||
79 | - tx := reponsitory.transactionContext.PgTx | ||
80 | - adminuserModel := &models.AdminUser{ | ||
81 | - Account: adminuser.Account, | ||
82 | - AdminName: adminuser.AdminName, | ||
83 | - Password: adminuser.Password, | ||
84 | - IsDefault: false, | ||
85 | - IsUsable: adminuser.IsUsable, | ||
86 | - Permission: adminuser.Permission, | ||
87 | - } | ||
88 | - //添加用户数据 | ||
89 | - _, err := tx.Model(adminuserModel).Insert() | ||
90 | - if err != nil { | ||
91 | - return nil, err | ||
92 | - } | ||
93 | - return nil, nil | ||
94 | -} | ||
95 | - | ||
96 | -func (reponsitory AdminUserRepository) Save(adminuser domain.AdminUser) (*domain.AdminUser, error) { | ||
97 | - if adminuser.Id == 0 { | ||
98 | - return reponsitory.addAdminUser(adminuser) | ||
99 | - } | ||
100 | - return reponsitory.updateAdminUser(adminuser) | ||
101 | -} | ||
102 | - | ||
103 | -func (reponsitory AdminUserRepository) Find(queryOption domain.AdminUserFindQuery) ([]domain.AdminUser, error) { | ||
104 | - db := reponsitory.transactionContext.PgDd | ||
105 | - adminuserModels := []models.AdminUser{} | ||
106 | - query := db.Model(&adminuserModels) | ||
107 | - if len(queryOption.AccountLike) > 0 { | ||
108 | - query = query.Where("account like ?", "%"+queryOption.AccountLike+"%") | ||
109 | - } | ||
110 | - | ||
111 | - if queryOption.Offset > -1 { | ||
112 | - query = query.Offset(queryOption.Offset) | ||
113 | - } | ||
114 | - if queryOption.Limit > 0 { | ||
115 | - query = query.Limit(queryOption.Limit) | ||
116 | - } else { | ||
117 | - query = query.Limit(20) | ||
118 | - } | ||
119 | - var ( | ||
120 | - err error | ||
121 | - adminuserReturn = make([]domain.AdminUser, 0) | ||
122 | - ) | ||
123 | - query = query.Order("admin_user.id DESC") | ||
124 | - err = query.Select() | ||
125 | - if err != nil { | ||
126 | - return adminuserReturn, err | ||
127 | - } | ||
128 | - for i := range adminuserModels { | ||
129 | - domainAdminUser, err := reponsitory.transformPgModelToDomainModel(&adminuserModels[i]) | ||
130 | - if err != nil { | ||
131 | - return adminuserReturn, err | ||
132 | - } | ||
133 | - adminuserReturn = append(adminuserReturn, domainAdminUser) | ||
134 | - } | ||
135 | - return adminuserReturn, nil | ||
136 | -} | ||
137 | - | ||
138 | -func (reponsitory AdminUserRepository) CountAll(queryOption domain.AdminUserFindQuery) (int, error) { | ||
139 | - db := reponsitory.transactionContext.PgDd | ||
140 | - adminuserModels := models.AdminUser{} | ||
141 | - query := db.Model(&adminuserModels) | ||
142 | - if len(queryOption.AccountLike) > 0 { | ||
143 | - query = query.Where("account like ?", "%"+queryOption.AccountLike+"%") | ||
144 | - } | ||
145 | - cnt, err := query.Count() | ||
146 | - return cnt, err | ||
147 | -} |
1 | -package controllers | ||
2 | - | ||
3 | -import ( | ||
4 | - "errors" | ||
5 | - | ||
6 | - "github.com/astaxie/beego/logs" | ||
7 | - adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query" | ||
8 | - adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service" | ||
9 | - adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command" | ||
10 | - adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query" | ||
11 | - adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service" | ||
12 | - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
13 | -) | ||
14 | - | ||
15 | -type AdminUserController struct { | ||
16 | - BaseController | ||
17 | -} | ||
18 | - | ||
19 | -////Prepare 重写 BaseController 的Prepare方法 | ||
20 | -func (c *AdminUserController) Prepare() { | ||
21 | - c.BaseController.Prepare() | ||
22 | - if ok := c.ValidJWTToken(); !ok { | ||
23 | - return | ||
24 | - } | ||
25 | - if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok { | ||
26 | - return | ||
27 | - } | ||
28 | -} | ||
29 | - | ||
30 | -type adminDetailParam struct { | ||
31 | - Id int64 `json:"id"` | ||
32 | - PermissionType []int64 `json:"permissionType"` | ||
33 | - Status int `json:"status"` | ||
34 | - Account string `json:"account"` | ||
35 | - IsAdmin int `json:"isAdmin"` | ||
36 | -} | ||
37 | - | ||
38 | -func (c *AdminUserController) SaveAdminUser() { | ||
39 | - //用与适配前端定义的数据结构 | ||
40 | - var ( | ||
41 | - param adminDetailParam | ||
42 | - err error | ||
43 | - ) | ||
44 | - if err = c.BindJsonData(¶m); err != nil { | ||
45 | - logs.Error(err) | ||
46 | - c.ResponseError(errors.New("json数据解析失败")) | ||
47 | - return | ||
48 | - } | ||
49 | - newAdminUserService := adminuserservice.NewAdminUserService(nil) | ||
50 | - cmd := &adminuserCmd.SaveAdminUserCommand{ | ||
51 | - Id: param.Id, | ||
52 | - Name: param.Account, | ||
53 | - Account: param.Account, | ||
54 | - PermissionId: param.PermissionType, | ||
55 | - IsUsable: false, | ||
56 | - } | ||
57 | - if param.Status == 1 { | ||
58 | - cmd.IsUsable = true | ||
59 | - } | ||
60 | - _, err = newAdminUserService.SaveAdminUser(cmd) | ||
61 | - if err != nil { | ||
62 | - c.ResponseError(err) | ||
63 | - return | ||
64 | - } | ||
65 | - c.ResponseData(nil) | ||
66 | - return | ||
67 | -} | ||
68 | - | ||
69 | -func (c *AdminUserController) GetAdminUser() { | ||
70 | - //用与适配前端定义的数据结构 | ||
71 | - type Paramter struct { | ||
72 | - Id int64 `json:"id"` | ||
73 | - } | ||
74 | - var ( | ||
75 | - param Paramter | ||
76 | - err error | ||
77 | - ) | ||
78 | - if err = c.BindJsonData(¶m); err != nil { | ||
79 | - logs.Error(err) | ||
80 | - c.ResponseError(errors.New("json数据解析失败")) | ||
81 | - return | ||
82 | - } | ||
83 | - newAdminUserService := adminuserservice.NewAdminUserService(nil) | ||
84 | - adminuser, err := newAdminUserService.GetAdminUser(&adminuserquery.GetAdminUserQuery{ | ||
85 | - Id: param.Id, | ||
86 | - }) | ||
87 | - if err != nil { | ||
88 | - c.ResponseError(err) | ||
89 | - return | ||
90 | - } | ||
91 | - rspData := adminDetailParam{ | ||
92 | - Id: adminuser.Id, | ||
93 | - Account: adminuser.Account, | ||
94 | - Status: 0, | ||
95 | - IsAdmin: 0, | ||
96 | - } | ||
97 | - for _, v := range adminuser.Permission { | ||
98 | - rspData.PermissionType = append(rspData.PermissionType, v.Id) | ||
99 | - } | ||
100 | - if adminuser.IsUsable { | ||
101 | - rspData.Status = 1 | ||
102 | - } | ||
103 | - if adminuser.IsDefault { | ||
104 | - rspData.IsAdmin = 1 | ||
105 | - } | ||
106 | - c.ResponseData(rspData) | ||
107 | - return | ||
108 | -} | ||
109 | - | ||
110 | -func (c *AdminUserController) ListAdminUser() { | ||
111 | - //用与适配前端定义的数据结构 | ||
112 | - type Paramter struct { | ||
113 | - SearchText string `json:"searchText"` | ||
114 | - PageSize int `json::"pageSize"` | ||
115 | - PageNumber int `json:"pageNumber"` | ||
116 | - } | ||
117 | - var ( | ||
118 | - param Paramter | ||
119 | - err error | ||
120 | - ) | ||
121 | - if err = c.BindJsonData(¶m); err != nil { | ||
122 | - logs.Error(err) | ||
123 | - c.ResponseError(errors.New("json数据解析失败")) | ||
124 | - return | ||
125 | - } | ||
126 | - if param.PageSize == 0 { | ||
127 | - param.PageSize = 20 | ||
128 | - } | ||
129 | - if param.PageNumber == 0 { | ||
130 | - param.PageNumber = 1 | ||
131 | - } | ||
132 | - newAdminUserService := adminuserservice.NewAdminUserService(nil) | ||
133 | - queryOption := &adminuserquery.ListAdminUserQuery{ | ||
134 | - AdminAccountMatch: param.SearchText, | ||
135 | - Limit: param.PageSize, | ||
136 | - Offset: param.PageSize * (param.PageNumber - 1), | ||
137 | - } | ||
138 | - adminusers, cnt, err := newAdminUserService.PageListAdminUser(queryOption) | ||
139 | - if err != nil { | ||
140 | - c.ResponseError(err) | ||
141 | - return | ||
142 | - } | ||
143 | - newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil) | ||
144 | - allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{ | ||
145 | - ParentId: 0, | ||
146 | - }) | ||
147 | - if err != nil { | ||
148 | - logs.Error("获取权限数据失败:%s", err) | ||
149 | - c.ResponseError(errors.New("服务异常")) | ||
150 | - return | ||
151 | - } | ||
152 | - permissionMap := map[int64]domain.AdminPermission{} | ||
153 | - for i := range allPermission { | ||
154 | - permissionMap[allPermission[i].Id] = allPermission[i] | ||
155 | - } | ||
156 | - listData := []map[string]interface{}{} | ||
157 | - //前端数据格式适配 | ||
158 | - for i := range adminusers { | ||
159 | - permissionTypes := []string{} | ||
160 | - for _, vv := range adminusers[i].Permission { | ||
161 | - if pm, ok := permissionMap[vv.Id]; ok { | ||
162 | - permissionTypes = append(permissionTypes, pm.Name) | ||
163 | - } | ||
164 | - } | ||
165 | - m := map[string]interface{}{ | ||
166 | - "id": adminusers[i].Id, | ||
167 | - "account": adminusers[i].Account, | ||
168 | - "permission": permissionTypes, | ||
169 | - "statue": 0, | ||
170 | - "isAdmin": 0, | ||
171 | - } | ||
172 | - if adminusers[i].IsUsable { | ||
173 | - m["statue"] = 1 | ||
174 | - } | ||
175 | - if adminusers[i].IsDefault { | ||
176 | - m["isAdmin"] = 1 | ||
177 | - } | ||
178 | - listData = append(listData, m) | ||
179 | - } | ||
180 | - c.ResponsePageList(listData, cnt, param.PageNumber) | ||
181 | - return | ||
182 | -} | ||
183 | - | ||
184 | -func (c *AdminUserController) ForbiddenAdminUser() { | ||
185 | - //用与适配前端定义的数据结构 | ||
186 | - type Paramter struct { | ||
187 | - Id int64 `json:"id"` | ||
188 | - Statue int `json:"statue"` | ||
189 | - } | ||
190 | - var ( | ||
191 | - param Paramter | ||
192 | - err error | ||
193 | - ) | ||
194 | - if err = c.BindJsonData(¶m); err != nil { | ||
195 | - logs.Error(err) | ||
196 | - c.ResponseError(errors.New("json数据解析失败")) | ||
197 | - return | ||
198 | - } | ||
199 | - newAdminUserService := adminuserservice.NewAdminUserService(nil) | ||
200 | - var isUsable bool | ||
201 | - if param.Statue == 1 { | ||
202 | - isUsable = true | ||
203 | - } else if param.Statue == 0 { | ||
204 | - isUsable = false | ||
205 | - } else { | ||
206 | - c.ResponseError(errors.New("参数错误")) | ||
207 | - return | ||
208 | - } | ||
209 | - | ||
210 | - err = newAdminUserService.UpdateAdminIsUsable(param.Id, isUsable) | ||
211 | - if err != nil { | ||
212 | - c.ResponseError(err) | ||
213 | - return | ||
214 | - } | ||
215 | - c.ResponseData(nil) | ||
216 | - return | ||
217 | -} | ||
218 | - | ||
219 | -//BeforeEditAdminUser 编辑管理员操作的前置接口 | ||
220 | -func (c *AdminUserController) BeforeEditAdminUser() { | ||
221 | - newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil) | ||
222 | - allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{ | ||
223 | - ParentId: 0, | ||
224 | - NotCode: []string{domain.PERMINSSION_ADMIN_USER}, | ||
225 | - }) | ||
226 | - if err != nil { | ||
227 | - logs.Error("获取权限数据失败:%s", err) | ||
228 | - c.ResponseError(errors.New("服务异常")) | ||
229 | - return | ||
230 | - } | ||
231 | - var rspData []map[string]interface{} | ||
232 | - for i := range allPermission { | ||
233 | - if allPermission[i].Code == domain.PERMINSSION_ADMIN_USER { | ||
234 | - continue | ||
235 | - } | ||
236 | - m := map[string]interface{}{ | ||
237 | - "id": allPermission[i].Id, | ||
238 | - "permissionName": allPermission[i].Name, | ||
239 | - } | ||
240 | - rspData = append(rspData, m) | ||
241 | - } | ||
242 | - c.ResponseData(rspData) | ||
243 | - return | ||
244 | -} |
-
请 注册 或 登录 后发表评论