2021-05-08 18:00:33 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/machine/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/machine/domain/repository"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type MachineScript interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.MachineScript]
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
// 分页获取机器脚本信息列表
|
|
|
|
|
|
GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Save(ctx context.Context, entity *entity.MachineScript) error
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Delete(ctx context.Context, id uint64)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func newMachineScriptApp(machineScriptRepo repository.MachineScript, machineApp Machine) MachineScript {
|
|
|
|
|
|
app := &machineScriptAppImpl{machineApp: machineApp}
|
|
|
|
|
|
app.Repo = machineScriptRepo
|
|
|
|
|
|
return app
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type machineScriptAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.MachineScript, repository.MachineScript]
|
|
|
|
|
|
|
|
|
|
|
|
machineApp Machine
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Common_Script_Machine_Id = 9999999
|
|
|
|
|
|
|
|
|
|
|
|
// 分页获取机器脚本信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
|
|
|
|
|
return m.GetRepo().GetPageList(condition, pageParam, toEntity, orderBy...)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存机器脚本
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *machineScriptAppImpl) Save(ctx context.Context, ms *entity.MachineScript) error {
|
2021-05-08 18:00:33 +08:00
|
|
|
|
// 如果机器id不为公共脚本id,则校验机器是否存在
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if machineId := ms.MachineId; machineId != Common_Script_Machine_Id {
|
|
|
|
|
|
_, err := m.machineApp.GetById(new(entity.Machine), machineId, "Name")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("该机器不存在")
|
|
|
|
|
|
}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if ms.Id != 0 {
|
2023-11-07 21:05:21 +08:00
|
|
|
|
return m.UpdateById(ctx, ms)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
2023-11-07 21:05:21 +08:00
|
|
|
|
return m.Insert(ctx, ms)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据id删除
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (m *machineScriptAppImpl) Delete(ctx context.Context, id uint64) {
|
|
|
|
|
|
m.DeleteById(ctx, id)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|