fix: rebuild MES as compilable go-zero+ent backend (renamed bj_power_mes), restore 3 workstation projects from pristine original, align naming; all Go projects go build clean
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
)
|
||||
|
||||
type CreatePermissionLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 添加权限
|
||||
func NewCreatePermissionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePermissionLogic {
|
||||
return &CreatePermissionLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreatePermissionLogic) CreatePermission(req *types.CreatePermissionReq) (resp *types.PermissionReply, err error) {
|
||||
p, err := l.svcCtx.EntClient.Permission.
|
||||
Create().
|
||||
SetName(req.Name).
|
||||
SetCode(req.Code).
|
||||
SetDescription(req.Description).
|
||||
SetParentId(*req.ParentId).
|
||||
SetSort(req.Sort).
|
||||
SetIsMenu(req.IsMenu).
|
||||
Save(l.ctx)
|
||||
|
||||
if err != nil {
|
||||
if ent.IsConstraintError(err) {
|
||||
return nil, errors.Wrapf(err, "permission code '%s' already exists", req.Code)
|
||||
}
|
||||
l.Logger.Errorf("create permission failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &types.PermissionReply{
|
||||
Id: p.ID,
|
||||
Name: p.Name,
|
||||
Code: p.Code,
|
||||
IsMenu: p.IsMenu,
|
||||
Description: p.Description,
|
||||
CreatedAt: p.CreatedAt.Unix(),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeletePermissionApiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除权限接口
|
||||
func NewDeletePermissionApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePermissionApiLogic {
|
||||
return &DeletePermissionApiLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeletePermissionApiLogic) DeletePermissionApi(req *types.DeletePermissionApiReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeletePermissionLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除权限
|
||||
func NewDeletePermissionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePermissionLogic {
|
||||
return &DeletePermissionLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeletePermissionLogic) DeletePermission(req *types.PathId) error {
|
||||
err := l.svcCtx.EntClient.Permission.DeleteOneID(req.Id).Exec(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.PermissionNotFound)
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListChildrenPermissionsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 子权限列表
|
||||
func NewListChildrenPermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListChildrenPermissionsLogic {
|
||||
return &ListChildrenPermissionsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListChildrenPermissionsLogic) ListChildrenPermissions(req *types.PathId) (resp *types.PermissionReply, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListPermissionIdsOfRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 通过角色获取权限ID列表
|
||||
func NewListPermissionIdsOfRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPermissionIdsOfRoleLogic {
|
||||
return &ListPermissionIdsOfRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListPermissionIdsOfRoleLogic) ListPermissionIdsOfRole(req *types.PathId) (resp []int, err error) {
|
||||
r, err := l.svcCtx.EntClient.Role.Get(l.ctx, req.Id)
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.RoleNotFound)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ps, err := r.QueryPermissions().All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = make([]int, 0)
|
||||
|
||||
for _, p := range ps {
|
||||
resp = append(resp, p.ID)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListPermissionsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 权限列表
|
||||
func NewListPermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPermissionsLogic {
|
||||
return &ListPermissionsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListPermissionsLogic) ListPermissions() (resp *types.PermissionReply, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
)
|
||||
|
||||
type PermissionsTreeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewPermissionsTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PermissionsTreeLogic {
|
||||
return &PermissionsTreeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *PermissionsTreeLogic) PermissionsTree() (*types.PermissionTree, error) {
|
||||
ps, err := l.svcCtx.EntClient.Permission.Query().All(l.ctx)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Failed to query all permissions: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
menus := genPermissionTree(0, ps)
|
||||
|
||||
return &types.PermissionTree{
|
||||
Children: menus,
|
||||
}, nil
|
||||
//tree, err := buildPermissionTree(permissions)
|
||||
//if err != nil {
|
||||
// l.Logger.Errorf("Failed to build permission tree: %v", err)
|
||||
// return nil, err
|
||||
//}
|
||||
//resp.Data = tree
|
||||
//return resp, nil
|
||||
}
|
||||
|
||||
func buildPermissionTree(permissions []*ent.Permission) ([]types.PermissionTree, error) {
|
||||
permissionMap := make(map[int64]*types.PermissionTree)
|
||||
rootNodes := make(map[int64]bool)
|
||||
|
||||
for _, p := range permissions {
|
||||
treePerm := &types.PermissionTree{
|
||||
Id: p.ID,
|
||||
Name: p.Name,
|
||||
Code: p.Code,
|
||||
IsMenu: p.IsMenu,
|
||||
Description: p.Description,
|
||||
Children: make([]types.PermissionTree, 0),
|
||||
}
|
||||
if p.ParentId != nil {
|
||||
treePerm.ParentId = p.ParentId
|
||||
}
|
||||
|
||||
permissionMap[int64(p.ID)] = treePerm
|
||||
|
||||
if p.ParentId == nil {
|
||||
rootNodes[int64(p.ID)] = true
|
||||
}
|
||||
|
||||
//if *p.ParentId != 0 {
|
||||
// if parent, ok := permissionMap[int64(treePerm.ParentId)]; ok {
|
||||
// parent.Children = append(parent.Children, *treePerm)
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
var treeSlice []types.PermissionTree
|
||||
for id, perm := range permissionMap {
|
||||
if rootNodes[id] {
|
||||
treeSlice = append(treeSlice, *perm)
|
||||
}
|
||||
}
|
||||
|
||||
return treeSlice, nil
|
||||
}
|
||||
|
||||
func genPermissionTree(parentId int, permissions []*ent.Permission) []types.PermissionTree {
|
||||
var menus []types.PermissionTree
|
||||
for _, p := range permissions {
|
||||
if p.ParentId != nil && *p.ParentId == parentId {
|
||||
var menu types.PermissionTree
|
||||
_ = copier.Copy(&menu, p)
|
||||
menu.Children = genPermissionTree(p.ID, permissions)
|
||||
menus = append(menus, menu)
|
||||
sort.SliceStable(menus, func(i, j int) bool {
|
||||
return menus[i].Sort < menus[j].Sort
|
||||
})
|
||||
}
|
||||
}
|
||||
return menus
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SetPermissionApisLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 设置权限接口
|
||||
func NewSetPermissionApisLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetPermissionApisLogic {
|
||||
return &SetPermissionApisLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SetPermissionApisLogic) SetPermissionApis(req *types.SetPermissionApisReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdatePermissionLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 编辑权限
|
||||
func NewUpdatePermissionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePermissionLogic {
|
||||
return &UpdatePermissionLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdatePermissionLogic) UpdatePermission(req *types.UpdatePermissionReq) error {
|
||||
permission, err := l.svcCtx.EntClient.Permission.Get(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
l.Logger.Errorf("Permission not found: %v", req.Id)
|
||||
return errorx.New(errorx.PermissionNotFound)
|
||||
}
|
||||
l.Logger.Errorf("Error getting permission: %v", err)
|
||||
return err
|
||||
}
|
||||
_, err = l.svcCtx.EntClient.Permission.UpdateOne(permission).
|
||||
SetName(req.Name).
|
||||
SetCode(req.Code).
|
||||
SetParentId(*req.ParentId).
|
||||
SetSort(req.Sort).
|
||||
SetIsMenu(req.IsMenu).
|
||||
SetDescription(req.Description).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Error updating permission: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user