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,37 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/ent/equipment"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type BatchDeleteEquipmentsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 批量删除设备
|
||||
func NewBatchDeleteEquipmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchDeleteEquipmentsLogic {
|
||||
return &BatchDeleteEquipmentsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BatchDeleteEquipmentsLogic) BatchDeleteEquipments(req *types.BatchDeleteEquipmentsReq) error {
|
||||
if len(req.Ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := l.svcCtx.EntClient.Equipment.
|
||||
Delete().
|
||||
Where(equipment.IDIn(req.Ids...)).
|
||||
Exec(l.ctx)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/constants"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateEquipmentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 创建设备
|
||||
func NewCreateEquipmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateEquipmentLogic {
|
||||
return &CreateEquipmentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateEquipmentLogic) CreateEquipment(req *types.CreateEquipmentReq) (resp int, err error) {
|
||||
tx, err := l.svcCtx.EntClient.BeginTx(l.ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
slotCount := req.SlotCount
|
||||
if slotCount < 1 {
|
||||
slotCount = 1
|
||||
}
|
||||
|
||||
eq, err := tx.Equipment.
|
||||
Create().
|
||||
SetName(req.Name).
|
||||
SetEquipmentTypeId(req.EquipmentTypeId).
|
||||
SetIpAddress(req.IpAddress).
|
||||
SetLocation(req.Location).
|
||||
SetRemark(req.Remark).
|
||||
SetSlotCount(slotCount).
|
||||
SetExchange(req.Exchange).
|
||||
SetBatch(req.Batch).
|
||||
SetStatus(constants.EquipmentStatus_Idle).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 创建槽位记录
|
||||
for i := 1; i <= slotCount; i++ {
|
||||
_, err = tx.EquipmentSlot.
|
||||
Create().
|
||||
SetEquipmentId(eq.ID).
|
||||
SetSlotNo(i).
|
||||
SetStatus(constants.SlotStatus_Empty).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return eq.ID, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteEquipmentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除设备
|
||||
func NewDeleteEquipmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteEquipmentLogic {
|
||||
return &DeleteEquipmentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteEquipmentLogic) DeleteEquipment(req *types.PathId) error {
|
||||
err := l.svcCtx.EntClient.Equipment.
|
||||
DeleteOneID(req.Id).
|
||||
Exec(l.ctx)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent/equipment"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetEquipmentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetEquipmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEquipmentLogic {
|
||||
return &GetEquipmentLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetEquipmentLogic) GetEquipment(req *types.PathId) (resp *types.EquipmentReply, err error) {
|
||||
eq, err := l.svcCtx.EntClient.Equipment.Query().
|
||||
WithEquipmentType().
|
||||
WithSlots().
|
||||
Where(equipment.IDEQ(req.Id)).
|
||||
Only(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errorx.NewDirectError("设备不存在")
|
||||
}
|
||||
|
||||
resp = &types.EquipmentReply{
|
||||
Id: eq.ID,
|
||||
Name: eq.Name,
|
||||
EquipmentTypeId: eq.EquipmentTypeId,
|
||||
IpAddress: eq.IpAddress,
|
||||
Location: eq.Location,
|
||||
Remark: eq.Remark,
|
||||
Status: string(eq.Status),
|
||||
Enabled: eq.Enabled,
|
||||
SlotCount: eq.SlotCount,
|
||||
Exchange: eq.Exchange,
|
||||
Batch: eq.Batch,
|
||||
}
|
||||
if eq.CreatedAt != nil {
|
||||
resp.CreatedAt = eq.CreatedAt.Unix()
|
||||
}
|
||||
if eq.UpdatedAt != nil {
|
||||
resp.UpdatedAt = eq.UpdatedAt.Unix()
|
||||
}
|
||||
if eq.Edges.EquipmentType != nil {
|
||||
resp.TypeCode = string(eq.Edges.EquipmentType.Code)
|
||||
resp.TypeName = eq.Edges.EquipmentType.Name
|
||||
}
|
||||
if eq.Edges.Slots != nil {
|
||||
resp.Slots = make([]types.EquipmentSlotReply, 0, len(eq.Edges.Slots))
|
||||
for _, s := range eq.Edges.Slots {
|
||||
slot := types.EquipmentSlotReply{
|
||||
Id: s.ID,
|
||||
SlotNo: s.SlotNo,
|
||||
Status: string(s.Status),
|
||||
}
|
||||
if s.CurrentJobId != nil {
|
||||
slot.CurrentJobId = *s.CurrentJobId
|
||||
}
|
||||
resp.Slots = append(resp.Slots, slot)
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListEquipmentTypesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询设备类型列表
|
||||
func NewListEquipmentTypesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListEquipmentTypesLogic {
|
||||
return &ListEquipmentTypesLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListEquipmentTypesLogic) ListEquipmentTypes() (resp []types.EquipmentTypeReply, err error) {
|
||||
etList, err := l.svcCtx.EntClient.EquipmentType.Query().All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = make([]types.EquipmentTypeReply, 0, len(etList))
|
||||
for _, et := range etList {
|
||||
resp = append(resp, types.EquipmentTypeReply{
|
||||
Id: et.ID,
|
||||
Code: string(et.Code),
|
||||
Name: et.Name,
|
||||
Movable: et.Movable,
|
||||
DefaultCapacity: et.DefaultCapacity,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"bj_power_mes/ent/equipment"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryEquipmentsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewQueryEquipmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryEquipmentsLogic {
|
||||
return &QueryEquipmentsLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *QueryEquipmentsLogic) QueryEquipments(req *types.QueryEquipmentsReq) (resp *types.QueryEquipmentsReply, err error) {
|
||||
query := l.svcCtx.EntClient.Equipment.Query().
|
||||
WithEquipmentType()
|
||||
|
||||
if req.Keyword != "" {
|
||||
query = query.Where(equipment.NameContainsFold(req.Keyword))
|
||||
}
|
||||
|
||||
total, err := query.Count(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Sort != "" {
|
||||
col := strcase.ToSnake(req.Sort) // 转换为数据库字段名
|
||||
if equipment.ValidColumn(col) { // 检查字段是否存在
|
||||
op := sql.OrderAsc() // 默认为升序
|
||||
if strings.HasPrefix(req.Order, "desc") {
|
||||
op = sql.OrderDesc()
|
||||
}
|
||||
query.Order(sql.OrderByField(col, op).ToFunc())
|
||||
}
|
||||
} else {
|
||||
query.Order(equipment.ByID())
|
||||
}
|
||||
|
||||
equipments, err := query.
|
||||
Offset((req.Page - 1) * req.Limit).
|
||||
Limit(req.Limit).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := make([]types.EquipmentReply, 0, len(equipments))
|
||||
for _, eq := range equipments {
|
||||
item := types.EquipmentReply{
|
||||
Id: eq.ID,
|
||||
Name: eq.Name,
|
||||
EquipmentTypeId: eq.EquipmentTypeId,
|
||||
IpAddress: eq.IpAddress,
|
||||
Location: eq.Location,
|
||||
Remark: eq.Remark,
|
||||
Status: string(eq.Status),
|
||||
Enabled: eq.Enabled,
|
||||
SlotCount: eq.SlotCount,
|
||||
Exchange: eq.Exchange,
|
||||
Batch: eq.Batch,
|
||||
}
|
||||
if eq.CreatedAt != nil {
|
||||
item.CreatedAt = eq.CreatedAt.Unix()
|
||||
}
|
||||
if eq.UpdatedAt != nil {
|
||||
item.UpdatedAt = eq.UpdatedAt.Unix()
|
||||
}
|
||||
if eq.Edges.EquipmentType != nil {
|
||||
item.TypeCode = string(eq.Edges.EquipmentType.Code)
|
||||
item.TypeName = eq.Edges.EquipmentType.Name
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
return &types.QueryEquipmentsReply{
|
||||
PageReply: types.PageReply{
|
||||
Page: req.Page,
|
||||
Limit: req.Limit,
|
||||
Total: total,
|
||||
},
|
||||
Data: list,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/constants"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/equipment"
|
||||
"bj_power_mes/internal/eventbus"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateEquipmentAvailabilityLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改设备调度可用状态
|
||||
func NewUpdateEquipmentAvailabilityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateEquipmentAvailabilityLogic {
|
||||
return &UpdateEquipmentAvailabilityLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateEquipmentAvailabilityLogic) UpdateEquipmentAvailability(req *types.UpdateEquipmentAvailabilityReq) error {
|
||||
eq, err := l.svcCtx.EntClient.Equipment.Query().
|
||||
WithEquipmentType().
|
||||
Where(equipment.IDEQ(req.Id)).
|
||||
Only(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.NewDirectError("设备不存在")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if et, eErr := eq.Edges.EquipmentTypeOrErr(); eErr == nil && et != nil {
|
||||
if et.Code != constants.EquipmentTypeCode_CNC {
|
||||
return errorx.NewDirectError("仅 CNC 机床支持修改调度可用状态")
|
||||
}
|
||||
} else {
|
||||
return errorx.NewDirectError("设备类型信息缺失,无法校验")
|
||||
}
|
||||
|
||||
if !req.Enabled && isEquipmentRunning(eq.Status) {
|
||||
return errorx.NewDirectError("设备运行中,不能设为不可用")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.EntClient.Equipment.UpdateOneID(req.Id).
|
||||
SetEnabled(req.Enabled).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.svcCtx.EventBus.Publish(l.ctx, eventbus.NewEquipmentAvailabilityChangedEvent(eq.ID, eq.Name, req.Enabled))
|
||||
return nil
|
||||
}
|
||||
|
||||
func isEquipmentRunning(status constants.EquipmentStatus) bool {
|
||||
switch status {
|
||||
case constants.EquipmentStatus_Loading,
|
||||
constants.EquipmentStatus_Processing,
|
||||
constants.EquipmentStatus_Unloading:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package equipment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/constants"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/equipmentslot"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateEquipmentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 编辑设备
|
||||
func NewUpdateEquipmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateEquipmentLogic {
|
||||
return &UpdateEquipmentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateEquipmentLogic) UpdateEquipment(req *types.UpdateEquipmentReq) error {
|
||||
tx, err := l.svcCtx.EntClient.BeginTx(l.ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
update := tx.Equipment.UpdateOneID(req.Id)
|
||||
if req.Name != "" {
|
||||
update.SetName(req.Name)
|
||||
}
|
||||
if req.EquipmentTypeId != 0 {
|
||||
update.SetEquipmentTypeId(req.EquipmentTypeId)
|
||||
}
|
||||
if req.IpAddress != "" {
|
||||
update.SetIpAddress(req.IpAddress)
|
||||
}
|
||||
if req.Location != "" {
|
||||
update.SetLocation(req.Location)
|
||||
}
|
||||
if req.Remark != "" {
|
||||
update.SetRemark(req.Remark)
|
||||
}
|
||||
if req.SlotCount > 0 {
|
||||
update.SetSlotCount(req.SlotCount)
|
||||
}
|
||||
update.SetExchange(req.Exchange)
|
||||
update.SetBatch(req.Batch)
|
||||
|
||||
eq, err := update.Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
// 同步槽位记录
|
||||
if req.SlotCount > 0 {
|
||||
existingSlots, err := tx.EquipmentSlot.Query().
|
||||
Where(equipmentslot.EquipmentIdEQ(eq.ID)).
|
||||
Order(ent.Asc(equipmentslot.FieldSlotNo)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
currentCount := len(existingSlots)
|
||||
if req.SlotCount > currentCount {
|
||||
// 新增槽位
|
||||
for i := currentCount + 1; i <= req.SlotCount; i++ {
|
||||
_, err = tx.EquipmentSlot.
|
||||
Create().
|
||||
SetEquipmentId(eq.ID).
|
||||
SetSlotNo(i).
|
||||
SetStatus(constants.SlotStatus_Empty).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if req.SlotCount < currentCount {
|
||||
// 删除多余槽位(从末尾开始)
|
||||
for i := currentCount - 1; i >= req.SlotCount; i-- {
|
||||
err = tx.EquipmentSlot.DeleteOneID(existingSlots[i].ID).Exec(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
Reference in New Issue
Block a user