正在显示
27 个修改的文件
包含
686 行增加
和
32 行删除
@@ -11,10 +11,12 @@ import ( | @@ -11,10 +11,12 @@ import ( | ||
11 | type ListCompanyCustomizeMenusCommand struct { | 11 | type ListCompanyCustomizeMenusCommand struct { |
12 | // 企业id | 12 | // 企业id |
13 | CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"` | 13 | CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"` |
14 | + // 菜单类别 web app | ||
15 | + MenuCategory string `json:"menuCategory,omitempty" valid:"Required"` | ||
14 | } | 16 | } |
15 | 17 | ||
16 | func (listCompanyCustomizeMenusCommand *ListCompanyCustomizeMenusCommand) Valid(validation *validation.Validation) { | 18 | func (listCompanyCustomizeMenusCommand *ListCompanyCustomizeMenusCommand) Valid(validation *validation.Validation) { |
17 | - validation.SetError("CustomValid", "未实现的自定义认证") | 19 | + |
18 | } | 20 | } |
19 | 21 | ||
20 | func (listCompanyCustomizeMenusCommand *ListCompanyCustomizeMenusCommand) ValidateCommand() error { | 22 | func (listCompanyCustomizeMenusCommand *ListCompanyCustomizeMenusCommand) ValidateCommand() error { |
@@ -10,11 +10,11 @@ import ( | @@ -10,11 +10,11 @@ import ( | ||
10 | 10 | ||
11 | type UpdateCompanyCustomizeMenusCommand struct { | 11 | type UpdateCompanyCustomizeMenusCommand struct { |
12 | // 企业id | 12 | // 企业id |
13 | - CompanyId int64 `cname:"企业id" json:"companyId,string" valid:"Required"` | 13 | + CompanyId int64 `cname:"企业id" json:"companyId" valid:"Required"` |
14 | // 菜单编号 | 14 | // 菜单编号 |
15 | - MenuId int64 `cname:"菜单编号" json:"menuId,string" valid:"Required"` | 15 | + MenuId int64 `cname:"菜单编号" json:"menuId" valid:"Required"` |
16 | // 菜单名称 | 16 | // 菜单名称 |
17 | - MenuName string `cname:"菜单名称" json:"menuName" valid:"Required"` | 17 | + //MenuName string `cname:"菜单名称" json:"menuName" valid:"Required"` |
18 | // 菜单别名 | 18 | // 菜单别名 |
19 | MenuAlias string `cname:"菜单别名" json:"menuAlias" valid:"Required"` | 19 | MenuAlias string `cname:"菜单别名" json:"menuAlias" valid:"Required"` |
20 | // 排序 | 20 | // 排序 |
@@ -22,7 +22,7 @@ type UpdateCompanyCustomizeMenusCommand struct { | @@ -22,7 +22,7 @@ type UpdateCompanyCustomizeMenusCommand struct { | ||
22 | } | 22 | } |
23 | 23 | ||
24 | func (updateCompanyCustomizeMenusCommand *UpdateCompanyCustomizeMenusCommand) Valid(validation *validation.Validation) { | 24 | func (updateCompanyCustomizeMenusCommand *UpdateCompanyCustomizeMenusCommand) Valid(validation *validation.Validation) { |
25 | - validation.SetError("CustomValid", "未实现的自定义认证") | 25 | + //validation.SetError("CustomValid", "未实现的自定义认证") |
26 | } | 26 | } |
27 | 27 | ||
28 | func (updateCompanyCustomizeMenusCommand *UpdateCompanyCustomizeMenusCommand) ValidateCommand() error { | 28 | func (updateCompanyCustomizeMenusCommand *UpdateCompanyCustomizeMenusCommand) ValidateCommand() error { |
1 | +package dto | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils" | ||
6 | +) | ||
7 | + | ||
8 | +func RetCustomizeMenu(menus []*domain.Menu, customizeMenus []*domain.CustomizeMenu) interface{} { | ||
9 | + var ret []interface{} | ||
10 | + menusMap := make(map[int64]*domain.Menu) | ||
11 | + for i := range menus { | ||
12 | + menusMap[menus[i].MenuId] = menus[i] | ||
13 | + } | ||
14 | + | ||
15 | + for i := range customizeMenus { | ||
16 | + m := customizeMenus[i] | ||
17 | + if menu, ok := menusMap[m.MenuId]; ok { | ||
18 | + menu.MenuAlias = m.MenuAlias | ||
19 | + } | ||
20 | + } | ||
21 | + | ||
22 | + for i := range menus { | ||
23 | + fieldMenu := utils.LoadCustomFieldToMap(menus[i], "MenuId", "ParentId", "MenuName", "Code", "Icon", "MenuAlias", "MenuType", "Remark", "Sort") | ||
24 | + fieldMenu["parentMenuName"] = "" | ||
25 | + if menu, ok := menusMap[menus[i].ParentId]; ok { | ||
26 | + fieldMenu["parentMenuName"] = menu.MenuName | ||
27 | + } | ||
28 | + ret = append(ret, fieldMenu) | ||
29 | + } | ||
30 | + return ret | ||
31 | +} |
@@ -5,9 +5,12 @@ import ( | @@ -5,9 +5,12 @@ import ( | ||
5 | "github.com/linmadan/egglib-go/core/application" | 5 | "github.com/linmadan/egglib-go/core/application" |
6 | "github.com/linmadan/egglib-go/utils/tool_funs" | 6 | "github.com/linmadan/egglib-go/utils/tool_funs" |
7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/command" | 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/command" |
8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/dto" | ||
8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/query" | 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/query" |
9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" | 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" |
10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" |
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/common" | ||
13 | + "strconv" | ||
11 | ) | 14 | ) |
12 | 15 | ||
13 | // 企业 | 16 | // 企业 |
@@ -123,7 +126,7 @@ func (companyService *CompanyService) ListCompany(listCompanyQuery *query.ListCo | @@ -123,7 +126,7 @@ func (companyService *CompanyService) ListCompany(listCompanyQuery *query.ListCo | ||
123 | } | 126 | } |
124 | } | 127 | } |
125 | 128 | ||
126 | -// 返回自定义菜单列表 | 129 | +// 返回自定义菜单列表(匹配有设置的菜单) |
127 | func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand *command.ListCompanyCustomizeMenusCommand) (interface{}, error) { | 130 | func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand *command.ListCompanyCustomizeMenusCommand) (interface{}, error) { |
128 | if err := listCompanyCustomizeMenusCommand.ValidateCommand(); err != nil { | 131 | if err := listCompanyCustomizeMenusCommand.ValidateCommand(); err != nil { |
129 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 132 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
@@ -138,10 +141,41 @@ func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCusto | @@ -138,10 +141,41 @@ func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCusto | ||
138 | defer func() { | 141 | defer func() { |
139 | transactionContext.RollbackTransaction() | 142 | transactionContext.RollbackTransaction() |
140 | }() | 143 | }() |
144 | + | ||
145 | + var menuRepository domain.MenuRepository | ||
146 | + if value, err := factory.CreateMenuRepository(map[string]interface{}{ | ||
147 | + "transactionContext": transactionContext, | ||
148 | + }); err != nil { | ||
149 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
150 | + } else { | ||
151 | + menuRepository = value | ||
152 | + } | ||
153 | + queryOptions := common.SimpleStructToMap(listCompanyCustomizeMenusCommand) | ||
154 | + if m, e := menuRepository.FindOne(map[string]interface{}{"code": listCompanyCustomizeMenusCommand.MenuCategory}); e == nil && m != nil { | ||
155 | + queryOptions["category"] = strconv.Itoa(int(m.MenuId)) | ||
156 | + } | ||
157 | + _, menus, err := menuRepository.Find(queryOptions) | ||
158 | + if err != nil { | ||
159 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
160 | + } | ||
161 | + var customizeMenuRepository domain.CustomizeMenuRepository | ||
162 | + if value, err := factory.CreateCustomizeMenuRepository(map[string]interface{}{ | ||
163 | + "transactionContext": transactionContext, | ||
164 | + }); err != nil { | ||
165 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
166 | + } else { | ||
167 | + customizeMenuRepository = value | ||
168 | + } | ||
169 | + _, customizeMenus, err := customizeMenuRepository.Find(queryOptions) | ||
170 | + if err != nil { | ||
171 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
172 | + } | ||
173 | + ret := dto.RetCustomizeMenu(menus, customizeMenus) | ||
174 | + | ||
141 | if err := transactionContext.CommitTransaction(); err != nil { | 175 | if err := transactionContext.CommitTransaction(); err != nil { |
142 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 176 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
143 | } | 177 | } |
144 | - return nil, nil | 178 | + return map[string]interface{}{"menus": ret}, nil |
145 | } | 179 | } |
146 | 180 | ||
147 | // 移除企业 | 181 | // 移除企业 |
@@ -242,10 +276,59 @@ func (companyService *CompanyService) UpdateCompanyCustomizeMenus(updateCompanyC | @@ -242,10 +276,59 @@ func (companyService *CompanyService) UpdateCompanyCustomizeMenus(updateCompanyC | ||
242 | defer func() { | 276 | defer func() { |
243 | transactionContext.RollbackTransaction() | 277 | transactionContext.RollbackTransaction() |
244 | }() | 278 | }() |
279 | + | ||
280 | + var menuRepository domain.MenuRepository | ||
281 | + if value, err := factory.CreateMenuRepository(map[string]interface{}{ | ||
282 | + "transactionContext": transactionContext, | ||
283 | + }); err != nil { | ||
284 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
285 | + } else { | ||
286 | + menuRepository = value | ||
287 | + } | ||
288 | + var menuName string | ||
289 | + if menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": updateCompanyCustomizeMenusCommand.MenuId}); err != nil || (menu != nil && menu.IsPublish != domain.MenuPublic) { | ||
290 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, "菜单不存在") | ||
291 | + } else { | ||
292 | + menuName = menu.MenuName | ||
293 | + } | ||
294 | + | ||
295 | + var customizeMenuRepository domain.CustomizeMenuRepository | ||
296 | + if value, err := factory.CreateCustomizeMenuRepository(map[string]interface{}{ | ||
297 | + "transactionContext": transactionContext, | ||
298 | + }); err != nil { | ||
299 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
300 | + } else { | ||
301 | + customizeMenuRepository = value | ||
302 | + } | ||
303 | + customizeMenu, e := customizeMenuRepository.FindOne(map[string]interface{}{ | ||
304 | + "companyId": updateCompanyCustomizeMenusCommand.CompanyId, | ||
305 | + "menuId": updateCompanyCustomizeMenusCommand.MenuId, | ||
306 | + "isPublish": domain.MenuPublic, | ||
307 | + }) | ||
308 | + if e == domain.ErrorNotFound { | ||
309 | + customizeMenu = &domain.CustomizeMenu{ | ||
310 | + CompanyId: updateCompanyCustomizeMenusCommand.CompanyId, | ||
311 | + MenuId: updateCompanyCustomizeMenusCommand.MenuId, | ||
312 | + MenuName: menuName, | ||
313 | + MenuAlias: updateCompanyCustomizeMenusCommand.MenuAlias, | ||
314 | + Sort: updateCompanyCustomizeMenusCommand.Sort, | ||
315 | + } | ||
316 | + } else if customizeMenu != nil { | ||
317 | + data := tool_funs.SimpleStructToMap(updateCompanyCustomizeMenusCommand) | ||
318 | + data["menuName"] = menuName | ||
319 | + if err := customizeMenu.Update(data); err != nil { | ||
320 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
321 | + } | ||
322 | + } else { | ||
323 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, e.Error()) | ||
324 | + } | ||
325 | + if customizeMenu, err = customizeMenuRepository.Save(customizeMenu); err != nil { | ||
326 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, e.Error()) | ||
327 | + } | ||
245 | if err := transactionContext.CommitTransaction(); err != nil { | 328 | if err := transactionContext.CommitTransaction(); err != nil { |
246 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 329 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
247 | } | 330 | } |
248 | - return nil, nil | 331 | + return customizeMenu, nil |
249 | } | 332 | } |
250 | 333 | ||
251 | func NewCompanyService(options map[string]interface{}) *CompanyService { | 334 | func NewCompanyService(options map[string]interface{}) *CompanyService { |
@@ -53,3 +53,11 @@ func CreateCompanyRepository(options map[string]interface{}) (domain.CompanyRepo | @@ -53,3 +53,11 @@ func CreateCompanyRepository(options map[string]interface{}) (domain.CompanyRepo | ||
53 | } | 53 | } |
54 | return repository.NewCompanyRepository(transactionContext) | 54 | return repository.NewCompanyRepository(transactionContext) |
55 | } | 55 | } |
56 | + | ||
57 | +func CreateCustomizeMenuRepository(options map[string]interface{}) (domain.CustomizeMenuRepository, error) { | ||
58 | + var transactionContext *pg.TransactionContext | ||
59 | + if value, ok := options["transactionContext"]; ok { | ||
60 | + transactionContext = value.(*pg.TransactionContext) | ||
61 | + } | ||
62 | + return repository.NewCustomizeMenuRepository(transactionContext) | ||
63 | +} |
@@ -24,9 +24,9 @@ type CreateMenuCommand struct { | @@ -24,9 +24,9 @@ type CreateMenuCommand struct { | ||
24 | // 菜单说明 | 24 | // 菜单说明 |
25 | Desc string `json:"desc,omitempty"` | 25 | Desc string `json:"desc,omitempty"` |
26 | // 菜单是否公开状态,[0:隐藏],[1:显示],默认显示 | 26 | // 菜单是否公开状态,[0:隐藏],[1:显示],默认显示 |
27 | - IsPublish int `json:"isPublish"` | 27 | + IsPublish int `json:"isPublish" valid:"Required"` |
28 | // 启用状态(启用:1 禁用:2),默认启用 | 28 | // 启用状态(启用:1 禁用:2),默认启用 |
29 | - EnableStatus int `json:"enableStatus"` | 29 | + EnableStatus int `json:"enableStatus" valid:"Required"` |
30 | } | 30 | } |
31 | 31 | ||
32 | func (createMenuCommand *CreateMenuCommand) Valid(validation *validation.Validation) { | 32 | func (createMenuCommand *CreateMenuCommand) Valid(validation *validation.Validation) { |
@@ -18,11 +18,11 @@ type ListMenuQuery struct { | @@ -18,11 +18,11 @@ type ListMenuQuery struct { | ||
18 | // 查询偏离量 | 18 | // 查询偏离量 |
19 | Offset int `json:"offset"` | 19 | Offset int `json:"offset"` |
20 | // 查询限制 | 20 | // 查询限制 |
21 | - Limit int `json:"limit" valid:"Required"` | 21 | + Limit int `json:"limit"` |
22 | 22 | ||
23 | // web分页 | 23 | // web分页 |
24 | PageNumber int `json:"pageNumber"` | 24 | PageNumber int `json:"pageNumber"` |
25 | - PageSize int `json:"pageSize" valid:"Required"` | 25 | + PageSize int `json:"pageSize"` |
26 | } | 26 | } |
27 | 27 | ||
28 | func (listMenuQuery *ListMenuQuery) Valid(validation *validation.Validation) { | 28 | func (listMenuQuery *ListMenuQuery) Valid(validation *validation.Validation) { |
@@ -52,7 +52,7 @@ func (menuService *MenuService) CreateMenu(createMenuCommand *command.CreateMenu | @@ -52,7 +52,7 @@ func (menuService *MenuService) CreateMenu(createMenuCommand *command.CreateMenu | ||
52 | menuRepository = value | 52 | menuRepository = value |
53 | } | 53 | } |
54 | // 1.菜单类型验证 | 54 | // 1.菜单类型验证 |
55 | - if newMenu.ValidMenuType() { | 55 | + if !newMenu.ValidMenuType() { |
56 | return nil, application.ThrowError(application.ARG_ERROR, domain.ErrorMenuType.Error()) | 56 | return nil, application.ThrowError(application.ARG_ERROR, domain.ErrorMenuType.Error()) |
57 | } | 57 | } |
58 | // 2.菜单编码验证 | 58 | // 2.菜单编码验证 |
@@ -248,8 +248,8 @@ func (menuService *MenuService) UpdateMenu(updateMenuCommand *command.UpdateMenu | @@ -248,8 +248,8 @@ func (menuService *MenuService) UpdateMenu(updateMenuCommand *command.UpdateMenu | ||
248 | } | 248 | } |
249 | // 1.验证code是否有重复的情况 | 249 | // 1.验证code是否有重复的情况 |
250 | if menu.Code != updateMenuCommand.Code { | 250 | if menu.Code != updateMenuCommand.Code { |
251 | - if m, e := menuRepository.FindOne(map[string]interface{}{"code": updateMenuCommand.Code}); e == nil && m.MenuId != menu.MenuId { | ||
252 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 251 | + if m, e := menuRepository.FindOne(map[string]interface{}{"code": updateMenuCommand.Code}); e == nil && m != nil && m.MenuId != menu.MenuId { |
252 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("菜单编码:%v已重复,请重新输入", updateMenuCommand.Code)) | ||
253 | } | 253 | } |
254 | } | 254 | } |
255 | // 2.验证父级节点是否有发生更新 | 255 | // 2.验证父级节点是否有发生更新 |
@@ -7,8 +7,8 @@ var POSTGRESQL_USER = "postgres" | @@ -7,8 +7,8 @@ var POSTGRESQL_USER = "postgres" | ||
7 | var POSTGRESQL_PASSWORD = "123456" | 7 | var POSTGRESQL_PASSWORD = "123456" |
8 | var POSTGRESQL_HOST = "127.0.0.1" | 8 | var POSTGRESQL_HOST = "127.0.0.1" |
9 | var POSTGRESQL_PORT = "5432" | 9 | var POSTGRESQL_PORT = "5432" |
10 | -var DISABLE_CREATE_TABLE = true | ||
11 | -var DISABLE_SQL_GENERATE_PRINT = true | 10 | +var DISABLE_CREATE_TABLE = false |
11 | +var DISABLE_SQL_GENERATE_PRINT = false | ||
12 | var DISABLE_SQL_GENERATE_COMMENT = true | 12 | var DISABLE_SQL_GENERATE_COMMENT = true |
13 | 13 | ||
14 | func init() { | 14 | func init() { |
pkg/domain/customize_menu.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +// 自定义菜单 (base)(菜单维护) | ||
4 | +type CustomizeMenu struct { | ||
5 | + // 自定义菜单id | ||
6 | + CustomizeMenusId int64 `json:"customizeMenusId,string"` | ||
7 | + // 企业id | ||
8 | + CompanyId int64 `json:"companyId,string"` | ||
9 | + // 菜单id | ||
10 | + MenuId int64 `json:"menuId,string"` | ||
11 | + // 菜单名称 | ||
12 | + MenuName string `json:"menuName"` | ||
13 | + // 菜单别名 | ||
14 | + MenuAlias string `json:"menuAlias"` | ||
15 | + // 排序 | ||
16 | + Sort int `json:"sort"` | ||
17 | +} | ||
18 | + | ||
19 | +type CustomizeMenuRepository interface { | ||
20 | + Save(customizeMenu *CustomizeMenu) (*CustomizeMenu, error) | ||
21 | + Remove(customizeMenu *CustomizeMenu) (*CustomizeMenu, error) | ||
22 | + FindOne(queryOptions map[string]interface{}) (*CustomizeMenu, error) | ||
23 | + Find(queryOptions map[string]interface{}) (int64, []*CustomizeMenu, error) | ||
24 | +} | ||
25 | + | ||
26 | +func (customizeMenu *CustomizeMenu) Identify() interface{} { | ||
27 | + if customizeMenu.CustomizeMenusId == 0 { | ||
28 | + return nil | ||
29 | + } | ||
30 | + return customizeMenu.CustomizeMenusId | ||
31 | +} | ||
32 | + | ||
33 | +func (customizeMenu *CustomizeMenu) Update(data map[string]interface{}) error { | ||
34 | + //if companyId, ok := data["companyId"]; ok { | ||
35 | + // customizeMenu.CompanyId = companyId.(int64) | ||
36 | + //} | ||
37 | + //if menuId, ok := data["menuId"]; ok { | ||
38 | + // customizeMenu.MenuId = menuId.(int64) | ||
39 | + //} | ||
40 | + if menuName, ok := data["menuName"]; ok { | ||
41 | + customizeMenu.MenuName = menuName.(string) | ||
42 | + } | ||
43 | + if menuAlias, ok := data["menuAlias"]; ok { | ||
44 | + customizeMenu.MenuAlias = menuAlias.(string) | ||
45 | + } | ||
46 | + if sort, ok := data["sort"]; ok { | ||
47 | + customizeMenu.Sort = sort.(int) | ||
48 | + } | ||
49 | + return nil | ||
50 | +} |
pkg/domain/domain.go
0 → 100644
@@ -31,6 +31,11 @@ const ( | @@ -31,6 +31,11 @@ const ( | ||
31 | MenuPrivate = 2 // 菜单未公开 | 31 | MenuPrivate = 2 // 菜单未公开 |
32 | ) | 32 | ) |
33 | 33 | ||
34 | +const ( | ||
35 | + WebMenuCode = "web" | ||
36 | + AppMenuCode = "app" | ||
37 | +) | ||
38 | + | ||
34 | // 菜单类型 | 39 | // 菜单类型 |
35 | type MenuType string | 40 | type MenuType string |
36 | 41 | ||
@@ -42,6 +47,8 @@ type Menu struct { | @@ -42,6 +47,8 @@ type Menu struct { | ||
42 | ParentId int64 `json:"parentId,omitempty"` | 47 | ParentId int64 `json:"parentId,omitempty"` |
43 | // 菜单名称 | 48 | // 菜单名称 |
44 | MenuName string `json:"menuName,omitempty"` | 49 | MenuName string `json:"menuName,omitempty"` |
50 | + // 菜单别名 | ||
51 | + MenuAlias string `json:"menuAlias,omitempty"` | ||
45 | // 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码) | 52 | // 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码) |
46 | Code string `json:"code,omitempty"` | 53 | Code string `json:"code,omitempty"` |
47 | // 权限编码 user:edit | 54 | // 权限编码 user:edit |
@@ -30,6 +30,8 @@ func init() { | @@ -30,6 +30,8 @@ func init() { | ||
30 | if !constant.DISABLE_CREATE_TABLE { | 30 | if !constant.DISABLE_CREATE_TABLE { |
31 | for _, model := range []interface{}{ | 31 | for _, model := range []interface{}{ |
32 | (*models.Menu)(nil), | 32 | (*models.Menu)(nil), |
33 | + (*models.CustomizeMenu)(nil), | ||
34 | + //(*models.User)(nil), | ||
33 | } { | 35 | } { |
34 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ | 36 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ |
35 | Temp: false, | 37 | Temp: false, |
@@ -8,7 +8,7 @@ import ( | @@ -8,7 +8,7 @@ import ( | ||
8 | type Company struct { | 8 | type Company struct { |
9 | tableName string `pg:"users.company,alias:company" comment:"企业"` | 9 | tableName string `pg:"users.company,alias:company" comment:"企业"` |
10 | // 企业id | 10 | // 企业id |
11 | - CompanyId int64 `comment:"企业id"` | 11 | + CompanyId int64 `pg:",pk" comment:"企业id"` |
12 | // 企业配置信息 | 12 | // 企业配置信息 |
13 | CompanyConfig *domain.CompanyConfig `comment:"企业配置信息"` | 13 | CompanyConfig *domain.CompanyConfig `comment:"企业配置信息"` |
14 | // 企业基本信息 | 14 | // 企业基本信息 |
1 | +package models | ||
2 | + | ||
3 | +type CustomizeMenu struct { | ||
4 | + tableName string `pg:"users.customize_menu" comment:"自定义菜单 (base)(菜单维护)"` | ||
5 | + // 自定义菜单id | ||
6 | + CustomizeMenuId int64 `pg:",pk" comment:"自定义菜单id"` | ||
7 | + // 企业id | ||
8 | + CompanyId int64 `comment:"企业id"` | ||
9 | + // 菜单id | ||
10 | + MenuId int64 `comment:"菜单id"` | ||
11 | + // 菜单名称 | ||
12 | + MenuName string `comment:"菜单名称"` | ||
13 | + // 菜单别名 | ||
14 | + MenuAlias string `comment:"菜单别名"` | ||
15 | + // 排序 | ||
16 | + Sort int `comment:"排序"` | ||
17 | +} |
@@ -8,7 +8,7 @@ import ( | @@ -8,7 +8,7 @@ import ( | ||
8 | type Org struct { | 8 | type Org struct { |
9 | tableName string `pg:"users.org,alias:org" comment:"组织"` | 9 | tableName string `pg:"users.org,alias:org" comment:"组织"` |
10 | // 组织ID | 10 | // 组织ID |
11 | - OrgId int64 `comment:"组织ID"` | 11 | + OrgId int64 `pg:",pk" comment:"组织ID"` |
12 | // 企业id | 12 | // 企业id |
13 | CompanyId int64 `comment:"企业id"` | 13 | CompanyId int64 `comment:"企业id"` |
14 | // 创建时间 | 14 | // 创建时间 |
@@ -8,7 +8,7 @@ import ( | @@ -8,7 +8,7 @@ import ( | ||
8 | type Role struct { | 8 | type Role struct { |
9 | tableName string `pg:"users.role,alias:role" comment:"角色"` | 9 | tableName string `pg:"users.role,alias:role" comment:"角色"` |
10 | // 角色ID | 10 | // 角色ID |
11 | - RoleId int64 `comment:"角色ID"` | 11 | + RoleId int64 `pg:",pk" comment:"角色ID"` |
12 | // 企业id | 12 | // 企业id |
13 | CompanyId int64 `comment:"企业id"` | 13 | CompanyId int64 `comment:"企业id"` |
14 | // 组织ID | 14 | // 组织ID |
@@ -8,7 +8,7 @@ import ( | @@ -8,7 +8,7 @@ import ( | ||
8 | type User struct { | 8 | type User struct { |
9 | tableName string `pg:"users.user,alias:user"` | 9 | tableName string `pg:"users.user,alias:user"` |
10 | // 用户Id 用户唯一标识 | 10 | // 用户Id 用户唯一标识 |
11 | - UserId int64 `comment:"用户Id"` | 11 | + UserId int64 `pg:",pk" comment:"用户Id"` |
12 | // 企业id | 12 | // 企业id |
13 | CompanyId int64 `comment:"企业id"` | 13 | CompanyId int64 `comment:"企业id"` |
14 | // 用户基础数据id | 14 | // 用户基础数据id |
@@ -8,7 +8,7 @@ import ( | @@ -8,7 +8,7 @@ import ( | ||
8 | type UserBase struct { | 8 | type UserBase struct { |
9 | tableName string `pg:"users.user_base,alias:user_base" comment:"用户基础"` | 9 | tableName string `pg:"users.user_base,alias:user_base" comment:"用户基础"` |
10 | // 用户基础数据id | 10 | // 用户基础数据id |
11 | - UserBaseId int64 `comment:"用户基础数据id"` | 11 | + UserBaseId int64 `pg:",pk" comment:"用户基础数据id"` |
12 | // 用户信息 | 12 | // 用户信息 |
13 | UserInfo *domain.UserInfo `comment:"用户信息"` | 13 | UserInfo *domain.UserInfo `comment:"用户信息"` |
14 | // 账号 | 14 | // 账号 |
1 | +package transform | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models" | ||
6 | +) | ||
7 | + | ||
8 | +func TransformToCustomizeMenuDomainModelFromPgModels(customizeMenuModel *models.CustomizeMenu) (*domain.CustomizeMenu, error) { | ||
9 | + return &domain.CustomizeMenu{ | ||
10 | + CustomizeMenusId: customizeMenuModel.CustomizeMenuId, | ||
11 | + CompanyId: customizeMenuModel.CompanyId, | ||
12 | + MenuId: customizeMenuModel.MenuId, | ||
13 | + MenuName: customizeMenuModel.MenuName, | ||
14 | + MenuAlias: customizeMenuModel.MenuAlias, | ||
15 | + Sort: customizeMenuModel.Sort, | ||
16 | + }, nil | ||
17 | +} |
@@ -10,6 +10,7 @@ func TransformToMenuDomainModelFromPgModels(menuModel *models.Menu) (*domain.Men | @@ -10,6 +10,7 @@ func TransformToMenuDomainModelFromPgModels(menuModel *models.Menu) (*domain.Men | ||
10 | MenuId: menuModel.MenuId, | 10 | MenuId: menuModel.MenuId, |
11 | ParentId: menuModel.ParentId, | 11 | ParentId: menuModel.ParentId, |
12 | MenuName: menuModel.MenuName, | 12 | MenuName: menuModel.MenuName, |
13 | + MenuAlias: menuModel.MenuName, | ||
13 | Code: menuModel.Code, | 14 | Code: menuModel.Code, |
14 | AccessCode: menuModel.AccessCode, | 15 | AccessCode: menuModel.AccessCode, |
15 | MenuType: menuModel.MenuType, | 16 | MenuType: menuModel.MenuType, |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/go-pg/pg/v10" | ||
6 | + | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
8 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
9 | + "github.com/linmadan/egglib-go/utils/snowflake" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
11 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models" | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform" | ||
13 | +) | ||
14 | + | ||
15 | +type CustomizeMenuRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func (repository *CustomizeMenuRepository) nextIdentify() (int64, error) { | ||
20 | + IdWorker, err := snowflake.NewIdWorker(1) | ||
21 | + if err != nil { | ||
22 | + return 0, err | ||
23 | + } | ||
24 | + id, err := IdWorker.NextId() | ||
25 | + return id, err | ||
26 | +} | ||
27 | +func (repository *CustomizeMenuRepository) Save(customizeMenu *domain.CustomizeMenu) (*domain.CustomizeMenu, error) { | ||
28 | + sqlBuildFields := []string{ | ||
29 | + "customize_menu_id", | ||
30 | + "company_id", | ||
31 | + "menu_id", | ||
32 | + "menu_name", | ||
33 | + "menu_alias", | ||
34 | + "sort", | ||
35 | + } | ||
36 | + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "customize_menu_id")) | ||
37 | + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "customize_menu_id")) | ||
38 | + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
39 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "customize_menu_id") | ||
40 | + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | ||
41 | + tx := repository.transactionContext.PgTx | ||
42 | + if customizeMenu.Identify() == nil { | ||
43 | + if _, err := tx.QueryOne( | ||
44 | + pg.Scan( | ||
45 | + &customizeMenu.CustomizeMenusId, | ||
46 | + &customizeMenu.CompanyId, | ||
47 | + &customizeMenu.MenuId, | ||
48 | + &customizeMenu.MenuName, | ||
49 | + &customizeMenu.MenuAlias, | ||
50 | + &customizeMenu.Sort, | ||
51 | + ), | ||
52 | + fmt.Sprintf("INSERT INTO users.customize_menu (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | ||
53 | + customizeMenu.CompanyId, | ||
54 | + customizeMenu.MenuId, | ||
55 | + customizeMenu.MenuName, | ||
56 | + customizeMenu.MenuAlias, | ||
57 | + customizeMenu.Sort, | ||
58 | + ); err != nil { | ||
59 | + return customizeMenu, err | ||
60 | + } | ||
61 | + } else { | ||
62 | + if _, err := tx.QueryOne( | ||
63 | + pg.Scan( | ||
64 | + &customizeMenu.CustomizeMenusId, | ||
65 | + &customizeMenu.CompanyId, | ||
66 | + &customizeMenu.MenuId, | ||
67 | + &customizeMenu.MenuName, | ||
68 | + &customizeMenu.MenuAlias, | ||
69 | + &customizeMenu.Sort, | ||
70 | + ), | ||
71 | + fmt.Sprintf("UPDATE users.customize_menu SET %s WHERE customize_menu_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
72 | + customizeMenu.CompanyId, | ||
73 | + customizeMenu.MenuId, | ||
74 | + customizeMenu.MenuName, | ||
75 | + customizeMenu.MenuAlias, | ||
76 | + customizeMenu.Sort, | ||
77 | + customizeMenu.Identify(), | ||
78 | + ); err != nil { | ||
79 | + return customizeMenu, err | ||
80 | + } | ||
81 | + } | ||
82 | + return customizeMenu, nil | ||
83 | +} | ||
84 | +func (repository *CustomizeMenuRepository) Remove(customizeMenu *domain.CustomizeMenu) (*domain.CustomizeMenu, error) { | ||
85 | + tx := repository.transactionContext.PgTx | ||
86 | + customizeMenuModel := new(models.CustomizeMenu) | ||
87 | + customizeMenuModel.CustomizeMenuId = customizeMenu.Identify().(int64) | ||
88 | + if _, err := tx.Model(customizeMenuModel).WherePK().Delete(); err != nil { | ||
89 | + return customizeMenu, err | ||
90 | + } | ||
91 | + return customizeMenu, nil | ||
92 | +} | ||
93 | +func (repository *CustomizeMenuRepository) FindOne(queryOptions map[string]interface{}) (*domain.CustomizeMenu, error) { | ||
94 | + tx := repository.transactionContext.PgTx | ||
95 | + customizeMenuModel := new(models.CustomizeMenu) | ||
96 | + query := sqlbuilder.BuildQuery(tx.Model(customizeMenuModel), queryOptions) | ||
97 | + query.SetWhereByQueryOption("customize_menu_id = ?", "customizeMenuId") | ||
98 | + query.SetWhereByQueryOption("menu_id = ?", "menuId") | ||
99 | + query.SetWhereByQueryOption("company_id = ?", "companyId") | ||
100 | + if err := query.First(); err != nil { | ||
101 | + if err.Error() == "pg: no rows in result set" { | ||
102 | + return nil, domain.ErrorNotFound | ||
103 | + } else { | ||
104 | + return nil, err | ||
105 | + } | ||
106 | + } | ||
107 | + if customizeMenuModel.CustomizeMenuId == 0 { | ||
108 | + return nil, nil | ||
109 | + } else { | ||
110 | + return transform.TransformToCustomizeMenuDomainModelFromPgModels(customizeMenuModel) | ||
111 | + } | ||
112 | +} | ||
113 | +func (repository *CustomizeMenuRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CustomizeMenu, error) { | ||
114 | + tx := repository.transactionContext.PgTx | ||
115 | + var customizeMenuModels []*models.CustomizeMenu | ||
116 | + customizeMenus := make([]*domain.CustomizeMenu, 0) | ||
117 | + query := sqlbuilder.BuildQuery(tx.Model(&customizeMenuModels), queryOptions) | ||
118 | + query.SetWhereByQueryOption("company_id =?", "companyId") | ||
119 | + query.SetOrderDirect("customize_menu_id", "DESC") | ||
120 | + if count, err := query.SelectAndCount(); err != nil { | ||
121 | + return 0, customizeMenus, err | ||
122 | + } else { | ||
123 | + for _, customizeMenuModel := range customizeMenuModels { | ||
124 | + if customizeMenu, err := transform.TransformToCustomizeMenuDomainModelFromPgModels(customizeMenuModel); err != nil { | ||
125 | + return 0, customizeMenus, err | ||
126 | + } else { | ||
127 | + customizeMenus = append(customizeMenus, customizeMenu) | ||
128 | + } | ||
129 | + } | ||
130 | + return int64(count), customizeMenus, nil | ||
131 | + } | ||
132 | +} | ||
133 | +func NewCustomizeMenuRepository(transactionContext *pgTransaction.TransactionContext) (*CustomizeMenuRepository, error) { | ||
134 | + if transactionContext == nil { | ||
135 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
136 | + } else { | ||
137 | + return &CustomizeMenuRepository{ | ||
138 | + transactionContext: transactionContext, | ||
139 | + }, nil | ||
140 | + } | ||
141 | +} |
@@ -69,7 +69,7 @@ func (repository *MenuRepository) Save(menu *domain.Menu) (*domain.Menu, error) | @@ -69,7 +69,7 @@ func (repository *MenuRepository) Save(menu *domain.Menu) (*domain.Menu, error) | ||
69 | &menu.IsPublish, | 69 | &menu.IsPublish, |
70 | &menu.EnableStatus, | 70 | &menu.EnableStatus, |
71 | ), | 71 | ), |
72 | - fmt.Sprintf("INSERT INTO user.menu (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | 72 | + fmt.Sprintf(`INSERT INTO users.menu (%s) VALUES (%s) RETURNING %s`, insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), |
73 | menu.ParentId, | 73 | menu.ParentId, |
74 | menu.MenuName, | 74 | menu.MenuName, |
75 | menu.Code, | 75 | menu.Code, |
@@ -102,7 +102,7 @@ func (repository *MenuRepository) Save(menu *domain.Menu) (*domain.Menu, error) | @@ -102,7 +102,7 @@ func (repository *MenuRepository) Save(menu *domain.Menu) (*domain.Menu, error) | ||
102 | &menu.IsPublish, | 102 | &menu.IsPublish, |
103 | &menu.EnableStatus, | 103 | &menu.EnableStatus, |
104 | ), | 104 | ), |
105 | - fmt.Sprintf("UPDATE user.menu SET %s WHERE menu_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | 105 | + fmt.Sprintf("UPDATE users.menu SET %s WHERE menu_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), |
106 | menu.ParentId, | 106 | menu.ParentId, |
107 | menu.MenuName, | 107 | menu.MenuName, |
108 | menu.Code, | 108 | menu.Code, |
@@ -164,8 +164,10 @@ func (repository *MenuRepository) Find(queryOptions map[string]interface{}) (int | @@ -164,8 +164,10 @@ func (repository *MenuRepository) Find(queryOptions map[string]interface{}) (int | ||
164 | query.Where(fmt.Sprintf("menu_name like '%%%v%%'", v)) | 164 | query.Where(fmt.Sprintf("menu_name like '%%%v%%'", v)) |
165 | } | 165 | } |
166 | query.SetWhereByQueryOption("parent_id = ?", "parentId") | 166 | query.SetWhereByQueryOption("parent_id = ?", "parentId") |
167 | + query.SetWhereByQueryOption("is_publish =?", "isPublish") | ||
167 | query.SetOffsetAndLimit(20) | 168 | query.SetOffsetAndLimit(20) |
168 | - query.SetOrderDirect("menu_id", "asc") | 169 | + query.SetOrderDirect("parent_id", "asc") |
170 | + query.SetOrderDirect("sort", "asc") | ||
169 | if count, err := query.SelectAndCount(); err != nil { | 171 | if count, err := query.SelectAndCount(); err != nil { |
170 | return 0, menus, err | 172 | return 0, menus, err |
171 | } else { | 173 | } else { |
pkg/infrastructure/utils/utils.go
0 → 100644
1 | +package utils | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "encoding/json" | ||
6 | + "fmt" | ||
7 | + jsonlib "github.com/linmadan/egglib-go/utils/json" | ||
8 | + "io" | ||
9 | + "reflect" | ||
10 | + "strconv" | ||
11 | + "strings" | ||
12 | + "time" | ||
13 | +) | ||
14 | + | ||
15 | +func CamelCase(name string, firstUpper bool) string { | ||
16 | + array := []byte(name) | ||
17 | + if len(array) == 0 { | ||
18 | + return "" | ||
19 | + } | ||
20 | + rspArray := make([]byte, len(array)) | ||
21 | + if firstUpper { | ||
22 | + copy(rspArray[:1], strings.ToUpper(string(array[:1]))) | ||
23 | + } else { | ||
24 | + copy(rspArray[:1], strings.ToLower(string(array[:1]))) | ||
25 | + } | ||
26 | + copy(rspArray[1:], array[1:]) | ||
27 | + return string(rspArray) | ||
28 | +} | ||
29 | + | ||
30 | +func ObjectToMap(o interface{}) map[string]interface{} { | ||
31 | + if o == nil { | ||
32 | + return nil | ||
33 | + } | ||
34 | + value := reflect.ValueOf(o) | ||
35 | + if value.Kind() != reflect.Ptr { | ||
36 | + return nil | ||
37 | + } | ||
38 | + elem := value.Elem() | ||
39 | + relType := elem.Type() | ||
40 | + m := make(map[string]interface{}) | ||
41 | + for i := 0; i < relType.NumField(); i++ { | ||
42 | + field := relType.Field(i) | ||
43 | + if elem.Field(i).IsZero() { | ||
44 | + continue | ||
45 | + } | ||
46 | + m[CamelCase(field.Name, false)] = elem.Field(i).Interface() | ||
47 | + } | ||
48 | + return m | ||
49 | +} | ||
50 | + | ||
51 | +// AssertString convert v to string value | ||
52 | +func AssertString(v interface{}) string { | ||
53 | + if v == nil { | ||
54 | + return "" | ||
55 | + } | ||
56 | + | ||
57 | + // if func (v *Type) String() string, we can't use Elem() | ||
58 | + switch vt := v.(type) { | ||
59 | + case fmt.Stringer: | ||
60 | + return vt.String() | ||
61 | + } | ||
62 | + | ||
63 | + val := reflect.ValueOf(v) | ||
64 | + if val.Kind() == reflect.Ptr && !val.IsNil() { | ||
65 | + val = val.Elem() | ||
66 | + } | ||
67 | + | ||
68 | + switch vt := val.Interface().(type) { | ||
69 | + case bool: | ||
70 | + return strconv.FormatBool(vt) | ||
71 | + case error: | ||
72 | + return vt.Error() | ||
73 | + case float32: | ||
74 | + return strconv.FormatFloat(float64(vt), 'f', -1, 32) | ||
75 | + case float64: | ||
76 | + return strconv.FormatFloat(vt, 'f', -1, 64) | ||
77 | + case fmt.Stringer: | ||
78 | + return vt.String() | ||
79 | + case int: | ||
80 | + return strconv.Itoa(vt) | ||
81 | + case int8: | ||
82 | + return strconv.Itoa(int(vt)) | ||
83 | + case int16: | ||
84 | + return strconv.Itoa(int(vt)) | ||
85 | + case int32: | ||
86 | + return strconv.Itoa(int(vt)) | ||
87 | + case int64: | ||
88 | + return strconv.FormatInt(vt, 10) | ||
89 | + case string: | ||
90 | + return vt | ||
91 | + case uint: | ||
92 | + return strconv.FormatUint(uint64(vt), 10) | ||
93 | + case uint8: | ||
94 | + return strconv.FormatUint(uint64(vt), 10) | ||
95 | + case uint16: | ||
96 | + return strconv.FormatUint(uint64(vt), 10) | ||
97 | + case uint32: | ||
98 | + return strconv.FormatUint(uint64(vt), 10) | ||
99 | + case uint64: | ||
100 | + return strconv.FormatUint(vt, 10) | ||
101 | + case []byte: | ||
102 | + return string(vt) | ||
103 | + default: | ||
104 | + return fmt.Sprint(val.Interface()) | ||
105 | + } | ||
106 | +} | ||
107 | + | ||
108 | +// ValidatePtr validate v is a ptr value | ||
109 | +func ValidatePtr(v *reflect.Value) error { | ||
110 | + // sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr, | ||
111 | + // panic otherwise | ||
112 | + if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() { | ||
113 | + return fmt.Errorf("not a valid pointer: %v", v) | ||
114 | + } | ||
115 | + | ||
116 | + return nil | ||
117 | +} | ||
118 | + | ||
119 | +func LoadCustomFieldToMap(src interface{}, fields ...string) map[string]interface{} { | ||
120 | + rsp := LoadCustomField(src, fields...) | ||
121 | + if rsp == nil { | ||
122 | + return map[string]interface{}{} | ||
123 | + } | ||
124 | + return rsp.(map[string]interface{}) | ||
125 | +} | ||
126 | + | ||
127 | +func LoadCustomField(src interface{}, fields ...string) interface{} { | ||
128 | + typeSrc := reflect.TypeOf(src) | ||
129 | + valueSrc := reflect.ValueOf(src) | ||
130 | + | ||
131 | + if v, ok := src.(reflect.Value); ok { | ||
132 | + valueSrc = v | ||
133 | + typeSrc = v.Type() | ||
134 | + } | ||
135 | + if typeSrc.Kind() == reflect.Ptr { | ||
136 | + valueSrc = valueSrc.Elem() | ||
137 | + } | ||
138 | + k := valueSrc.Kind() | ||
139 | + switch k { | ||
140 | + case reflect.Array, reflect.Slice: | ||
141 | + len := valueSrc.Len() | ||
142 | + retSliceMap := make([]map[string]interface{}, 0) | ||
143 | + if len == 0 { | ||
144 | + return retSliceMap | ||
145 | + } | ||
146 | + for i := 0; i < len; i++ { | ||
147 | + v := valueSrc.Index(i) | ||
148 | + retSliceMap = append(retSliceMap, (LoadCustomField(v, fields...)).(map[string]interface{})) | ||
149 | + } | ||
150 | + return retSliceMap | ||
151 | + case reflect.Struct: | ||
152 | + retSliceMap := make(map[string]interface{}) | ||
153 | + for _, filed := range fields { | ||
154 | + f := valueSrc.FieldByName(filed) | ||
155 | + if !f.IsValid() { | ||
156 | + continue | ||
157 | + } | ||
158 | + v := f.Interface() | ||
159 | + if t, ok := v.(time.Time); ok { | ||
160 | + v = t.Local().Format("2006-01-02 15:04:05") | ||
161 | + } | ||
162 | + retSliceMap[CamelCase(filed, false)] = v | ||
163 | + } | ||
164 | + return retSliceMap | ||
165 | + default: | ||
166 | + return src | ||
167 | + } | ||
168 | + return src | ||
169 | +} | ||
170 | + | ||
171 | +func AppendCustomField(src interface{}, options map[string]interface{}) interface{} { | ||
172 | + var mapSrc map[string]interface{} | ||
173 | + var ok bool | ||
174 | + mapSrc, ok = src.(map[string]interface{}) | ||
175 | + if !ok { | ||
176 | + jsonlib.Unmarshal([]byte(jsonlib.MarshalToString(src)), &mapSrc) | ||
177 | + } | ||
178 | + for field, value := range options { | ||
179 | + mapSrc[CamelCase(field, false)] = value | ||
180 | + } | ||
181 | + return mapSrc | ||
182 | +} | ||
183 | + | ||
184 | +/* | ||
185 | + | ||
186 | +json 格式化 | ||
187 | + | ||
188 | +*/ | ||
189 | + | ||
190 | +func Marshal(v interface{}) ([]byte, error) { | ||
191 | + return json.Marshal(v) | ||
192 | +} | ||
193 | + | ||
194 | +func Unmarshal(data []byte, v interface{}) error { | ||
195 | + decoder := json.NewDecoder(bytes.NewReader(data)) | ||
196 | + if err := unmarshalUseNumber(decoder, v); err != nil { | ||
197 | + return formatError(string(data), err) | ||
198 | + } | ||
199 | + | ||
200 | + return nil | ||
201 | +} | ||
202 | + | ||
203 | +func UnmarshalFromString(str string, v interface{}) error { | ||
204 | + decoder := json.NewDecoder(strings.NewReader(str)) | ||
205 | + if err := unmarshalUseNumber(decoder, v); err != nil { | ||
206 | + return formatError(str, err) | ||
207 | + } | ||
208 | + | ||
209 | + return nil | ||
210 | +} | ||
211 | + | ||
212 | +func UnmarshalFromReader(reader io.Reader, v interface{}) error { | ||
213 | + var buf strings.Builder | ||
214 | + teeReader := io.TeeReader(reader, &buf) | ||
215 | + decoder := json.NewDecoder(teeReader) | ||
216 | + if err := unmarshalUseNumber(decoder, v); err != nil { | ||
217 | + return formatError(buf.String(), err) | ||
218 | + } | ||
219 | + | ||
220 | + return nil | ||
221 | +} | ||
222 | + | ||
223 | +func unmarshalUseNumber(decoder *json.Decoder, v interface{}) error { | ||
224 | + decoder.UseNumber() | ||
225 | + return decoder.Decode(v) | ||
226 | +} | ||
227 | + | ||
228 | +func formatError(v string, err error) error { | ||
229 | + return fmt.Errorf("string: `%s`, error: `%s`", v, err.Error()) | ||
230 | +} | ||
231 | + | ||
232 | +type ReflectVal struct { | ||
233 | + T reflect.Type | ||
234 | + V reflect.Value | ||
235 | +} | ||
236 | + | ||
237 | +/* | ||
238 | + 拷贝当前对象到目标对象,具有相同属性的值 | ||
239 | +*/ | ||
240 | +func CopyObject(src, dst interface{}) { | ||
241 | + var srcMap = make(map[string]ReflectVal) | ||
242 | + | ||
243 | + vs := reflect.ValueOf(src) | ||
244 | + ts := reflect.TypeOf(src) | ||
245 | + vd := reflect.ValueOf(dst) | ||
246 | + td := reflect.TypeOf(dst) | ||
247 | + | ||
248 | + ls := vs.Elem().NumField() | ||
249 | + for i := 0; i < ls; i++ { | ||
250 | + srcMap[ts.Elem().Field(i).Name] = ReflectVal{ | ||
251 | + T: vs.Elem().Field(i).Type(), | ||
252 | + V: vs.Elem().Field(i), | ||
253 | + } | ||
254 | + } | ||
255 | + | ||
256 | + ld := vd.Elem().NumField() | ||
257 | + for i := 0; i < ld; i++ { | ||
258 | + n := td.Elem().Field(i).Name | ||
259 | + t := vd.Elem().Field(i).Type() | ||
260 | + if v, ok := srcMap[n]; ok && v.T == t && vd.Elem().Field(i).CanSet() { | ||
261 | + vd.Elem().Field(i).Set(v.V) | ||
262 | + } | ||
263 | + } | ||
264 | +} |
@@ -74,3 +74,21 @@ func (controller *CompanyController) UpdateCompanyCustomizeMenus() { | @@ -74,3 +74,21 @@ func (controller *CompanyController) UpdateCompanyCustomizeMenus() { | ||
74 | data, err := companyService.UpdateCompanyCustomizeMenus(updateCompanyCustomizeMenusCommand) | 74 | data, err := companyService.UpdateCompanyCustomizeMenus(updateCompanyCustomizeMenusCommand) |
75 | controller.Response(data, err) | 75 | controller.Response(data, err) |
76 | } | 76 | } |
77 | + | ||
78 | +func (controller *CompanyController) SearchCompanyCustomizeMenus() { | ||
79 | + companyService := service.NewCompanyService(nil) | ||
80 | + listCompanyCustomizeMenusCommand := &command.ListCompanyCustomizeMenusCommand{} | ||
81 | + controller.Unmarshal(listCompanyCustomizeMenusCommand) | ||
82 | + listCompanyCustomizeMenusCommand.CompanyId = 1 | ||
83 | + data, err := companyService.ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand) | ||
84 | + controller.Response(data, err) | ||
85 | +} | ||
86 | + | ||
87 | +func (controller *CompanyController) AdapterUpdateCompanyCustomizeMenus() { | ||
88 | + companyService := service.NewCompanyService(nil) | ||
89 | + updateCompanyCustomizeMenusCommand := &command.UpdateCompanyCustomizeMenusCommand{} | ||
90 | + controller.Unmarshal(updateCompanyCustomizeMenusCommand) | ||
91 | + updateCompanyCustomizeMenusCommand.CompanyId = 1 | ||
92 | + data, err := companyService.UpdateCompanyCustomizeMenus(updateCompanyCustomizeMenusCommand) | ||
93 | + controller.Response(data, err) | ||
94 | +} |
@@ -13,4 +13,8 @@ func init() { | @@ -13,4 +13,8 @@ func init() { | ||
13 | web.Router("/company/search", &controllers.CompanyController{}, "Post:ListCompany") | 13 | web.Router("/company/search", &controllers.CompanyController{}, "Post:ListCompany") |
14 | web.Router("/company/:companyId/customize-menus/", &controllers.CompanyController{}, "Get:ListCompanyCustomizeMenus") | 14 | web.Router("/company/:companyId/customize-menus/", &controllers.CompanyController{}, "Get:ListCompanyCustomizeMenus") |
15 | web.Router("/company/:companyId/customize-menus", &controllers.CompanyController{}, "Put:UpdateCompanyCustomizeMenus") | 15 | web.Router("/company/:companyId/customize-menus", &controllers.CompanyController{}, "Put:UpdateCompanyCustomizeMenus") |
16 | + | ||
17 | + // 适配web | ||
18 | + web.Router("/v1/web/menus/search", &controllers.CompanyController{}, "Post:SearchCompanyCustomizeMenus") | ||
19 | + web.Router("/v1/web/menus", &controllers.CompanyController{}, "Put:AdapterUpdateCompanyCustomizeMenus") | ||
16 | } | 20 | } |
@@ -10,15 +10,15 @@ func init() { | @@ -10,15 +10,15 @@ func init() { | ||
10 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Put:UpdateMenu") | 10 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Put:UpdateMenu") |
11 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Get:GetMenu") | 11 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Get:GetMenu") |
12 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Delete:RemoveMenu") | 12 | web.Router("/menus/:menuId", &controllers.MenuController{}, "Delete:RemoveMenu") |
13 | - web.Router("/menus/search", &controllers.MenuController{}, "Get:ListMenu") | 13 | + web.Router("/menus/", &controllers.MenuController{}, "Get:ListMenu") |
14 | web.Router("/menus/search", &controllers.MenuController{}, "Post:SearchMenu") | 14 | web.Router("/menus/search", &controllers.MenuController{}, "Post:SearchMenu") |
15 | 15 | ||
16 | - web.Router("/v1/web/menus/", &controllers.MenuController{}, "Post:CreateMenu") | ||
17 | - web.Router("/v1/web/menus/:menuId", &controllers.MenuController{}, "Put:UpdateMenu") | ||
18 | - web.Router("/v1/web/menus/:menuId", &controllers.MenuController{}, "Get:GetMenu") | ||
19 | - web.Router("/v1/web/menus/:menuId", &controllers.MenuController{}, "Delete:RemoveMenu") | ||
20 | - web.Router("/v1/web/menus/search", &controllers.MenuController{}, "Get:ListMenu") | ||
21 | - web.Router("/v1/web/menus/search", &controllers.MenuController{}, "Post:SearchMenu") | 16 | + web.Router("/v1/opt/menus/", &controllers.MenuController{}, "Post:CreateMenu") |
17 | + web.Router("/v1/opt/menus/:menuId", &controllers.MenuController{}, "Put:UpdateMenu") | ||
18 | + web.Router("/v1/opt/menus/:menuId", &controllers.MenuController{}, "Get:GetMenu") | ||
19 | + web.Router("/v1/opt/menus/:menuId", &controllers.MenuController{}, "Delete:RemoveMenu") | ||
20 | + web.Router("/v1/opt/menus/search", &controllers.MenuController{}, "Get:ListMenu") | ||
21 | + web.Router("/v1/opt/menus/search", &controllers.MenuController{}, "Post:SearchMenu") | ||
22 | 22 | ||
23 | web.Router("/v1/web/common/dictionary/search", &controllers.CommonController{}, "Post:DictionarySearch") | 23 | web.Router("/v1/web/common/dictionary/search", &controllers.CommonController{}, "Post:DictionarySearch") |
24 | } | 24 | } |
-
请 注册 或 登录 后发表评论