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"
|
2025-06-16 20:13:03 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
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
|
|
|
|
// 分页获取机器脚本信息列表
|
2025-05-20 21:04:47 +08:00
|
|
|
|
GetPageList(condition *entity.MachineScript, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.MachineScript], error)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
2025-06-16 20:13:03 +08:00
|
|
|
|
// GetScriptCategorys 获取脚本分类
|
|
|
|
|
|
GetScriptCategorys(ctx context.Context) ([]string, error)
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-16 20:13:03 +08:00
|
|
|
|
var _ (MachineScript) = (*machineScriptAppImpl)(nil)
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
2024-12-16 23:29:18 +08:00
|
|
|
|
machineApp Machine `inject:"T"`
|
2024-01-21 22:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
const Common_Script_Machine_Id = 9999999
|
|
|
|
|
|
|
|
|
|
|
|
// 分页获取机器脚本信息列表
|
2025-05-20 21:04:47 +08:00
|
|
|
|
func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, pageParam model.PageParam, orderBy ...string) (*model.PageResult[*entity.MachineScript], error) {
|
|
|
|
|
|
return m.GetRepo().GetPageList(condition, pageParam, orderBy...)
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-16 20:13:03 +08:00
|
|
|
|
func (m *machineScriptAppImpl) GetScriptCategorys(ctx context.Context) ([]string, error) {
|
|
|
|
|
|
scripts, err := m.ListByCond(new(entity.MachineScript), "category")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return collx.ArrayRemoveBlank(collx.ArrayDeduplicate(collx.ArrayMap(scripts, func(script *entity.MachineScript) string { return script.Category }))), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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 {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
_, err := m.machineApp.GetById(machineId, "Name")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBiz("machine not found")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|