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,45 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bj_power_mes/ent/user"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddRoleUsersLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 添加角色用户
|
||||
func NewAddRoleUsersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddRoleUsersLogic {
|
||||
return &AddRoleUsersLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddRoleUsersLogic) AddRoleUsers(req *types.AddRoleUsersReq) error {
|
||||
|
||||
err := l.svcCtx.EntClient.User.Update().Where(user.IDIn(req.Users...)).ClearRole().Exec(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorf("删除用户与角色的关联失败: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = l.svcCtx.EntClient.Role.UpdateOneID(req.Id).AddUserIDs(req.Users...).Exec(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorf("添加用户到角色失败: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Infof("成功将用户添加到角色 %d", req.Id)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"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 CreateRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 添加角色
|
||||
func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
|
||||
return &CreateRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleReq) (resp *types.RoleReply, err error) {
|
||||
r, err := l.svcCtx.EntClient.Role.Create().SetName(req.Name).SetNillableDescription(&req.Description).Save(l.ctx)
|
||||
if ent.IsValidationError(err) {
|
||||
l.Error(err)
|
||||
return nil, err
|
||||
} else if err != nil {
|
||||
var e *pgconn.PgError
|
||||
if errors.As(err, &e) {
|
||||
if e.Code == "23505" {
|
||||
l.Error(err)
|
||||
return nil, errorx.New(errorx.RoleAlreadyExists)
|
||||
}
|
||||
}
|
||||
l.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &types.RoleReply{
|
||||
Id: r.ID,
|
||||
Name: r.Name,
|
||||
Description: r.Description,
|
||||
CreatedAt: r.CreatedAt.Unix(),
|
||||
UpdatedAt: r.UpdatedAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package role
|
||||
|
||||
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 DeleteRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除角色
|
||||
func NewDeleteRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRoleLogic {
|
||||
return &DeleteRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteRoleLogic) DeleteRole(req *types.PathId) error {
|
||||
err := l.svcCtx.EntClient.Role.DeleteOneID(req.Id).Exec(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.RoleNotFound)
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteRoleUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除角色,用户
|
||||
func NewDeleteRoleUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRoleUserLogic {
|
||||
return &DeleteRoleUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 删除角色中的用户
|
||||
func (l *DeleteRoleUserLogic) DeleteRoleUser(req *types.DeleteRoleUserReq) error {
|
||||
|
||||
// 从角色中移除指定的用户
|
||||
err := l.svcCtx.EntClient.Role.UpdateOneID(req.Id).RemoveUserIDs(req.UserId).Exec(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorf("从角色 %d 中移除用户失败: %v", req.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Infof("成功从角色 %d 中移除用户", req.Id)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/iancoleman/strcase"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/role"
|
||||
"strings"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryRolesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询角色分页列表
|
||||
func NewQueryRolesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryRolesLogic {
|
||||
return &QueryRolesLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QueryRolesLogic) QueryRoles(req *types.QueryRolesReq) (resp *types.QueryRolesReply, err error) {
|
||||
query := l.svcCtx.EntClient.Role.Query()
|
||||
if req.Name != "" {
|
||||
query.Where(role.NameContains(req.Name))
|
||||
}
|
||||
|
||||
total, err := query.Count(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Sort != "" {
|
||||
col := strcase.ToSnake(req.Sort) // 转换为数据库字段名
|
||||
if role.ValidColumn(col) { // 检查字段是否存在
|
||||
op := sql.OrderAsc() // 默认为升序
|
||||
if strings.HasPrefix(req.Order, "desc") {
|
||||
op = sql.OrderDesc()
|
||||
}
|
||||
query.Order(sql.OrderByField(col, op).ToFunc())
|
||||
}
|
||||
} else {
|
||||
query.Order(role.ByCreatedAt(sql.OrderDesc()))
|
||||
}
|
||||
|
||||
query = query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit)
|
||||
|
||||
roles, err := query.All(l.ctx)
|
||||
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.RoleNotFound)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = &types.QueryRolesReply{
|
||||
PageReply: types.PageReply{
|
||||
Page: req.Page,
|
||||
Limit: req.Limit,
|
||||
Total: total,
|
||||
},
|
||||
Data: make([]types.RoleReply, len(roles)),
|
||||
}
|
||||
|
||||
for i, r := range roles {
|
||||
resp.Data[i] = types.RoleReply{
|
||||
Id: r.ID,
|
||||
Name: r.Name,
|
||||
CreatedAt: r.CreatedAt.Unix(),
|
||||
UpdatedAt: r.UpdatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SetRolePermissionsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 设置角色权限
|
||||
func NewSetRolePermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetRolePermissionsLogic {
|
||||
return &SetRolePermissionsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SetRolePermissionsLogic) SetRolePermissions(req *types.SetRolePermissionsReq) error {
|
||||
|
||||
if req.Id == 0 || len(req.Permissions) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := l.svcCtx.EntClient.Role.UpdateOneID(req.Id).
|
||||
AddPermissionIDs(req.Permissions...).
|
||||
Exec(l.ctx)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("设置角色权限失败: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func toIntSlice(int64Slice []int64) []int {
|
||||
var intSlice []int
|
||||
for _, v := range int64Slice {
|
||||
intSlice = append(intSlice, int(v))
|
||||
}
|
||||
return intSlice
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package role
|
||||
|
||||
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 UpdateRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 编辑角色
|
||||
func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleLogic {
|
||||
return &UpdateRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 编辑角色
|
||||
func (l *UpdateRoleLogic) UpdateRole(req *types.UpdateRoleReq) error {
|
||||
role, err := l.svcCtx.EntClient.Role.Get(l.ctx, req.Id)
|
||||
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.RoleNotFound)
|
||||
}
|
||||
return err
|
||||
}
|
||||
_, err = l.svcCtx.EntClient.Role.UpdateOne(role).
|
||||
SetName(req.Name).
|
||||
SetDescription(req.Description).
|
||||
Save(l.ctx)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user