ifms_go/internal/mods/autocontrol/biz/device_run_log.biz.go

107 lines
2.8 KiB
Go

package biz
import (
"context"
"time"
"ifms/internal/mods/autocontrol/dal"
"ifms/internal/mods/autocontrol/schema"
"ifms/pkg/errors"
"ifms/pkg/util"
)
// 设备运行日志表
type DeviceRunLog struct {
Trans *util.Trans
DeviceRunLogDAL *dal.DeviceRunLog
}
// Query device run logs from the data access object based on the provided parameters and options.
func (a *DeviceRunLog) Query(ctx context.Context, params schema.DeviceRunLogQueryParam) (*schema.DeviceRunLogQueryResult, error) {
params.Pagination = true
result, err := a.DeviceRunLogDAL.Query(ctx, params, schema.DeviceRunLogQueryOptions{
QueryOptions: util.QueryOptions{
OrderFields: []util.OrderByParam{
{Field: "created_at", Direction: util.DESC},
},
},
})
if err != nil {
return nil, err
}
return result, nil
}
// Get the specified device run log from the data access object.
func (a *DeviceRunLog) Get(ctx context.Context, id string) (*schema.DeviceRunLog, error) {
deviceRunLog, err := a.DeviceRunLogDAL.Get(ctx, id)
if err != nil {
return nil, err
} else if deviceRunLog == nil {
return nil, errors.NotFound("", "Device run log not found")
}
return deviceRunLog, nil
}
// Create a new device run log in the data access object.
func (a *DeviceRunLog) Create(ctx context.Context, formItem *schema.DeviceRunLogForm) (*schema.DeviceRunLog, error) {
deviceRunLog := &schema.DeviceRunLog{
CreatedAt: time.Now(),
}
if err := formItem.FillTo(deviceRunLog); err != nil {
return nil, err
}
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.DeviceRunLogDAL.Create(ctx, deviceRunLog); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return deviceRunLog, nil
}
// Update the specified device run log in the data access object.
func (a *DeviceRunLog) Update(ctx context.Context, id string, formItem *schema.DeviceRunLogForm) error {
deviceRunLog, err := a.DeviceRunLogDAL.Get(ctx, id)
if err != nil {
return err
} else if deviceRunLog == nil {
return errors.NotFound("", "Device run log not found")
}
if err := formItem.FillTo(deviceRunLog); err != nil {
return err
}
deviceRunLog.UpdatedAt = time.Now()
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.DeviceRunLogDAL.Update(ctx, deviceRunLog); err != nil {
return err
}
return nil
})
}
// Delete the specified device run log from the data access object.
func (a *DeviceRunLog) Delete(ctx context.Context, id string) error {
exists, err := a.DeviceRunLogDAL.Exists(ctx, id)
if err != nil {
return err
} else if !exists {
return errors.NotFound("", "Device run log not found")
}
return a.Trans.Exec(ctx, func(ctx context.Context) error {
if err := a.DeviceRunLogDAL.Delete(ctx, id); err != nil {
return err
}
return nil
})
}