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,57 @@
|
||||
package dept
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
)
|
||||
|
||||
type CreateDeptLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 创建部门
|
||||
func NewCreateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDeptLogic {
|
||||
return &CreateDeptLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateDeptLogic) CreateDept(req *types.CreateDeptReq) (resp *types.DeptReply, err error) {
|
||||
d, err := l.svcCtx.EntClient.Department.Create().
|
||||
SetName(req.Name).
|
||||
SetParentId(req.ParentId).
|
||||
SetOrder(req.Order).
|
||||
Save(l.ctx)
|
||||
|
||||
if ent.IsValidationError(err) {
|
||||
l.Error("Validation error when creating department:", err)
|
||||
return nil, err
|
||||
} else if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
l.Error("Department already exists:", err)
|
||||
return nil, errorx.New(errorx.DeptAlreadyExists)
|
||||
}
|
||||
l.Error("Error creating department:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.DeptReply{
|
||||
Id: d.ID,
|
||||
Name: d.Name,
|
||||
ParentId: d.ParentId,
|
||||
CreatedAt: d.CreatedAt.Unix(),
|
||||
UpdatedAt: d.UpdatedAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package dept
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
)
|
||||
|
||||
type DeleteDeptLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除部门
|
||||
func NewDeleteDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteDeptLogic {
|
||||
return &DeleteDeptLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteDept 用于删除指定 ID 的部门
|
||||
func (l *DeleteDeptLogic) DeleteDept(req *types.PathId) error {
|
||||
|
||||
err := l.svcCtx.EntClient.Department.DeleteOneID(req.Id).Exec(l.ctx)
|
||||
|
||||
if ent.IsNotFound(err) {
|
||||
l.Logger.Errorf("Department not found with ID: %d", req.Id)
|
||||
return errorx.New(errorx.DeptNotFound)
|
||||
} else if err != nil {
|
||||
l.Logger.Errorf("Failed to delete department with ID: %d, error: %v", req.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("Department deleted successfully with ID: %d", req.Id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package dept
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
)
|
||||
|
||||
type DeptsTreeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 部门树
|
||||
func NewDeptsTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeptsTreeLogic {
|
||||
return &DeptsTreeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeptsTreeLogic) DeptsTree() (resp []*types.DeptTree, err error) {
|
||||
// 从数据库获取所有部门
|
||||
depts, err := l.svcCtx.EntClient.Department.Query().All(l.ctx)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Failed to query all departments: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
// 构建部门树
|
||||
tree, err := buildDeptTree(depts)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Failed to build department tree: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tree, nil
|
||||
}
|
||||
|
||||
// buildDeptTree 函数用于构建部门树
|
||||
func buildDeptTree(depts []*ent.Department) ([]*types.DeptTree, error) {
|
||||
deptMap := make(map[int]*types.DeptTree)
|
||||
rootDepts := make([]*types.DeptTree, 0)
|
||||
|
||||
for _, dept := range depts {
|
||||
treeDept := &types.DeptTree{
|
||||
Id: dept.ID,
|
||||
Name: dept.Name,
|
||||
Children: make([]*types.DeptTree, 0),
|
||||
}
|
||||
|
||||
// 使用部门的 ID 作为键
|
||||
deptMap[dept.ID] = treeDept
|
||||
|
||||
if dept.ParentId == 0 {
|
||||
rootDepts = append(rootDepts, treeDept)
|
||||
} else if parent, ok := deptMap[dept.ParentId]; ok {
|
||||
parent.Children = append(parent.Children, treeDept)
|
||||
}
|
||||
}
|
||||
|
||||
return rootDepts, nil
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package dept
|
||||
|
||||
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 GetDeptLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 通过ID获取部门详情
|
||||
func NewGetDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeptLogic {
|
||||
return &GetDeptLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetDeptLogic) GetDept(req *types.PathId) (resp *types.DeptReply, err error) {
|
||||
d, err := l.svcCtx.EntClient.Department.Get(l.ctx, req.Id)
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.DeptNotFound)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.DeptReply{
|
||||
Id: d.ID,
|
||||
Name: d.Name,
|
||||
ParentId: d.ParentId,
|
||||
Order: d.Order,
|
||||
CreatedAt: d.CreatedAt.Unix(),
|
||||
UpdatedAt: d.UpdatedAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package dept
|
||||
|
||||
import (
|
||||
"context"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/iancoleman/strcase"
|
||||
"bj_power_mes/ent/department"
|
||||
"strings"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryDeptsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询部门分页列表
|
||||
func NewQueryDeptsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryDeptsLogic {
|
||||
return &QueryDeptsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QueryDeptsLogic) QueryDepts(req *types.QueryDeptsReq) (resp *types.QueryDeptsReply, err error) {
|
||||
resp = &types.QueryDeptsReply{}
|
||||
|
||||
query := l.svcCtx.EntClient.Department.Query()
|
||||
|
||||
total, err := query.Count(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to count depts: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Sort != "" {
|
||||
col := strcase.ToSnake(req.Sort) // 转换为数据库字段名
|
||||
if department.ValidColumn(col) { // 检查字段是否存在
|
||||
op := sql.OrderAsc() // 默认为升序
|
||||
if strings.HasPrefix(req.Order, "desc") {
|
||||
op = sql.OrderDesc()
|
||||
}
|
||||
query.Order(sql.OrderByField(col, op).ToFunc())
|
||||
}
|
||||
} else {
|
||||
query.Order(department.ByCreatedAt(sql.OrderDesc()))
|
||||
}
|
||||
|
||||
depts, err := query.
|
||||
Limit(req.Limit).
|
||||
Offset((req.Page - 1) * req.Limit).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to fetch depts: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var deptList []types.DeptReply
|
||||
for _, dept := range depts {
|
||||
deptList = append(deptList, types.DeptReply{
|
||||
Id: int(dept.ID),
|
||||
Name: dept.Name,
|
||||
ParentId: dept.ParentId,
|
||||
Order: dept.Order,
|
||||
CreatedAt: dept.CreatedAt.Unix(),
|
||||
UpdatedAt: dept.UpdatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
resp.Total = total
|
||||
resp.Data = deptList
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package dept
|
||||
|
||||
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 UpdateDeptLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 编辑部门
|
||||
func NewUpdateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeptLogic {
|
||||
return &UpdateDeptLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateDeptLogic) UpdateDept(req *types.UpdateDeptReq) error {
|
||||
_, err := l.svcCtx.EntClient.Department.UpdateOneID(req.Id).SetName(req.Name).Save(l.ctx)
|
||||
if ent.IsValidationError(err) {
|
||||
l.Error(err)
|
||||
return err
|
||||
} else if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.DeptNotFound)
|
||||
} else if err != nil {
|
||||
var e *pgconn.PgError
|
||||
if errors.As(err, &e) {
|
||||
if e.Code == "23505" {
|
||||
l.Error(err)
|
||||
return errorx.New(errorx.DeptAlreadyExists)
|
||||
}
|
||||
}
|
||||
l.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user